Skip to content

Commit

Permalink
Add --yes flag cosign import-key-pair to skip the overwrite conf…
Browse files Browse the repository at this point in the history
…irmation. (#3383)

* Add --yes flag to skip overwrite confirmation.

Signed-off-by: zhaoyonghe <yonghe.zhao@yahoo.com>

* Change the flag description.

Signed-off-by: zhaoyonghe <yonghe.zhao@yahoo.com>

---------

Signed-off-by: zhaoyonghe <yonghe.zhao@yahoo.com>
  • Loading branch information
zhaoyonghe committed Nov 28, 2023
1 parent 0763bff commit 878b6c7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
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 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.

0 comments on commit 878b6c7

Please sign in to comment.