-
Notifications
You must be signed in to change notification settings - Fork 107
feat(remote): add support for policy.json allow/deny #1013
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
Open
TerryHowe
wants to merge
3
commits into
oras-project:main
Choose a base branch
from
TerryHowe:feature-policy-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,110
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,169 @@ | ||
| /* | ||
| Copyright The ORAS 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 policy | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/oras-project/oras-go/v3/errdef" | ||
| ) | ||
|
|
||
| // ImageReference represents a reference to an image | ||
| type ImageReference struct { | ||
| // Transport is the transport type (e.g., "docker") | ||
| Transport TransportName | ||
| // Scope is the scope within the transport (e.g., "docker.io/library/nginx") | ||
| Scope string | ||
| // Reference is the full reference (e.g., "docker.io/library/nginx:latest") | ||
| Reference string | ||
| } | ||
|
|
||
| // SignedByVerifier verifies GPG/simple signing signatures. | ||
| // Implementations should verify that the image is signed with a valid key | ||
| // as specified in the PRSignedBy requirement. | ||
| type SignedByVerifier interface { | ||
| Verify(ctx context.Context, req *PRSignedBy, image ImageReference) (bool, error) | ||
| } | ||
|
|
||
| // SigstoreVerifier verifies sigstore signatures. | ||
| // Implementations should verify that the image is signed with valid sigstore | ||
| // signatures as specified in the PRSigstoreSigned requirement. | ||
| type SigstoreVerifier interface { | ||
| Verify(ctx context.Context, req *PRSigstoreSigned, image ImageReference) (bool, error) | ||
| } | ||
|
|
||
| // Evaluator evaluates policy requirements against image references | ||
| type Evaluator struct { | ||
| policy *Policy | ||
| signedByVerifier SignedByVerifier | ||
| sigstoreVerifier SigstoreVerifier | ||
| } | ||
|
|
||
| // EvaluatorOption configures an Evaluator | ||
| type EvaluatorOption func(*Evaluator) | ||
|
|
||
| // WithSignedByVerifier sets the verifier for PRSignedBy requirements. | ||
| // If not set, evaluating PRSignedBy requirements will return ErrUnsupported. | ||
| func WithSignedByVerifier(v SignedByVerifier) EvaluatorOption { | ||
| return func(e *Evaluator) { | ||
| e.signedByVerifier = v | ||
| } | ||
| } | ||
|
|
||
| // WithSigstoreVerifier sets the verifier for PRSigstoreSigned requirements. | ||
| // If not set, evaluating PRSigstoreSigned requirements will return ErrUnsupported. | ||
| func WithSigstoreVerifier(v SigstoreVerifier) EvaluatorOption { | ||
| return func(e *Evaluator) { | ||
| e.sigstoreVerifier = v | ||
| } | ||
| } | ||
|
|
||
| // NewEvaluator creates a new policy evaluator | ||
| func NewEvaluator(policy *Policy, opts ...EvaluatorOption) (*Evaluator, error) { | ||
| if policy == nil { | ||
| return nil, fmt.Errorf("policy cannot be nil: %w", errdef.ErrMissingReference) | ||
| } | ||
|
|
||
| if err := policy.Validate(); err != nil { | ||
| return nil, fmt.Errorf("invalid policy: %w", err) | ||
| } | ||
|
|
||
| e := &Evaluator{ | ||
| policy: policy, | ||
| } | ||
|
|
||
| for _, opt := range opts { | ||
| opt(e) | ||
| } | ||
|
|
||
| return e, nil | ||
| } | ||
|
|
||
| // IsImageAllowed determines if an image is allowed by the policy | ||
| func (e *Evaluator) IsImageAllowed(ctx context.Context, image ImageReference) (bool, error) { | ||
| reqs := e.policy.GetRequirementsForImage(image.Transport, image.Scope) | ||
|
|
||
| if len(reqs) == 0 { | ||
| // No requirements: treat as a policy error and reject by default for safety. | ||
| return false, fmt.Errorf("no policy requirements found for %s:%s", image.Transport, image.Scope) | ||
| } | ||
|
|
||
| // All requirements must be satisfied | ||
| for _, req := range reqs { | ||
| allowed, err := e.evaluateRequirement(ctx, req, image) | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to evaluate requirement %s: %w", req.Type(), err) | ||
| } | ||
| if !allowed { | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| return true, nil | ||
| } | ||
|
|
||
| // evaluateRequirement evaluates a single policy requirement | ||
| func (e *Evaluator) evaluateRequirement(ctx context.Context, req PolicyRequirement, image ImageReference) (bool, error) { | ||
| switch r := req.(type) { | ||
| case *InsecureAcceptAnything: | ||
| return e.evaluateInsecureAcceptAnything(ctx, r, image) | ||
| case *Reject: | ||
| return e.evaluateReject(ctx, r, image) | ||
| case *PRSignedBy: | ||
| return e.evaluateSignedBy(ctx, r, image) | ||
| case *PRSigstoreSigned: | ||
| return e.evaluateSigstoreSigned(ctx, r, image) | ||
| default: | ||
| return false, fmt.Errorf("unknown requirement type: %T", req) | ||
| } | ||
| } | ||
|
|
||
| // evaluateInsecureAcceptAnything always accepts the image | ||
| func (e *Evaluator) evaluateInsecureAcceptAnything(ctx context.Context, req *InsecureAcceptAnything, image ImageReference) (bool, error) { | ||
| return true, nil | ||
| } | ||
|
|
||
| // evaluateReject always rejects the image | ||
| func (e *Evaluator) evaluateReject(ctx context.Context, req *Reject, image ImageReference) (bool, error) { | ||
| return false, nil | ||
| } | ||
|
|
||
| // evaluateSignedBy evaluates a signedBy requirement | ||
| func (e *Evaluator) evaluateSignedBy(ctx context.Context, req *PRSignedBy, image ImageReference) (bool, error) { | ||
| if e.signedByVerifier == nil { | ||
| return false, fmt.Errorf("signedBy verification requires a SignedByVerifier: %w", errdef.ErrUnsupported) | ||
| } | ||
| return e.signedByVerifier.Verify(ctx, req, image) | ||
| } | ||
|
|
||
| // evaluateSigstoreSigned evaluates a sigstoreSigned requirement | ||
| func (e *Evaluator) evaluateSigstoreSigned(ctx context.Context, req *PRSigstoreSigned, image ImageReference) (bool, error) { | ||
| if e.sigstoreVerifier == nil { | ||
| return false, fmt.Errorf("sigstoreSigned verification requires a SigstoreVerifier: %w", errdef.ErrUnsupported) | ||
| } | ||
| return e.sigstoreVerifier.Verify(ctx, req, image) | ||
| } | ||
|
|
||
| // ShouldAcceptImage is a convenience function that returns true if the image is allowed | ||
| func ShouldAcceptImage(ctx context.Context, policy *Policy, image ImageReference) (bool, error) { | ||
| evaluator, err := NewEvaluator(policy) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| return evaluator.IsImageAllowed(ctx, image) | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.