|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "os" |
5 | 6 | "path" |
6 | 7 | "testing" |
7 | 8 |
|
| 9 | + "github.com/getsops/sops/v3/keys" |
8 | 10 | "github.com/stretchr/testify/assert" |
9 | 11 | ) |
10 | 12 |
|
@@ -97,6 +99,17 @@ creation_rules: |
97 | 99 | - arn: foo |
98 | 100 | context: |
99 | 101 | baz: bam |
| 102 | + - arn: foo |
| 103 | + aws_profile: bar |
| 104 | + context: |
| 105 | + baz: bam |
| 106 | + - arn: foo |
| 107 | + role: '123' |
| 108 | + - arn: foo |
| 109 | + aws_profile: bar |
| 110 | + context: |
| 111 | + baz: bam |
| 112 | + role: '123' |
100 | 113 | pgp: |
101 | 114 | - bar |
102 | 115 | gcp_kms: |
@@ -132,113 +145,124 @@ creation_rules: |
132 | 145 | - 'https://foo.vault:8200/v1/foo/keys/foo-key' |
133 | 146 | - merge: |
134 | 147 | - merge: |
135 | | - - kms: |
| 148 | + - pgp: |
136 | 149 | # key01 |
| 150 | + - foo |
| 151 | + kms: |
| 152 | + # key02 |
137 | 153 | - arn: foo |
138 | 154 | aws_profile: foo |
139 | | - pgp: |
140 | | - # key02 |
141 | | - - foo |
142 | | - gcp_kms: |
143 | 155 | # key03 |
| 156 | + - arn: foo |
| 157 | + aws_profile: bar |
| 158 | + context: |
| 159 | + baz: bam |
| 160 | + role: '123' |
| 161 | + gcp_kms: |
| 162 | + # key04 |
144 | 163 | - resource_id: foo |
145 | 164 | azure_keyvault: |
146 | | - # key04 |
| 165 | + # key05 |
147 | 166 | - vaultUrl: https://foo.vault.azure.net |
148 | 167 | key: foo-key |
149 | 168 | version: fooversion |
150 | 169 | hc_vault: |
151 | | - # key05 |
152 | | - - 'https://bar.vault:8200/v1/bar/keys/bar-key' |
153 | | - - kms: |
154 | 170 | # key06 |
155 | | - - arn: bar |
156 | | - aws_profile: bar |
157 | | - pgp: |
| 171 | + - 'https://bar.vault:8200/v1/bar/keys/bar-key' |
| 172 | + - pgp: |
158 | 173 | # key07 |
159 | 174 | - bar |
160 | | - gcp_kms: |
| 175 | + kms: |
161 | 176 | # key08 |
162 | | - - resource_id: bar |
| 177 | + - arn: bar |
| 178 | + aws_profile: bar |
| 179 | + gcp_kms: |
163 | 180 | # key09 |
| 181 | + - resource_id: bar |
| 182 | + # key10 |
164 | 183 | - resource_id: baz |
165 | 184 | azure_keyvault: |
166 | | - # key10 |
| 185 | + # key11 |
167 | 186 | - vaultUrl: https://bar.vault.azure.net |
168 | 187 | key: bar-key |
169 | 188 | version: barversion |
170 | 189 | hc_vault: |
171 | | - # key01 - duplicate#1 |
| 190 | + # key12 |
172 | 191 | - 'https://baz.vault:8200/v1/baz/keys/baz-key' |
| 192 | + pgp: |
| 193 | + # key13 |
| 194 | + - baz |
173 | 195 | kms: |
174 | | - # key11 |
| 196 | + # key14 |
175 | 197 | - arn: baz |
176 | 198 | aws_profile: baz |
177 | | - pgp: |
178 | | - # key12 |
179 | | - - baz |
180 | 199 | gcp_kms: |
181 | | - # key03 - duplicate#2 |
182 | | - # --> should be removed when loading config |
| 200 | + # duplicate of key09 |
183 | 201 | - resource_id: bar |
184 | 202 | azure_keyvault: |
185 | | - # key04 - duplicate#3 |
| 203 | + # duplicate of key05 |
186 | 204 | - vaultUrl: https://foo.vault.azure.net |
187 | 205 | key: foo-key |
188 | 206 | version: fooversion |
189 | 207 | hc_vault: |
190 | | - # key13 - duplicate#4 - but from different key_group |
191 | | - # --> should stay |
| 208 | + # key15 (duplicate of key00, but that's in a different key_group) |
192 | 209 | - 'https://foo.vault:8200/v1/foo/keys/foo-key' |
193 | | - - kms: |
194 | | - # key14 |
| 210 | + - pgp: |
| 211 | + # key16 |
| 212 | + - qux |
| 213 | + kms: |
| 214 | + # key17 |
195 | 215 | - arn: qux |
196 | 216 | aws_profile: qux |
197 | | - # key14 - duplicate#5 |
| 217 | + # key18 |
198 | 218 | - arn: baz |
199 | 219 | aws_profile: bar |
200 | | - pgp: |
201 | | - # key15 |
202 | | - - qux |
| 220 | + # key19 |
| 221 | + - arn: baz |
| 222 | + role: '123' |
203 | 223 | gcp_kms: |
204 | | - # key16 |
| 224 | + # key20 |
205 | 225 | - resource_id: qux |
206 | | - # key17 |
| 226 | + # key21 |
207 | 227 | - resource_id: fnord |
208 | 228 | azure_keyvault: |
209 | | - # key18 |
| 229 | + # key22 |
210 | 230 | - vaultUrl: https://baz.vault.azure.net |
211 | 231 | key: baz-key |
212 | 232 | version: bazversion |
213 | 233 | hc_vault: |
214 | | - # key19 |
| 234 | + # key23 |
215 | 235 | - 'https://qux.vault:8200/v1/qux/keys/qux-key' |
216 | | - # everything below this should be loaded, |
217 | | - # since it is not in a merge block |
| 236 | + pgp: |
| 237 | + # duplicate of key07 |
| 238 | + - bar |
218 | 239 | kms: |
219 | | - # duplicated key06 |
| 240 | + # duplicate of key08 |
220 | 241 | - arn: bar |
221 | 242 | aws_profile: bar |
222 | | - # key20 |
| 243 | + # key24 |
223 | 244 | - arn: fnord |
224 | 245 | aws_profile: fnord |
225 | | - pgp: |
226 | | - # duplicated key07 |
227 | | - - bar |
| 246 | + # duplicate of key03 |
| 247 | + - arn: foo |
| 248 | + aws_profile: bar |
| 249 | + context: |
| 250 | + baz: bam |
| 251 | + role: '123' |
228 | 252 | gcp_kms: |
229 | | - # duplicated key08 |
| 253 | + # duplicate of key09 |
230 | 254 | - resource_id: bar |
231 | | - # key21 |
| 255 | + # duplicate of key21 |
232 | 256 | - resource_id: fnord |
233 | 257 | azure_keyvault: |
234 | | - # duplicated key10 |
| 258 | + # duplicate of key11 |
235 | 259 | - vaultUrl: https://bar.vault.azure.net |
236 | 260 | key: bar-key |
237 | 261 | version: barversion |
238 | 262 | hc_vault: |
239 | | - # duplicated 'key01 - duplicate#2' |
| 263 | + # duplicate of key12 |
240 | 264 | - 'https://baz.vault:8200/v1/baz/keys/baz-key' |
241 | | - # key22 |
| 265 | + # key25 |
242 | 266 | - 'https://fnord.vault:8200/v1/fnord/keys/fnord-key' |
243 | 267 | `) |
244 | 268 |
|
@@ -447,6 +471,25 @@ func TestLoadConfigFileWithGroups(t *testing.T) { |
447 | 471 | "baz": &bam, |
448 | 472 | }, |
449 | 473 | }, |
| 474 | + { |
| 475 | + Arn: "foo", |
| 476 | + AwsProfile: "bar", |
| 477 | + Context: map[string]*string{ |
| 478 | + "baz": &bam, |
| 479 | + }, |
| 480 | + }, |
| 481 | + { |
| 482 | + Arn: "foo", |
| 483 | + Role: "123", |
| 484 | + }, |
| 485 | + { |
| 486 | + Arn: "foo", |
| 487 | + AwsProfile: "bar", |
| 488 | + Context: map[string]*string{ |
| 489 | + "baz": &bam, |
| 490 | + }, |
| 491 | + Role: "123", |
| 492 | + }, |
450 | 493 | }, |
451 | 494 | PGP: []string{"bar"}, |
452 | 495 | GCPKMS: []gcpKmsKey{{ResourceID: "foo"}}, |
@@ -474,12 +517,52 @@ func TestLoadConfigFileWithGroups(t *testing.T) { |
474 | 517 | assert.Equal(t, expected, conf) |
475 | 518 | } |
476 | 519 |
|
| 520 | +func id(key keys.MasterKey) string { |
| 521 | + return fmt.Sprintf("%s: %s", key.TypeToIdentifier(), key.ToString()) |
| 522 | +} |
| 523 | + |
| 524 | +func ids(keys []keys.MasterKey) []string { |
| 525 | + result := make([]string, 0, len(keys)) |
| 526 | + for _, key := range keys { |
| 527 | + result = append(result, id(key)) |
| 528 | + } |
| 529 | + return result |
| 530 | +} |
| 531 | + |
477 | 532 | func TestLoadConfigFileWithMerge(t *testing.T) { |
478 | 533 | conf, err := parseCreationRuleForFile(parseConfigFile(sampleConfigWithMergeType, t), "/conf/path", "whatever", nil) |
479 | 534 | assert.Nil(t, err) |
480 | 535 | assert.Equal(t, 2, len(conf.KeyGroups)) |
481 | | - assert.Equal(t, 1, len(conf.KeyGroups[0])) |
482 | | - assert.Equal(t, 23, len(conf.KeyGroups[1])) |
| 536 | + assert.Equal(t, []string{ |
| 537 | + "hc_vault: https://foo.vault:8200/v1/foo/keys/foo-key", |
| 538 | + }, ids(conf.KeyGroups[0])) |
| 539 | + assert.Equal(t, []string{ |
| 540 | + "pgp: foo", // key01 |
| 541 | + "kms: foo||foo", //key02 |
| 542 | + "kms: foo+123|baz:bam|bar", //key03 |
| 543 | + "gcp_kms: foo", //key04 |
| 544 | + "azure_kv: https://foo.vault.azure.net/keys/foo-key/fooversion", //key05 |
| 545 | + "hc_vault: https://bar.vault:8200/v1/bar/keys/bar-key", //key06 |
| 546 | + "pgp: bar", //key07 |
| 547 | + "kms: bar||bar", //key08 |
| 548 | + "gcp_kms: bar", //key09 |
| 549 | + "gcp_kms: baz", //key10 |
| 550 | + "azure_kv: https://bar.vault.azure.net/keys/bar-key/barversion", //key11 |
| 551 | + "hc_vault: https://baz.vault:8200/v1/baz/keys/baz-key", //key12 |
| 552 | + "pgp: baz", //key13 |
| 553 | + "kms: baz||baz", //key14 |
| 554 | + "hc_vault: https://foo.vault:8200/v1/foo/keys/foo-key", //key15 |
| 555 | + "pgp: qux", //key16 |
| 556 | + "kms: qux||qux", //key17 |
| 557 | + "kms: baz||bar", //key18 |
| 558 | + "kms: baz+123", //key19 |
| 559 | + "gcp_kms: qux", //key20 |
| 560 | + "gcp_kms: fnord", //key21 |
| 561 | + "azure_kv: https://baz.vault.azure.net/keys/baz-key/bazversion", //key22 |
| 562 | + "hc_vault: https://qux.vault:8200/v1/qux/keys/qux-key", //key23 |
| 563 | + "kms: fnord||fnord", //key24 |
| 564 | + "hc_vault: https://fnord.vault:8200/v1/fnord/keys/fnord-key", //key25 |
| 565 | + }, ids(conf.KeyGroups[1])) |
483 | 566 | } |
484 | 567 |
|
485 | 568 | func TestLoadConfigFileWithNoMatchingRules(t *testing.T) { |
@@ -555,6 +638,9 @@ func TestKeyGroupsForFileWithGroups(t *testing.T) { |
555 | 638 | assert.Equal(t, "bar", conf.KeyGroups[0][0].ToString()) |
556 | 639 | assert.Equal(t, "foo||bar", conf.KeyGroups[0][1].ToString()) |
557 | 640 | assert.Equal(t, "foo|baz:bam", conf.KeyGroups[0][2].ToString()) |
| 641 | + assert.Equal(t, "foo|baz:bam|bar", conf.KeyGroups[0][3].ToString()) |
| 642 | + assert.Equal(t, "foo+123", conf.KeyGroups[0][4].ToString()) |
| 643 | + assert.Equal(t, "foo+123|baz:bam|bar", conf.KeyGroups[0][5].ToString()) |
558 | 644 | assert.Equal(t, "qux", conf.KeyGroups[1][0].ToString()) |
559 | 645 | assert.Equal(t, "baz||foo", conf.KeyGroups[1][1].ToString()) |
560 | 646 | } |
|
0 commit comments