-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and add tests, implement a feature
What we didn't have in the `bash` or the `ec-cli` was the ability to have multiple sources of policy (Rego) files, e.g. from multiple git repositories or branches. Now this is available in the `ec-cli`. This does however refactor the implementation into smaller more manageable files, hopefully makes it easier to maintain. Tests were added. Ref. https://issues.redhat.com/browse/HACBS-372
- Loading branch information
Showing
17 changed files
with
1,004 additions
and
222 deletions.
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,86 @@ | ||
/* | ||
Copyright © 2022 Red Hat, Inc. | ||
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 image | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
"github.com/sigstore/cosign/cmd/cosign/cli/rekor" | ||
"github.com/sigstore/cosign/pkg/cosign" | ||
"github.com/sigstore/cosign/pkg/oci" | ||
"github.com/sigstore/cosign/pkg/signature" | ||
) | ||
|
||
type imageValidator struct { | ||
reference name.Reference | ||
checkOpts cosign.CheckOpts | ||
attestations []oci.Signature | ||
} | ||
|
||
// NewImageValidator constructs a new imageValidator with the provided parameters | ||
func NewImageValidator(ctx context.Context, image string, publicKey string, rekorURL string) (*imageValidator, error) { | ||
ref, err := name.ParseReference(image) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
verifier, err := signature.PublicKeyFromKeyRef(ctx, publicKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
checkOpts := cosign.CheckOpts{} | ||
checkOpts.SigVerifier = verifier | ||
|
||
if rekorURL != "" { | ||
rekorClient, err := rekor.NewClient(rekorURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
checkOpts.RekorClient = rekorClient | ||
} | ||
|
||
return &imageValidator{ | ||
reference: ref, | ||
checkOpts: checkOpts, | ||
}, nil | ||
} | ||
|
||
func (i *imageValidator) ValidateImageSignature(ctx context.Context) error { | ||
// TODO check what to do with _, _ | ||
_, _, err := cosign.VerifyImageSignatures(ctx, i.reference, &i.checkOpts) | ||
|
||
return err | ||
} | ||
|
||
func (i *imageValidator) ValidateAttestationSignature(ctx context.Context) error { | ||
// TODO check what to do with _ | ||
attestations, _, err := cosign.VerifyImageAttestations(ctx, i.reference, &i.checkOpts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
i.attestations = attestations | ||
|
||
return nil | ||
} | ||
|
||
func (i *imageValidator) Attestations() []oci.Signature { | ||
return i.attestations | ||
} |
Oops, something went wrong.