-
Notifications
You must be signed in to change notification settings - Fork 547
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
Feature: adds rekor support for cosign attach command #2994
Closed
mritunjaysharma394
wants to merge
1
commit into
sigstore:main
from
mritunjaysharma394:rekor-attach-support
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// | ||
// 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 attach | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
"github.com/sigstore/cosign/v2/cmd/cosign/cli/options" | ||
"github.com/sigstore/cosign/v2/cmd/cosign/cli/rekor" | ||
"github.com/sigstore/cosign/v2/cmd/cosign/cli/sign" | ||
"github.com/sigstore/cosign/v2/pkg/cosign" | ||
"github.com/sigstore/cosign/v2/pkg/cosign/bundle" | ||
"github.com/sigstore/cosign/v2/pkg/oci/mutate" | ||
ociremote "github.com/sigstore/cosign/v2/pkg/oci/remote" | ||
"github.com/sigstore/cosign/v2/pkg/oci/static" | ||
"github.com/sigstore/rekor/pkg/generated/client" | ||
"github.com/sigstore/rekor/pkg/generated/models" | ||
) | ||
|
||
type tlogUploadFn func(*client.Rekor, []byte) (*models.LogEntryAnon, error) | ||
|
||
func uploadToTlog(ctx context.Context, sv *sign.SignerVerifier, rekorURL string, upload tlogUploadFn) (*bundle.RekorBundle, error) { | ||
rekorBytes, err := sv.Bytes(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rekorClient, err := rekor.NewClient(rekorURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
entry, err := upload(rekorClient, rekorBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fmt.Fprintln(os.Stderr, "tlog entry created with index:", *entry.LogIndex) | ||
return bundle.EntryToBundle(entry), nil | ||
} | ||
|
||
func RekorCmd(ctx context.Context, regOpts options.RegistryOptions, rekorURL, sigRef, payloadRef, certRef, certChainRef, imageRef string) error { | ||
b64SigBytes, err := signatureBytes(sigRef) | ||
if err != nil { | ||
return err | ||
} else if len(b64SigBytes) == 0 { | ||
return errors.New("empty signature") | ||
} | ||
|
||
ref, err := name.ParseReference(imageRef, regOpts.NameOptions()...) | ||
if err != nil { | ||
return err | ||
} | ||
ociremoteOpts, err := regOpts.ClientOpts(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
digest, err := ociremote.ResolveDigest(ref, ociremoteOpts...) | ||
if err != nil { | ||
return err | ||
} | ||
// Overwrite "ref" with a digest to avoid a race where we use a tag | ||
// multiple times, and it potentially points to different things at | ||
// each access. | ||
ref = digest // nolint | ||
|
||
var payload []byte | ||
if payloadRef == "" { | ||
payload, err = cosign.ObsoletePayload(ctx, digest) | ||
} else { | ||
payload, err = os.ReadFile(filepath.Clean(payloadRef)) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sig, err := static.NewSignature(payload, string(b64SigBytes)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var cert []byte | ||
var certChain []byte | ||
|
||
if certRef != "" { | ||
cert, err = os.ReadFile(filepath.Clean(certRef)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if certChainRef != "" { | ||
certChain, err = os.ReadFile(filepath.Clean(certChainRef)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
sv, err := sign.SignerFromKeyOpts(ctx, certRef, certChainRef, options.KeyOpts{}) | ||
if err != nil { | ||
return fmt.Errorf("getting signer: %w", err) | ||
} | ||
defer sv.Close() | ||
|
||
bundle, err := uploadToTlog(ctx, sv, rekorURL, func(r *client.Rekor, b []byte) (*models.LogEntryAnon, error) { | ||
checkSum := sha256.New() | ||
if _, err := checkSum.Write(payload); err != nil { | ||
return nil, err | ||
} | ||
return cosign.TLogUpload(ctx, r, b64SigBytes, checkSum, b) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
recorSig, err := mutate.Signature(sig, mutate.WithCertChain(cert, certChain), mutate.WithBundle(bundle)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
se, err := ociremote.SignedEntity(digest, ociremoteOpts...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Attach the signature to the entity. | ||
newSE, err := mutate.AttachSignatureToEntity(se, recorSig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Publish the signatures associated with this entity | ||
return ociremote.WriteSignatures(digest.Repository, newSE, ociremoteOpts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bundle should already exist, correct? This command, like attach signature, should take an existing bundle and attach it to the container metadata.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, Yes even I felt the same initially but I was skeptical and had little less idea being new to the code base on how we will get rekor Bundle struct back? What should the user pass as a flag? Should rekor-url be passed (like it is passed with attest command?) or should we straight up use
sig.Bundle()
to see if it exists or not and if it does then write/mutate signature using withBundle() opts ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A rekor bundle is usually in the format:
My only doubt is how will we allow the user to pass it to be attached? Or it hasn't has to be passed at all? And we can leverage
sig.Bundle()
because it already exists but was not attached in containers manifest (sounds contradicting to me as I am unsure about the passing thing)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would accept a path to a file with the bundle.
sig.Bundle
is where it's stored, and if it's set, it will be attached to the container.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it so we will read that file and unmarshal it in the form of
*bundle.RekorBundle
and do something like this, right?: