forked from spinnaker/keel
/
SecurityGroupHandler.kt
404 lines (377 loc) · 13.9 KB
/
SecurityGroupHandler.kt
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright 2018 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.keel.ec2.resource
import com.netflix.spinnaker.keel.api.Exportable
import com.netflix.spinnaker.keel.api.Moniker
import com.netflix.spinnaker.keel.api.Resource
import com.netflix.spinnaker.keel.api.ResourceDiff
import com.netflix.spinnaker.keel.api.SimpleLocations
import com.netflix.spinnaker.keel.api.SimpleRegionSpec
import com.netflix.spinnaker.keel.api.actuation.Task
import com.netflix.spinnaker.keel.api.actuation.TaskLauncher
import com.netflix.spinnaker.keel.api.ec2.AllPorts
import com.netflix.spinnaker.keel.api.ec2.CLOUD_PROVIDER
import com.netflix.spinnaker.keel.api.ec2.CidrRule
import com.netflix.spinnaker.keel.api.ec2.CrossAccountReferenceRule
import com.netflix.spinnaker.keel.api.ec2.EC2_SECURITY_GROUP_V1
import com.netflix.spinnaker.keel.api.ec2.IngressPorts
import com.netflix.spinnaker.keel.api.ec2.PortRange
import com.netflix.spinnaker.keel.api.ec2.ReferenceRule
import com.netflix.spinnaker.keel.api.ec2.SecurityGroup
import com.netflix.spinnaker.keel.api.ec2.SecurityGroupOverride
import com.netflix.spinnaker.keel.api.ec2.SecurityGroupRule
import com.netflix.spinnaker.keel.api.ec2.SecurityGroupRule.Protocol
import com.netflix.spinnaker.keel.api.ec2.SecurityGroupSpec
import com.netflix.spinnaker.keel.api.id
import com.netflix.spinnaker.keel.api.plugins.Resolver
import com.netflix.spinnaker.keel.api.plugins.ResourceHandler
import com.netflix.spinnaker.keel.api.serviceAccount
import com.netflix.spinnaker.keel.clouddriver.CloudDriverCache
import com.netflix.spinnaker.keel.clouddriver.CloudDriverService
import com.netflix.spinnaker.keel.clouddriver.ResourceNotFound
import com.netflix.spinnaker.keel.clouddriver.model.SecurityGroupModel
import com.netflix.spinnaker.keel.diff.DefaultResourceDiff
import com.netflix.spinnaker.keel.diff.toIndividualDiffs
import com.netflix.spinnaker.keel.model.Job
import com.netflix.spinnaker.keel.orca.OrcaService
import com.netflix.spinnaker.keel.retrofit.isNotFound
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import retrofit2.HttpException
class SecurityGroupHandler(
private val cloudDriverService: CloudDriverService,
private val cloudDriverCache: CloudDriverCache,
private val orcaService: OrcaService,
private val taskLauncher: TaskLauncher,
resolvers: List<Resolver<*>>
) : ResourceHandler<SecurityGroupSpec, Map<String, SecurityGroup>>(resolvers) {
override val supportedKind = EC2_SECURITY_GROUP_V1
override suspend fun toResolvedType(resource: Resource<SecurityGroupSpec>): Map<String, SecurityGroup> =
with(resource.spec) {
locations.regions.map { region ->
region.name to SecurityGroup(
moniker = Moniker(app = moniker.app, stack = moniker.stack, detail = moniker.detail),
location = SecurityGroup.Location(
account = locations.account,
vpc = locations.vpc ?: error("No vpc supplied or resolved"),
region = region.name
),
description = overrides[region.name]?.description ?: description,
inboundRules = overrides[region.name]?.inboundRules ?: inboundRules
)
}.toMap()
}
override suspend fun current(resource: Resource<SecurityGroupSpec>): Map<String, SecurityGroup> =
cloudDriverService.getSecurityGroup(resource.spec, resource.serviceAccount)
override suspend fun upsert(
resource: Resource<SecurityGroupSpec>,
resourceDiff: ResourceDiff<Map<String, SecurityGroup>>
): List<Task> =
coroutineScope {
resourceDiff
.toIndividualDiffs()
.filter { diff -> diff.hasChanges() }
.map { diff ->
val spec = diff.desired
val job: Job
val verb: Pair<String, String>
when (diff.current) {
null -> {
job = spec.toCreateJob()
verb = Pair("Create", "create")
}
else -> {
job = spec.toUpdateJob()
verb = Pair("Update", "update")
}
}
log.info("${verb.first} security group using task: $job")
async {
taskLauncher.submitJob(
resource = resource,
description = "${verb.first} security group ${spec.moniker} in ${spec.location.account}/${spec.location.region}",
correlationId = "${resource.id}:${spec.location.region}",
job = job
)
}
}
.map { it.await() }
}
override suspend fun export(exportable: Exportable): SecurityGroupSpec {
val summaries = exportable.regions.associateWith { region ->
try {
cloudDriverCache.securityGroupByName(
account = exportable.account,
region = region,
name = exportable.moniker.toString()
)
} catch (e: ResourceNotFound) {
null
}
}
.filterValues { it != null }
val securityGroups =
coroutineScope {
summaries.map { (region, summary) ->
async {
try {
cloudDriverService.getSecurityGroup(
exportable.user,
exportable.account,
CLOUD_PROVIDER,
summary!!.name,
region,
summary.vpcId
)
.toSecurityGroup()
} catch (e: HttpException) {
if (e.isNotFound) {
null
} else {
throw e
}
}
}
}
.mapNotNull { it.await() }
.associateBy { it.location.region }
}
if (securityGroups.isEmpty()) {
throw ResourceNotFound("Could not find security group: ${exportable.moniker} " +
"in account: ${exportable.account}")
}
val base = securityGroups.values.first()
val spec = SecurityGroupSpec(
moniker = base.moniker,
locations = SimpleLocations(
account = exportable.account,
vpc = base.location.vpc,
regions = securityGroups.keys.map {
SimpleRegionSpec(it)
}
.toSet()
),
description = base.description,
inboundRules = base.inboundRules,
overrides = mutableMapOf()
)
spec.generateOverrides(securityGroups)
return spec
}
private fun SecurityGroupSpec.generateOverrides(
regionalGroups: Map<String, SecurityGroup>
) =
regionalGroups.forEach { (region, securityGroup) ->
val inboundDiff =
DefaultResourceDiff(securityGroup.inboundRules, this.inboundRules)
.hasChanges()
val vpcDiff = securityGroup.location.vpc != this.locations.vpc
val descriptionDiff = securityGroup.description != this.description
if (inboundDiff || vpcDiff || descriptionDiff) {
(this.overrides as MutableMap)[region] = SecurityGroupOverride(
vpc = if (vpcDiff) {
securityGroup.location.vpc
} else {
null
},
description = if (descriptionDiff) {
securityGroup.description
} else {
null
},
inboundRules = if (inboundDiff) {
securityGroup.inboundRules
} else {
null
}
)
}
}
override suspend fun actuationInProgress(resource: Resource<SecurityGroupSpec>): Boolean =
resource
.spec
.locations
.regions
.map { it.name }
.any { region ->
orcaService
.getCorrelatedExecutions("${resource.id}:$region")
.isNotEmpty()
}
private suspend fun CloudDriverService.getSecurityGroup(
spec: SecurityGroupSpec,
serviceAccount: String
): Map<String, SecurityGroup> =
coroutineScope {
spec.locations.regions.map { region ->
async {
try {
getSecurityGroup(
serviceAccount,
spec.locations.account,
CLOUD_PROVIDER,
spec.moniker.toString(),
region.name,
cloudDriverCache.networkBy(spec.locations.vpc, spec.locations.account, region.name).id
)
.toSecurityGroup()
} catch (e: HttpException) {
if (e.isNotFound) {
null
} else {
throw e
}
}
}
}
.mapNotNull { it.await() }
.associateBy { it.location.region }
}
private fun SecurityGroupModel.toSecurityGroup() =
SecurityGroup(
moniker = Moniker(app = moniker.app, stack = moniker.stack, detail = moniker.detail),
location = SecurityGroup.Location(
account = accountName,
vpc = vpcId?.let { cloudDriverCache.networkBy(it).name }
?: error("Only security groups in a VPC are supported"),
region = region
),
description = description,
inboundRules = inboundRules.flatMap { rule ->
val ingressGroup = rule.securityGroup
val ingressRange = rule.range
val protocol = rule.protocol!!.clouddriverProtocolToKeel()
when {
ingressGroup != null -> rule.portRanges
?.map { it.toPortRange() }
?.map { portRange ->
when {
ingressGroup.accountName != accountName || ingressGroup.vpcId != vpcId -> CrossAccountReferenceRule(
protocol,
ingressGroup.name,
ingressGroup.accountName!!,
cloudDriverCache.networkBy(ingressGroup.vpcId!!).name!!,
portRange
)
else -> ReferenceRule(
protocol,
if (ingressGroup.name == name) null else ingressGroup.name, // if it's a self-referential rule keel models the name as null
portRange
)
}
} ?: emptyList()
ingressRange != null -> rule.portRanges
?.map { it.toPortRange() }
?.map { portRange ->
CidrRule(
protocol,
portRange,
ingressRange.ip + ingressRange.cidr
)
} ?: emptyList()
else -> emptyList()
}
}
.toSet()
)
private fun SecurityGroupModel.SecurityGroupRulePortRange.toPortRange(): IngressPorts =
(startPort to endPort).let { (start, end) ->
if (start == null || end == null) AllPorts else PortRange(start, end)
}
private fun String.clouddriverProtocolToKeel(): Protocol =
if (this == "-1") Protocol.ALL else Protocol.valueOf(toUpperCase())
private fun SecurityGroup.toCreateJob(): Job =
Job(
"upsertSecurityGroup",
mapOf(
"application" to moniker.app,
"credentials" to location.account,
"cloudProvider" to CLOUD_PROVIDER,
"name" to moniker.toString(),
"regions" to listOf(location.region),
"vpcId" to cloudDriverCache.networkBy(location.vpc, location.account, location.region).id,
"description" to description,
"securityGroupIngress" to inboundRules
// we have to do a 2-phase create for self-referencing ingress rules as the referenced
// security group must exist prior to the rule being applied. We filter then out here and
// the subsequent diff will apply the additional group(s).
.filterNot { it.isSelfReference }
.mapNotNull {
it.referenceRuleToJob(this)
},
"ipIngress" to inboundRules.mapNotNull {
it.cidrRuleToJob()
},
"accountName" to location.account
)
)
private fun SecurityGroup.toUpdateJob(): Job =
Job(
"upsertSecurityGroup",
mapOf(
"application" to moniker.app,
"credentials" to location.account,
"cloudProvider" to CLOUD_PROVIDER,
"name" to moniker.toString(),
"regions" to listOf(location.region),
"vpcId" to cloudDriverCache.networkBy(location.vpc, location.account, location.region).id,
"description" to description,
"securityGroupIngress" to inboundRules.mapNotNull {
it.referenceRuleToJob(this)
},
"ipIngress" to inboundRules.mapNotNull {
it.cidrRuleToJob()
},
"accountName" to location.account
)
)
private fun SecurityGroupRule.referenceRuleToJob(securityGroup: SecurityGroup): Map<String, Any?>? =
when (this) {
is ReferenceRule -> mapOf(
"type" to protocol.type,
"startPort" to (portRange as? PortRange)?.startPort,
"endPort" to (portRange as? PortRange)?.endPort,
"name" to (name ?: securityGroup.moniker.toString())
)
is CrossAccountReferenceRule -> mapOf(
"type" to protocol.type,
"startPort" to (portRange as? PortRange)?.startPort,
"endPort" to (portRange as? PortRange)?.endPort,
"name" to name,
"accountName" to account,
"crossAccountEnabled" to true,
"vpcId" to cloudDriverCache.networkBy(
vpc,
account,
securityGroup.location.region
).id
)
else -> null
}
private fun SecurityGroupRule.cidrRuleToJob(): Map<String, Any?>? =
when (this) {
is CidrRule ->
mapOf<String, Any?>(
"type" to protocol.type,
"startPort" to (portRange as? PortRange)?.startPort,
"endPort" to (portRange as? PortRange)?.endPort,
"cidr" to blockRange
)
else -> null
}
private val Protocol.type: String
get() = if (this == Protocol.ALL) "-1" else name.toLowerCase()
}