| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package secretstore |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/plugin/framework/confgroup" |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | "gopkg.in/yaml.v2" |
| 12 | ) |
| 13 | |
| 14 | func TestConfigValidateAndIdentity(t *testing.T) { |
| 15 | cfg := Config{ |
| 16 | "name": "prod", |
| 17 | "kind": string(KindVault), |
| 18 | "__source__": "/etc/netdata/secretstores.yaml", |
| 19 | "__source_type__": confgroup.TypeUser, |
| 20 | "auth": map[string]any{ |
| 21 | "mode": "token", |
| 22 | }, |
| 23 | } |
| 24 | |
| 25 | require.NoError(t, cfg.Validate()) |
| 26 | assert.Equal(t, "prod", cfg.Name()) |
| 27 | assert.Equal(t, KindVault, cfg.Kind()) |
| 28 | assert.Equal(t, "vault:prod", cfg.ExposedKey()) |
| 29 | assert.Equal(t, "/etc/netdata/secretstores.yaml:vault:prod", cfg.UID()) |
| 30 | } |
| 31 | |
| 32 | func TestConfigValidateRejectsInvalidStructuralFields(t *testing.T) { |
| 33 | tests := map[string]struct { |
| 34 | cfg Config |
| 35 | want string |
| 36 | }{ |
| 37 | "nil": { |
| 38 | cfg: nil, |
| 39 | want: "store config is nil", |
| 40 | }, |
| 41 | "missing name": { |
| 42 | cfg: Config{ |
| 43 | "kind": string(KindVault), |
| 44 | "__source__": "src", |
| 45 | "__source_type__": confgroup.TypeDyncfg, |
| 46 | }, |
| 47 | want: "store name is required", |
| 48 | }, |
| 49 | "invalid name": { |
| 50 | cfg: Config{ |
| 51 | "name": "prod:west", |
| 52 | "kind": string(KindVault), |
| 53 | "__source__": "src", |
| 54 | "__source_type__": confgroup.TypeDyncfg, |
| 55 | }, |
| 56 | want: "invalid store name", |
| 57 | }, |
| 58 | "invalid kind": { |
| 59 | cfg: Config{ |
| 60 | "name": "prod", |
| 61 | "kind": "wat", |
| 62 | "__source__": "src", |
| 63 | "__source_type__": confgroup.TypeDyncfg, |
| 64 | }, |
| 65 | want: "invalid store kind", |
| 66 | }, |
| 67 | "missing source": { |
| 68 | cfg: Config{ |
| 69 | "name": "prod", |
| 70 | "kind": string(KindVault), |
| 71 | "__source_type__": confgroup.TypeDyncfg, |
| 72 | }, |
| 73 | want: "store source is required", |
| 74 | }, |
| 75 | "invalid source type": { |
| 76 | cfg: Config{ |
| 77 | "name": "prod", |
| 78 | "kind": string(KindVault), |
| 79 | "__source__": "src", |
| 80 | "__source_type__": "invalid", |
| 81 | }, |
| 82 | want: "invalid store source type", |
| 83 | }, |
| 84 | } |
| 85 | |
| 86 | for name, tc := range tests { |
| 87 | t.Run(name, func(t *testing.T) { |
| 88 | err := tc.cfg.Validate() |
| 89 | require.Error(t, err) |
| 90 | assert.ErrorContains(t, err, tc.want) |
| 91 | }) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestConfigHashExcludesMetadataKeys(t *testing.T) { |
| 96 | base := Config{ |
| 97 | "name": "prod", |
| 98 | "kind": string(KindVault), |
| 99 | "auth": map[string]any{ |
| 100 | "mode": "token", |
| 101 | "mode_token": map[string]any{ |
| 102 | "token": "vault-token", |
| 103 | }, |
| 104 | }, |
| 105 | "__source__": "user.yaml", |
| 106 | "__source_type__": confgroup.TypeUser, |
| 107 | } |
| 108 | |
| 109 | metaChanged := Config{ |
| 110 | "name": "prod", |
| 111 | "kind": string(KindVault), |
| 112 | "auth": map[string]any{ |
| 113 | "mode": "token", |
| 114 | "mode_token": map[string]any{ |
| 115 | "token": "vault-token", |
| 116 | }, |
| 117 | }, |
| 118 | "__source__": "dyncfg", |
| 119 | "__source_type__": confgroup.TypeDyncfg, |
| 120 | } |
| 121 | |
| 122 | payloadChanged := Config{ |
| 123 | "name": "prod", |
| 124 | "kind": string(KindVault), |
| 125 | "auth": map[string]any{ |
| 126 | "mode": "token", |
| 127 | "mode_token": map[string]any{ |
| 128 | "token": "other-token", |
| 129 | }, |
| 130 | }, |
| 131 | "__source__": "user.yaml", |
| 132 | "__source_type__": confgroup.TypeUser, |
| 133 | } |
| 134 | |
| 135 | assert.Equal(t, base.Hash(), metaChanged.Hash()) |
| 136 | assert.NotEqual(t, base.Hash(), payloadChanged.Hash()) |
| 137 | } |
| 138 | |
| 139 | func TestConfigHashMatchesAcrossEquivalentMapShapes(t *testing.T) { |
| 140 | rawMap := Config{ |
| 141 | "name": "prod", |
| 142 | "kind": string(KindVault), |
| 143 | "__source__": "user.yaml", |
| 144 | "__source_type__": confgroup.TypeUser, |
| 145 | "auth": map[string]any{ |
| 146 | "mode": "token", |
| 147 | "selectors": []any{ |
| 148 | "VAULT_TOKEN", |
| 149 | map[string]any{"alt": "VAULT_TOKEN_ALT"}, |
| 150 | }, |
| 151 | }, |
| 152 | } |
| 153 | |
| 154 | var rawYAML Config |
| 155 | require.NoError(t, yaml.Unmarshal([]byte(` |
| 156 | name: prod |
| 157 | kind: vault |
| 158 | __source__: user.yaml |
| 159 | __source_type__: user |
| 160 | auth: |
| 161 | mode: token |
| 162 | selectors: |
| 163 | - VAULT_TOKEN |
| 164 | - alt: VAULT_TOKEN_ALT |
| 165 | `), &rawYAML)) |
| 166 | |
| 167 | assert.Equal(t, rawMap.Hash(), rawYAML.Hash()) |
| 168 | } |
| 169 | |
| 170 | func TestConfigSourceTypePriority(t *testing.T) { |
| 171 | assert.Equal(t, 16, Config{"__source_type__": confgroup.TypeDyncfg}.SourceTypePriority()) |
| 172 | assert.Equal(t, 8, Config{"__source_type__": confgroup.TypeUser}.SourceTypePriority()) |
| 173 | assert.Equal(t, 2, Config{"__source_type__": confgroup.TypeStock}.SourceTypePriority()) |
| 174 | assert.Equal(t, 0, Config{"__source_type__": "other"}.SourceTypePriority()) |
| 175 | } |