Skip to content

Commit

Permalink
ask confirmation before importing gpg key
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Apr 24, 2024
1 parent 395d2e3 commit db13a40
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mgradm/cmd/gpg/add/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
},
}

gpgAddKeyCmd.Flags().BoolP("force", "f", false, L("Run the import"))
gpgAddKeyCmd.Flags().BoolP("force", "f", false, L("Import without asking confirmation"))
utils.AddBackendFlag(gpgAddKeyCmd)
return gpgAddKeyCmd
}
Expand All @@ -60,9 +60,6 @@ func gpgAddKeys(globalFlags *types.GlobalFlags, flags *gpgAddFlags, cmd *cobra.C
}
gpgAddCmd := []string{"gpg", "--no-default-keyring", "--import", "--import-options", "import-minimal"}

if !flags.Force {
gpgAddCmd = append(gpgAddCmd, "--dry-run")
}
gpgAddCmd = append(gpgAddCmd, "--keyring", customKeyringPath)

scriptDir, err := os.MkdirTemp("", "mgradm-*")
Expand Down Expand Up @@ -90,6 +87,15 @@ func gpgAddKeys(globalFlags *types.GlobalFlags, flags *gpgAddFlags, cmd *cobra.C
log.Error().Err(err).Msgf(L("failed to show key %s"), hostKeyPath)
continue
}
if !flags.Force {
ret, err := utils.YesNo("Do you want to continue")
if err != nil {
return err
}
if !ret {
return nil
}
}

containerKeyPath := filepath.Join(filepath.Dir(customKeyringPath), keyname)

Expand Down
20 changes: 20 additions & 0 deletions shared/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ func AskIfMissing(value *string, prompt string, min int, max int, checker func(s
}
}

// YesNo asks a question in CLI.
func YesNo(question string) (bool, error) {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [y/N]?", question)

response, err := reader.ReadString('\n')
if err != nil {
return false, err
}

response = strings.ToLower(strings.TrimSpace(response))

if strings.ToLower(response) == "y" || strings.ToLower(response) == "yes" {
return true, nil
}
return false, nil
}
}

// ComputeImage assembles the container image from its name and tag.
func ComputeImage(name string, tag string, appendToName ...string) (string, error) {
imageValid := regexp.MustCompile("^((?:[^:/]+(?::[0-9]+)?/)?[^:]+)(?::([^:]+))?$")
Expand Down

0 comments on commit db13a40

Please sign in to comment.