|
| 1 | +/* |
| 2 | +Copyright The ORAS Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package policy |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | +) |
| 22 | + |
| 23 | +// ImageReference represents a reference to an image |
| 24 | +type ImageReference struct { |
| 25 | + // Transport is the transport type (e.g., "docker") |
| 26 | + Transport TransportName |
| 27 | + // Scope is the scope within the transport (e.g., "docker.io/library/nginx") |
| 28 | + Scope string |
| 29 | + // Reference is the full reference (e.g., "docker.io/library/nginx:latest") |
| 30 | + Reference string |
| 31 | +} |
| 32 | + |
| 33 | +// Evaluator evaluates policy requirements against image references |
| 34 | +type Evaluator struct { |
| 35 | + policy *Policy |
| 36 | +} |
| 37 | + |
| 38 | +// NewEvaluator creates a new policy evaluator |
| 39 | +func NewEvaluator(policy *Policy) (*Evaluator, error) { |
| 40 | + if policy == nil { |
| 41 | + return nil, fmt.Errorf("policy cannot be nil") |
| 42 | + } |
| 43 | + |
| 44 | + if err := policy.Validate(); err != nil { |
| 45 | + return nil, fmt.Errorf("invalid policy: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + return &Evaluator{ |
| 49 | + policy: policy, |
| 50 | + }, nil |
| 51 | +} |
| 52 | + |
| 53 | +// IsImageAllowed determines if an image is allowed by the policy |
| 54 | +func (e *Evaluator) IsImageAllowed(ctx context.Context, image ImageReference) (bool, error) { |
| 55 | + reqs := e.policy.GetRequirementsForImage(image.Transport, image.Scope) |
| 56 | + |
| 57 | + if len(reqs) == 0 { |
| 58 | + // No requirements means reject by default for safety |
| 59 | + return false, fmt.Errorf("no policy requirements found for %s:%s", image.Transport, image.Scope) |
| 60 | + } |
| 61 | + |
| 62 | + // All requirements must be satisfied |
| 63 | + for _, req := range reqs { |
| 64 | + allowed, err := e.evaluateRequirement(ctx, req, image) |
| 65 | + if err != nil { |
| 66 | + return false, fmt.Errorf("failed to evaluate requirement %s: %w", req.Type(), err) |
| 67 | + } |
| 68 | + if !allowed { |
| 69 | + return false, nil |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return true, nil |
| 74 | +} |
| 75 | + |
| 76 | +// evaluateRequirement evaluates a single policy requirement |
| 77 | +func (e *Evaluator) evaluateRequirement(ctx context.Context, req PolicyRequirement, image ImageReference) (bool, error) { |
| 78 | + switch r := req.(type) { |
| 79 | + case *InsecureAcceptAnything: |
| 80 | + return e.evaluateInsecureAcceptAnything(ctx, r, image) |
| 81 | + case *Reject: |
| 82 | + return e.evaluateReject(ctx, r, image) |
| 83 | + case *PRSignedBy: |
| 84 | + return e.evaluateSignedBy(ctx, r, image) |
| 85 | + case *PRSigstoreSigned: |
| 86 | + return e.evaluateSigstoreSigned(ctx, r, image) |
| 87 | + default: |
| 88 | + return false, fmt.Errorf("unknown requirement type: %T", req) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// evaluateInsecureAcceptAnything always accepts the image |
| 93 | +func (e *Evaluator) evaluateInsecureAcceptAnything(ctx context.Context, req *InsecureAcceptAnything, image ImageReference) (bool, error) { |
| 94 | + return true, nil |
| 95 | +} |
| 96 | + |
| 97 | +// evaluateReject always rejects the image |
| 98 | +func (e *Evaluator) evaluateReject(ctx context.Context, req *Reject, image ImageReference) (bool, error) { |
| 99 | + return false, nil |
| 100 | +} |
| 101 | + |
| 102 | +// evaluateSignedBy evaluates a signedBy requirement |
| 103 | +// Note: This is a placeholder implementation. Full signature verification |
| 104 | +// would require integration with GPG/signing libraries. |
| 105 | +func (e *Evaluator) evaluateSignedBy(ctx context.Context, req *PRSignedBy, image ImageReference) (bool, error) { |
| 106 | + // TODO: Implement actual signature verification |
| 107 | + // This would involve: |
| 108 | + // 1. Fetching the image manifest and signatures |
| 109 | + // 2. Verifying signatures using the provided GPG keys |
| 110 | + // 3. Checking identity matching rules |
| 111 | + return false, fmt.Errorf("signedBy verification not yet implemented") |
| 112 | +} |
| 113 | + |
| 114 | +// evaluateSigstoreSigned evaluates a sigstoreSigned requirement |
| 115 | +// Note: This is a placeholder implementation. Full signature verification |
| 116 | +// would require integration with sigstore libraries. |
| 117 | +func (e *Evaluator) evaluateSigstoreSigned(ctx context.Context, req *PRSigstoreSigned, image ImageReference) (bool, error) { |
| 118 | + // TODO: Implement actual sigstore verification |
| 119 | + // This would involve: |
| 120 | + // 1. Fetching the image manifest and sigstore signatures |
| 121 | + // 2. Verifying signatures using sigstore |
| 122 | + // 3. Optionally verifying Fulcio certificates and Rekor transparency log |
| 123 | + // 4. Checking identity matching rules |
| 124 | + return false, fmt.Errorf("sigstoreSigned verification not yet implemented") |
| 125 | +} |
| 126 | + |
| 127 | +// ShouldAcceptImage is a convenience function that returns true if the image is allowed |
| 128 | +func ShouldAcceptImage(ctx context.Context, policy *Policy, image ImageReference) (bool, error) { |
| 129 | + evaluator, err := NewEvaluator(policy) |
| 130 | + if err != nil { |
| 131 | + return false, err |
| 132 | + } |
| 133 | + |
| 134 | + return evaluator.IsImageAllowed(ctx, image) |
| 135 | +} |
0 commit comments