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

Use cosign.ConfirmPrompt more consistently #2039

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 6 additions & 21 deletions cmd/cosign/cli/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@
package cli

import (
"bufio"
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/spf13/cobra"

"github.com/sigstore/cosign/cmd/cosign/cli/options"
"github.com/sigstore/cosign/pkg/cosign"
ociremote "github.com/sigstore/cosign/pkg/oci/remote"
)

Expand All @@ -50,27 +49,13 @@ func Clean() *cobra.Command {
return cmd
}

// confirmPromptDestructive prompts the user for confirmation for an action. Ignores
// skipConfirmation.
func confirmPromptDestructive(msg string) (bool, error) {
fmt.Fprintf(os.Stderr, "%s\n\nAre you sure you want to continue? (y/[N]): ", msg)
reader := bufio.NewReader(os.Stdin)
r, err := reader.ReadString('\n')
func CleanCmd(ctx context.Context, regOpts options.RegistryOptions, cleanType, imageRef string, force bool) error {
ok, err := cosign.ConfirmPrompt(prompt(cleanType), force)
if err != nil {
return false, err
return err
}
return strings.Trim(r, "\n") == "Y" || strings.Trim(r, "\n") == "y", nil
}

func CleanCmd(ctx context.Context, regOpts options.RegistryOptions, cleanType, imageRef string, force bool) error {
if !force {
ok, err := confirmPromptDestructive(prompt(cleanType))
if err != nil {
return err
}
if !ok {
return nil
}
if !ok {
return nil
}

ref, err := name.ParseReference(imageRef)
Expand Down
26 changes: 13 additions & 13 deletions cmd/cosign/cli/fulcio/fulcio.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ import (
)

const (
FlowNormal = "normal"
FlowDevice = "device"
FlowToken = "token"
flowNormal = "normal"
flowDevice = "device"
flowToken = "token"
// spacing is intentional to have this indented
PrivacyStatement = `
privacyStatement = `
Note that there may be personally identifiable information associated with this signed artifact.
This may include the email address associated with the account with which you authenticate.
This information will be used for signing this artifact and will be stored in public transparency logs and cannot be removed later.`
PrivacyStatementConfirmation = " By typing 'y', you attest that you grant (or have permission to grant) and agree to have this information stored permanently in transparency logs."
privacyStatementConfirmation = " By typing 'y', you attest that you grant (or have permission to grant) and agree to have this information stored permanently in transparency logs."
)

type oidcConnector interface {
Expand Down Expand Up @@ -95,12 +95,12 @@ func getCertForOauthID(priv *ecdsa.PrivateKey, fc api.Client, connector oidcConn
func GetCert(ctx context.Context, priv *ecdsa.PrivateKey, idToken, flow, oidcIssuer, oidcClientID, oidcClientSecret, oidcRedirectURL string, fClient api.Client) (*api.CertificateResponse, error) {
c := &realConnector{}
switch flow {
case FlowDevice:
case flowDevice:
c.flow = oauthflow.NewDeviceFlowTokenGetter(
oidcIssuer, oauthflow.SigstoreDeviceURL, oauthflow.SigstoreTokenURL)
case FlowNormal:
case flowNormal:
c.flow = oauthflow.DefaultIDTokenGetter
case FlowToken:
case flowToken:
c.flow = &oauthflow.StaticTokenGetter{RawToken: idToken}
default:
return nil, fmt.Errorf("unsupported oauth flow: %s", flow)
Expand Down Expand Up @@ -151,27 +151,27 @@ func NewSigner(ctx context.Context, ko options.KeyOpts) (*Signer, error) {
}
fmt.Fprintln(os.Stderr, "Retrieving signed certificate...")

fmt.Fprintln(os.Stderr, PrivacyStatement)
fmt.Fprintln(os.Stderr, privacyStatement)

var flow string
switch {
case ko.FulcioAuthFlow != "":
// Caller manually set flow option.
flow = ko.FulcioAuthFlow
case idToken != "":
flow = FlowToken
flow = flowToken
case !term.IsTerminal(0):
fmt.Fprintln(os.Stderr, "Non-interactive mode detected, using device flow.")
flow = FlowDevice
flow = flowDevice
default:
ok, err := cosign.ConfirmPrompt(PrivacyStatementConfirmation, ko.SkipConfirmation)
ok, err := cosign.ConfirmPrompt(privacyStatementConfirmation, ko.SkipConfirmation)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("no confirmation")
}
flow = FlowNormal
flow = flowNormal
}
Resp, err := GetCert(ctx, priv, idToken, flow, ko.OIDCIssuer, ko.OIDCClientID, ko.OIDCClientSecret, ko.OIDCRedirectURL, fClient) // TODO, use the chain.
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/cosign/cli/options/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ var _ Interface = (*CleanOptions)(nil)
func (c *CleanOptions) AddFlags(cmd *cobra.Command) {
c.Registry.AddFlags(cmd)
cmd.Flags().StringVarP(&c.CleanType, "type", "", "all", "a type of clean: <signature|attestation|sbom|all> (default: all)")
// TODO: Rename to --skip-confirmation for consistency?
Copy link
Member

Choose a reason for hiding this comment

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

it makes sense a lot to me, may I open an issue for this? @imjasonh

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! I think we'll probably want --force to continue to exist for 1+ release, with a warning that it's renamed to --skip-confirmation, and use --force's value to set o.SkipConfirmation. If both are set and they disagree, then fail. Does that sound right?

Copy link
Member

Choose a reason for hiding this comment

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

Absolutely !

Copy link
Member

Choose a reason for hiding this comment

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

Issue: #2044

cmd.Flags().BoolVarP(&c.Force, "force", "f", false, "do not prompt for confirmation")
}