-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathhooks_test.go
More file actions
521 lines (475 loc) · 15.3 KB
/
hooks_test.go
File metadata and controls
521 lines (475 loc) · 15.3 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
package auth
import (
"context"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/cloudposse/atmos/pkg/auth/types"
log "github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/schema"
)
// stubAuthManager implements types.AuthManager for focused unit tests.
type stubAuthManager struct {
defaultIdentity string
defaultErr error
whoami *types.WhoamiInfo
envVars map[string]string // Environment variables to return from GetEnvironmentVariables
}
func (s *stubAuthManager) Authenticate(ctx context.Context, identityName string) (*types.WhoamiInfo, error) {
return s.whoami, nil
}
func (s *stubAuthManager) Whoami(ctx context.Context, identityName string) (*types.WhoamiInfo, error) {
return s.whoami, nil
}
func (s *stubAuthManager) GetCachedCredentials(ctx context.Context, identityName string) (*types.WhoamiInfo, error) {
return s.whoami, nil
}
func (s *stubAuthManager) AuthenticateProvider(ctx context.Context, providerName string) (*types.WhoamiInfo, error) {
return nil, nil
}
func (s *stubAuthManager) Validate() error { return nil }
func (s *stubAuthManager) GetDefaultIdentity(_ bool) (string, error) {
return s.defaultIdentity, s.defaultErr
}
func (s *stubAuthManager) ListIdentities() []string { return []string{"one", "two"} }
func (s *stubAuthManager) GetProviderForIdentity(identityName string) string { return "prov" }
func (s *stubAuthManager) GetFilesDisplayPath(providerName string) string { return "~/.aws/atmos" }
func (s *stubAuthManager) GetProviderKindForIdentity(identityName string) (string, error) {
return "kind", nil
}
func (s *stubAuthManager) GetChain() []string { return []string{"prov", "id"} }
func (s *stubAuthManager) GetStackInfo() *schema.ConfigAndStacksInfo {
return &schema.ConfigAndStacksInfo{}
}
func (s *stubAuthManager) ListProviders() []string { return []string{"prov"} }
func (s *stubAuthManager) GetIdentities() map[string]schema.Identity {
return map[string]schema.Identity{}
}
func (s *stubAuthManager) GetProviders() map[string]schema.Provider {
return map[string]schema.Provider{}
}
func (s *stubAuthManager) Logout(ctx context.Context, identityName string, deleteKeychain bool) error {
return nil
}
func (s *stubAuthManager) LogoutProvider(ctx context.Context, providerName string, deleteKeychain bool) error {
return nil
}
func (s *stubAuthManager) LogoutAll(ctx context.Context, deleteKeychain bool) error {
return nil
}
func (s *stubAuthManager) GetEnvironmentVariables(identityName string) (map[string]string, error) {
if s.envVars != nil {
return s.envVars, nil
}
return make(map[string]string), nil
}
func (s *stubAuthManager) PrepareShellEnvironment(ctx context.Context, identityName string, currentEnv []string) ([]string, error) {
// Merge envVars into currentEnv.
// This simulates what the real PrepareShellEnvironment does.
envMap := make(map[string]string)
// Parse currentEnv into map.
for _, envVar := range currentEnv {
if idx := strings.IndexByte(envVar, '='); idx >= 0 {
key := envVar[:idx]
value := envVar[idx+1:]
envMap[key] = value
}
}
// Merge in envVars from stub.
for k, v := range s.envVars {
envMap[k] = v
}
// Convert back to list.
result := make([]string, 0, len(envMap))
for k, v := range envMap {
result = append(result, k+"="+v)
}
return result, nil
}
func (s *stubAuthManager) ExecuteIntegration(ctx context.Context, integrationName string) error {
return nil
}
func (s *stubAuthManager) ExecuteIdentityIntegrations(ctx context.Context, identityName string) error {
return nil
}
func (s *stubAuthManager) GetIntegration(integrationName string) (*schema.Integration, error) {
return nil, nil
}
func (s *stubAuthManager) ResolvePrincipalSetting(identityName, key string) (interface{}, bool) {
return nil, false
}
func (s *stubAuthManager) ResolveProviderConfig(identityName string) (*schema.Provider, bool) {
return nil, false
}
func TestGetConfigLogLevels(t *testing.T) {
tests := []struct {
name string
atmosLogLevel string
authLogLevel string
setupGlobalLevel log.Level // Set global log level before calling getConfigLogLevels.
expectedAtmosStr string
expectedAuthStr string
}{
{
name: "nil config falls back to Info",
setupGlobalLevel: log.InfoLevel,
expectedAtmosStr: "info",
expectedAuthStr: "info",
},
{
name: "empty config falls back to current global level",
setupGlobalLevel: log.WarnLevel,
atmosLogLevel: "",
authLogLevel: "",
expectedAtmosStr: "warn",
expectedAuthStr: "warn",
},
{
name: "exact case Debug",
setupGlobalLevel: log.DebugLevel,
atmosLogLevel: "Debug",
authLogLevel: "",
expectedAtmosStr: "debug",
expectedAuthStr: "debug",
},
{
name: "lowercase warning",
setupGlobalLevel: log.WarnLevel,
atmosLogLevel: "Warning",
authLogLevel: "warning",
expectedAtmosStr: "warn",
expectedAuthStr: "warn",
},
{
name: "uppercase WARN",
setupGlobalLevel: log.WarnLevel,
atmosLogLevel: "Warning",
authLogLevel: "WARN",
expectedAtmosStr: "warn",
expectedAuthStr: "warn",
},
{
name: "mixed case WaRnInG",
setupGlobalLevel: log.WarnLevel,
atmosLogLevel: "Warning",
authLogLevel: "WaRnInG",
expectedAtmosStr: "warn",
expectedAuthStr: "warn",
},
{
name: "warn alias",
setupGlobalLevel: log.WarnLevel,
atmosLogLevel: "Warning",
authLogLevel: "warn",
expectedAtmosStr: "warn",
expectedAuthStr: "warn",
},
{
name: "auth overrides atmos level",
setupGlobalLevel: log.DebugLevel,
atmosLogLevel: "Debug",
authLogLevel: "Error",
expectedAtmosStr: "debug",
expectedAuthStr: "error",
},
{
name: "trace level",
setupGlobalLevel: log.TraceLevel,
atmosLogLevel: "Trace",
authLogLevel: "trace",
expectedAtmosStr: "trace",
expectedAuthStr: "trace",
},
{
name: "off level",
setupGlobalLevel: log.FatalLevel,
atmosLogLevel: "Off",
authLogLevel: "Off",
expectedAtmosStr: "fatal",
expectedAuthStr: "fatal",
},
{
name: "invalid auth level falls back to atmos level",
setupGlobalLevel: log.WarnLevel,
atmosLogLevel: "Warning",
authLogLevel: "InvalidLevel",
expectedAtmosStr: "warn",
expectedAuthStr: "warn", // Falls back to atmos level.
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up global log level to simulate what setupLogger() does.
log.SetLevel(tt.setupGlobalLevel)
var cfg *schema.AtmosConfiguration
if tt.name == "nil config falls back to Info" {
cfg = nil
} else {
cfg = &schema.AtmosConfiguration{
Logs: schema.Logs{
Level: tt.atmosLogLevel,
},
Auth: schema.AuthConfig{
Logs: schema.Logs{
Level: tt.authLogLevel,
},
},
}
}
atmos, auth := getConfigLogLevels(cfg)
assert.Equal(t, tt.expectedAtmosStr, log.LevelToString(atmos),
"Atmos log level mismatch for config: atmos=%q, auth=%q", tt.atmosLogLevel, tt.authLogLevel)
assert.Equal(t, tt.expectedAuthStr, log.LevelToString(auth),
"Auth log level mismatch for config: atmos=%q, auth=%q", tt.atmosLogLevel, tt.authLogLevel)
})
}
}
func TestDecodeAuthConfigFromStack(t *testing.T) {
// Success with minimal providers/identities map.
stack := &schema.ConfigAndStacksInfo{
ComponentAuthSection: schema.AtmosSectionMapType{
"providers": map[string]any{
"aws-sso": map[string]any{
"kind": "aws/iam-identity-center",
"region": "us-east-1",
"start_url": "https://example.awsapps.com/start",
},
},
"identities": map[string]any{
"dev": map[string]any{
"kind": "aws/permission-set",
"via": map[string]any{
"provider": "aws-sso",
},
"principal": map[string]any{
"name": "Developer",
"account": map[string]any{
"name": "dev",
},
},
},
},
},
}
cfg, err := decodeAuthConfigFromStack(stack)
assert.NoError(t, err)
assert.Contains(t, cfg.Providers, "aws-sso")
assert.Contains(t, cfg.Identities, "dev")
// Invalid type should surface ErrInvalidAuthConfig.
bad := &schema.ConfigAndStacksInfo{ComponentAuthSection: schema.AtmosSectionMapType{"providers": 42}}
_, err = decodeAuthConfigFromStack(bad)
assert.Error(t, err)
}
func TestResolveTargetIdentityName(t *testing.T) {
// Directly specified on stack wins.
stack := &schema.ConfigAndStacksInfo{Identity: "explicit"}
name, err := resolveTargetIdentityName(stack, &stubAuthManager{defaultIdentity: "default"})
assert.NoError(t, err)
assert.Equal(t, "explicit", name)
// Fallback to manager default.
stack.Identity = ""
name, err = resolveTargetIdentityName(stack, &stubAuthManager{defaultIdentity: "team"})
assert.NoError(t, err)
assert.Equal(t, "team", name)
// Manager error returns ErrDefaultIdentity.
_, err = resolveTargetIdentityName(stack, &stubAuthManager{defaultErr: errors.New("boom")})
assert.Error(t, err)
// Manager returns empty default -> ErrNoDefaultIdentity.
_, err = resolveTargetIdentityName(stack, &stubAuthManager{defaultIdentity: ""})
assert.Error(t, err)
}
func TestAuthenticateAndWriteEnv(t *testing.T) {
m := &stubAuthManager{whoami: &types.WhoamiInfo{Provider: "p", Identity: "i"}}
atmosCfg := &schema.AtmosConfiguration{}
stack := &schema.ConfigAndStacksInfo{ComponentEnvSection: schema.AtmosSectionMapType{"FOO": "BAR"}}
err := authenticateAndWriteEnv(context.Background(), m, "dev", atmosCfg, stack)
assert.NoError(t, err)
}
func TestAuthenticateAndWriteEnv_AddsEnvironmentVariables(t *testing.T) {
tests := []struct {
name string
envVars map[string]string
initialEnvSection schema.AtmosSectionMapType
expectedKeys []string
}{
{
name: "adds AWS environment variables to empty section",
envVars: map[string]string{
"AWS_CONFIG_FILE": "/path/to/config",
"AWS_SHARED_CREDENTIALS_FILE": "/path/to/creds",
"AWS_PROFILE": "my-profile",
"AWS_REGION": "us-east-1",
"AWS_EC2_METADATA_DISABLED": "true",
},
initialEnvSection: nil,
expectedKeys: []string{"AWS_CONFIG_FILE", "AWS_SHARED_CREDENTIALS_FILE", "AWS_PROFILE", "AWS_REGION", "AWS_EC2_METADATA_DISABLED"},
},
{
name: "merges with existing environment variables",
envVars: map[string]string{
"AWS_PROFILE": "my-profile",
"AWS_REGION": "us-west-2",
},
initialEnvSection: schema.AtmosSectionMapType{
"EXISTING_VAR": "value",
"TF_VAR_foo": "bar",
},
expectedKeys: []string{"EXISTING_VAR", "TF_VAR_foo", "AWS_PROFILE", "AWS_REGION"},
},
{
name: "handles no environment variables from identity",
envVars: map[string]string{},
initialEnvSection: schema.AtmosSectionMapType{"FOO": "BAR"},
expectedKeys: []string{"FOO"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create stub manager that returns our test env vars.
stub := &stubAuthManager{
whoami: &types.WhoamiInfo{Provider: "test-provider", Identity: "test-identity"},
}
// Override GetEnvironmentVariables to return test data.
stub.envVars = tt.envVars
atmosCfg := &schema.AtmosConfiguration{}
stack := &schema.ConfigAndStacksInfo{
ComponentEnvSection: tt.initialEnvSection,
}
err := authenticateAndWriteEnv(context.Background(), stub, "test-identity", atmosCfg, stack)
assert.NoError(t, err)
// Verify all expected keys are present in ComponentEnvSection.
assert.NotNil(t, stack.ComponentEnvSection, "ComponentEnvSection should not be nil")
for _, key := range tt.expectedKeys {
assert.Contains(t, stack.ComponentEnvSection, key, "ComponentEnvSection should contain %s", key)
}
// Verify the values match what we provided.
for k, expectedValue := range tt.envVars {
actualValue, exists := stack.ComponentEnvSection[k]
assert.True(t, exists, "Expected key %s to exist in ComponentEnvSection", k)
assert.Equal(t, expectedValue, actualValue, "Value mismatch for key %s", k)
}
})
}
}
func TestTerraformPreHook_NoAuthConfigEarlyExit(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
stack := &schema.ConfigAndStacksInfo{ComponentAuthSection: schema.AtmosSectionMapType{}}
err := TerraformPreHook(atmosCfg, stack)
assert.NoError(t, err)
}
func TestTerraformPreHook_InvalidAuthConfig(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
// Malformed auth section.
stack := &schema.ConfigAndStacksInfo{ComponentAuthSection: schema.AtmosSectionMapType{"providers": 42}}
err := TerraformPreHook(atmosCfg, stack)
assert.Error(t, err)
}
// TestComponentEnvSectionToList tests conversion from ComponentEnvSection map to env list.
func TestComponentEnvSectionToList(t *testing.T) {
tests := []struct {
name string
envSection map[string]any
validate func(t *testing.T, result []string)
}{
{
name: "nil map",
envSection: nil,
validate: func(t *testing.T, result []string) {
assert.Empty(t, result)
},
},
{
name: "empty map",
envSection: map[string]any{},
validate: func(t *testing.T, result []string) {
assert.Empty(t, result)
},
},
{
name: "string values",
envSection: map[string]any{
"STRING_VAR": "value",
"ANOTHER": "test",
},
validate: func(t *testing.T, result []string) {
assert.Len(t, result, 2)
assert.Contains(t, result, "STRING_VAR=value")
assert.Contains(t, result, "ANOTHER=test")
},
},
{
name: "numeric values",
envSection: map[string]any{
"INT_VAR": 123,
"FLOAT_VAR": 45.67,
},
validate: func(t *testing.T, result []string) {
assert.Len(t, result, 2)
assert.Contains(t, result, "INT_VAR=123")
assert.Contains(t, result, "FLOAT_VAR=45.67")
},
},
{
name: "boolean values",
envSection: map[string]any{
"BOOL_TRUE": true,
"BOOL_FALSE": false,
},
validate: func(t *testing.T, result []string) {
assert.Len(t, result, 2)
assert.Contains(t, result, "BOOL_TRUE=true")
assert.Contains(t, result, "BOOL_FALSE=false")
},
},
{
name: "null values are excluded",
envSection: map[string]any{
"VALID": "value",
"NULL": nil,
"ALSO_OK": "test",
},
validate: func(t *testing.T, result []string) {
// nil values should be excluded.
assert.Len(t, result, 2)
assert.Contains(t, result, "VALID=value")
assert.Contains(t, result, "ALSO_OK=test")
// Verify NULL is not present.
for _, envVar := range result {
assert.NotContains(t, envVar, "NULL=")
}
},
},
{
name: "mixed types",
envSection: map[string]any{
"STRING": "text",
"NUM": 42,
"BOOL": true,
"NIL": nil,
},
validate: func(t *testing.T, result []string) {
assert.Len(t, result, 3)
assert.Contains(t, result, "STRING=text")
assert.Contains(t, result, "NUM=42")
assert.Contains(t, result, "BOOL=true")
},
},
{
name: "empty string value",
envSection: map[string]any{
"EMPTY": "",
},
validate: func(t *testing.T, result []string) {
assert.Len(t, result, 1)
assert.Contains(t, result, "EMPTY=")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := componentEnvSectionToList(tt.envSection)
tt.validate(t, result)
})
}
}