-
Notifications
You must be signed in to change notification settings - Fork 12
/
app_test.go
364 lines (302 loc) · 12.6 KB
/
app_test.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
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
/*
* Copyright (c) 2018 Uber Technologies, 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 assumerole_test
import (
"bytes"
"errors"
"fmt"
"testing"
"time"
assumerole "github.com/uber/assume-role-cli"
"github.com/uber/assume-role-cli/mocks"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var awsAccessDeniedError = awserr.New("AccessDenied", "Not authorized to perform sts:AssumeRole", errors.New("test"))
var fooCredentials = &assumerole.TemporaryCredentials{
AccessKeyID: "ABC123",
SecretAccessKey: "supersecret",
SessionToken: "123tok",
Expires: time.Now(),
}
var fooProfileWithMFA = &assumerole.ProfileConfiguration{
Expires: fooCredentials.Expires,
MFASerial: "arn:aws:iam::000000000000:mfa/bob",
RoleARN: "arn:aws:iam::000000000000:role/testRole",
RoleSessionName: "bob",
}
var fooProfileWithoutMFA = &assumerole.ProfileConfiguration{
Expires: fooCredentials.Expires,
RoleARN: "arn:aws:iam::000000000000:role/testRole-fromassumedrole",
RoleSessionName: "bob-session",
}
type test struct {
AssumeRoleMain *assumerole.App
MockAWS *mocks.MockAWSProvider
MockAWSConfig *mocks.MockAWSConfigProvider
MockClock *testClock
MockStdin *bytes.Buffer
MockStderr *bytes.Buffer
}
type testClock struct {
time time.Time
}
func (c *testClock) Now() time.Time {
return c.time
}
func (c *testClock) SetTime(t time.Time) {
c.time = t
}
func newTestAssumeRole(t *testing.T, customOptions ...assumerole.Option) *test {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockAWS := mocks.NewMockAWSProvider(mockCtrl)
mockAWSConfig := mocks.NewMockAWSConfigProvider(mockCtrl)
mockClock := &testClock{}
mockStdin := &bytes.Buffer{}
mockStderr := &bytes.Buffer{}
// Combine default test options with anything overridden for a particular
// test
testAssumeRoleOptions := append([]assumerole.Option{
assumerole.WithAWS(mockAWS),
assumerole.WithAWSConfig(mockAWSConfig),
assumerole.WithClock(mockClock),
assumerole.WithStdin(mockStdin),
assumerole.WithStderr(mockStderr),
}, customOptions...)
main, err := assumerole.NewApp(testAssumeRoleOptions...)
require.NoError(t, err)
return &test{
AssumeRoleMain: main,
MockAWS: mockAWS,
MockAWSConfig: mockAWSConfig,
MockClock: mockClock,
MockStdin: mockStdin,
MockStderr: mockStderr,
}
}
func TestAssumeRoleWithMFAFirstTime(t *testing.T) {
test := newTestAssumeRole(t)
test.MockAWS.EXPECT().CurrentPrincipalARN().Return("arn:aws:iam::000000000000:user/bob", nil)
test.MockAWS.EXPECT().Username().Return("bob", nil)
test.MockAWS.EXPECT().MFADevices().Return([]string{fooProfileWithMFA.MFASerial}, nil)
test.MockAWS.EXPECT().AssumeRole(fooProfileWithMFA.RoleARN, "bob").Return(nil, awsAccessDeniedError)
test.MockAWS.EXPECT().AssumeRoleWithMFA(fooProfileWithMFA.RoleARN, "bob", fooProfileWithMFA.MFASerial, "123456").Return(fooCredentials, nil)
test.MockAWSConfig.EXPECT().GetProfile("000000000000-testRole").Return(nil, nil)
test.MockAWSConfig.EXPECT().SetProfile("000000000000-testRole", fooProfileWithMFA).Return(nil)
test.MockAWSConfig.EXPECT().SetCredentials("000000000000-testRole", fooCredentials)
test.MockStdin.WriteString("123456" + "\n")
creds, err := test.AssumeRoleMain.AssumeRole(assumerole.AssumeRoleParameters{
UserRole: fooProfileWithMFA.RoleARN,
})
assert.NoError(t, err)
assert.Equal(t, fooCredentials, creds)
}
func TestErrorNoMFADevices(t *testing.T) {
test := newTestAssumeRole(t)
test.MockAWS.EXPECT().CurrentPrincipalARN().Return("arn:aws:iam::000000000000:user/bob", nil)
test.MockAWS.EXPECT().Username().Return("bob", nil)
test.MockAWS.EXPECT().MFADevices().Return([]string{}, nil)
test.MockAWS.EXPECT().AssumeRole(fooProfileWithMFA.RoleARN, "bob").Return(nil, awsAccessDeniedError)
test.MockAWS.EXPECT().AssumeRoleWithMFA(fooProfileWithMFA.RoleARN, "bob", fooProfileWithMFA.MFASerial, "123456").Return(fooCredentials, nil)
test.MockAWSConfig.EXPECT().GetProfile("000000000000-testRole").Return(nil, nil)
test.MockAWSConfig.EXPECT().SetProfile("000000000000-testRole", fooProfileWithMFA).Return(nil)
test.MockAWSConfig.EXPECT().SetCredentials("000000000000-testRole", fooCredentials)
test.MockStdin.WriteString("123456" + "\n")
creds, err := test.AssumeRoleMain.AssumeRole(assumerole.AssumeRoleParameters{
UserRole: fooProfileWithMFA.RoleARN,
})
require.Error(t, err)
assert.Contains(t, err.Error(), "error trying to AssumeRole without MFA")
assert.Contains(t, err.Error(), "error trying to AssumeRole with MFA")
assert.Nil(t, creds)
}
func TestMFAPromptInvalid(t *testing.T) {
test := newTestAssumeRole(t)
expectedCredentials := &assumerole.TemporaryCredentials{}
test.MockAWS.EXPECT().CurrentPrincipalARN().Return("arn:aws:iam::000000000000:user/bob", nil)
test.MockAWS.EXPECT().Username().Return("bob", nil)
test.MockAWS.EXPECT().MFADevices().Return([]string{
"foo",
"bar",
}, nil)
test.MockAWS.EXPECT().AssumeRole("arn:aws:iam::000000000000:role/testRole", "bob").Return(nil, nil)
test.MockAWS.EXPECT().AssumeRoleWithMFA("arn:aws:iam::000000000000:role/testRole", "bob", "foo", "123456").Return(expectedCredentials, nil)
test.MockAWSConfig.EXPECT().GetProfile("000000000000-testRole").Return(nil, nil)
test.MockAWSConfig.EXPECT().SetProfile("000000000000-testRole", gomock.Any()).Return(nil)
test.MockAWSConfig.EXPECT().SetCredentials("000000000000-testRole", gomock.Any()).Return(nil)
// Write responses for the prompts
test.MockStdin.WriteString("asd\n") // invalid
test.MockStdin.WriteString("3\n") // invalid
test.MockStdin.WriteString("1\n")
test.MockStdin.WriteString("123456\n")
creds, err := test.AssumeRoleMain.AssumeRole(assumerole.AssumeRoleParameters{
UserRole: "arn:aws:iam::000000000000:role/testRole",
})
require.NoError(t, err)
require.Exactly(t, expectedCredentials, creds)
assert.Equal(t, `[1]: foo
[2]: bar
Select MFA device: Invalid input (not a number)
[1]: foo
[2]: bar
Select MFA device: Invalid input (not in range)
[1]: foo
[2]: bar
Select MFA device: Enter MFA token: `, test.MockStderr.String())
}
func TestAssumeRoleWithAssumedRoleSuccess(t *testing.T) {
test := newTestAssumeRole(t)
test.MockAWS.EXPECT().CurrentPrincipalARN().Return("arn:aws:sts::000000000000:assumed-role/testRole/bob", nil)
test.MockAWS.EXPECT().AssumeRole(fooProfileWithoutMFA.RoleARN, "bob-session").Return(fooCredentials, nil)
test.MockAWSConfig.EXPECT().GetProfile("000000000000-testRole-fromassumedrole").Return(nil, nil)
test.MockAWSConfig.EXPECT().SetProfile("000000000000-testRole-fromassumedrole", fooProfileWithoutMFA).Return(nil)
test.MockAWSConfig.EXPECT().SetCredentials("000000000000-testRole-fromassumedrole", fooCredentials)
creds, err := test.AssumeRoleMain.AssumeRole(assumerole.AssumeRoleParameters{
UserRole: fooProfileWithoutMFA.RoleARN,
RoleSessionName: "bob-session",
})
assert.NoError(t, err)
assert.Equal(t, fooCredentials, creds)
}
func TestAssumeRoleWithAssumedRoleDoesNotTryMFA(t *testing.T) {
test := newTestAssumeRole(t)
test.MockAWS.EXPECT().CurrentPrincipalARN().Return("arn:aws:sts::000000000000:assumed-role/testRole/bob", nil)
test.MockAWS.EXPECT().AssumeRole(fooProfileWithoutMFA.RoleARN, "bob-session").Return(nil, awsAccessDeniedError)
test.MockAWSConfig.EXPECT().GetProfile("000000000000-testRole-fromassumedrole").Return(nil, nil)
creds, err := test.AssumeRoleMain.AssumeRole(assumerole.AssumeRoleParameters{
UserRole: fooProfileWithoutMFA.RoleARN,
RoleSessionName: "bob-session",
})
assert.Error(t, err)
assert.Nil(t, creds)
}
func TestConfigRolePrefix(t *testing.T) {
config, err := assumerole.LoadConfig("fixtures/test-config-roleprefix/assume-role.yaml")
require.NoError(t, err)
test := newTestAssumeRole(t, assumerole.WithConfig(config))
test.MockAWS.EXPECT().CurrentPrincipalARN().Return("arn:aws:iam::000000000000:user/bob", nil)
test.MockAWS.EXPECT().Username().Return("bob", nil)
test.MockAWS.EXPECT().MFADevices().Return([]string{fooProfileWithMFA.MFASerial}, nil)
test.MockAWS.EXPECT().AssumeRole(fooProfileWithMFA.RoleARN, "bob").Return(nil, nil)
test.MockAWS.EXPECT().AssumeRoleWithMFA(fooProfileWithMFA.RoleARN, "bob", fooProfileWithMFA.MFASerial, "123456").Return(fooCredentials, nil)
test.MockAWSConfig.EXPECT().GetProfile("foobar-testRole").Return(nil, nil)
test.MockAWSConfig.EXPECT().SetProfile("foobar-testRole", fooProfileWithMFA).Return(nil)
test.MockAWSConfig.EXPECT().SetCredentials("foobar-testRole", fooCredentials)
test.MockStdin.WriteString("123456" + "\n")
creds, err := test.AssumeRoleMain.AssumeRole(assumerole.AssumeRoleParameters{
UserRole: "testRole",
})
assert.NoError(t, err)
assert.Equal(t, fooCredentials, creds)
}
func TestCredentialsExpiry(t *testing.T) {
mockNow := time.Date(2018, 04, 23, 23, 45, 43, 0, time.UTC)
mockCreds := &assumerole.TemporaryCredentials{}
config := &assumerole.Config{
RefreshBeforeExpiry: 5 * time.Minute,
}
tests := []struct {
credentialExpiry time.Time
expectRefresh bool
forceRefresh bool
}{
{
// credentials are expiring exactly now
credentialExpiry: mockNow,
expectRefresh: true,
},
{
// expired 1s ago
credentialExpiry: mockNow.Add(-time.Second),
expectRefresh: true,
},
{
// expiring in 3m
credentialExpiry: mockNow.Add(3 * time.Minute),
// should trigger a refresh, because it is within the refresh
// horizon even though it's not expired yet.
expectRefresh: true,
},
{
// expiring in 10m (still valid)
credentialExpiry: mockNow.Add(10 * time.Minute),
expectRefresh: false,
},
{
// expiring in 10m (still valid), but force refresh
credentialExpiry: mockNow.Add(10 * time.Minute),
expectRefresh: true,
forceRefresh: true,
},
{
// expired 20m ago
credentialExpiry: mockNow.Add(-20 * time.Minute),
expectRefresh: true,
},
{
// expired 20m ago
// check if force refresh doesn't mess up refreshing with expired creds
credentialExpiry: mockNow.Add(-20 * time.Minute),
expectRefresh: true,
forceRefresh: true,
},
}
for i, tt := range tests {
test := newTestAssumeRole(t, assumerole.WithConfig(config))
// Base expectations
test.MockAWS.EXPECT().CurrentPrincipalARN().Return("arn:aws:iam::000000000000:user/bob", nil)
test.MockAWS.EXPECT().Username().Return("bob", nil)
test.MockAWSConfig.EXPECT().GetProfile("123-testRole").Return(&assumerole.ProfileConfiguration{
Expires: tt.credentialExpiry,
}, nil)
test.MockAWSConfig.EXPECT().SetProfile("123-testRole", gomock.Any()).Return(nil)
test.MockAWSConfig.EXPECT().SetCredentials("123-testRole", gomock.Any()).Return(nil)
// Set mock time.Now()
test.MockClock.SetTime(mockNow)
if tt.expectRefresh {
// If we're expecting a refresh, the app should call out to AWS's
// AssumeRole, and get the credentials back.
test.MockAWS.EXPECT().AssumeRole(gomock.Any(), gomock.Any()).Return(mockCreds, nil)
} else {
// If there's no refresh, there should be no AssumeRole call.
test.MockAWS.EXPECT().AssumeRole(gomock.Any(), gomock.Any()).Do(func(roleARN string, sessionName string) {
assert.Fail(t, fmt.Sprintf("unexpected credentials refresh; table test index: %d", i))
})
// Credentials should be fetched from cache.
test.MockAWSConfig.EXPECT().GetCredentials(gomock.Any()).Return(mockCreds, nil)
}
creds, err := test.AssumeRoleMain.AssumeRole(assumerole.AssumeRoleParameters{
ForceRefresh: tt.forceRefresh,
UserRole: "arn:aws:iam::123:role/testRole",
})
assert.NoError(t, err)
assert.Equal(t, mockCreds, creds)
}
}
func TestCurrentRoleIsAssumedRole(t *testing.T) {
test := newTestAssumeRole(t)
test.MockAWS.EXPECT().CurrentPrincipalARN().Return("arn:aws:iam::000000000000:user/bob", nil)
isAssumedRole, err := test.AssumeRoleMain.CurrentPrincipalIsAssumedRole()
assert.NoError(t, err)
assert.Equal(t, false, isAssumedRole)
test.MockAWS.EXPECT().CurrentPrincipalARN().Return("arn:aws:sts::000000000000:assumed-role/testRole/bob", nil)
isAssumedRole, err = test.AssumeRoleMain.CurrentPrincipalIsAssumedRole()
assert.NoError(t, err)
assert.Equal(t, true, isAssumedRole)
}