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

Add --yes flag cosign import-key-pair to skip the overwrite confirmation. #3383

Merged
merged 2 commits into from
Nov 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/cosign/cli/import_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CAVEATS:
the COSIGN_PASSWORD environment variable to provide one.`,
PersistentPreRun: options.BindViper,
RunE: func(cmd *cobra.Command, args []string) error {
return importkeypair.ImportKeyPairCmd(cmd.Context(), o.Key, o.OutputKeyPrefix, args)
return importkeypair.ImportKeyPairCmd(cmd.Context(), *o, args)
},
}

Expand Down
15 changes: 9 additions & 6 deletions cmd/cosign/cli/importkeypair/import_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"os"

"github.com/sigstore/cosign/v2/cmd/cosign/cli/options"
icos "github.com/sigstore/cosign/v2/internal/pkg/cosign"
"github.com/sigstore/cosign/v2/internal/ui"
"github.com/sigstore/cosign/v2/pkg/cosign"
Expand All @@ -33,14 +34,14 @@ var (
)

// nolint
func ImportKeyPairCmd(ctx context.Context, keyVal string, outputKeyPrefixVal string, args []string) error {
keys, err := cosign.ImportKeyPair(keyVal, GetPass)
func ImportKeyPairCmd(ctx context.Context, o options.ImportKeyPairOptions, args []string) error {
keys, err := cosign.ImportKeyPair(o.Key, GetPass)
if err != nil {
return err
}

privateKeyFileName := outputKeyPrefixVal + ".key"
publicKeyFileName := outputKeyPrefixVal + ".pub"
privateKeyFileName := o.OutputKeyPrefix + ".key"
publicKeyFileName := o.OutputKeyPrefix + ".pub"

fileExists, err := icos.FileExists(privateKeyFileName)
if err != nil {
Expand All @@ -49,8 +50,10 @@ func ImportKeyPairCmd(ctx context.Context, keyVal string, outputKeyPrefixVal str

if fileExists {
ui.Warnf(ctx, "File %s already exists. Overwrite?", privateKeyFileName)
if err := ui.ConfirmContinue(ctx); err != nil {
return err
if !o.SkipConfirmation {
if err := ui.ConfirmContinue(ctx); err != nil {
return err
}
}
}
// TODO: make sure the perms are locked down first.
Expand Down
7 changes: 6 additions & 1 deletion cmd/cosign/cli/importkeypair/import_key_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/sigstore/cosign/v2/cmd/cosign/cli/options"
icos "github.com/sigstore/cosign/v2/internal/pkg/cosign"
)

Expand Down Expand Up @@ -62,7 +63,11 @@ func TestImportOfKeys(t *testing.T) {
// framework if there is no value set by the user when running the
// command.
outputtedKeyPairFileName := "my-test"
ImportKeyPairCmd(context.Background(), privateKeyFileName, outputtedKeyPairFileName, nil)
ImportKeyPairCmd(context.Background(), options.ImportKeyPairOptions{
Key: privateKeyFileName,
OutputKeyPrefix: outputtedKeyPairFileName,
SkipConfirmation: false,
}, nil)

// removes temporary RSA private key used for test
checkIfFileExistsThenDelete(privateKeyFileName, t)
Expand Down
5 changes: 5 additions & 0 deletions cmd/cosign/cli/options/import_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ImportKeyPairOptions struct {

// Filename used for outputted keys
OutputKeyPrefix string

SkipConfirmation bool
}

var _ Interface = (*ImportKeyPairOptions)(nil)
Expand All @@ -39,4 +41,7 @@ func (o *ImportKeyPairOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.OutputKeyPrefix, "output-key-prefix", "o", "import-cosign",
"name used for outputted key pairs")
_ = cmd.Flags().SetAnnotation("output-key-prefix", cobra.BashCompFilenameExt, []string{})

cmd.Flags().BoolVarP(&o.SkipConfirmation, "yes", "y", false,
"skip confirmation prompts for non-destructive operations")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: is file overwrite considered as destructive operation? Should we change it to a different name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should say destructive. Given the simplicity of this command, you could just say "skip confirmation prompts for overwriting existing key"

}
1 change: 1 addition & 0 deletions doc/cosign_import-key-pair.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.