-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy paths3site_build.go
274 lines (251 loc) · 9.6 KB
/
s3site_build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//go:build !lambdabinary
// +build !lambdabinary
package sparta
import (
"github.com/aws/aws-sdk-go-v2/aws"
awsv2S3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
gof "github.com/awslabs/goformation/v5/cloudformation"
gofiam "github.com/awslabs/goformation/v5/cloudformation/iam"
goflambda "github.com/awslabs/goformation/v5/cloudformation/lambda"
gofs3 "github.com/awslabs/goformation/v5/cloudformation/s3"
cfCustomResources "github.com/mweagle/Sparta/v3/aws/cloudformation/resources"
spartaIAM "github.com/mweagle/Sparta/v3/aws/iam"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
const (
// OutputS3SiteURL is the keyname used in the CloudFormation Output
// that stores the S3 backed static site provisioned with this Sparta application
// @enum OutputKey
OutputS3SiteURL = "S3SiteURL"
)
// Create the resource, which will be part of the stack definition and use a CustomResource
// to copy the content. Which means we need PutItem access to the target Bucket. Use
// Cloudformation to create a random bucketname:
// http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html
// Need to create the S3 target bucket
// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketWebsite-property
// export marshals the API data to a CloudFormation compatible representation
func (s3Site *S3Site) export(serviceName string,
binaryName string,
s3ArtifactBucket string,
s3CodeResource *goflambda.Function_Code,
s3ResourcesKey string,
apiGatewayOutputs map[string]gof.Output,
roleNameMap map[string]string,
template *gof.Template,
logger *zerolog.Logger) error {
if s3Site.WebsiteConfiguration == nil {
s3Site.WebsiteConfiguration = &awsv2S3Types.WebsiteConfiguration{
ErrorDocument: &awsv2S3Types.ErrorDocument{
Key: aws.String("error.html"),
},
IndexDocument: &awsv2S3Types.IndexDocument{
Suffix: aws.String("index.html"),
},
}
}
// Ensure everything is set
if s3Site.WebsiteConfiguration.ErrorDocument == nil {
s3Site.WebsiteConfiguration.ErrorDocument = &awsv2S3Types.ErrorDocument{
Key: aws.String("error.html"),
}
}
if s3Site.WebsiteConfiguration.IndexDocument == nil {
s3Site.WebsiteConfiguration.IndexDocument = &awsv2S3Types.IndexDocument{
Suffix: aws.String("index.html"),
}
}
//////////////////////////////////////////////////////////////////////////////
// 1 - Create the S3 bucket. The "BucketName" property is empty s.t.
// AWS will assign a unique one.
s3WebsiteConfig := &gofs3.Bucket_WebsiteConfiguration{
ErrorDocument: *s3Site.WebsiteConfiguration.ErrorDocument.Key,
IndexDocument: *s3Site.WebsiteConfiguration.IndexDocument.Suffix,
}
s3Bucket := &gofs3.Bucket{
AccessControl: "PublicRead",
WebsiteConfiguration: s3WebsiteConfig,
}
s3Bucket.BucketName = s3Site.BucketName
s3Bucket.AWSCloudFormationDeletionPolicy = "Delete"
s3BucketResourceName := s3Site.CloudFormationS3ResourceName()
template.Resources[s3BucketResourceName] = s3Bucket
template.Outputs[OutputS3SiteURL] = gof.Output{
Description: "S3 Website URL",
Value: gof.GetAtt(s3BucketResourceName, "WebsiteURL"),
}
// Represents the S3 ARN that is provisioned
s3SiteBucketResourceValue := gof.Join("", []string{
"arn:aws:s3:::",
s3BucketResourceName,
})
s3SiteBucketAllKeysResourceValue := gof.Join("", []string{
"arn:aws:s3:::",
gof.Ref(s3BucketResourceName),
"/*"})
//////////////////////////////////////////////////////////////////////////////
// 2 - Add a bucket policy to enable anonymous access, as the PublicRead
// canned ACL doesn't seem to do what is implied.
// TODO - determine if this is needed or if PublicRead is being misued
s3SiteBucketPolicy := &gofs3.BucketPolicy{
Bucket: gof.Ref(s3BucketResourceName),
PolicyDocument: ArbitraryJSONObject{
"Version": "2012-10-17",
"Statement": []ArbitraryJSONObject{
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": ArbitraryJSONObject{
"AWS": "*",
},
"Action": "s3:GetObject",
"Resource": s3SiteBucketAllKeysResourceValue,
},
},
},
}
s3BucketPolicyResourceName := stableCloudformationResourceName("S3SiteBucketPolicy")
template.Resources[s3BucketPolicyResourceName] = s3SiteBucketPolicy
//////////////////////////////////////////////////////////////////////////////
// 3 - Create the IAM role for the lambda function
// The lambda function needs to download the posted resource content, as well
// as manage the S3 bucket that hosts the site.
statements := CommonIAMStatements.Core
statements = append(statements, spartaIAM.PolicyStatement{
Action: []string{"s3:ListBucket",
"s3:ListObjectsPages"},
Effect: "Allow",
Resource: s3SiteBucketResourceValue,
})
statements = append(statements, spartaIAM.PolicyStatement{
Action: []string{"s3:DeleteObject",
"s3:PutObject",
"s3:DeleteObjects"},
Effect: "Allow",
Resource: s3SiteBucketAllKeysResourceValue,
})
statements = append(statements, spartaIAM.PolicyStatement{
Action: []string{"s3:GetObject"},
Effect: "Allow",
Resource: gof.Join("", []string{
"arn:aws:s3:::",
s3ArtifactBucket,
"/",
s3ResourcesKey}),
})
iamPolicyList := []gofiam.Role_Policy{}
iamPolicyList = append(iamPolicyList,
gofiam.Role_Policy{
PolicyDocument: ArbitraryJSONObject{
"Version": "2012-10-17",
"Statement": statements,
},
PolicyName: "S3SiteMgmnt",
},
)
iamS3Role := &gofiam.Role{
AssumeRolePolicyDocument: AssumePolicyDocument,
Policies: iamPolicyList,
}
iamRoleName := stableCloudformationResourceName("S3SiteIAMRole")
iamS3Role.AWSCloudFormationDependsOn = []string{s3BucketResourceName}
template.Resources[iamRoleName] = iamS3Role
iamRoleRef := gof.GetAtt(iamRoleName, "Arn")
// Create the IAM role and CustomAction handler to do the work
//////////////////////////////////////////////////////////////////////////////
// 4 - Create the lambda function definition that executes with the
// dynamically provisioned IAM policy. This is similar to what happens in
// EnsureCustomResourceHandler, but due to the more complex IAM rules
// there's a bit of duplication
// handlerName := lambdaExportNameForCustomResourceType(cloudformationresources.ZipToS3Bucket)
logger.Debug().
Interface("CustomResourceType", cfCustomResources.ZipToS3Bucket).
Msg("Sparta CloudFormation custom resource handler info")
// Since this is a custom resource command, stuff the type in the environment
userDispatchMap := map[string]string{
EnvVarCustomResourceTypeName: cfCustomResources.ZipToS3Bucket,
}
lambdaEnv, lambdaEnvErr := lambdaFunctionEnvironment(userDispatchMap,
cfCustomResources.ZipToS3Bucket,
nil,
logger)
if lambdaEnvErr != nil {
return errors.Wrapf(lambdaEnvErr, "Failed to create S3 site resource")
}
customResourceHandlerDef := &goflambda.Function{
Code: s3CodeResource,
Description: customResourceDescription(serviceName, "S3 static site"),
Role: iamRoleRef,
MemorySize: 256,
Timeout: 180,
// Let AWS assign the function name
/*
FunctionName: lambdaFunctionName.String(),
*/
Environment: lambdaEnv,
}
if s3CodeResource.ImageUri != "" {
customResourceHandlerDef.PackageType = "Image"
} else {
customResourceHandlerDef.Runtime = string(Go1LambdaRuntime)
customResourceHandlerDef.Handler = binaryName
}
lambdaResourceName := stableCloudformationResourceName("S3SiteCreator")
customResourceHandlerDef.AWSCloudFormationDependsOn = []string{s3BucketResourceName,
iamRoleName}
template.Resources[lambdaResourceName] = customResourceHandlerDef
//////////////////////////////////////////////////////////////////////////////
// 5 - Create the custom resource that invokes the site bootstrapper lambda to
// actually populate the S3 with content
customResourceName := CloudFormationResourceName("S3SiteBuilder")
newResource, err := newCloudFormationResource(cfCustomResources.ZipToS3Bucket, logger)
if nil != err {
return errors.Wrapf(err, "Failed to create ZipToS3Bucket CustomResource")
}
zipResource, zipResourceOK := newResource.(*cfCustomResources.ZipToS3BucketResource)
if !zipResourceOK {
return errors.Errorf("Failed to type assert *cfCustomResources.ZipToS3BucketResource custom resource")
}
zipRequest := cfCustomResources.ZipToS3BucketResourceRequest{
CustomResourceRequest: cfCustomResources.CustomResourceRequest{
ServiceToken: gof.GetAtt(lambdaResourceName, "Arn"),
},
SrcKeyName: s3ResourcesKey,
SrcBucket: s3ArtifactBucket,
DestBucket: gof.Ref(s3BucketResourceName),
}
// Build the manifest data with any output info...
manifestData := make(map[string]interface{})
for eachKey, eachOutput := range apiGatewayOutputs {
manifestData[eachKey] = map[string]interface{}{
"Description": eachOutput.Description,
"Value": eachOutput.Value,
}
}
if len(s3Site.UserManifestData) != 0 {
manifestData["userdata"] = s3Site.UserManifestData
}
zipRequest.Manifest = manifestData
zipResource.AWSCloudFormationDependsOn = []string{
lambdaResourceName,
s3BucketResourceName,
}
zipResource.Properties = cfCustomResources.ToCustomResourceProperties(zipRequest)
template.Resources[customResourceName] = zipResource
return nil
}
// NewS3Site returns a new S3Site pointer initialized with the
// static resources at the supplied path. If resources is a directory,
// the contents will be recursively archived and used to populate
// the new S3 bucket.
func NewS3Site(resources string) (*S3Site, error) {
// We'll ensure its valid during the build step, since
// there could be a go:generate command in the source that
// actually builds it.
site := &S3Site{
resources: resources,
UserManifestData: map[string]interface{}{},
}
return site, nil
}