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

Move cosign download to cosign download signature. #392

Merged
merged 1 commit into from
Jun 26, 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* BREAKING: Move `cosign upload-blob` to `cosign upload blob`.
* BREAKING: Move `cosign upload` to `cosign attach signature`.
* BREAKING: Move `cosign download` to `cosign download signature`.

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $ cat gcpkms.sig | base64 | cosign attach signature -signature - us-central1-doc
Now (on another machine) download the public key, payload, signatures and verify it!

```shell
$ cosign download us-central1-docker.pkg.dev/dlorenc-vmtest2/test/taskrun > signatures.json
$ cosign download signature us-central1-docker.pkg.dev/dlorenc-vmtest2/test/taskrun > signatures.json
# There could be multiple signatures, let's pretend it's the last one.
# Extract the payload and signature, base64 decoding them.
$ cat signatures.json | tail -1 | jq -r .Payload | base64 -D > payload
Expand Down
2 changes: 1 addition & 1 deletion USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ invalid or missing annotation in claim: map[sig:original]
Each signature is printed to stdout in a json format:

```
$ cosign download us-central1-docker.pkg.dev/dlorenc-vmtest2/test/taskrun
$ cosign download signature us-central1-docker.pkg.dev/dlorenc-vmtest2/test/taskrun
{"Base64Signature":"Ejy6ipGJjUzMDoQFePWixqPBYF0iSnIvpMWps3mlcYNSEcRRZelL7GzimKXaMjxfhy5bshNGvDT5QoUJ0tqUAg==","Payload":"eyJDcml0aWNhbCI6eyJJZGVudGl0eSI6eyJkb2NrZXItcmVmZXJlbmNlIjoiIn0sIkltYWdlIjp7IkRvY2tlci1tYW5pZmVzdC1kaWdlc3QiOiI4N2VmNjBmNTU4YmFkNzliZWVhNjQyNWEzYjI4OTg5ZjAxZGQ0MTcxNjQxNTBhYjNiYWFiOThkY2JmMDRkZWY4In0sIlR5cGUiOiIifSwiT3B0aW9uYWwiOm51bGx9"}
```

Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/attach/sig.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func SignatureCmd(ctx context.Context, sigRef, payloadRef, imageRef string) erro
repo := ref.Context()
img := repo.Digest(get.Digest.String())

sigRepo, err := cli.SignatureRepositoryForImage(ref)
sigRepo, err := cli.TargetRepositoryForImage(ref)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func CleanCmd(_ context.Context, imageRef string) error {
return err
}

sigRepo, err := SignatureRepositoryForImage(ref)
sigRepo, err := TargetRepositoryForImage(ref)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func CopyCmd(ctx context.Context, srcImg, dstImg string, sigOnly, force bool) er
return err
}

srcSigRepo, err := SignatureRepositoryForImage(srcRef)
srcSigRepo, err := TargetRepositoryForImage(srcRef)
if err != nil {
return err
}
Expand Down
40 changes: 40 additions & 0 deletions cmd/cosign/cli/download/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// 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 download

import (
"context"
"flag"

"github.com/peterbourgon/ff/v3/ffcli"
)

func Download() *ffcli.Command {
var (
flagset = flag.NewFlagSet("cosign download", flag.ExitOnError)
)

return &ffcli.Command{
Name: "download",
ShortUsage: "cosign download",
ShortHelp: "download contains tools to download artifacts and attached artifacts in a registry",
FlagSet: flagset,
Subcommands: []*ffcli.Command{Signature()},
Exec: func(ctx context.Context, args []string) error {
return flag.ErrHelp
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cli
package download

import (
"context"
Expand All @@ -25,34 +25,34 @@ import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/peterbourgon/ff/v3/ffcli"

"github.com/sigstore/cosign/cmd/cosign/cli"
"github.com/sigstore/cosign/pkg/cosign"
)

func Download() *ffcli.Command {
func Signature() *ffcli.Command {
var (
flagset = flag.NewFlagSet("cosign download", flag.ExitOnError)
flagset = flag.NewFlagSet("cosign download signature", flag.ExitOnError)
)
return &ffcli.Command{
Name: "download",
ShortUsage: "cosign download <image uri>",
Name: "signature",
ShortUsage: "cosign download signature <image uri>",
ShortHelp: "Download signatures from the supplied container image",
FlagSet: flagset,
Exec: func(ctx context.Context, args []string) error {
if len(args) != 1 {
return flag.ErrHelp
}
return DownloadCmd(ctx, args[0])
return SignatureCmd(ctx, args[0])
},
}
}

func DownloadCmd(ctx context.Context, imageRef string) error {
func SignatureCmd(ctx context.Context, imageRef string) error {
ref, err := name.ParseReference(imageRef)
if err != nil {
return err
}
sigRepo, err := SignatureRepositoryForImage(ref)
sigRepo, err := cli.TargetRepositoryForImage(ref)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func SignCmd(ctx context.Context, so SignOpts,
continue
}

sigRepo, err := SignatureRepositoryForImage(ref)
sigRepo, err := TargetRepositoryForImage(ref)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/triangulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func MungeCmd(_ context.Context, imageRef string) error {
return err
}

sigRepo, err := SignatureRepositoryForImage(ref)
sigRepo, err := TargetRepositoryForImage(ref)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cosign/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func TlogServer() string {
return rekorServer
}

func SignatureRepositoryForImage(signedImg name.Reference) (name.Repository, error) {
func TargetRepositoryForImage(img name.Reference) (name.Repository, error) {
wantRepo := os.Getenv(repoEnv)
if wantRepo == "" {
return signedImg.Context(), nil
return img.Context(), nil
}
return name.NewRepository(wantRepo)
}
6 changes: 3 additions & 3 deletions cmd/cosign/cli/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/google/go-containerregistry/pkg/name"
)

func TestSignatureRepositoryForImage(t *testing.T) {
func TestTargetRepositoryForImage(t *testing.T) {
tests := []struct {
desc string
image name.Reference
Expand Down Expand Up @@ -71,9 +71,9 @@ func TestSignatureRepositoryForImage(t *testing.T) {
os.Setenv(repoEnv, test.envRepo)
defer os.Unsetenv(repoEnv)

got, err := SignatureRepositoryForImage(test.image)
got, err := TargetRepositoryForImage(test.image)
if err != nil {
t.Fatalf("SignatureRepositoryForImage returned error: %v", err)
t.Fatalf("TargetRepositoryForImage returned error: %v", err)
}
if got.Name() != test.want.Name() {
t.Errorf("expected %s got %s", test.want, got.Name())
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (c *VerifyCommand) Exec(ctx context.Context, args []string) error {
if err != nil {
return err
}
sigRepo, err := SignatureRepositoryForImage(ref)
sigRepo, err := TargetRepositoryForImage(ref)
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/cosign/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/sigstore/cosign/cmd/cosign/cli"
"github.com/sigstore/cosign/cmd/cosign/cli/attach"
"github.com/sigstore/cosign/cmd/cosign/cli/download"
"github.com/sigstore/cosign/cmd/cosign/cli/pivcli"
"github.com/sigstore/cosign/cmd/cosign/cli/upload"
)
Expand All @@ -48,12 +49,14 @@ func main() {
cli.Verify(), cli.Sign(), cli.Generate(), cli.SignBlob(), cli.VerifyBlob(),
// Upload sub-tree
upload.Upload(),
// Upload sub-tree
// Download sub-tree
download.Download(),
// Attach sub-tree
attach.Attach(),
// PIV sub-tree
pivcli.PivKey(),
// PIV sub-tree
cli.Copy(), cli.Clean(), cli.Triangulate(), cli.Download(),
cli.Copy(), cli.Clean(), cli.Triangulate(),
cli.Version()},
Exec: func(context.Context, []string) error {
return flag.ErrHelp
Expand Down
2 changes: 1 addition & 1 deletion cmd/sget/cli/sget.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func SgetCmd(ctx context.Context, imageRef, keyRef string) (io.ReadCloser, error
}

if co.PubKey != nil || cli.EnableExperimental() {
sigRepo, err := cli.SignatureRepositoryForImage(ref)
sigRepo, err := cli.TargetRepositoryForImage(ref)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions copasetic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func main() {
if err != nil {
return nil, err
}
sigRepo, err := cli.SignatureRepositoryForImage(ref)
sigRepo, err := cli.TargetRepositoryForImage(ref)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func main() {
if err != nil {
return nil, err
}
sigRepo, err := cli.SignatureRepositoryForImage(ref)
sigRepo, err := cli.TargetRepositoryForImage(ref)
if err != nil {
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (

"github.com/sigstore/cosign/cmd/cosign/cli"
"github.com/sigstore/cosign/cmd/cosign/cli/attach"
"github.com/sigstore/cosign/cmd/cosign/cli/download"
"github.com/sigstore/cosign/cmd/cosign/cli/upload"
sget "github.com/sigstore/cosign/cmd/sget/cli"
"github.com/sigstore/cosign/pkg/cosign"
Expand Down Expand Up @@ -84,15 +85,15 @@ func TestSignVerify(t *testing.T) {
// Verify should fail at first
mustErr(verify(pubKeyPath, imgName, true, nil), t)
// So should download
mustErr(cli.DownloadCmd(ctx, imgName), t)
mustErr(download.SignatureCmd(ctx, imgName), t)

// Now sign the image
so := cli.SignOpts{KeyRef: privKeyPath, Pf: passFunc}
must(cli.SignCmd(ctx, so, imgName, true, "", false, false), t)

// Now verify and download should work!
must(verify(pubKeyPath, imgName, true, nil), t)
must(cli.DownloadCmd(ctx, imgName), t)
must(download.SignatureCmd(ctx, imgName), t)

// Look for a specific annotation
mustErr(verify(pubKeyPath, imgName, true, map[string]interface{}{"foo": "bar"}), t)
Expand Down Expand Up @@ -127,7 +128,7 @@ func TestSignVerifyClean(t *testing.T) {

// Now verify and download should work!
must(verify(pubKeyPath, imgName, true, nil), t)
must(cli.DownloadCmd(ctx, imgName), t)
must(download.SignatureCmd(ctx, imgName), t)

// Now clean signature from the given image
must(cli.CleanCmd(ctx, imgName), t)
Expand Down Expand Up @@ -186,20 +187,20 @@ func TestDuplicateSign(t *testing.T) {
// Verify should fail at first
mustErr(verify(pubKeyPath, imgName, true, nil), t)
// So should download
mustErr(cli.DownloadCmd(ctx, imgName), t)
mustErr(download.SignatureCmd(ctx, imgName), t)

// Now sign the image
so := cli.SignOpts{KeyRef: privKeyPath, Pf: passFunc}
must(cli.SignCmd(ctx, so, imgName, true, "", false, false), t)

// Now verify and download should work!
must(verify(pubKeyPath, imgName, true, nil), t)
must(cli.DownloadCmd(ctx, imgName), t)
must(download.SignatureCmd(ctx, imgName), t)

// Signing again should work just fine...
must(cli.SignCmd(ctx, so, imgName, true, "", false, false), t)
// but a duplicate signature should not be a uploaded
sigRepo, err := cli.SignatureRepositoryForImage(ref)
sigRepo, err := cli.TargetRepositoryForImage(ref)
if err != nil {
t.Fatalf("failed to get signature repository: %v", err)
}
Expand Down Expand Up @@ -437,7 +438,7 @@ func TestUploadDownload(t *testing.T) {
}

// Now download it!
sigRepo, err := cli.SignatureRepositoryForImage(ref)
sigRepo, err := cli.TargetRepositoryForImage(ref)
if err != nil {
t.Fatalf("failed to get signature repository: %v", err)
}
Expand Down