|
| 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