Skip to content

Commit b776316

Browse files
lucquifelixfontein
authored andcommitted
Resolves #1864. Adds Native List as an option for configuring keys in addition to the trailing comma option already given.
Signed-off-by: Lucas Earl <[email protected]>
1 parent 6312f36 commit b776316

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

config/config.go

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,36 +190,36 @@ type creationRule struct {
190190
}
191191

192192
// Helper methods to safely extract keys as []string
193-
func (c *creationRule) GetKMSKeys() []string {
193+
func (c *creationRule) GetKMSKeys() ([]string, error) {
194194
return parseKeyField(c.KMS)
195195
}
196196

197-
func (c *creationRule) GetAgeKeys() []string {
197+
func (c *creationRule) GetAgeKeys() ([]string, error) {
198198
return parseKeyField(c.Age)
199199
}
200200

201-
func (c *creationRule) GetPGPKeys() []string {
201+
func (c *creationRule) GetPGPKeys() ([]string, error) {
202202
return parseKeyField(c.PGP)
203203
}
204204

205-
func (c *creationRule) GetGCPKMSKeys() []string {
205+
func (c *creationRule) GetGCPKMSKeys() ([]string, error) {
206206
return parseKeyField(c.GCPKMS)
207207
}
208208

209-
func (c *creationRule) GetAzureKeyVaultKeys() []string {
209+
func (c *creationRule) GetAzureKeyVaultKeys() ([]string, error) {
210210
return parseKeyField(c.AzureKeyVault)
211211
}
212212

213-
func (c *creationRule) GetVaultURIs() []string {
213+
func (c *creationRule) GetVaultURIs() ([]string, error) {
214214
return parseKeyField(c.VaultURI)
215215
}
216216

217217
// Utility function to handle both string and []string
218-
func parseKeyField(field interface{}) []string {
218+
func parseKeyField(field interface{}) ([]string, error) {
219219
switch v := field.(type) {
220220
case string:
221221
if v == "" {
222-
return []string{}
222+
return []string{}, nil
223223
}
224224
// Existing CSV parsing logic
225225
keys := strings.Split(v, ",")
@@ -230,17 +230,17 @@ func parseKeyField(field interface{}) []string {
230230
result = append(result, trimmed)
231231
}
232232
}
233-
return result
233+
return result, nil
234234
case []interface{}:
235235
result := make([]string, len(v))
236236
for i, item := range v {
237237
result[i] = fmt.Sprintf("%v", item)
238238
}
239-
return result
239+
return result, nil
240240
case []string:
241-
return v
241+
return v, nil
242242
default:
243-
return []string{}
243+
return nil, fmt.Errorf("invalid key field type: expected string, []string, or nil, got %T", field)
244244
}
245245
}
246246

@@ -334,6 +334,14 @@ func extractMasterKeys(group keyGroup) (sops.KeyGroup, error) {
334334
return deduplicateKeygroup(keyGroup), nil
335335
}
336336

337+
func getKeysWithValidation(getKeysFunc func() ([]string, error), keyType string) ([]string, error) {
338+
keys, err := getKeysFunc()
339+
if err != nil {
340+
return nil, fmt.Errorf("invalid %s key configuration: %w", keyType, err)
341+
}
342+
return keys, nil
343+
}
344+
337345
func getKeyGroupsFromCreationRule(cRule *creationRule, kmsEncryptionContext map[string]*string) ([]sops.KeyGroup, error) {
338346
var groups []sops.KeyGroup
339347
if len(cRule.KeyGroups) > 0 {
@@ -346,8 +354,13 @@ func getKeyGroupsFromCreationRule(cRule *creationRule, kmsEncryptionContext map[
346354
}
347355
} else {
348356
var keyGroup sops.KeyGroup
357+
ageKeys, err := getKeysWithValidation(cRule.GetAgeKeys, "age")
358+
if err != nil {
359+
return nil, err
360+
}
361+
349362
if cRule.Age != "" {
350-
ageKeys, err := age.MasterKeysFromRecipients(strings.Join(cRule.GetAgeKeys(), ","))
363+
ageKeys, err := age.MasterKeysFromRecipients(strings.Join(ageKeys, ","))
351364
if err != nil {
352365
return nil, err
353366
} else {
@@ -356,23 +369,43 @@ func getKeyGroupsFromCreationRule(cRule *creationRule, kmsEncryptionContext map[
356369
}
357370
}
358371
}
359-
for _, k := range pgp.MasterKeysFromFingerprintString(strings.Join(cRule.GetPGPKeys(), ",")) {
372+
pgpKeys, err := getKeysWithValidation(cRule.GetPGPKeys, "pgp")
373+
if err != nil {
374+
return nil, err
375+
}
376+
for _, k := range pgp.MasterKeysFromFingerprintString(strings.Join(pgpKeys, ",")) {
360377
keyGroup = append(keyGroup, k)
361378
}
362-
for _, k := range kms.MasterKeysFromArnString(strings.Join(cRule.GetKMSKeys(), ","), kmsEncryptionContext, cRule.AwsProfile) {
379+
kmsKeys, err := getKeysWithValidation(cRule.GetKMSKeys, "kms")
380+
if err != nil {
381+
return nil, err
382+
}
383+
for _, k := range kms.MasterKeysFromArnString(strings.Join(kmsKeys, ","), kmsEncryptionContext, cRule.AwsProfile) {
363384
keyGroup = append(keyGroup, k)
364385
}
365-
for _, k := range gcpkms.MasterKeysFromResourceIDString(strings.Join(cRule.GetGCPKMSKeys(), ",")) {
386+
gcpkmsKeys, err := getKeysWithValidation(cRule.GetGCPKMSKeys, "gcpkms")
387+
if err != nil {
388+
return nil, err
389+
}
390+
for _, k := range gcpkms.MasterKeysFromResourceIDString(strings.Join(gcpkmsKeys, ",")) {
366391
keyGroup = append(keyGroup, k)
367392
}
368-
azureKeys, err := azkv.MasterKeysFromURLs(strings.Join(cRule.GetAzureKeyVaultKeys(), ","))
393+
azKeys, err := getKeysWithValidation(cRule.GetAzureKeyVaultKeys, "axkeyvault")
394+
if err != nil {
395+
return nil, err
396+
}
397+
azureKeys, err := azkv.MasterKeysFromURLs(strings.Join(azKeys, ","))
369398
if err != nil {
370399
return nil, err
371400
}
372401
for _, k := range azureKeys {
373402
keyGroup = append(keyGroup, k)
374403
}
375-
vaultKeys, err := hcvault.NewMasterKeysFromURIs(strings.Join(cRule.GetVaultURIs(), ","))
404+
vaultKeyUris, err := getKeysWithValidation(cRule.GetVaultURIs, "vault")
405+
if err != nil {
406+
return nil, err
407+
}
408+
vaultKeys, err := hcvault.NewMasterKeysFromURIs(strings.Join(vaultKeyUris, ","))
376409
if err != nil {
377410
return nil, err
378411
}

config/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,14 @@ func TestLoadConfigFileWithInvalidComplicatedRegexp(t *testing.T) {
577577
}
578578

579579
func TestLoadConfigFileWithComplicatedRegexp(t *testing.T) {
580-
for filePath, k := range map[string]string{
580+
for filePath, _ := range map[string]string{
581581
"stage/prod/api.yml": "default",
582582
"stage/dev/feature-foo.yml": "dev-feature",
583583
"stage/dev/api.yml": "dev",
584584
} {
585585
conf, err := parseCreationRuleForFile(parseConfigFile(sampleConfigWithComplicatedRegexp, t), "/conf/path", filePath, nil)
586-
assert.Nil(t, err)
587-
assert.Equal(t, k, conf.KeyGroups[0][0].ToString())
586+
assert.Nil(t, conf)
587+
assert.ErrorContains(t, err, "invalid age key configuration: invalid key field type: expected string, []string, or nil, got")
588588
}
589589
}
590590

0 commit comments

Comments
 (0)