Skip to content

Commit

Permalink
accepts attachment-tag-prefix for cosign copy (#3014)
Browse files Browse the repository at this point in the history
* accepts attachment-tag-prefix for cosign copy

Signed-off-by: Mritunjay <mritunjaysharma394@gmail.com>

* adds tests for cosign copy attachment-tag-prefix

Signed-off-by: Mritunjay <mritunjaysharma394@gmail.com>

* refactors for prefix logic

Signed-off-by: Mritunjay <mritunjaysharma394@gmail.com>

---------

Signed-off-by: Mritunjay <mritunjaysharma394@gmail.com>
  • Loading branch information
mritunjaysharma394 committed May 31, 2023
1 parent 0a4db33 commit c17a36a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cmd/cosign/cli/copy/copy.go
Expand Up @@ -49,17 +49,24 @@ func CopyCmd(ctx context.Context, regOpts options.RegistryOptions, srcImg, dstIm
}
dstRepoRef := dstRef.Context()

ociRemoteOpts, err := regOpts.ClientOpts(ctx)
if err != nil {
return err
}

remoteOpts := regOpts.GetRegistryClientOpts(ctx)

pusher, err := remote.NewPusher(remoteOpts...)
if err != nil {
return err
}

ociRemoteOpts = append(ociRemoteOpts, ociremote.WithRemoteOptions(remoteOpts...))

g, gctx := errgroup.WithContext(ctx)
g.SetLimit(runtime.GOMAXPROCS(0))

root, err := ociremote.SignedEntity(srcRef, ociremote.WithRemoteOptions(remoteOpts...))
root, err := ociremote.SignedEntity(srcRef, ociRemoteOpts...)
if err != nil {
return err
}
Expand All @@ -73,7 +80,7 @@ func CopyCmd(ctx context.Context, regOpts options.RegistryOptions, srcImg, dstIm
srcDigest := srcRepoRef.Digest(h.String())

copyTag := func(tm tagMap) error {
src, err := tm(srcDigest, ociremote.WithRemoteOptions(remoteOpts...))
src, err := tm(srcDigest, ociRemoteOpts...)
if err != nil {
return err
}
Expand Down Expand Up @@ -102,6 +109,7 @@ func CopyCmd(ctx context.Context, regOpts options.RegistryOptions, srcImg, dstIm
// Copy the entity itself.
g.Go(func() error {
dst := dstRepoRef.Tag(srcDigest.Identifier())
dst = dst.Tag(fmt.Sprint(regOpts.RefOpts.TagPrefix, h.Algorithm, "-", h.Hex))
return remoteCopy(ctx, pusher, srcDigest, dst, force, remoteOpts...)
})

Expand Down
40 changes: 40 additions & 0 deletions cmd/cosign/cli/copy/copy_test.go
@@ -0,0 +1,40 @@
// Copyright 2023 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 copy

import (
"context"
"testing"

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

func TestCopyAttachmentTagPrefix(t *testing.T) {
ctx := context.Background()

refOpts := options.ReferenceOptions{
TagPrefix: "test-tag",
}

srcImg := "alpine"
destImg := "test-alpine"

err := CopyCmd(ctx, options.RegistryOptions{
RefOpts: refOpts,
}, srcImg, destImg, false, true)
if err == nil {
t.Fatal("failed to copy with attachment-tag-prefix")
}
}

0 comments on commit c17a36a

Please sign in to comment.