-
Notifications
You must be signed in to change notification settings - Fork 48
/
provision_annotations.go
240 lines (215 loc) · 7.96 KB
/
provision_annotations.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
// +build !lambdabinary
package sparta
import (
"reflect"
"runtime"
spartaCF "github.com/mweagle/Sparta/aws/cloudformation"
spartaIAM "github.com/mweagle/Sparta/aws/iam"
gocf "github.com/mweagle/go-cloudformation"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// eventSourceMappingPoliciesForResource returns the IAM specific privileges for each
// type of supported AWS Lambda EventSourceMapping
func eventSourceMappingPoliciesForResource(resource *resourceRef,
template *gocf.Template,
logger *logrus.Logger) ([]spartaIAM.PolicyStatement, error) {
policyStatements := []spartaIAM.PolicyStatement{}
if isResolvedResourceType(resource, template, ":dynamodb:", &gocf.DynamoDBTable{}) {
policyStatements = append(policyStatements, CommonIAMStatements.DynamoDB...)
} else if isResolvedResourceType(resource, template, ":kinesis:", &gocf.KinesisStream{}) {
policyStatements = append(policyStatements, CommonIAMStatements.Kinesis...)
} else if isResolvedResourceType(resource, template, ":sqs:", &gocf.SQSQueue{}) {
policyStatements = append(policyStatements, CommonIAMStatements.SQS...)
} else {
logger.WithFields(logrus.Fields{
"Resource": resource,
}).Debug("No additional EventSource IAM permissions found for event type")
}
return policyStatements, nil
}
// annotationFunc represents an internal annotation function
// called to stich the template together
type annotationFunc func(lambdaAWSInfos []*LambdaAWSInfo,
template *gocf.Template,
logger *logrus.Logger) error
func annotateBuildInformation(lambdaAWSInfo *LambdaAWSInfo,
template *gocf.Template,
buildID string,
logger *logrus.Logger) (*gocf.Template, error) {
// Add the build id s.t. the logger can get stamped...
if lambdaAWSInfo.Options == nil {
lambdaAWSInfo.Options = &LambdaFunctionOptions{}
}
lambdaEnvironment := lambdaAWSInfo.Options.Environment
if lambdaEnvironment == nil {
lambdaAWSInfo.Options.Environment = make(map[string]*gocf.StringExpr)
}
return template, nil
}
func annotateDiscoveryInfo(lambdaAWSInfo *LambdaAWSInfo,
template *gocf.Template,
logger *logrus.Logger) (*gocf.Template, error) {
depMap := make(map[string]string)
// Update the metdata with a reference to the output of each
// depended on item...
for _, eachDependsKey := range lambdaAWSInfo.DependsOn {
dependencyText, dependencyTextErr := discoveryResourceInfoForDependency(template, eachDependsKey, logger)
if dependencyTextErr != nil {
return nil, errors.Wrapf(dependencyTextErr, "Failed to determine discovery info for resource")
}
depMap[eachDependsKey] = string(dependencyText)
}
if lambdaAWSInfo.Options == nil {
lambdaAWSInfo.Options = &LambdaFunctionOptions{}
}
lambdaEnvironment := lambdaAWSInfo.Options.Environment
if lambdaEnvironment == nil {
lambdaAWSInfo.Options.Environment = make(map[string]*gocf.StringExpr)
}
discoveryInfo, discoveryInfoErr := discoveryInfoForResource(lambdaAWSInfo.LogicalResourceName(),
depMap)
if discoveryInfoErr != nil {
return nil, errors.Wrap(discoveryInfoErr, "Failed to create resource discovery info")
}
// Update the env map
lambdaAWSInfo.Options.Environment[envVarDiscoveryInformation] = discoveryInfo
return template, nil
}
func annotateCodePipelineEnvironments(lambdaAWSInfo *LambdaAWSInfo, logger *logrus.Logger) {
if nil != codePipelineEnvironments {
if nil == lambdaAWSInfo.Options {
lambdaAWSInfo.Options = defaultLambdaFunctionOptions()
}
if nil == lambdaAWSInfo.Options.Environment {
lambdaAWSInfo.Options.Environment = make(map[string]*gocf.StringExpr)
}
for _, eachEnvironment := range codePipelineEnvironments {
logger.WithFields(logrus.Fields{
"Environment": eachEnvironment,
"LambdaFunction": lambdaAWSInfo.lambdaFunctionName(),
}).Debug("Annotating Lambda environment for CodePipeline")
for eachKey := range eachEnvironment {
lambdaAWSInfo.Options.Environment[eachKey] = gocf.Ref(eachKey).String()
}
}
}
}
func annotateEventSourceMappings(lambdaAWSInfos []*LambdaAWSInfo,
template *gocf.Template,
logger *logrus.Logger) error {
//
// BEGIN
// Inline closure to handle the update of a lambda function that includes
// an eventSourceMapping entry.
annotatePermissions := func(lambdaAWSInfo *LambdaAWSInfo,
eventSourceMapping *EventSourceMapping,
mappingIndex int,
resource *resourceRef) error {
annotateStatements, annotateStatementsErr := eventSourceMappingPoliciesForResource(resource,
template,
logger)
// Early exit?
if annotateStatementsErr != nil {
return annotateStatementsErr
} else if len(annotateStatements) <= 0 {
return nil
}
// If we have statements, let's go ahead and ensure they
// include a reference to our ARN
populatedStatements := []spartaIAM.PolicyStatement{}
for _, eachStatement := range annotateStatements {
populatedStatements = append(populatedStatements,
spartaIAM.PolicyStatement{
Action: eachStatement.Action,
Effect: "Allow",
Resource: spartaCF.DynamicValueToStringExpr(eventSourceMapping.EventSourceArn).String(),
})
}
// Something to push onto the resource. The resource
// is hopefully defined in this template. It technically
// could be a string literal, in which case we're not going
// to have a lot of luck with that...
cfResource, cfResourceOk := template.Resources[lambdaAWSInfo.LogicalResourceName()]
if !cfResourceOk {
return errors.Errorf("Unable to locate lambda function for annotation")
}
lambdaResource, lambdaResourceOk := cfResource.Properties.(gocf.LambdaFunction)
if !lambdaResourceOk {
return errors.Errorf("CloudFormation resource exists, but is incorrect type: %s (%v)",
cfResource.Properties.CfnResourceType(),
cfResource.Properties)
}
// Ok, go get the IAM Role
resourceRef, resourceRefErr := resolveResourceRef(lambdaResource.Role)
if resourceRefErr != nil {
return errors.Wrapf(resourceRefErr, "Failed to resolve IAM Role for event source mappings: %#v",
lambdaResource.Role)
}
// If it's not nil and also not a literal, go ahead and try and update it
if resourceRef != nil &&
resourceRef.RefType != resourceLiteral &&
resourceRef.RefType != resourceStringFunc {
// Excellent, go ahead and find the role in the template
// and stitch things together
iamRole, iamRoleExists := template.Resources[resourceRef.ResourceName]
if !iamRoleExists {
return errors.Errorf("IAM role not found: %s", resourceRef.ResourceName)
}
// Coerce to the IAMRole and update the statements
typedIAMRole, typedIAMRoleOk := iamRole.Properties.(gocf.IAMRole)
if !typedIAMRoleOk {
return errors.Errorf("Failed to type convert iamRole to proper IAMRole resource")
}
policyList := typedIAMRole.Policies
if policyList == nil {
policyList = &gocf.IAMRolePolicyList{}
}
*policyList = append(*policyList,
gocf.IAMRolePolicy{
PolicyDocument: ArbitraryJSONObject{
"Version": "2012-10-17",
"Statement": populatedStatements,
},
PolicyName: gocf.String("LambdaEventSourceMappingPolicy"),
})
typedIAMRole.Policies = policyList
}
return nil
}
//
// END
annotationErr := visitResolvedEventSourceMapping(annotatePermissions,
lambdaAWSInfos,
template,
logger)
if annotationErr != nil {
return errors.Wrapf(annotationErr,
"Failed to annotate template for EventSourceMappings")
}
return nil
}
func annotateMaterializedTemplate(
lambdaAWSInfos []*LambdaAWSInfo,
template *gocf.Template,
logger *logrus.Logger) (*gocf.Template, error) {
// Setup the annotation functions
annotationFuncs := []annotationFunc{
annotateEventSourceMappings,
}
for _, eachAnnotationFunc := range annotationFuncs {
funcName := runtime.FuncForPC(reflect.ValueOf(eachAnnotationFunc).Pointer()).Name()
logger.WithFields(logrus.Fields{
"Annotator": funcName,
}).Debug("Evaluating annotator")
annotationErr := eachAnnotationFunc(lambdaAWSInfos,
template,
logger)
if annotationErr != nil {
return nil, errors.Wrapf(annotationErr,
"Function %s failed to annotate template",
funcName)
}
}
return template, nil
}