-
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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") | ||||||
| } | ||||||
|
|
||||||
| 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 means reject by default for safety | ||||||
|
||||||
| // No requirements means reject by default for safety | |
| // No requirements: treat as a policy error and reject by default for safety. |
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.
nit:
fmt.Errorf("policy cannot be nil")— should this be a sentinel error? Inoras-goconvention, consider returning an existingerrdeferror or at least using%wfor wrappable errors.