/
AllowLaunchAtomicOperationUnitSpec.groovy
353 lines (300 loc) · 13.4 KB
/
AllowLaunchAtomicOperationUnitSpec.groovy
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
/*
* Copyright 2014 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.clouddriver.aws.deploy.ops
import com.amazonaws.services.ec2.AmazonEC2
import com.amazonaws.services.ec2.model.CreateTagsRequest
import com.amazonaws.services.ec2.model.DeleteTagsRequest
import com.amazonaws.services.ec2.model.DescribeImagesRequest
import com.amazonaws.services.ec2.model.DescribeImagesResult
import com.amazonaws.services.ec2.model.DescribeTagsResult
import com.amazonaws.services.ec2.model.Image
import com.amazonaws.services.ec2.model.ModifyImageAttributeRequest
import com.amazonaws.services.ec2.model.Tag
import com.amazonaws.services.ec2.model.TagDescription
import com.netflix.spinnaker.clouddriver.aws.TestCredential
import com.netflix.spinnaker.clouddriver.aws.deploy.description.AllowLaunchDescription
import com.netflix.spinnaker.clouddriver.aws.security.AmazonClientProvider
import com.netflix.spinnaker.clouddriver.aws.security.NetflixAmazonCredentials
import com.netflix.spinnaker.clouddriver.data.task.Task
import com.netflix.spinnaker.clouddriver.data.task.TaskRepository
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsProvider
import spock.lang.Specification
class AllowLaunchAtomicOperationUnitSpec extends Specification {
def setupSpec() {
TaskRepository.threadLocalTask.set(Mock(Task))
}
void "image amiId is resolved from name"() {
setup:
def ec2 = Mock(AmazonEC2)
def provider = Stub(AmazonClientProvider) {
getAmazonEC2(_, _, true) >> ec2
}
def target = Stub(NetflixAmazonCredentials) {
getAccountId() >> '12345'
}
def source = Stub(NetflixAmazonCredentials) {
getAccountId() >> '67890'
}
def creds = Stub(AccountCredentialsProvider) {
getCredentials('target') >> target
}
def op = new AllowLaunchAtomicOperation(new AllowLaunchDescription(amiName: 'super-awesome-ami', targetAccount: 'target', credentials: source))
op.accountCredentialsProvider = creds
op.amazonClientProvider = provider
when:
op.operate([])
then:
ec2.describeTags(_) >> new DescribeTagsResult()
1 * ec2.describeImages(_) >> { DescribeImagesRequest dir ->
assert dir.executableUsers
assert dir.executableUsers.size() == 1
assert dir.executableUsers.first() == '12345'
assert dir.filters
assert dir.filters.size() == 1
assert dir.filters.first().name == 'name'
assert dir.filters.first().values == ['super-awesome-ami']
new DescribeImagesResult().withImages(new Image().withImageId('ami-12345').withOwnerId('67890'))
}
}
void "image attribute modification is invoked on request"() {
setup:
def prodCredentials = TestCredential.named('prod')
def testCredentials = TestCredential.named('test')
def sourceAmazonEc2 = Mock(AmazonEC2) {
describeTags(_) >> new DescribeTagsResult()
describeImages(_) >> new DescribeImagesResult().withImages(new Image().withImageId('ami-123456').withOwnerId(testCredentials.accountId))
}
def targetAmazonEc2 = Mock(AmazonEC2) {
describeTags(_) >> new DescribeTagsResult()
describeImages(_) >> null
}
def provider = Mock(AmazonClientProvider)
def description = new AllowLaunchDescription(targetAccount: "prod", amiName: "ami-123456", region: "us-west-1", credentials: testCredentials)
def op = new AllowLaunchAtomicOperation(description)
op.amazonClientProvider = provider
op.accountCredentialsProvider = Mock(AccountCredentialsProvider)
when:
op.operate([])
then:
with(op.accountCredentialsProvider){
1 * getCredentials("prod") >> prodCredentials
}
with(provider) {
1 * getAmazonEC2(testCredentials, _, true) >> sourceAmazonEc2
1 * getAmazonEC2(prodCredentials, _, true) >> targetAmazonEc2
}
with(sourceAmazonEc2) {
1 * modifyImageAttribute(_) >> { ModifyImageAttributeRequest request ->
assert request.launchPermission.add.get(0).userId == prodCredentials.accountId
}
}
}
void "should replicate tags"() {
def prodCredentials = TestCredential.named('prod')
def testCredentials = TestCredential.named('test')
def sourceAmazonEc2 = Mock(AmazonEC2)
def targetAmazonEc2 = Mock(AmazonEC2)
def provider = Mock(AmazonClientProvider)
def description = new AllowLaunchDescription(targetAccount: "prod", amiName: "ami-123456", region: "us-west-1", credentials: testCredentials)
def op = new AllowLaunchAtomicOperation(description)
op.amazonClientProvider = provider
op.accountCredentialsProvider = Mock(AccountCredentialsProvider)
when:
op.operate([])
then:
with(op.accountCredentialsProvider){
1 * getCredentials("prod") >> prodCredentials
}
with(provider) {
1 * getAmazonEC2(testCredentials, _, true) >> sourceAmazonEc2
1 * getAmazonEC2(prodCredentials, _, true) >> targetAmazonEc2
}
with(sourceAmazonEc2) {
1 * describeImages(_) >> new DescribeImagesResult().withImages(new Image().withImageId("ami-123456").withOwnerId(testCredentials.accountId))
1 * modifyImageAttribute(_)
1 * describeTags(_) >> constructDescribeTagsResult([a:"1", b: "2"])
}
with(targetAmazonEc2) {
1 * describeTags(_) >> constructDescribeTagsResult([a:"1", b:"1", c: "2"])
1 * deleteTags(new DeleteTagsRequest(resources: ["ami-123456"], tags: [new Tag(key: "b", value: "1"), new Tag(key: "c", value: "2")]))
1 * createTags(new CreateTagsRequest(resources: ["ami-123456"], tags: [new Tag(key: "b", value: "2")]))
}
}
void "should skip tag replication when target account is the same as the requesting account"() {
def testCredentials = TestCredential.named('test')
def sourceAmazonEc2 = Mock(AmazonEC2)
def targetAmazonEc2 = Mock(AmazonEC2)
def provider = Mock(AmazonClientProvider)
def description = new AllowLaunchDescription(targetAccount: "test", amiName: "ami-123456", region: "us-west-1", credentials: testCredentials)
def op = new AllowLaunchAtomicOperation(description)
op.amazonClientProvider = provider
op.accountCredentialsProvider = Mock(AccountCredentialsProvider)
when:
op.operate([])
then:
with(op.accountCredentialsProvider){
1 * getCredentials("test") >> testCredentials
}
with(provider) {
1 * getAmazonEC2(testCredentials, _, true) >> sourceAmazonEc2
1 * getAmazonEC2(testCredentials, _, true) >> targetAmazonEc2
}
with(targetAmazonEc2) {
1 * describeImages(_) >> new DescribeImagesResult().withImages(new Image().withImageId("ami-123456").withOwnerId(testCredentials.accountId))
}
0 * _
}
void "should lookup owner account of resolved ami"() {
setup:
def ownerCredentials = TestCredential.named('owner')
def sourceCredentials = TestCredential.named('source')
def targetCredentials = TestCredential.named('target')
def ownerAmazonEc2 = Mock(AmazonEC2)
def sourceAmazonEc2 = Mock(AmazonEC2)
def targetAmazonEc2 = Mock(AmazonEC2)
def description = new AllowLaunchDescription(targetAccount: 'target', amiName: 'ami-123456', region: 'us-west-1', credentials: sourceCredentials)
def op = new AllowLaunchAtomicOperation(description)
op.amazonClientProvider = Mock(AmazonClientProvider)
op.accountCredentialsProvider = Mock(AccountCredentialsProvider)
when:
op.operate([])
then:
with(op.accountCredentialsProvider) {
1 * getCredentials('target') >> targetCredentials
1 * getAll() >> [sourceCredentials, targetCredentials, ownerCredentials]
}
with(op.amazonClientProvider) {
1 * getAmazonEC2(sourceCredentials, _, true) >> sourceAmazonEc2
1 * getAmazonEC2(targetCredentials, _, true) >> targetAmazonEc2
1 * getAmazonEC2(ownerCredentials, _, true) >> ownerAmazonEc2
}
with(sourceAmazonEc2) {
1 * describeImages(_) >> new DescribeImagesResult().withImages(new Image().withImageId("ami-123456").withOwnerId(ownerCredentials.accountId))
}
with(ownerAmazonEc2) {
1 * modifyImageAttribute(_)
1 * describeTags(_) >> constructDescribeTagsResult([a:"1", b: "2"])
}
with(targetAmazonEc2) {
1 * describeTags(_) >> constructDescribeTagsResult([a:"1", b:"1", c: "2"])
1 * deleteTags(new DeleteTagsRequest(resources: ["ami-123456"], tags: [new Tag(key: "b", value: "1"), new Tag(key: "c", value: "2")]))
1 * createTags(new CreateTagsRequest(resources: ["ami-123456"], tags: [new Tag(key: "b", value: "2")]))
}
}
void "should return resolved public AMI without performing additional operations"() {
setup:
def ownerCredentials = TestCredential.named('owner')
def targetCredentials = TestCredential.named('target')
def ownerAmazonEc2 = Mock(AmazonEC2)
def targetAmazonEc2 = Mock(AmazonEC2)
def description = new AllowLaunchDescription(targetAccount: 'target', amiName: 'ami-123456', region: 'us-west-1', credentials: ownerCredentials)
def op = new AllowLaunchAtomicOperation(description)
op.amazonClientProvider = Mock(AmazonClientProvider)
op.accountCredentialsProvider = Mock(AccountCredentialsProvider)
when:
op.operate([])
then:
with(op.accountCredentialsProvider) {
1 * getCredentials('target') >> targetCredentials
}
with(op.amazonClientProvider) {
1 * getAmazonEC2(targetCredentials, _, true) >> targetAmazonEc2
1 * getAmazonEC2(ownerCredentials, _, true) >> ownerAmazonEc2
}
with(targetAmazonEc2) {
1 * describeImages(_) >> new DescribeImagesResult().withImages(
new Image()
.withImageId("ami-123456")
.withOwnerId(ownerCredentials.accountId)
.withPublic(true))
}
0 * _
}
void 'should return resolved third-party private AMI when account allows'() {
setup:
def sourceCredentials = TestCredential.named('source')
def targetCredentials = TestCredential.named('target', [allowPrivateThirdPartyImages: true])
def sourceAmazonEc2 = Mock(AmazonEC2)
def targetAmazonEc2 = Mock(AmazonEC2)
def description = new AllowLaunchDescription(targetAccount: 'target', amiName: 'ami-123456', region: 'us-west-2', credentials: sourceCredentials)
def op = new AllowLaunchAtomicOperation(description)
op.amazonClientProvider = Mock(AmazonClientProvider)
op.accountCredentialsProvider = Mock(AccountCredentialsProvider)
when:
op.operate([])
then:
with(op.accountCredentialsProvider) {
1 * getCredentials('target') >> targetCredentials
}
with(op.amazonClientProvider) {
1 * getAmazonEC2(targetCredentials, _, true) >> targetAmazonEc2
1 * getAmazonEC2(sourceCredentials, _, true) >> sourceAmazonEc2
}
with(targetAmazonEc2) {
3 * describeImages(_) >> new DescribeImagesResult()
}
with(sourceAmazonEc2) {
3 * describeImages(_) >> new DescribeImagesResult()
}
with(targetAmazonEc2) {
1 * describeImages(_) >> new DescribeImagesResult().withImages(
new Image()
.withImageId('ami-123456')
.withOwnerId('thirdparty')
)
}
}
Closure<DescribeTagsResult> constructDescribeTagsResult = { Map tags ->
new DescribeTagsResult(tags: tags.collect {new TagDescription(key: it.key, value: it.value) })
}
void "should skip allow launch on resolved target AMI if found but still perform tag syncing"() {
setup:
def ownerCredentials = TestCredential.named('owner')
def targetCredentials = TestCredential.named('target')
def ownerAmazonEc2 = Mock(AmazonEC2)
def targetAmazonEc2 = Mock(AmazonEC2)
def description = new AllowLaunchDescription(targetAccount: 'target', amiName: 'ami-123456', region: 'us-west-1', credentials: ownerCredentials)
def op = new AllowLaunchAtomicOperation(description)
op.amazonClientProvider = Mock(AmazonClientProvider)
op.accountCredentialsProvider = Mock(AccountCredentialsProvider)
when:
op.operate([])
then:
with(op.accountCredentialsProvider) {
1 * getCredentials('target') >> targetCredentials
}
with(op.amazonClientProvider) {
1 * getAmazonEC2(targetCredentials, _, true) >> targetAmazonEc2
1 * getAmazonEC2(ownerCredentials, _, true) >> ownerAmazonEc2
}
with(targetAmazonEc2) {
1 * describeImages(_) >> new DescribeImagesResult().withImages(
new Image()
.withImageId("ami-123456")
.withOwnerId(ownerCredentials.accountId))
1 * describeTags(_) >> new DescribeTagsResult().withTags(new TagDescription().withKey("existingTag").withValue("existingValue"))
1 * createTags(_) >> { CreateTagsRequest ctr ->
assert ctr.getTags().size() == 1
assert ctr.getTags().get(0).getKey() == "missingTag"
assert ctr.getTags().get(0).getValue() == "missingValue"
}
}
with (ownerAmazonEc2) {
1 * describeTags(_) >> new DescribeTagsResult().withTags(new TagDescription().withKey("existingTag").withValue("existingValue"), new TagDescription().withKey("missingTag").withValue("missingValue"))
}
0 * _
}
}