@@ -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+
337345func 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 }
0 commit comments