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

Migrate generate to cobra. #788

Merged
merged 1 commit into from
Sep 24, 2021
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
1 change: 1 addition & 0 deletions cmd/cosign/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func New() *cobra.Command {

// Add sub-commands.
addPublicKey(cmd)
addGenerate(cmd)
addSign(cmd)
addSignBlob(cmd)
addGenerateKeyPair(cmd)
Expand Down
60 changes: 60 additions & 0 deletions cmd/cosign/cli/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"flag"

"github.com/spf13/cobra"

"github.com/sigstore/cosign/cmd/cosign/cli/generate"
"github.com/sigstore/cosign/cmd/cosign/cli/options"
)

func addGenerate(topLevel *cobra.Command) {
o := &options.GenerateOptions{}

cmd := &cobra.Command{
Use: "generate",
Short: "Generates (unsigned) signature payloads from the supplied container image.\ncosign generate [--a key=value] <image uri>",
Long: `Generates an unsigned payload from the supplied container image and flags.
This payload matches the one generated by the "cosign sign" command and can be used if you need
to sign payloads with your own tooling or algorithms.`,
Example: `
# Generate a simple payload for an image
cosign generate <IMAGE>

# Generate a payload with specific annotations
cosign generate -a foo=bar <IMAGE>

# Use this payload in another tool
gpg --output image.sig --detach-sig <(cosign generate <IMAGE>)`,

RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return flag.ErrHelp
}
annotationMap, err := o.AnnotationsMap()
if err != nil {
return err
}
return generate.GenerateCmd(cmd.Context(), o.RegistryOpts, args[0], annotationMap.Annotations, cmd.OutOrStdout())
},
}

options.AddGenerateOptions(cmd, o)
topLevel.AddCommand(cmd)
}
10 changes: 4 additions & 6 deletions cmd/cosign/cli/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"io"
"os"

"github.com/google/go-containerregistry/pkg/name"
"github.com/peterbourgon/ff/v3/ffcli"
Expand All @@ -31,6 +30,8 @@ import (
"github.com/sigstore/sigstore/pkg/signature/payload"
)

// Generate subcommand for ffcli.
// Deprecated: this will be deleted when the migration from ffcli to cobra is done.
func Generate() *ffcli.Command {
var (
flagset = flag.NewFlagSet("cosign generate", flag.ExitOnError)
Expand All @@ -49,7 +50,7 @@ This payload matches the one generated by the "cosign sign" command and can be u
to sign payloads with your own tooling or algorithms.

EXAMPLES
# Generage a simple payload for an image
# Generate a simple payload for an image
cosign generate <IMAGE>

# Generate a payload with specific annotations
Expand All @@ -59,10 +60,7 @@ EXAMPLES
gpg --output image.sig --detach-sig <(cosign generate <IMAGE>)`,
FlagSet: flagset,
Exec: func(ctx context.Context, args []string) error {
if len(args) != 1 {
return flag.ErrHelp
}
return GenerateCmd(ctx, regOpts, args[0], annotations.Annotations, os.Stdout)
panic("this command is now implemented in cobra.")
},
}
}
Expand Down
51 changes: 51 additions & 0 deletions cmd/cosign/cli/options/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package options

import (
"fmt"
"strings"

"github.com/spf13/cobra"

sigs "github.com/sigstore/cosign/pkg/signature"
)

// AnnotationOptions is the top level wrapper for the annotations.
type AnnotationOptions struct {
Annotations []string
}

func (s *AnnotationOptions) AnnotationsMap() (sigs.AnnotationsMap, error) {
ann := sigs.AnnotationsMap{}
for _, a := range s.Annotations {
kv := strings.Split(a, "=")
if len(kv) != 2 {
return ann, fmt.Errorf("unable to parse annotation: %s", a)
}
if ann.Annotations == nil {
ann.Annotations = map[string]interface{}{}
}
ann.Annotations[kv[0]] = kv[1]
}
return ann, nil
}

// AddAnnotationOptions adds annotation options to cmd.
func AddAnnotationOptions(cmd *cobra.Command, o *AnnotationOptions) {
cmd.Flags().StringSliceVarP(&o.Annotations, "annotations", "a", nil,
"extra key=value pairs to sign")
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/sigstore/cosign/pkg/signature"
)

func TestSignOptions_AnnotationsMap(t *testing.T) {
func TestAnnotationOptions_AnnotationsMap(t *testing.T) {
tests := []struct {
name string
annotations []string
Expand All @@ -47,7 +47,7 @@ func TestSignOptions_AnnotationsMap(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &SignOptions{
s := &AnnotationOptions{
Annotations: tt.annotations,
}
got, err := s.AnnotationsMap()
Expand Down
34 changes: 34 additions & 0 deletions cmd/cosign/cli/options/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package options

import (
"github.com/spf13/cobra"
)

// GenerateOptions is the top level wrapper for the generate command.
type GenerateOptions struct {
AnnotationOptions
RegistryOpts RegistryOpts
}

// AddGenerateOptions adds the generate command options to cmd.
func AddGenerateOptions(cmd *cobra.Command, o *GenerateOptions) {
AddAnnotationOptions(cmd, &o.AnnotationOptions)

cmd.Flags().BoolVar(&o.RegistryOpts.AllowInsecure, "allow-insecure-registry", false,
"whether to allow insecure connections to registries. Don't use this for anything but testing")
}
25 changes: 2 additions & 23 deletions cmd/cosign/cli/options/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
package options

import (
"fmt"
"strings"

"github.com/spf13/cobra"

sigs "github.com/sigstore/cosign/pkg/signature"
)

// SignOptions is the top level wrapper for the sign command.
Expand All @@ -40,25 +35,10 @@ type SignOptions struct {
OIDC OIDCOptions
Attachment string

Annotations []string
AnnotationOptions
RegistryOpts RegistryOpts
}

func (s *SignOptions) AnnotationsMap() (sigs.AnnotationsMap, error) {
ann := sigs.AnnotationsMap{}
for _, a := range s.Annotations {
kv := strings.Split(a, "=")
if len(kv) != 2 {
return ann, fmt.Errorf("unable to parse annotation: %s", a)
}
if ann.Annotations == nil {
ann.Annotations = map[string]interface{}{}
}
ann.Annotations[kv[0]] = kv[1]
}
return ann, nil
}

// AddSignOptions adds the sign command options to cmd.
func AddSignOptions(cmd *cobra.Command, o *SignOptions) {
cmd.Flags().StringVar(&o.Key, "key", "",
Expand All @@ -84,8 +64,7 @@ func AddSignOptions(cmd *cobra.Command, o *SignOptions) {
cmd.Flags().StringVar(&o.Attachment, "attachment", "",
"related image attachment to sign (sbom), default none")

cmd.Flags().StringSliceVarP(&o.Annotations, "annotations", "a", nil,
"extra key=value pairs to sign")
AddAnnotationOptions(cmd, &o.AnnotationOptions)

cmd.Flags().BoolVar(&o.RegistryOpts.AllowInsecure, "allow-insecure-registry", false,
"whether to allow insecure connections to registries. Don't use this for anything but testing")
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
if len(os.Args) > 1 {
switch os.Args[1] {
case "public-key", "generate-key-pair",
"sign", "sign-blob",
"generate", "sign", "sign-blob",
"attest":
// cobra.
default:
Expand Down