@@ -24,40 +24,47 @@ import (
24
24
)
25
25
26
26
const (
27
- BridgeNetworkMode = "bridge"
28
- AWSVPCNetworkMode = "awsvpc"
29
- invalidEgressConfigFormat = `no service connect %s in the egress config. %s`
30
- portCollisionFormat = `%s port collision detected in the ingress config with the %s port=%d, and listener name=%s`
31
- invalidIngressPortFormat = `the %s port=%d in the ingress config is not valid: %w`
32
- warningIngressPortFormat = `Service connect config validation: %s port should not exist for %s mode in the ingress config`
33
- invalidDnsEntryFormat = `no %s in the DNS config hostname=%s, address=%s`
27
+ BridgeNetworkMode = "bridge"
28
+ AWSVPCNetworkMode = "awsvpc"
29
+ missingContainerInTaskFormat = `service connect container name=%s does not exist in the task`
30
+ duplicateContainerInTaskFormat = `found %d duplicate service connect container name=%s in the task`
31
+ invalidCidrFormat = `CIDR=%s is not a valid %s CIDR`
32
+ portCollisionFormat = `%s port collision detected in the ingress config with %s port=%d, and listener name=%s`
33
+ invalidDnsAddressFormat = `hostname=%s, address=%s in the DNS config is not valid: %w`
34
+ noScSupportNetworkModeFormat = `service connect does not support for %s newtork mode`
35
+ missingListenerInIngressFormat = `missing listener name in the ingress config with intercept port=%d`
36
+ invalidPortRangeFormat = `port=%d is not a valid port. A valid port ranges from 1 through 65535`
37
+ invalidIpAddressFormat = `address=%s is not a valid IP address`
38
+ invalidIngressPortFormat = `%s port=%d in the ingress config is not valid: %w`
39
+ warningIngressPortFormat = `Service connect config: %s port should not exist in the ingress config for %s network mode`
40
+ missingDnsEntryFormat = `missing %s in the DNS config with hostname=%s, and address=%s`
34
41
)
35
42
36
- // validateContainerName validates the service connect container name.
43
+ // validateContainerName validates the service connect container name exists in the task and no duplication .
37
44
func validateContainerName (scContainerName string , taskContainers []* ecsacs.Container ) error {
38
45
// service connect container name is required
39
46
if scContainerName == "" {
40
47
return fmt .Errorf ("missing service connect container name" )
41
48
}
42
49
43
- // validate the specified service connect container name exists in the task definition
44
- numOfFoundSCContainer := 0
50
+ // validate the specified service connect container name exists in the task
51
+ numOfFoundScContainer := 0
45
52
for _ , container := range taskContainers {
46
53
if aws .StringValue (container .Name ) == scContainerName {
47
- numOfFoundSCContainer += 1
54
+ numOfFoundScContainer += 1
48
55
}
49
56
}
50
57
51
- if numOfFoundSCContainer == 0 {
52
- return fmt .Errorf ("service connect container name=%s does not exist in the task" , scContainerName )
53
- } else if numOfFoundSCContainer > 1 {
54
- return fmt .Errorf ("found %d duplicate service connect container name=%s exist in the task" , numOfFoundSCContainer , scContainerName )
58
+ if numOfFoundScContainer == 0 {
59
+ return fmt .Errorf (missingContainerInTaskFormat , scContainerName )
60
+ } else if numOfFoundScContainer > 1 {
61
+ return fmt .Errorf (duplicateContainerInTaskFormat , numOfFoundScContainer , scContainerName )
55
62
}
56
63
57
64
return nil
58
65
}
59
66
60
- // validateEgressConfig validates the service connect egress config.
67
+ // validateEgressConfig validates the listener name, IPv4 CIDR format and IPv6 CIDR format in the service connect egress config.
61
68
func validateEgressConfig (scEgressConfig * EgressConfig , ipv6Enabled bool ) error {
62
69
// egress config can be empty for the first service since there are no other tasks that it can talk to
63
70
if scEgressConfig == nil {
@@ -66,32 +73,21 @@ func validateEgressConfig(scEgressConfig *EgressConfig, ipv6Enabled bool) error
66
73
67
74
// ListenerName is required if the egress config exists
68
75
if scEgressConfig .ListenerName == "" {
69
- return fmt .Errorf (invalidEgressConfigFormat , "listener name" , "" )
70
- }
71
-
72
- // VIP is required if the egress config exists
73
- // IPV4CIDR should be always required because an IPv6-only mode is not supoorted at this moment
74
- if scEgressConfig .VIP .IPV4CIDR == "" {
75
- return fmt .Errorf (invalidEgressConfigFormat , "VIP IPv4CIDR" , "" )
76
- }
77
-
78
- // IPV6CIDR is required when IPv6 is enabled
79
- if ipv6Enabled && scEgressConfig .VIP .IPV6CIDR == "" {
80
- return fmt .Errorf (invalidEgressConfigFormat , "VIP IPv6CIDR" , "It must not be empty when the task is IPv6 enabled" )
76
+ return fmt .Errorf ("missing listener name in the egress config" )
81
77
}
82
78
83
79
// validate IPV4CIDR if it exists
84
80
if scEgressConfig .VIP .IPV4CIDR != "" {
85
- trimmedIpv4cidr := strings .TrimSpace (scEgressConfig .VIP .IPV4CIDR )
86
- if err := validateCIDR (trimmedIpv4cidr , "IPv4" ); err != nil {
81
+ trimmedIpv4Cidr := strings .TrimSpace (scEgressConfig .VIP .IPV4CIDR )
82
+ if err := validateCIDR (trimmedIpv4Cidr , "IPv4" ); err != nil {
87
83
return err
88
84
}
89
85
}
90
86
91
87
// validate IPV6CIDR if it exists
92
88
if scEgressConfig .VIP .IPV6CIDR != "" {
93
- trimmedIpv6cidr := strings .TrimSpace (scEgressConfig .VIP .IPV6CIDR )
94
- if err := validateCIDR (trimmedIpv6cidr , "IPv6" ); err != nil {
89
+ trimmedIpv6Cidr := strings .TrimSpace (scEgressConfig .VIP .IPV6CIDR )
90
+ if err := validateCIDR (trimmedIpv6Cidr , "IPv6" ); err != nil {
95
91
return err
96
92
}
97
93
}
@@ -108,7 +104,7 @@ func validateCIDR(cidr, protocol string) error {
108
104
}
109
105
}
110
106
111
- return fmt .Errorf ("cidr=%s is not a valid %s CIDR" , cidr , protocol )
107
+ return fmt .Errorf (invalidCidrFormat , cidr , protocol )
112
108
}
113
109
114
110
// getProtocol returns validity of the given IP based on the target protocol.
@@ -128,27 +124,22 @@ func getProtocol(ip net.IP, protocol string) bool {
128
124
return false
129
125
}
130
126
131
- // validateDnsConfig validates the service connnect DNS config.
132
- func validateDnsConfig (scDnsConfligList []DNSConfigEntry , scEgressConfig * EgressConfig , ipv6Enabled bool ) error {
133
- // DNS config associates to egress config
134
- if len (scDnsConfligList ) == 0 && scEgressConfig != nil {
135
- return fmt .Errorf ("no service connect DNS config. The DNS config is required when the egress config exists" )
136
- }
137
-
127
+ // validateDnsConfig validates hostnames and addresses in the service connnect DNS config.
128
+ func validateDnsConfig (scDnsConfligList []DNSConfigEntry ) error {
138
129
for _ , dnsEntry := range scDnsConfligList {
139
130
// HostName is required
140
131
if dnsEntry .HostName == "" {
141
- return fmt .Errorf (invalidDnsEntryFormat , "hostname" , dnsEntry .HostName , dnsEntry .Address )
132
+ return fmt .Errorf (missingDnsEntryFormat , "hostname" , dnsEntry .HostName , dnsEntry .Address )
142
133
}
143
134
144
135
// Address is required
145
136
if dnsEntry .Address == "" {
146
- return fmt .Errorf (invalidDnsEntryFormat , "address" , dnsEntry .HostName , dnsEntry .Address )
137
+ return fmt .Errorf (missingDnsEntryFormat , "address" , dnsEntry .HostName , dnsEntry .Address )
147
138
}
148
139
149
140
// validate the address is a valid IPv4/IPv6 address
150
141
if err := validateAddress (dnsEntry .Address ); err != nil {
151
- return fmt .Errorf ("invalid address in the DNS config hostname=%s, address=%s: %w" , dnsEntry .HostName , dnsEntry .Address , err )
142
+ return fmt .Errorf (invalidDnsAddressFormat , dnsEntry .HostName , dnsEntry .Address , err )
152
143
}
153
144
}
154
145
@@ -158,7 +149,7 @@ func validateDnsConfig(scDnsConfligList []DNSConfigEntry, scEgressConfig *Egress
158
149
// validateAddress validates the passed address is a valid IPv4/IPv6 address.
159
150
func validateAddress (address string ) error {
160
151
if ip := net .ParseIP (address ); ip == nil {
161
- return fmt .Errorf ("address=%s is not a valid IP address" , address )
152
+ return fmt .Errorf (invalidIpAddressFormat , address )
162
153
}
163
154
return nil
164
155
}
@@ -176,7 +167,7 @@ func validateIngressConfig(scIngressConfigList []IngressConfigEntry, taskNetwork
176
167
return err
177
168
}
178
169
default :
179
- return fmt .Errorf ("service connect does not support for %s newtork mode" , taskNetworkMode )
170
+ return fmt .Errorf (noScSupportNetworkModeFormat , taskNetworkMode )
180
171
}
181
172
182
173
return nil
@@ -215,7 +206,7 @@ func validateIngressConfigEntry(scIngressConfigList []IngressConfigEntry, networ
215
206
if err := validateInterceptPort (interceptPortValue , entry .ListenerName , interceptAndListenerPortsMap ); err != nil {
216
207
return err
217
208
}
218
- // Save the listener port value
209
+ // save the listener port value
219
210
interceptAndListenerPortsMap [interceptPortValue ] = true
220
211
}
221
212
@@ -225,7 +216,7 @@ func validateIngressConfigEntry(scIngressConfigList []IngressConfigEntry, networ
225
216
if err := validateListenerPort (listenerPortValue , entry .ListenerName , interceptAndListenerPortsMap ); err != nil {
226
217
return err
227
218
}
228
- // Save the listener port value
219
+ // save the listener port value
229
220
interceptAndListenerPortsMap [listenerPortValue ] = true
230
221
}
231
222
@@ -235,7 +226,7 @@ func validateIngressConfigEntry(scIngressConfigList []IngressConfigEntry, networ
235
226
if err := validateHostPort (hostPortValue , entry .ListenerName , hostPortsMap ); err != nil {
236
227
return err
237
228
}
238
- // Save the host port value
229
+ // save the host port value
239
230
hostPortsMap [hostPortValue ] = true
240
231
}
241
232
}
@@ -250,7 +241,7 @@ func validateInterceptPort(interceptPortValue uint16, listenerName string, inter
250
241
}
251
242
252
243
if listenerName == "" {
253
- return fmt .Errorf ("no listener name in the ingress config with the intercept port=%d" , interceptPortValue )
244
+ return fmt .Errorf (missingListenerInIngressFormat , interceptPortValue )
254
245
}
255
246
256
247
if present := interceptAndListenerPortsMap [interceptPortValue ]; present {
@@ -293,10 +284,13 @@ func validatePort(port uint16) error {
293
284
return nil
294
285
}
295
286
296
- return fmt .Errorf ("the port=%d is an invalid port. A valid port ranges from 1 through 65535" , port )
287
+ return fmt .Errorf (invalidPortRangeFormat , port )
297
288
}
298
289
299
- // ValidateSCConfig validates service connect container name, config, egress config, and ingress config.
290
+ // ValidateServiceConnectConfig validates service connect container name,
291
+ // fields in egress config, dns config and ingress config when
292
+ // 1) fields consumed and proceeded by ECS Agent
293
+ // 2) fields with a global standard, e.g. CIDR format
300
294
func ValidateServiceConnectConfig (scConfig * Config ,
301
295
taskContainers []* ecsacs.Container ,
302
296
taskNetworkMode string ,
@@ -305,16 +299,11 @@ func ValidateServiceConnectConfig(scConfig *Config,
305
299
return err
306
300
}
307
301
308
- // egress config and ingress config should not both be nil/empty
309
- if scConfig .EgressConfig == nil && len (scConfig .IngressConfig ) == 0 {
310
- return fmt .Errorf ("egress config and ingress config should not both be nil/empty" )
311
- }
312
-
313
302
if err := validateEgressConfig (scConfig .EgressConfig , ipv6Enabled ); err != nil {
314
303
return err
315
304
}
316
305
317
- if err := validateDnsConfig (scConfig .DNSConfig , scConfig . EgressConfig , ipv6Enabled ); err != nil {
306
+ if err := validateDnsConfig (scConfig .DNSConfig ); err != nil {
318
307
return err
319
308
}
320
309
0 commit comments