Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ask confirmation before importing gpg key #264

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(L("Do you want to continue"))
mbussolotto marked this conversation as resolved.
Show resolved Hide resolved
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