|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/GoCodeAlone/workflow/config" |
| 8 | +) |
| 9 | + |
| 10 | +func TestInjectSecrets_MultiStoreRouting(t *testing.T) { |
| 11 | + // Set secrets in the environment |
| 12 | + t.Setenv("DB_PASS", "pg-password") |
| 13 | + t.Setenv("JWT_KEY", "jwt-signing-key") |
| 14 | + |
| 15 | + wfCfg := &config.WorkflowConfig{ |
| 16 | + SecretStores: map[string]*config.SecretStoreConfig{ |
| 17 | + "local-env": {Provider: "env"}, |
| 18 | + }, |
| 19 | + Secrets: &config.SecretsConfig{ |
| 20 | + DefaultStore: "local-env", |
| 21 | + Entries: []config.SecretEntry{ |
| 22 | + {Name: "DB_PASS", Store: "local-env"}, |
| 23 | + {Name: "JWT_KEY"}, // uses defaultStore |
| 24 | + }, |
| 25 | + }, |
| 26 | + } |
| 27 | + |
| 28 | + secrets, err := injectSecrets(context.Background(), wfCfg, "local") |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("injectSecrets: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + if secrets["DB_PASS"] != "pg-password" { |
| 34 | + t.Errorf("DB_PASS: got %q, want pg-password", secrets["DB_PASS"]) |
| 35 | + } |
| 36 | + if secrets["JWT_KEY"] != "jwt-signing-key" { |
| 37 | + t.Errorf("JWT_KEY: got %q, want jwt-signing-key", secrets["JWT_KEY"]) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestInjectSecrets_EnvOverrideRouting(t *testing.T) { |
| 42 | + t.Setenv("MY_API_KEY", "api-key-value") |
| 43 | + |
| 44 | + wfCfg := &config.WorkflowConfig{ |
| 45 | + SecretStores: map[string]*config.SecretStoreConfig{ |
| 46 | + "local": {Provider: "env"}, |
| 47 | + }, |
| 48 | + Secrets: &config.SecretsConfig{ |
| 49 | + DefaultStore: "local", |
| 50 | + Entries: []config.SecretEntry{ |
| 51 | + {Name: "MY_API_KEY"}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + Environments: map[string]*config.EnvironmentConfig{ |
| 55 | + "staging": {SecretsStoreOverride: "local"}, // routes to env provider |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + secrets, err := injectSecrets(context.Background(), wfCfg, "staging") |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("injectSecrets: %v", err) |
| 62 | + } |
| 63 | + if secrets["MY_API_KEY"] != "api-key-value" { |
| 64 | + t.Errorf("MY_API_KEY: got %q, want api-key-value", secrets["MY_API_KEY"]) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestInjectSecrets_UnknownStore_Error(t *testing.T) { |
| 69 | + wfCfg := &config.WorkflowConfig{ |
| 70 | + Secrets: &config.SecretsConfig{ |
| 71 | + Entries: []config.SecretEntry{ |
| 72 | + {Name: "MY_SECRET", Store: "nonexistent-provider"}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + _, err := injectSecrets(context.Background(), wfCfg, "") |
| 78 | + if err == nil { |
| 79 | + t.Error("expected error for unknown store provider") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestInjectSecrets_LegacyProvider(t *testing.T) { |
| 84 | + t.Setenv("LEGACY_SECRET", "legacy-value") |
| 85 | + |
| 86 | + wfCfg := &config.WorkflowConfig{ |
| 87 | + Secrets: &config.SecretsConfig{ |
| 88 | + Provider: "env", // legacy field |
| 89 | + Entries: []config.SecretEntry{ |
| 90 | + {Name: "LEGACY_SECRET"}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + secrets, err := injectSecrets(context.Background(), wfCfg, "") |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("injectSecrets (legacy): %v", err) |
| 98 | + } |
| 99 | + if secrets["LEGACY_SECRET"] != "legacy-value" { |
| 100 | + t.Errorf("LEGACY_SECRET: got %q, want legacy-value", secrets["LEGACY_SECRET"]) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestInjectSecrets_EmptyEntries(t *testing.T) { |
| 105 | + wfCfg := &config.WorkflowConfig{ |
| 106 | + Secrets: &config.SecretsConfig{ |
| 107 | + Provider: "env", |
| 108 | + Entries: nil, |
| 109 | + }, |
| 110 | + } |
| 111 | + secrets, err := injectSecrets(context.Background(), wfCfg, "") |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("unexpected error: %v", err) |
| 114 | + } |
| 115 | + if secrets != nil { |
| 116 | + t.Errorf("expected nil for empty entries, got %v", secrets) |
| 117 | + } |
| 118 | +} |
0 commit comments