Skip to content

Commit

Permalink
implement pubkey-output flag for sign-blob subcommand
Browse files Browse the repository at this point in the history
This commit implements the pubkey-output flag for a sign-blob subcommand. With this flag the user is able to store the public key without reading the public key from stdout.

Signed-off-by: Christian Rebischke <chris@shibumi.dev>
  • Loading branch information
shibumi committed Nov 9, 2021
1 parent 55471fc commit 74a15c9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/cosign/cli/options/signblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type SignBlobOptions struct {
Key string
Base64Output bool
Output string // TODO: this should be the root output file arg.
PubKeyOutput string
SecurityKey SecurityKeyOptions
Fulcio FulcioOptions
Rekor RekorOptions
Expand All @@ -49,4 +50,7 @@ func (o *SignBlobOptions) AddFlags(cmd *cobra.Command) {

cmd.Flags().StringVar(&o.Output, "output", "",
"write the signature to FILE")

cmd.Flags().StringVar(&o.PubKeyOutput, "pubkey-output", "",
"write the public key/certificate to FILE")
}
24 changes: 22 additions & 2 deletions cmd/cosign/cli/sign/sign_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type KeyOpts struct {
}

// nolint
func SignBlobCmd(ctx context.Context, ko KeyOpts, regOpts options.RegistryOptions, payloadPath string, b64 bool, output string) ([]byte, error) {
func SignBlobCmd(ctx context.Context, ko KeyOpts, regOpts options.RegistryOptions, payloadPath string, b64 bool, output string, pubKeyOutput string) ([]byte, error) {
var payload []byte
var err error

Expand All @@ -76,9 +76,9 @@ func SignBlobCmd(ctx context.Context, ko KeyOpts, regOpts options.RegistryOption
return nil, errors.Wrap(err, "signing blob")
}

var rekorBytes []byte
if options.EnableExperimental() {
// TODO: Refactor with sign.go
var rekorBytes []byte
if sv.Cert != nil {
fmt.Fprintf(os.Stderr, "signing with ephemeral certificate:\n%s\n", string(sv.Cert))
rekorBytes = sv.Cert
Expand All @@ -100,6 +100,26 @@ func SignBlobCmd(ctx context.Context, ko KeyOpts, regOpts options.RegistryOption
fmt.Fprintln(os.Stderr, "tlog entry created with index:", *entry.LogIndex)
}

if pubKeyOutput != "" {
f, err := os.Create(pubKeyOutput)
if err != nil {
return nil, err
}
defer f.Close()

if b64 {
_, err = f.Write([]byte(base64.StdEncoding.EncodeToString(rekorBytes)))
if err != nil {
return nil, err
}
} else {
_, err = f.Write(rekorBytes)
if err != nil {
return nil, err
}
}
}

if output != "" {
f, err := os.Create(output)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/signblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func SignBlob() *cobra.Command {
OIDCClientSecret: o.OIDC.ClientSecret,
}
for _, blob := range args {
if _, err := sign.SignBlobCmd(cmd.Context(), ko, o.Registry, blob, o.Base64Output, o.Output); err != nil {
if _, err := sign.SignBlobCmd(cmd.Context(), ko, o.Registry, blob, o.Base64Output, o.Output, o.PubKeyOutput); err != nil {
return errors.Wrapf(err, "signing %s", blob)
}
}
Expand Down

0 comments on commit 74a15c9

Please sign in to comment.