Skip to content

Commit feede9d

Browse files
miguelhbritoMiguel Pereira
andauthored
add validation retry for gcp service account file prompts (#948)
Co-authored-by: Miguel Pereira <[email protected]>
1 parent 3c4b5c1 commit feede9d

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

cmd/ocm/create/cluster/cmd.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,16 +1613,22 @@ func promptGcpAuth(fs *pflag.FlagSet, connection *sdk.Connection) error {
16131613
return err
16141614
}
16151615
case c.AuthenticationKey:
1616-
// TODO: re-prompt when selected file is not readable / invalid JSON
1617-
err = arguments.PromptFilePath(fs, "service-account-file", true)
1618-
if err != nil {
1619-
return err
1620-
}
1621-
1622-
if args.gcpServiceAccountFile == "" {
1623-
return fmt.Errorf("a valid GCP service account file must be specified for CCS clusters")
1624-
}
1625-
err = constructGCPCredentials(args.gcpServiceAccountFile, &args.ccs)
1616+
err = arguments.PromptFilePath(fs, "service-account-file", true,
1617+
func(val interface{}) error {
1618+
gcpServiceAccountFile, ok := val.(string)
1619+
if !ok {
1620+
return fmt.Errorf("invalid file path")
1621+
}
1622+
gcpServiceAccountFile, err := arguments.ResolveRelativePath(gcpServiceAccountFile)
1623+
if err != nil {
1624+
return fmt.Errorf("invalid file path: %v", err)
1625+
}
1626+
if err := constructGCPCredentials(gcpServiceAccountFile, &args.ccs); err != nil {
1627+
return err
1628+
}
1629+
return nil
1630+
},
1631+
)
16261632
if err != nil {
16271633
return err
16281634
}
@@ -1870,9 +1876,9 @@ func fetchFlavours(client *cmv1.Client) (flavours []*cmv1.Flavour, err error) {
18701876
return
18711877
}
18721878

1873-
func constructGCPCredentials(filePath arguments.FilePath, value *c.CCS) error {
1879+
func constructGCPCredentials(filePath string, value *c.CCS) error {
18741880
// Open our jsonFile
1875-
jsonFile, err := os.Open(filePath.String())
1881+
jsonFile, err := os.Open(filePath)
18761882
if err != nil {
18771883
return err
18781884
}

pkg/arguments/interactive.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,12 @@ func PromptPassword(fs *pflag.FlagSet, flagName string) error {
254254

255255
// PromptFilePath sets a FilePath flag value interactively, unless already set.
256256
// Does nothing in non-interactive mode.
257-
func PromptFilePath(fs *pflag.FlagSet, flagName string, required bool) error {
257+
func PromptFilePath(
258+
fs *pflag.FlagSet,
259+
flagName string,
260+
required bool,
261+
validators ...survey.Validator,
262+
) error {
258263
flag := fs.Lookup(flagName)
259264
if flag.Value.Type() != "filepath" {
260265
return fmt.Errorf("PromptFilePath can't be used on flag %q of type %q",
@@ -277,11 +282,14 @@ func PromptFilePath(fs *pflag.FlagSet, flagName string, required bool) error {
277282
if required {
278283
validator = survey.WithValidator(survey.Required)
279284
}
280-
err := survey.AskOne(prompt, &response, validator)
285+
err := survey.AskOne(prompt, &response, validator, func(options *survey.AskOptions) error {
286+
options.Validators = append(options.Validators, validators...)
287+
return nil
288+
})
281289
if err != nil {
282290
return err
283291
}
284-
response, err = resolveRelativePath(response)
292+
response, err = ResolveRelativePath(response)
285293
if err != nil {
286294
return err
287295
}
@@ -293,7 +301,7 @@ func PromptFilePath(fs *pflag.FlagSet, flagName string, required bool) error {
293301

294302
// Golang does not support tilde file paths https://github.com/golang/go/issues/57569
295303
// However, we try to resolve this by manually so user can proceed further
296-
func resolveRelativePath(path string) (string, error) {
304+
func ResolveRelativePath(path string) (string, error) {
297305
if !strings.Contains(path, "~") {
298306
return path, nil
299307
}

0 commit comments

Comments
 (0)