| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package secretstore_test |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "encoding/json" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/logger" |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/agent/secrets/secretstore" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | type providerBackedConfig struct { |
| 16 | kind secretstore.StoreKind |
| 17 | name string |
| 18 | config map[string]any |
| 19 | } |
| 20 | |
| 21 | func providerBackedConfigs() []providerBackedConfig { |
| 22 | return []providerBackedConfig{ |
| 23 | { |
| 24 | kind: secretstore.KindAWSSM, |
| 25 | name: "aws_prod", |
| 26 | config: map[string]any{ |
| 27 | "name": "aws_prod", |
| 28 | "auth_mode": "env", |
| 29 | "region": "us-east-1", |
| 30 | }, |
| 31 | }, |
| 32 | { |
| 33 | kind: secretstore.KindAzureKV, |
| 34 | name: "azure_prod", |
| 35 | config: map[string]any{ |
| 36 | "name": "azure_prod", |
| 37 | "mode": "managed_identity", |
| 38 | }, |
| 39 | }, |
| 40 | { |
| 41 | kind: secretstore.KindGCPSM, |
| 42 | name: "gcp_prod", |
| 43 | config: map[string]any{ |
| 44 | "name": "gcp_prod", |
| 45 | "mode": "metadata", |
| 46 | }, |
| 47 | }, |
| 48 | { |
| 49 | kind: secretstore.KindVault, |
| 50 | name: "vault_prod", |
| 51 | config: map[string]any{ |
| 52 | "name": "vault_prod", |
| 53 | "mode": "token", |
| 54 | "mode_token": map[string]any{ |
| 55 | "token": "vault-token", |
| 56 | }, |
| 57 | "addr": "https://vault.example", |
| 58 | }, |
| 59 | }, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func testSingleVaultConfig() map[string]any { |
| 64 | return map[string]any{ |
| 65 | "name": "vault_prod", |
| 66 | "mode": "token", |
| 67 | "mode_token": map[string]any{ |
| 68 | "token": "vault-token", |
| 69 | }, |
| 70 | "addr": "https://vault.example", |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func newStoreFromConfig(t *testing.T, _ secretstore.Service, kind secretstore.StoreKind, cfg map[string]any) secretstore.Config { |
| 75 | t.Helper() |
| 76 | |
| 77 | name, _ := cfg["name"].(string) |
| 78 | if name == "" { |
| 79 | name, _ = cfg["id"].(string) |
| 80 | } |
| 81 | require.NotEmpty(t, name) |
| 82 | |
| 83 | payload := secretstore.Config(cloneTestMap(cfg)) |
| 84 | delete(payload, "name") |
| 85 | delete(payload, "id") |
| 86 | delete(payload, "kind") |
| 87 | delete(payload, "enabled") |
| 88 | delete(payload, "description") |
| 89 | payload.SetName(name) |
| 90 | payload.SetKind(kind) |
| 91 | payload.SetSource("dyncfg") |
| 92 | payload.SetSourceType("dyncfg") |
| 93 | return payload |
| 94 | } |
| 95 | |
| 96 | func decodeSchema(t *testing.T, raw string) map[string]any { |
| 97 | t.Helper() |
| 98 | |
| 99 | var out map[string]any |
| 100 | require.NoError(t, json.Unmarshal([]byte(raw), &out)) |
| 101 | return out |
| 102 | } |
| 103 | |
| 104 | func cloneTestMap(in map[string]any) map[string]any { |
| 105 | if len(in) == 0 { |
| 106 | return nil |
| 107 | } |
| 108 | out := make(map[string]any, len(in)) |
| 109 | for k, v := range in { |
| 110 | switch tv := v.(type) { |
| 111 | case map[string]any: |
| 112 | out[k] = cloneTestMap(tv) |
| 113 | case []any: |
| 114 | out[k] = cloneTestSlice(tv) |
| 115 | default: |
| 116 | out[k] = tv |
| 117 | } |
| 118 | } |
| 119 | return out |
| 120 | } |
| 121 | |
| 122 | func cloneTestSlice(in []any) []any { |
| 123 | if len(in) == 0 { |
| 124 | return nil |
| 125 | } |
| 126 | out := make([]any, 0, len(in)) |
| 127 | for _, v := range in { |
| 128 | switch tv := v.(type) { |
| 129 | case map[string]any: |
| 130 | out = append(out, cloneTestMap(tv)) |
| 131 | case []any: |
| 132 | out = append(out, cloneTestSlice(tv)) |
| 133 | default: |
| 134 | out = append(out, tv) |
| 135 | } |
| 136 | } |
| 137 | return out |
| 138 | } |
| 139 | |
| 140 | func captureLoggerOutput(t *testing.T, fn func(log *logger.Logger)) string { |
| 141 | t.Helper() |
| 142 | |
| 143 | var buf bytes.Buffer |
| 144 | log := logger.NewWithWriter(&buf) |
| 145 | fn(log) |
| 146 | return buf.String() |
| 147 | } |