-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathdescribe_component_nested_authmanager_test.go
More file actions
577 lines (499 loc) · 20.8 KB
/
describe_component_nested_authmanager_test.go
File metadata and controls
577 lines (499 loc) · 20.8 KB
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package exec
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"github.com/cloudposse/atmos/pkg/auth/types"
"github.com/cloudposse/atmos/pkg/schema"
)
// setupMockStateGetter configures the global stateGetter to return mock values
// for terraform state lookups. This allows tests to run without actual terraform state files.
// Returns a cleanup function that must be deferred to restore the original state getter.
func setupMockStateGetter(t *testing.T, ctrl *gomock.Controller) func() {
t.Helper()
mockStateGetter := NewMockTerraformStateGetter(ctrl)
originalGetter := stateGetter
// Configure mock to return values for all components in the fixture.
// Level 3 components (no dependencies).
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "level3-component", "subnet_id", gomock.Any(), gomock.Any(), gomock.Any()).
Return("subnet-level3-12345", nil).
AnyTimes()
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "level3-component", "cidr_block", gomock.Any(), gomock.Any(), gomock.Any()).
Return("10.0.3.0/24", nil).
AnyTimes()
// Level 2 components.
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "level2-component", "vpc_id", gomock.Any(), gomock.Any(), gomock.Any()).
Return("vpc-level2-67890", nil).
AnyTimes()
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "level2-component", "level3_subnet_id", gomock.Any(), gomock.Any(), gomock.Any()).
Return("subnet-level3-12345", nil).
AnyTimes()
// Auth override scenario components.
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "auth-override-level3", "database_host", gomock.Any(), gomock.Any(), gomock.Any()).
Return("db.example.com", nil).
AnyTimes()
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "auth-override-level2", "service_name", gomock.Any(), gomock.Any(), gomock.Any()).
Return("api-service", nil).
AnyTimes()
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "auth-override-level2", "database_config", gomock.Any(), gomock.Any(), gomock.Any()).
Return("db.example.com", nil).
AnyTimes()
// Multi-auth scenario components.
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "multi-auth-level3", "shared_resource_id", gomock.Any(), gomock.Any(), gomock.Any()).
Return("shared-12345", nil).
AnyTimes()
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "multi-auth-level2", "vpc_id", gomock.Any(), gomock.Any(), gomock.Any()).
Return("vpc-account-b", nil).
AnyTimes()
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "multi-auth-level2", "shared_resource", gomock.Any(), gomock.Any(), gomock.Any()).
Return("shared-12345", nil).
AnyTimes()
// Mixed inheritance scenario components.
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "mixed-inherit-component", "config_value", gomock.Any(), gomock.Any(), gomock.Any()).
Return("inherited-auth-config", nil).
AnyTimes()
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "mixed-override-component", "override_value", gomock.Any(), gomock.Any(), gomock.Any()).
Return("override-specific-value", nil).
AnyTimes()
// Deep nesting scenario components.
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "deep-level4", "data_source", gomock.Any(), gomock.Any(), gomock.Any()).
Return("primary-db", nil).
AnyTimes()
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "deep-level3", "data_ref", gomock.Any(), gomock.Any(), gomock.Any()).
Return("primary-db", nil).
AnyTimes()
mockStateGetter.EXPECT().
GetState(gomock.Any(), gomock.Any(), "test", "deep-level2", "nested_data", gomock.Any(), gomock.Any(), gomock.Any()).
Return("primary-db", nil).
AnyTimes()
stateGetter = mockStateGetter
return func() { stateGetter = originalGetter }
}
// TestNestedAuthManagerPropagation verifies that AuthManager propagates correctly
// through multiple levels of nested !terraform.state YAML functions.
//
// Nesting structure:
//
// Level 1: level1-component
// └─ !terraform.state level2-component ... (triggers level2 evaluation)
// Level 2: level2-component
// └─ !terraform.state level3-component ... (triggers level3 evaluation)
// Level 3: level3-component (base, no nested functions)
//
// Without the fix:
// - Level 2 and 3 would have nil AuthManager
// - YAML functions would fail with IMDS timeout errors
//
// With the fix:
// - AuthManager flows through all levels
// - All nested evaluations have AuthContext
// - No IMDS timeout errors occur.
func TestNestedAuthManagerPropagation(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// Setup mock state getter to return expected values without actual terraform state.
cleanup := setupMockStateGetter(t, ctrl)
defer cleanup()
// Setup: Create AuthContext with AWS credentials.
expectedAuthContext := &schema.AuthContext{
AWS: &schema.AWSAuthContext{
Profile: "test-nested-identity",
Region: "us-east-1",
CredentialsFile: "/tmp/test-nested-creds",
ConfigFile: "/tmp/test-nested-config",
},
}
// Create stackInfo with AuthContext (what AuthManager.Authenticate() populates).
authStackInfo := &schema.ConfigAndStacksInfo{
AuthContext: expectedAuthContext,
Stack: "test",
}
// Create mock AuthManager that returns our authStackInfo.
mockAuthManager := types.NewMockAuthManager(ctrl)
mockAuthManager.EXPECT().
GetStackInfo().
Return(authStackInfo).
AnyTimes()
// Use the nested propagation fixture.
workDir := "../../tests/fixtures/scenarios/authmanager-nested-propagation"
t.Chdir(workDir)
// Test Level 1 component which has nested !terraform.state functions
// that reference level2-component.
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: "level1-component",
Stack: "test",
ProcessTemplates: true, // Enable to process templates
ProcessYamlFunctions: true, // Enable to test nested functions
Skip: nil,
AuthManager: mockAuthManager,
})
// Verify the function succeeded.
// If AuthManager propagation fails, we'd see IMDS timeout errors here.
require.NoError(t, err, "ExecuteDescribeComponent should succeed with nested functions")
require.NotNil(t, componentSection, "Result should not be nil")
// Verify component configuration is present.
assert.NotEmpty(t, componentSection, "Component should have configuration")
// Verify the nested values were resolved (they won't actually exist in real state,
// but the YAML parsing should complete without IMDS errors).
vars, ok := componentSection["vars"].(map[string]any)
require.True(t, ok, "vars should be a map")
assert.Contains(t, vars, "level2_vpc_id", "Should have level2_vpc_id from nested function")
assert.Contains(t, vars, "level2_subnet", "Should have level2_subnet from doubly-nested function")
}
// TestNestedAuthManagerPropagationLevel2 tests starting from Level 2 component
// to verify AuthManager still propagates to Level 3.
func TestNestedAuthManagerPropagationLevel2(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// Setup mock state getter to return expected values without actual terraform state.
cleanup := setupMockStateGetter(t, ctrl)
defer cleanup()
expectedAuthContext := &schema.AuthContext{
AWS: &schema.AWSAuthContext{
Profile: "test-level2-identity",
Region: "us-west-2",
CredentialsFile: "/tmp/test-level2-creds",
ConfigFile: "/tmp/test-level2-config",
},
}
authStackInfo := &schema.ConfigAndStacksInfo{
AuthContext: expectedAuthContext,
Stack: "test",
}
mockAuthManager := types.NewMockAuthManager(ctrl)
mockAuthManager.EXPECT().
GetStackInfo().
Return(authStackInfo).
AnyTimes()
workDir := "../../tests/fixtures/scenarios/authmanager-nested-propagation"
t.Chdir(workDir)
// Test Level 2 component which references level3-component.
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: "level2-component",
Stack: "test",
ProcessTemplates: true,
ProcessYamlFunctions: true,
Skip: nil,
AuthManager: mockAuthManager,
})
require.NoError(t, err, "Level 2 should work with nested AuthManager")
require.NotNil(t, componentSection)
vars, ok := componentSection["vars"].(map[string]any)
require.True(t, ok)
assert.Contains(t, vars, "level3_subnet_id", "Should have nested value from level3")
}
// TestNestedAuthManagerPropagationLevel3 tests the base component (no nesting).
func TestNestedAuthManagerPropagationLevel3(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
expectedAuthContext := &schema.AuthContext{
AWS: &schema.AWSAuthContext{
Profile: "test-level3-identity",
Region: "us-east-1",
},
}
authStackInfo := &schema.ConfigAndStacksInfo{
AuthContext: expectedAuthContext,
Stack: "test",
}
mockAuthManager := types.NewMockAuthManager(ctrl)
mockAuthManager.EXPECT().
GetStackInfo().
Return(authStackInfo).
AnyTimes()
workDir := "../../tests/fixtures/scenarios/authmanager-nested-propagation"
t.Chdir(workDir)
// Test Level 3 component (base, no nested functions).
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: "level3-component",
Stack: "test",
ProcessTemplates: false,
ProcessYamlFunctions: false,
Skip: nil,
AuthManager: mockAuthManager,
})
require.NoError(t, err, "Level 3 base component should work")
require.NotNil(t, componentSection)
// Verify base component has expected vars.
vars, ok := componentSection["vars"].(map[string]any)
require.True(t, ok)
assert.Equal(t, "test", vars["stage"])
assert.Contains(t, vars, "subnet_id")
}
// TestNestedAuthManagerNilHandling verifies graceful handling of nil cases
// in nested scenarios.
func TestNestedAuthManagerNilHandling(t *testing.T) {
workDir := "../../tests/fixtures/scenarios/authmanager-nested-propagation"
t.Chdir(workDir)
tests := []struct {
name string
component string
authManager types.AuthManager
description string
}{
{
name: "level1_nil_authmanager",
component: "level1-component",
authManager: nil,
description: "Level 1 should work when AuthManager is nil",
},
{
name: "level2_nil_authmanager",
component: "level2-component",
authManager: nil,
description: "Level 2 should work when AuthManager is nil",
},
{
name: "level3_nil_authmanager",
component: "level3-component",
authManager: nil,
description: "Level 3 should work when AuthManager is nil",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: tt.component,
Stack: "test",
ProcessTemplates: false, // Disable to avoid state access
ProcessYamlFunctions: false, // Disable to avoid state access
Skip: nil,
AuthManager: tt.authManager,
})
require.NoError(t, err, tt.description)
require.NotNil(t, componentSection)
})
}
}
// TestNestedAuthManagerWithNilStackInfo verifies handling when AuthManager
// returns nil stackInfo in nested scenarios.
func TestNestedAuthManagerWithNilStackInfo(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockAuthManager := types.NewMockAuthManager(ctrl)
mockAuthManager.EXPECT().
GetStackInfo().
Return(nil).
AnyTimes()
workDir := "../../tests/fixtures/scenarios/authmanager-nested-propagation"
t.Chdir(workDir)
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: "level1-component",
Stack: "test",
ProcessTemplates: false,
ProcessYamlFunctions: false,
Skip: nil,
AuthManager: mockAuthManager,
})
require.NoError(t, err, "Should handle nil stackInfo in nested scenarios")
require.NotNil(t, componentSection)
}
// TestNestedAuthManagerWithNilAuthContext verifies handling when AuthContext
// is nil in nested scenarios.
func TestNestedAuthManagerWithNilAuthContext(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// Create stackInfo without AuthContext.
authStackInfo := &schema.ConfigAndStacksInfo{
AuthContext: nil,
Stack: "test",
}
mockAuthManager := types.NewMockAuthManager(ctrl)
mockAuthManager.EXPECT().
GetStackInfo().
Return(authStackInfo).
AnyTimes()
workDir := "../../tests/fixtures/scenarios/authmanager-nested-propagation"
t.Chdir(workDir)
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: "level1-component",
Stack: "test",
ProcessTemplates: false,
ProcessYamlFunctions: false,
Skip: nil,
AuthManager: mockAuthManager,
})
require.NoError(t, err, "Should handle nil AuthContext in nested scenarios")
require.NotNil(t, componentSection)
}
// setupNestedAuthTest creates a mock AuthManager and sets up the test directory.
// Returns the mock controller, AuthManager, and cleanup function for use in nested auth tests.
func setupNestedAuthTest(t *testing.T, profile, region string) (*gomock.Controller, types.AuthManager, func()) {
t.Helper()
ctrl := gomock.NewController(t)
// Setup mock state getter to return expected values without actual terraform state.
stateCleanup := setupMockStateGetter(t, ctrl)
parentAuthContext := &schema.AuthContext{
AWS: &schema.AWSAuthContext{
Profile: profile,
Region: region,
},
}
parentStackInfo := &schema.ConfigAndStacksInfo{
AuthContext: parentAuthContext,
Stack: "test",
}
mockAuthManager := types.NewMockAuthManager(ctrl)
mockAuthManager.EXPECT().
GetStackInfo().
Return(parentStackInfo).
AnyTimes()
workDir := "../../tests/fixtures/scenarios/authmanager-nested-propagation"
t.Chdir(workDir)
return ctrl, mockAuthManager, stateCleanup
}
// TestNestedAuthManagerScenario2_AuthOverrideAtMiddleLevel verifies that
// a component in the middle of a nested chain can override authentication.
//
// Nesting structure:
//
// Level 1: auth-override-level1 (no auth override, uses parent)
// └─ !terraform.state auth-override-level2 ... (triggers level2 evaluation)
// Level 2: auth-override-level2 (HAS auth override → account 222222222222)
// └─ !terraform.state auth-override-level3 ... (triggers level3 evaluation)
// Level 3: auth-override-level3 (no auth override, inherits from level2)
//
// Expected Behavior:
// - Level 1 uses parent AuthManager
// - Level 2 creates new AuthManager with account 222222222222
// - Level 3 inherits Level 2's AuthManager
// - No IMDS timeout errors at any level.
func TestNestedAuthManagerScenario2_AuthOverrideAtMiddleLevel(t *testing.T) {
ctrl, mockAuthManager, stateCleanup := setupNestedAuthTest(t, "parent-profile", "us-east-1")
defer ctrl.Finish()
defer stateCleanup()
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: "auth-override-level1",
Stack: "test",
ProcessTemplates: true,
ProcessYamlFunctions: true,
Skip: nil,
AuthManager: mockAuthManager,
})
require.NoError(t, err, "Should succeed with auth override in middle level")
require.NotNil(t, componentSection)
vars, ok := componentSection["vars"].(map[string]any)
require.True(t, ok, "Should have vars section")
assert.Contains(t, vars, "api_endpoint", "Should have api_endpoint from nested level2")
assert.Contains(t, vars, "api_database", "Should have api_database from doubly-nested level3")
}
// TestNestedAuthManagerScenario3_MultipleAuthOverrides verifies that
// multiple components in a chain can each have their own auth overrides.
//
// Nesting structure:
//
// Level 1: multi-auth-level1 (auth override → account 444444444444)
// └─ !terraform.state multi-auth-level2 ... (triggers level2 evaluation)
// Level 2: multi-auth-level2 (auth override → account 333333333333)
// └─ !terraform.state multi-auth-level3 ... (triggers level3 evaluation)
// Level 3: multi-auth-level3 (no auth override, inherits from level2)
//
// Expected Behavior:
// - Level 1 uses account 444444444444 (AccountCAccess)
// - Level 2 uses account 333333333333 (AccountBAccess) - its own override
// - Level 3 uses account 333333333333 (inherited from Level 2)
// - Each level with auth config creates its own AuthManager.
func TestNestedAuthManagerScenario3_MultipleAuthOverrides(t *testing.T) {
ctrl, mockAuthManager, stateCleanup := setupNestedAuthTest(t, "global-profile", "us-east-1")
defer ctrl.Finish()
defer stateCleanup()
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: "multi-auth-level1",
Stack: "test",
ProcessTemplates: true,
ProcessYamlFunctions: true,
Skip: nil,
AuthManager: mockAuthManager,
})
require.NoError(t, err, "Should succeed with multiple auth overrides")
require.NotNil(t, componentSection)
vars, ok := componentSection["vars"].(map[string]any)
require.True(t, ok, "Should have vars section")
assert.Contains(t, vars, "vpc_reference", "Should have vpc_reference from level2")
assert.Contains(t, vars, "shared_reference", "Should have shared_reference from level3")
}
// TestNestedAuthManagerScenario4_MixedInheritance verifies that in the same
// top-level component, some nested components can override auth while others inherit.
//
// Nesting structure:
//
// mixed-top-level (no auth override, uses parent auth)
// ├─ !terraform.state mixed-inherit-component ... (inherits parent auth)
// └─ !terraform.state mixed-override-component ... (uses its own auth)
// └─ !terraform.state mixed-inherit-component ... (inherits mixed-override's auth)
//
// Expected Behavior:
// - mixed-top-level uses parent AuthManager
// - mixed-inherit-component inherits parent AuthManager when called directly
// - mixed-override-component uses account 555555555555 (its own auth)
// - When mixed-override calls mixed-inherit, it inherits account 555555555555.
func TestNestedAuthManagerScenario4_MixedInheritance(t *testing.T) {
ctrl, mockAuthManager, stateCleanup := setupNestedAuthTest(t, "parent-profile", "us-west-2")
defer ctrl.Finish()
defer stateCleanup()
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: "mixed-top-level",
Stack: "test",
ProcessTemplates: true,
ProcessYamlFunctions: true,
Skip: nil,
AuthManager: mockAuthManager,
})
require.NoError(t, err, "Should succeed with mixed auth inheritance")
require.NotNil(t, componentSection)
vars, ok := componentSection["vars"].(map[string]any)
require.True(t, ok, "Should have vars section")
assert.Contains(t, vars, "value_from_inherit", "Should have value from inherit component")
assert.Contains(t, vars, "value_from_override", "Should have value from override component")
}
// TestNestedAuthManagerScenario5_DeepNesting verifies authentication in
// 4-level deep nesting with auth overrides at non-adjacent levels.
//
// Nesting structure:
//
// Level 1: deep-level1 (auth override → account 777777777777)
// └─ !terraform.state deep-level2 ... (triggers level2 evaluation)
// Level 2: deep-level2 (no auth override, inherits from level1)
// └─ !terraform.state deep-level3 ... (triggers level3 evaluation)
// Level 3: deep-level3 (auth override → account 666666666666)
// └─ !terraform.state deep-level4 ... (triggers level4 evaluation)
// Level 4: deep-level4 (no auth override, inherits from level3)
//
// Expected Behavior:
// - Level 1 uses account 777777777777 (Level1Access)
// - Level 2 inherits account 777777777777 from Level 1
// - Level 3 switches to account 666666666666 (Level3Access) - its own override
// - Level 4 inherits account 666666666666 from Level 3.
func TestNestedAuthManagerScenario5_DeepNesting(t *testing.T) {
ctrl, mockAuthManager, stateCleanup := setupNestedAuthTest(t, "global-profile", "us-east-1")
defer ctrl.Finish()
defer stateCleanup()
componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{
Component: "deep-level1",
Stack: "test",
ProcessTemplates: true,
ProcessYamlFunctions: true,
Skip: nil,
AuthManager: mockAuthManager,
})
require.NoError(t, err, "Should succeed with 4-level deep nesting and multiple auth overrides")
require.NotNil(t, componentSection)
vars, ok := componentSection["vars"].(map[string]any)
require.True(t, ok, "Should have vars section")
assert.Contains(t, vars, "final_data", "Should have final_data from 4-level deep nesting")
}