forked from coinbase/fenrir
/
validations.go
232 lines (188 loc) · 6.25 KB
/
validations.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
package template
import (
"crypto/sha1"
"fmt"
"strings"
"github.com/awslabs/goformation/v4/cloudformation"
"github.com/coinbase/fenrir/aws"
"github.com/coinbase/fenrir/aws/subnet"
"github.com/coinbase/step/utils/to"
)
func ValidateTemplateResources(
projectName, configName, region, accountId string,
template *cloudformation.Template,
s3shas map[string]string,
iamc aws.IAMAPI,
ec2c aws.EC2API,
s3c aws.S3API,
kinc aws.KINAPI,
ddbc aws.DDBAPI,
sqsc aws.SQSAPI,
snsc aws.SNSAPI,
kmsc aws.KMSAPI,
lambdac aws.LambdaAPI,
cwlc aws.CWLAPI,
) error {
for name, a := range template.Resources {
switch a.AWSCloudFormationType() {
case "AWS::Serverless::Function":
res, err := template.GetServerlessFunctionWithName(name)
if err != nil {
return err
}
if err := ValidateAWSServerlessFunction(
projectName, configName, region, accountId, name,
template, res, s3shas,
iamc, ec2c, s3c, kinc, ddbc, sqsc, snsc, kmsc, cwlc); err != nil {
return err
}
case "AWS::Serverless::Api":
res, err := template.GetServerlessApiWithName(name)
if err != nil {
return err
}
if err := ValidateAWSServerlessApi(projectName, configName, name, template, res, s3shas); err != nil {
return err
}
case "AWS::Serverless::LayerVersion":
res, err := template.GetServerlessLayerVersionWithName(name)
if err != nil {
return err
}
if err := ValidateAWSServerlessLayerVersion(projectName, configName, name, template, res, s3shas); err != nil {
return err
}
case "AWS::Serverless::SimpleTable":
res, err := template.GetServerlessSimpleTableWithName(name)
if err != nil {
return err
}
if err := ValidateAWSServerlessSimpleTable(projectName, configName, name, template, res); err != nil {
return err
}
case "AWS::SQS::Queue":
res, err := template.GetSQSQueueWithName(name)
if err != nil {
return err
}
if err := ValidateAWSSQSQueue(projectName, configName, name, template, res); err != nil {
return err
}
case "AWS::CloudFront::Distribution":
res, err := template.GetCloudFrontDistributionWithName(name)
if err != nil {
return err
}
if err := ValidateAWSCloudFrontDistribution(projectName, configName, name, template, res); err != nil {
return err
}
case "AWS::CloudWatch::Alarm":
res, err := template.GetCloudWatchAlarmWithName(name)
if err != nil {
return err
}
if err := ValidateAWSCloudWatchAlarm(projectName, configName, name, template, res); err != nil {
return err
}
case "AWS::ElasticLoadBalancingV2::LoadBalancer":
res, err := template.GetElasticLoadBalancingV2LoadBalancerWithName(name)
if err != nil {
return err
}
if err := ValidateAWSElasticLoadBalancingV2LoadBalancer(projectName, configName, name, template, ec2c, res); err != nil {
return err
}
case "AWS::ElasticLoadBalancingV2::TargetGroup":
res, err := template.GetElasticLoadBalancingV2TargetGroupWithName(name)
if err != nil {
return err
}
if err := ValidateAWSElasticLoadBalancingV2TargetGroup(projectName, configName, name, template, lambdac, res); err != nil {
return err
}
case "AWS::ElasticLoadBalancingV2::Listener":
res, err := template.GetElasticLoadBalancingV2ListenerWithName(name)
if err != nil {
return err
}
if err := ValidateAWSElasticLoadBalancingV2Listener(projectName, configName, name, template, res); err != nil {
return err
}
case "AWS::ElasticLoadBalancingV2::ListenerRule":
res, err := template.GetElasticLoadBalancingV2ListenerRuleWithName(name)
if err != nil {
return err
}
if err := ValidateAWSElasticLoadBalancingV2ListenerRule(projectName, configName, name, template, res); err != nil {
return err
}
case "AWS::Lambda::Permission":
res, err := template.GetLambdaPermissionWithName(name)
if err != nil {
return err
}
if err := ValidateAWSLambdaPermission(projectName, configName, name, template, res); err != nil {
return err
}
default:
return fmt.Errorf("Unsupported type %q for %q", a.AWSCloudFormationType(), name)
}
}
return nil
}
func ValidateSubnet(sub *subnet.Subnet) error {
if sub.DeployWithFenrirTag == nil {
return fmt.Errorf("DeployWithFenrir Tag is nil")
}
return nil
}
// UTILS
func ValidateResource(prefix, projectName, configName, serviceName string, res interface {
ProjectName() *string
ConfigName() *string
ServiceName() *string
}) error {
if !(aws.HasProjectName(res, &projectName) || aws.HasAllValue(res.ProjectName())) {
return fmt.Errorf("Incorrect ProjectName for %v: has %q requires %q", prefix, to.Strs(res.ProjectName()), projectName)
}
if !(aws.HasConfigName(res, &configName) || aws.HasAllValue(res.ConfigName())) {
return fmt.Errorf("Incorrect ConfigName for %v: has %q requires %q", prefix, to.Strs(res.ConfigName()), configName)
}
if !(aws.HasServiceName(res, &serviceName) || aws.HasAllValue(res.ServiceName())) {
return fmt.Errorf("Incorrect ServiceName for %v: has %q requires %q", prefix, to.Strs(res.ServiceName()), serviceName)
}
return nil
}
func hasCorrectTags(projectName, configName string, tags map[string]string) error {
if tags["ProjectName"] == projectName && tags["ConfigName"] == configName {
return nil
}
if tags[fmt.Sprintf("FenrirAllowed:%v:%v", projectName, configName)] == "true" {
return nil
}
if tags["FenrirAllAllowed"] == "true" {
return nil
}
return fmt.Errorf("ProjectName (%v != %v) OR ConfigName (%v != %v) tags incorrect", tags["ProjectName"], projectName, tags["ConfigName"], configName)
}
func strA(strl []string) []*string {
stra := []*string{}
for _, s := range strl {
stra = append(stra, to.Strp(s))
}
return stra
}
func normalizeName(prefix, projectName, configName, resourceName string, maxLength int) string {
str := fmt.Sprintf("%v-%v-%v-%v", prefix, projectName, configName, resourceName)
str = strings.Replace(str, "/", "-", -1)
if len(str) > maxLength {
digest := sha1.Sum([]byte(str))
// Truncate to `maxLength` characters
// Replace the last 8 characters with a digest (4 bytes = 8 hex chars)
str = fmt.Sprintf("%s%x", str[:maxLength-8], digest[:4])
}
return str
}
func resourceError(resource cloudformation.Resource, name, errStr string) error {
return fmt.Errorf("%v#%v: %v", resource.AWSCloudFormationType(), name, errStr)
}