Skip to content

Commit 39e9cd5

Browse files
committed
feature: add policy.json support
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
1 parent 983aba6 commit 39e9cd5

File tree

8 files changed

+2576
-0
lines changed

8 files changed

+2576
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 configuration
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 https://github.com/oras-project/oras-go/issues/1029
107+
return false, fmt.Errorf("signedBy verification not yet implemented")
108+
}
109+
110+
// evaluateSigstoreSigned evaluates a sigstoreSigned requirement
111+
// Note: This is a placeholder implementation. Full signature verification
112+
// would require integration with sigstore libraries.
113+
func (e *Evaluator) evaluateSigstoreSigned(ctx context.Context, req *PRSigstoreSigned, image ImageReference) (bool, error) {
114+
// TODO: Implement actual sigstore verification https://github.com/oras-project/oras-go/issues/1029
115+
return false, fmt.Errorf("sigstoreSigned verification not yet implemented")
116+
}
117+
118+
// ShouldAcceptImage is a convenience function that returns true if the image is allowed
119+
func ShouldAcceptImage(ctx context.Context, policy *Policy, image ImageReference) (bool, error) {
120+
evaluator, err := NewEvaluator(policy)
121+
if err != nil {
122+
return false, err
123+
}
124+
125+
return evaluator.IsImageAllowed(ctx, image)
126+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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 configuration_test
17+
18+
import (
19+
"context"
20+
"fmt"
21+
"log"
22+
"os"
23+
"path/filepath"
24+
25+
"oras.land/oras-go/v2/registry/remote/internal/configuration"
26+
)
27+
28+
// ExamplePolicy_basic demonstrates creating a basic policy
29+
func ExamplePolicy_basic() {
30+
// Create a policy that rejects everything by default
31+
p := &configuration.Policy{
32+
Default: configuration.PolicyRequirements{&configuration.Reject{}},
33+
}
34+
35+
// Add a transport-specific policy for docker that accepts anything
36+
p.Transports = map[configuration.TransportName]configuration.TransportScopes{
37+
configuration.TransportDocker: {
38+
"": configuration.PolicyRequirements{&configuration.InsecureAcceptAnything{}},
39+
},
40+
}
41+
42+
fmt.Println("Policy created with default reject and docker accept")
43+
// Output: Policy created with default reject and docker accept
44+
}
45+
46+
// ExamplePolicy_signedBy demonstrates creating a policy with signature verification
47+
func ExamplePolicy_signedBy() {
48+
p := &configuration.Policy{
49+
Default: configuration.PolicyRequirements{&configuration.Reject{}},
50+
Transports: map[configuration.TransportName]configuration.TransportScopes{
51+
configuration.TransportDocker: {
52+
"docker.io/myorg": configuration.PolicyRequirements{
53+
&configuration.PRSignedBy{
54+
KeyType: "GPGKeys",
55+
KeyPath: "/path/to/trusted-key.gpg",
56+
SignedIdentity: &configuration.SignedIdentity{
57+
Type: configuration.MatchRepository,
58+
},
59+
},
60+
},
61+
},
62+
},
63+
}
64+
_ = p
65+
66+
fmt.Println("Policy requires GPG signatures for docker.io/myorg")
67+
// Output: Policy requires GPG signatures for docker.io/myorg
68+
}
69+
70+
// ExampleLoadPolicy demonstrates loading a policy from a file
71+
func ExampleLoadPolicy() {
72+
// Create a temporary policy file
73+
tmpDir := os.TempDir()
74+
policyPath := filepath.Join(tmpDir, "example-policy.json")
75+
76+
// Create and save a policy
77+
p := &configuration.Policy{
78+
Default: configuration.PolicyRequirements{&configuration.Reject{}},
79+
Transports: map[configuration.TransportName]configuration.TransportScopes{
80+
configuration.TransportDocker: {
81+
"": configuration.PolicyRequirements{&configuration.InsecureAcceptAnything{}},
82+
},
83+
},
84+
}
85+
86+
if err := configuration.SavePolicy(p, policyPath); err != nil {
87+
log.Fatalf("Failed to save policy: %v", err)
88+
}
89+
defer os.Remove(policyPath)
90+
91+
// Load the policy
92+
loaded, err := configuration.LoadPolicy(policyPath)
93+
if err != nil {
94+
log.Fatalf("Failed to load policy: %v", err)
95+
}
96+
97+
fmt.Printf("Loaded policy with %d default requirements\n", len(loaded.Default))
98+
// Output: Loaded policy with 1 default requirements
99+
}
100+
101+
// ExampleEvaluator_IsImageAllowed demonstrates evaluating a policy
102+
func ExampleEvaluator_IsImageAllowed() {
103+
// Create a permissive policy for testing
104+
p := &configuration.Policy{
105+
Default: configuration.PolicyRequirements{&configuration.InsecureAcceptAnything{}},
106+
}
107+
108+
// Create an evaluator
109+
evaluator, err := configuration.NewEvaluator(p)
110+
if err != nil {
111+
log.Fatalf("Failed to create evaluator: %v", err)
112+
}
113+
114+
// Check if an image is allowed
115+
image := configuration.ImageReference{
116+
Transport: configuration.TransportDocker,
117+
Scope: "docker.io/library/nginx",
118+
Reference: "docker.io/library/nginx:latest",
119+
}
120+
121+
allowed, err := evaluator.IsImageAllowed(context.Background(), image)
122+
if err != nil {
123+
log.Fatalf("Failed to evaluate policy: %v", err)
124+
}
125+
126+
fmt.Printf("Image allowed: %v\n", allowed)
127+
// Output: Image allowed: true
128+
}
129+
130+
// ExamplePolicy_GetRequirementsForImage demonstrates getting requirements for a specific image
131+
func ExamplePolicy_GetRequirementsForImage() {
132+
p := &configuration.Policy{
133+
Default: configuration.PolicyRequirements{&configuration.Reject{}},
134+
Transports: map[configuration.TransportName]configuration.TransportScopes{
135+
configuration.TransportDocker: {
136+
"": configuration.PolicyRequirements{&configuration.InsecureAcceptAnything{}},
137+
"docker.io/library/nginx": configuration.PolicyRequirements{&configuration.Reject{}},
138+
},
139+
},
140+
}
141+
142+
// Get requirements for nginx specifically
143+
nginxReqs := p.GetRequirementsForImage(configuration.TransportDocker, "docker.io/library/nginx")
144+
fmt.Printf("Nginx requirements: %s\n", nginxReqs[0].Type())
145+
146+
// Get requirements for other docker images
147+
otherReqs := p.GetRequirementsForImage(configuration.TransportDocker, "docker.io/library/alpine")
148+
fmt.Printf("Other docker requirements: %s\n", otherReqs[0].Type())
149+
150+
// Output:
151+
// Nginx requirements: reject
152+
// Other docker requirements: insecureAcceptAnything
153+
}
154+
155+
// ExamplePolicy_sigstore demonstrates creating a sigstore-based policy
156+
func ExamplePolicy_sigstore() {
157+
p := &configuration.Policy{
158+
Default: configuration.PolicyRequirements{&configuration.Reject{}},
159+
Transports: map[configuration.TransportName]configuration.TransportScopes{
160+
configuration.TransportDocker: {
161+
"docker.io/myorg": configuration.PolicyRequirements{
162+
&configuration.PRSigstoreSigned{
163+
KeyPath: "/path/to/cosign.pub",
164+
Fulcio: &configuration.FulcioConfig{
165+
CAPath: "/path/to/fulcio-ca.pem",
166+
OIDCIssuer: "https://oauth2.sigstore.dev/auth",
167+
SubjectEmail: "user@example.com",
168+
},
169+
RekorPublicKeyPath: "/path/to/rekor.pub",
170+
SignedIdentity: &configuration.SignedIdentity{
171+
Type: configuration.MatchRepository,
172+
},
173+
},
174+
},
175+
},
176+
},
177+
}
178+
_ = p
179+
180+
fmt.Println("Policy requires sigstore signatures for docker.io/myorg")
181+
// Output: Policy requires sigstore signatures for docker.io/myorg
182+
}

0 commit comments

Comments
 (0)