-
Notifications
You must be signed in to change notification settings - Fork 86
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
feat: use new verify workflow #373
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9b1209
feat: use new verify workflow
binbin-li 819281d
refactor: refactor
binbin-li 2860bb2
refactor: address pr comments
binbin-li 6ad5608
refactor: refactor result format
binbin-li 8b66938
refactor: refator input/output format
binbin-li 8329127
refactor: todo to issue link
binbin-li 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/notaryproject/notation-go" | ||
"github.com/notaryproject/notation-go/dir" | ||
"github.com/notaryproject/notation-go/signature" | ||
notationregistry "github.com/notaryproject/notation-go/registry" | ||
"github.com/notaryproject/notation-go/verification" | ||
"github.com/notaryproject/notation/internal/cmd" | ||
"github.com/notaryproject/notation/internal/envelope" | ||
"github.com/notaryproject/notation/internal/slices" | ||
"github.com/notaryproject/notation/pkg/cache" | ||
"github.com/notaryproject/notation/pkg/configutil" | ||
"github.com/opencontainers/go-digest" | ||
"github.com/notaryproject/notation/internal/ioutil" | ||
|
||
"github.com/spf13/cobra" | ||
"oras.land/oras-go/v2/registry" | ||
) | ||
|
||
type verifyOpts struct { | ||
RemoteFlagOpts | ||
signatures []string | ||
certs []string | ||
certFiles []string | ||
pull bool | ||
reference string | ||
SecureFlagOpts | ||
reference string | ||
pluginConfig string | ||
} | ||
|
||
func verifyCommand(opts *verifyOpts) *cobra.Command { | ||
if opts == nil { | ||
opts = &verifyOpts{} | ||
} | ||
command := &cobra.Command{ | ||
Use: "verify [reference]", | ||
Short: "Verify OCI artifacts", | ||
Long: `Verify OCI artifacts | ||
Use: "verify [flags] <reference>", | ||
Short: "Verifies OCI Artifacts", | ||
Long: `Verifies OCI Artifacts | ||
|
||
Prerequisite: a trusted certificate needs to be generated or added using the command "notation cert". | ||
|
||
Example - Verify a signature using the trusted certificate: | ||
notation verify <registry>/<repository>:<tag> | ||
|
||
Example - Verify a signature associated with an OCI artifact identified by the digest: | ||
notation verify <registry>/<repository>@<digest> | ||
|
||
Example - Verify a signature using a trusted certificate in a specified path: | ||
notation verify --cert-file <cert_path> <registry>/<repository>:<tag> | ||
`, | ||
notation verify [--plugin-config <key>=<value>,...] [--username <username>] [--password <password>] <reference>`, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return errors.New("missing reference") | ||
|
@@ -59,119 +43,75 @@ Example - Verify a signature using a trusted certificate in a specified path: | |
return runVerify(cmd, opts) | ||
}, | ||
} | ||
setFlagSignature(command.Flags(), &opts.signatures) | ||
command.Flags().StringSliceVarP(&opts.certs, "cert", "c", []string{}, "certificate names for verification") | ||
command.Flags().StringSliceVar(&opts.certFiles, cmd.PflagCertFile.Name, []string{}, "certificate files for verification") | ||
command.Flags().BoolVar(&opts.pull, "pull", true, "pull remote signatures before verification") | ||
opts.ApplyFlags(command.Flags()) | ||
command.Flags().StringVarP(&opts.pluginConfig, "plugin-config", "c", "", "list of comma-separated {key}={value} pairs that are passed as is to the plugin") | ||
return command | ||
} | ||
|
||
func runVerify(command *cobra.Command, opts *verifyOpts) error { | ||
// initialize | ||
verifier, err := getVerifier(opts) | ||
// resolve the given reference and set the digest. | ||
ref, err := resolveReference(command, opts) | ||
if err != nil { | ||
return err | ||
} | ||
manifestDesc, err := getManifestDescriptorFromContext(command.Context(), &opts.RemoteFlagOpts, opts.reference) | ||
|
||
// initialize verifier. | ||
verifier, err := getVerifier(opts, ref) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sigPaths := opts.signatures | ||
if len(sigPaths) == 0 { | ||
if !opts.Local && opts.pull { | ||
if err := pullSignatures(command, opts.reference, &opts.SecureFlagOpts, digest.Digest(manifestDesc.Digest)); err != nil { | ||
return err | ||
} | ||
} | ||
manifestDigest := digest.Digest(manifestDesc.Digest) | ||
sigDigests, err := cache.SignatureDigests(manifestDigest) | ||
if err != nil { | ||
return err | ||
} | ||
for _, sigDigest := range sigDigests { | ||
sigPaths = append(sigPaths, dir.Path.CachedSignature(manifestDigest, sigDigest)) | ||
} | ||
} | ||
|
||
// core process | ||
if err := verifySignatures(command.Context(), verifier, manifestDesc, sigPaths); err != nil { | ||
// set up verification plugin config. | ||
configs, err := cmd.ParseFlagPluginConfig(opts.pluginConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// write out | ||
fmt.Println(manifestDesc.Digest) | ||
return nil | ||
// core verify process. | ||
ctx := verification.WithPluginConfig(command.Context(), configs) | ||
outcomes, err := verifier.Verify(ctx, ref.String()) | ||
|
||
// write out. | ||
return ioutil.PrintVerificationResults(os.Stdout, outcomes, err, ref.Reference) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should return the error from |
||
} | ||
|
||
func verifySignatures(ctx context.Context, verifier notation.Verifier, manifestDesc notation.Descriptor, sigPaths []string) error { | ||
if len(sigPaths) == 0 { | ||
return errors.New("verification failure: no signatures found") | ||
func getVerifier(opts *verifyOpts, ref registry.Reference) (*verification.Verifier, error) { | ||
authClient, plainHTTP, err := getAuthClient(&opts.SecureFlagOpts, ref) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var lastErr error | ||
for _, path := range sigPaths { | ||
sig, err := os.ReadFile(path) | ||
if err != nil { | ||
lastErr = fmt.Errorf("verification failure: %v", err) | ||
continue | ||
} | ||
// pass in nonempty annotations if needed | ||
sigMediaType, err := envelope.SpeculateSignatureEnvelopeFormat(sig) | ||
if err != nil { | ||
lastErr = fmt.Errorf("verification failure: %v", err) | ||
continue | ||
} | ||
opts := notation.VerifyOptions{ | ||
SignatureMediaType: sigMediaType, | ||
} | ||
desc, err := verifier.Verify(ctx, sig, opts) | ||
if err != nil { | ||
lastErr = fmt.Errorf("verification failure: %v", err) | ||
continue | ||
} | ||
|
||
if !desc.Equal(manifestDesc) { | ||
lastErr = fmt.Errorf("verification failure: %s", manifestDesc.Digest) | ||
continue | ||
} | ||
return nil | ||
} | ||
return lastErr | ||
repo := notationregistry.NewRepositoryClient(authClient, ref, plainHTTP) | ||
|
||
return verification.NewVerifier(repo) | ||
} | ||
|
||
func getVerifier(opts *verifyOpts) (notation.Verifier, error) { | ||
certPaths, err := appendCertPathFromName(opts.certFiles, opts.certs) | ||
func resolveReference(command *cobra.Command, opts *verifyOpts) (registry.Reference, error) { | ||
ref, err := registry.ParseReference(opts.reference) | ||
if err != nil { | ||
return nil, err | ||
return registry.Reference{}, err | ||
} | ||
if len(certPaths) == 0 { | ||
cfg, err := configutil.LoadConfigOnce() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(cfg.VerificationCertificates.Certificates) == 0 { | ||
return nil, errors.New("trust certificate not specified") | ||
} | ||
for _, ref := range cfg.VerificationCertificates.Certificates { | ||
certPaths = append(certPaths, ref.Path) | ||
} | ||
|
||
if isDigestReference(opts.reference) { | ||
return ref, nil | ||
} | ||
return signature.NewVerifierFromFiles(certPaths) | ||
|
||
// Resolve tag reference to digest reference. | ||
manifestDesc, err := getManifestDescriptorFromReference(command.Context(), &opts.SecureFlagOpts, opts.reference) | ||
if err != nil { | ||
return registry.Reference{}, err | ||
} | ||
|
||
ref.Reference = manifestDesc.Digest.String() | ||
return ref, nil | ||
} | ||
|
||
func appendCertPathFromName(paths, names []string) ([]string, error) { | ||
for _, name := range names { | ||
cfg, err := configutil.LoadConfigOnce() | ||
if err != nil { | ||
return nil, err | ||
} | ||
idx := slices.Index(cfg.VerificationCertificates.Certificates, name) | ||
if idx < 0 { | ||
return nil, errors.New("verification certificate not found: " + name) | ||
} | ||
paths = append(paths, cfg.VerificationCertificates.Certificates[idx].Path) | ||
func isDigestReference(reference string) bool { | ||
parts := strings.SplitN(reference, "/", 2) | ||
if len(parts) == 1 { | ||
return false | ||
} | ||
return paths, nil | ||
|
||
index := strings.Index(parts[1], "@") | ||
return index != -1 | ||
} |
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
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
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.
Do we need to delete it?
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'll keep the
Prerequisite
comment.