Skip to content

Commit ce5694a

Browse files
lucquifelixfontein
authored andcommitted
Addressing felixfontein's latest review. Adds a key type field to the ParseKeyField fn.
Signed-off-by: Lucas Earl <[email protected]>
1 parent b776316 commit ce5694a

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

config/config.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,31 +191,35 @@ type creationRule struct {
191191

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

197197
func (c *creationRule) GetAgeKeys() ([]string, error) {
198-
return parseKeyField(c.Age)
198+
return parseKeyField(c.Age, "age")
199199
}
200200

201201
func (c *creationRule) GetPGPKeys() ([]string, error) {
202-
return parseKeyField(c.PGP)
202+
return parseKeyField(c.PGP, "pgp")
203203
}
204204

205205
func (c *creationRule) GetGCPKMSKeys() ([]string, error) {
206-
return parseKeyField(c.GCPKMS)
206+
return parseKeyField(c.GCPKMS, "gcp_kms")
207207
}
208208

209209
func (c *creationRule) GetAzureKeyVaultKeys() ([]string, error) {
210-
return parseKeyField(c.AzureKeyVault)
210+
return parseKeyField(c.AzureKeyVault, "azure_keyvault")
211211
}
212212

213213
func (c *creationRule) GetVaultURIs() ([]string, error) {
214-
return parseKeyField(c.VaultURI)
214+
return parseKeyField(c.VaultURI, "hc_vault_transit_uri")
215215
}
216216

217217
// Utility function to handle both string and []string
218-
func parseKeyField(field interface{}) ([]string, error) {
218+
func parseKeyField(field interface{}, fieldName string) ([]string, error) {
219+
if field == nil {
220+
return []string{}, nil
221+
}
222+
219223
switch v := field.(type) {
220224
case string:
221225
if v == "" {
@@ -234,13 +238,17 @@ func parseKeyField(field interface{}) ([]string, error) {
234238
case []interface{}:
235239
result := make([]string, len(v))
236240
for i, item := range v {
237-
result[i] = fmt.Sprintf("%v", item)
241+
if str, ok := item.(string); ok {
242+
result[i] = str
243+
} else {
244+
return nil, fmt.Errorf("invalid %s key configuration: expected string in list, got %T", fieldName, item)
245+
}
238246
}
239247
return result, nil
240248
case []string:
241249
return v, nil
242250
default:
243-
return nil, fmt.Errorf("invalid key field type: expected string, []string, or nil, got %T", field)
251+
return nil, fmt.Errorf("invalid %s key configuration: expected string, []string, or nil, got %T", fieldName, field)
244252
}
245253
}
246254

@@ -359,7 +367,7 @@ func getKeyGroupsFromCreationRule(cRule *creationRule, kmsEncryptionContext map[
359367
return nil, err
360368
}
361369

362-
if cRule.Age != "" {
370+
if len(ageKeys) > 0 {
363371
ageKeys, err := age.MasterKeysFromRecipients(strings.Join(ageKeys, ","))
364372
if err != nil {
365373
return nil, err
@@ -390,7 +398,7 @@ func getKeyGroupsFromCreationRule(cRule *creationRule, kmsEncryptionContext map[
390398
for _, k := range gcpkms.MasterKeysFromResourceIDString(strings.Join(gcpkmsKeys, ",")) {
391399
keyGroup = append(keyGroup, k)
392400
}
393-
azKeys, err := getKeysWithValidation(cRule.GetAzureKeyVaultKeys, "axkeyvault")
401+
azKeys, err := getKeysWithValidation(cRule.GetAzureKeyVaultKeys, "azure_keyvault")
394402
if err != nil {
395403
return nil, err
396404
}

config/config_test.go

Lines changed: 4 additions & 4 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, _ := range map[string]string{
580+
for filePath, k := 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, conf)
587-
assert.ErrorContains(t, err, "invalid age key configuration: invalid key field type: expected string, []string, or nil, got")
586+
assert.Nil(t, err)
587+
assert.Equal(t, k, conf.KeyGroups[0][0].ToString())
588588
}
589589
}
590590

@@ -741,7 +741,7 @@ creation_rules:
741741
t.Fatal("Expected configuration but got nil")
742742
}
743743

744-
assert.True(t, len(conf.KeyGroups) > 0)
744+
assert.True(t, len(conf.KeyGroups) == 1)
745745
assert.True(t, len(conf.KeyGroups[0]) == 6)
746746

747747
keyTypeCounts := make(map[string]int)

0 commit comments

Comments
 (0)