Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 0163ea0

Browse files
committed
Validate crypto key input on gcp init
1 parent 0236dea commit 0163ea0

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

internals/secrethub/service_gcp_init.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (cmd *ServiceGCPInitCommand) Run() error {
8484
if err != nil {
8585
return err
8686
}
87-
kmsKey, err := ui.ChooseDynamicOptions(cmd.io, "What is the KMS key you want to use for encrypting the service account's key?", kmsKeyLister.KeyOptions(keyring), true, "kms key")
87+
kmsKey, err := ui.ChooseDynamicOptionsValidate(cmd.io, "What is the KMS key you want to use for encrypting the service account's key?", kmsKeyLister.KeyOptions(keyring), "kms key", validateGCPCryptoKey)
8888
if err != nil {
8989
return err
9090
}
@@ -311,6 +311,13 @@ func validateGCPKeyring(keyring string) error {
311311
return nil
312312
}
313313

314+
func validateGCPCryptoKey(cryptoKey string) error {
315+
if !regexp.MustCompile("^projects/[a-zA-Z0-9-]*/locations/[a-zA-Z0-9-]*/keyRings/[a-zA-Z0-9-_]*/cryptoKeys/[a-zA-Z0-9-_]*$").MatchString(cryptoKey) {
316+
return errors.New("GCP crypto key should be in the form \"projects/<project-id>/locations/<location>/keyRings/<key-ring>/cryptoKeys/<key>\"")
317+
}
318+
return nil
319+
}
320+
314321
func (l *gcpKMSKeyOptionLister) KeyOptions(keyring string) func() ([]ui.Option, bool, error) {
315322
return func() ([]ui.Option, bool, error) {
316323
resp, err := l.kmsService.Projects.Locations.KeyRings.CryptoKeys.List(keyring).PageSize(10).Filter("purpose:ENCRYPT_DECRYPT").PageToken(l.nextPage).Do()

0 commit comments

Comments
 (0)