Debian Codegraph @

verifier/trustpolicy/oci_test.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package trustpolicy
15
16 import (
17 "encoding/json"
18 "fmt"
19 "os"
20 "path/filepath"
21 "testing"
22
23 "github.com/notaryproject/notation-go/dir"
24 )
25
26 func TestLoadDocumentFromFileLocation(t *testing.T) {
27 tempRoot := t.TempDir()
28 dir.UserConfigDir = tempRoot
29 path := filepath.Join(tempRoot, "trustpolicy.json")
30 policyJson, _ := json.Marshal(dummyPolicyDocument())
31 if err := os.WriteFile(path, policyJson, 0600); err != nil {
32 t.Fatalf("TestLoadDocument write policy file failed. Error: %v", err)
33 }
34 t.Cleanup(func() { os.RemoveAll(tempRoot) })
35
36 if _, err := LoadDocument(); err != nil {
37 t.Fatalf("LoadDocument() should not throw error for an existing policy file. Error: %v", err)
38 }
39 }
40
41 func TestLoadDocumentError(t *testing.T) {
42 tempRoot := t.TempDir()
43 dir.UserConfigDir = tempRoot
44 if _, err := LoadDocument(); err == nil {
45 t.Fatalf("LoadDocument() should throw error if trust policy is not found")
46 }
47 }
48
49 // TestApplicableTrustPolicy tests filtering policies against registry scopes
50 func TestApplicableTrustPolicy(t *testing.T) {
51 policyDoc := dummyPolicyDocument()
52
53 policyStatement := policyDoc.TrustPolicies[0]
54 policyStatement.Name = "test-statement-name-1"
55 registryScope := "registry.wabbit-networks.io/software/unsigned/net-utils"
56 registryUri := fmt.Sprintf("%s@sha256:hash", registryScope)
57 policyStatement.RegistryScopes = []string{registryScope}
58 policyStatement.SignatureVerification = SignatureVerification{VerificationLevel: "strict"}
59
60 policyDoc.TrustPolicies = []TrustPolicy{
61 policyStatement,
62 }
63 // existing Registry Scope
64 policy, err := (&policyDoc).GetApplicableTrustPolicy(registryUri)
65 if policy.Name != policyStatement.Name || err != nil {
66 t.Fatalf("GetApplicableTrustPolicy() should return %q for registry scope %q", policyStatement.Name, registryScope)
67 }
68
69 // non-existing Registry Scope
70 policy, err = (&policyDoc).GetApplicableTrustPolicy("non.existing.scope/repo@sha256:hash")
71 if policy != nil || err == nil || err.Error() != "artifact \"non.existing.scope/repo@sha256:hash\" has no applicable trust policy statement. Trust policy applicability for a given artifact is determined by registryScopes. To create a trust policy, see: https://notaryproject.dev/docs/quickstart/#create-a-trust-policy" {
72 t.Fatalf("GetApplicableTrustPolicy() should return nil for non existing registry scope")
73 }
74
75 // wildcard registry scope
76 wildcardStatement := TrustPolicy{
77 Name: "test-statement-name-2",
78 SignatureVerification: SignatureVerification{VerificationLevel: "skip"},
79 TrustStores: []string{},
80 TrustedIdentities: []string{},
81 RegistryScopes: []string{"*"},
82 }
83
84 policyDoc.TrustPolicies = []TrustPolicy{
85 policyStatement,
86 wildcardStatement,
87 }
88 policy, err = (&policyDoc).GetApplicableTrustPolicy("some.registry.that/has.no.policy@sha256:hash")
89 if policy.Name != wildcardStatement.Name || err != nil {
90 t.Fatalf("GetApplicableTrustPolicy() should return wildcard policy for registry scope \"some.registry.that/has.no.policy\"")
91 }
92 }
93
94 // TestValidatePolicyDocument calls policyDoc.Validate()
95 // and tests various validations on policy elements
96 func TestValidateInvalidPolicyDocument(t *testing.T) {
97 // Sanity check
98 var nilPolicyDoc *Document
99 err := nilPolicyDoc.Validate()
100 if err == nil || err.Error() != "trust policy document cannot be nil" {
101 t.Fatalf("nil policyDoc should return error")
102 }
103
104 // Invalid Version
105 policyDoc := dummyPolicyDocument()
106 policyDoc.Version = "invalid"
107 err = policyDoc.Validate()
108 if err == nil || err.Error() != "trust policy document uses unsupported version \"invalid\"" {
109 t.Fatalf("invalid version should return error")
110 }
111
112 // No Policy Statements
113 policyDoc = dummyPolicyDocument()
114 policyDoc.TrustPolicies = nil
115 err = policyDoc.Validate()
116 if err == nil || err.Error() != "trust policy document can not have zero trust policy statements" {
117 t.Fatalf("zero policy statements should return error")
118 }
119
120 // No Policy Statement Name
121 policyDoc = dummyPolicyDocument()
122 policyDoc.TrustPolicies[0].Name = ""
123 err = policyDoc.Validate()
124 if err == nil || err.Error() != "trust policy: a trust policy statement is missing a name, every statement requires a name" {
125 t.Fatalf("policy statement with no name should return an error")
126 }
127
128 // No Registry Scopes
129 policyDoc = dummyPolicyDocument()
130 policyDoc.TrustPolicies[0].RegistryScopes = nil
131 err = policyDoc.Validate()
132 if err == nil || err.Error() != "trust policy statement \"test-statement-name\" has zero registry scopes, it must specify registry scopes with at least one value" {
133 t.Fatalf("policy statement with registry scopes should return error")
134 }
135
136 // Multiple policy statements with same registry scope
137 policyDoc = dummyPolicyDocument()
138 policyStatement1 := policyDoc.TrustPolicies[0].clone()
139 policyStatement2 := policyDoc.TrustPolicies[0].clone()
140 policyStatement2.Name = "test-statement-name-2"
141 policyDoc.TrustPolicies = []TrustPolicy{*policyStatement1, *policyStatement2}
142 err = policyDoc.Validate()
143 if err == nil || err.Error() != "registry scope \"registry.acme-rockets.io/software/net-monitor\" is present in multiple trust policy statements, one registry scope value can only be associated with one statement" {
144 t.Fatalf("Policy statements with same registry scope should return error %q", err)
145 }
146
147 // Registry scopes with a wildcard
148 policyDoc = dummyPolicyDocument()
149 policyDoc.TrustPolicies[0].RegistryScopes = []string{"*", "registry.acme-rockets.io/software/net-monitor"}
150 err = policyDoc.Validate()
151 if err == nil || err.Error() != "trust policy statement \"test-statement-name\" uses wildcard registry scope '*', a wildcard scope cannot be used in conjunction with other scope values" {
152 t.Fatalf("policy statement with more than a wildcard registry scope should return error")
153 }
154
155 // Invalid SignatureVerification
156 policyDoc = dummyPolicyDocument()
157 policyDoc.TrustPolicies[0].SignatureVerification = SignatureVerification{VerificationLevel: "invalid"}
158 err = policyDoc.Validate()
159 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" has invalid signatureVerification: invalid signature verification level \"invalid\"" {
160 t.Fatalf("policy statement with invalid SignatureVerification should return error")
161 }
162
163 // Invalid SignatureVerification VerifyTimestamp
164 policyDoc = dummyPolicyDocument()
165 policyDoc.TrustPolicies[0].SignatureVerification.VerifyTimestamp = "invalid"
166 expectedErrMsg := "trust policy: trust policy statement \"test-statement-name\" has invalid signatureVerification: verifyTimestamp must be \"always\" or \"afterCertExpiry\", but got \"invalid\""
167 err = policyDoc.Validate()
168 if err == nil || err.Error() != expectedErrMsg {
169 t.Fatalf("expected %s, but got %s", expectedErrMsg, err)
170 }
171
172 // strict SignatureVerification should have a trust store
173 policyDoc = dummyPolicyDocument()
174 policyDoc.TrustPolicies[0].TrustStores = []string{}
175 err = policyDoc.Validate()
176 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" is either missing trust stores or trusted identities, both must be specified" {
177 t.Fatalf("strict SignatureVerification should have a trust store")
178 }
179
180 // strict SignatureVerification should have trusted identities
181 policyDoc = dummyPolicyDocument()
182 policyDoc.TrustPolicies[0].TrustedIdentities = []string{}
183 err = policyDoc.Validate()
184 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" is either missing trust stores or trusted identities, both must be specified" {
185 t.Fatalf("strict SignatureVerification should have trusted identities")
186 }
187
188 // skip SignatureVerification should not have trust store or trusted identities
189 policyDoc = dummyPolicyDocument()
190 policyDoc.TrustPolicies[0].SignatureVerification = SignatureVerification{VerificationLevel: "skip"}
191 err = policyDoc.Validate()
192 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" is set to skip signature verification but configured with trust stores and/or trusted identities, remove them if signature verification needs to be skipped" {
193 t.Fatalf("strict SignatureVerification should have trusted identities")
194 }
195
196 // Empty Trusted Identity should throw error
197 policyDoc = dummyPolicyDocument()
198 policyDoc.TrustPolicies[0].TrustedIdentities = []string{""}
199 err = policyDoc.Validate()
200 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" has an empty trusted identity" {
201 t.Fatalf("policy statement with empty trusted identity should return error")
202 }
203
204 // Trusted Identity without separator should throw error
205 policyDoc = dummyPolicyDocument()
206 policyDoc.TrustPolicies[0].TrustedIdentities = []string{"x509.subject"}
207 err = policyDoc.Validate()
208 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" has trusted identity \"x509.subject\" missing separator" {
209 t.Fatalf("policy statement with trusted identity missing separator should return error")
210 }
211
212 // Empty Trusted Identity value should throw error
213 policyDoc = dummyPolicyDocument()
214 policyDoc.TrustPolicies[0].TrustedIdentities = []string{"x509.subject:"}
215 err = policyDoc.Validate()
216 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" has trusted identity \"x509.subject:\" without an identity value" {
217 t.Fatalf("policy statement with trusted identity missing identity value should return error")
218 }
219
220 // trust store/trusted identities are optional for skip SignatureVerification
221 policyDoc = dummyPolicyDocument()
222 policyDoc.TrustPolicies[0].SignatureVerification = SignatureVerification{VerificationLevel: "skip"}
223 policyDoc.TrustPolicies[0].TrustStores = []string{}
224 policyDoc.TrustPolicies[0].TrustedIdentities = []string{}
225 err = policyDoc.Validate()
226 if err != nil {
227 t.Fatalf("skip SignatureVerification should not require a trust store or trusted identities")
228 }
229
230 // Trust Store missing separator
231 policyDoc = dummyPolicyDocument()
232 policyDoc.TrustPolicies[0].TrustStores = []string{"ca"}
233 err = policyDoc.Validate()
234 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" has malformed trust store value \"ca\". The required format is <TrustStoreType>:<TrustStoreName>" {
235 t.Fatalf("policy statement with trust store missing separator should return error")
236 }
237
238 // Invalid Trust Store type
239 policyDoc = dummyPolicyDocument()
240 policyDoc.TrustPolicies[0].TrustStores = []string{"invalid:test-trust-store"}
241 err = policyDoc.Validate()
242 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" uses an unsupported trust store type \"invalid\" in trust store value \"invalid:test-trust-store\"" {
243 t.Fatalf("policy statement with invalid trust store type should return error")
244 }
245
246 // Empty Named Store
247 policyDoc = dummyPolicyDocument()
248 policyDoc.TrustPolicies[0].TrustStores = []string{"ca:"}
249 err = policyDoc.Validate()
250 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" uses an unsupported trust store name \"\" in trust store value \"ca:\". Named store name needs to follow [a-zA-Z0-9_.-]+ format" {
251 t.Fatalf("policy statement with trust store missing named store should return error")
252 }
253
254 // trusted identities with a wildcard
255 policyDoc = dummyPolicyDocument()
256 policyDoc.TrustPolicies[0].TrustedIdentities = []string{"*", "test-identity"}
257 err = policyDoc.Validate()
258 if err == nil || err.Error() != "trust policy: trust policy statement \"test-statement-name\" uses a wildcard trusted identity '*', a wildcard identity cannot be used in conjunction with other values" {
259 t.Fatalf("policy statement with more than a wildcard trusted identity should return error")
260 }
261
262 // Policy Document with duplicate policy statement names
263 policyDoc = dummyPolicyDocument()
264 policyStatement1 = policyDoc.TrustPolicies[0].clone()
265 policyStatement2 = policyDoc.TrustPolicies[0].clone()
266 policyStatement2.RegistryScopes = []string{"registry.acme-rockets.io/software/legacy/metrics"}
267 policyDoc.TrustPolicies = []TrustPolicy{*policyStatement1, *policyStatement2}
268 err = policyDoc.Validate()
269 if err == nil || err.Error() != "multiple trust policy statements use the same name \"test-statement-name\", statement names must be unique" {
270 t.Fatalf("policy statements with same name should return error")
271 }
272 }
273
274 // TestValidRegistryScopes tests valid scopes are accepted
275 func TestValidRegistryScopes(t *testing.T) {
276 policyDoc := dummyPolicyDocument()
277 validScopes := []string{
278 "*", "example.com/rep", "example.com:8080/rep/rep2", "example.com/rep/subrep/subsub",
279 "10.10.10.10:8080/rep/rep2", "domain/rep", "domain:1234/rep",
280 }
281
282 for _, scope := range validScopes {
283 policyDoc.TrustPolicies[0].RegistryScopes = []string{scope}
284 err := policyDoc.Validate()
285 if err != nil {
286 t.Fatalf("valid registry scope should not return error. Error : %q", err)
287 }
288 }
289 }
290
291 // TestInvalidRegistryScopes tests invalid scopes are rejected
292 func TestInvalidRegistryScopes(t *testing.T) {
293 policyDoc := dummyPolicyDocument()
294 invalidScopes := []string{
295 "", "1:1", "a,b", "abcd", "1111", "1,2", "example.com/rep:tag",
296 "example.com/rep/subrep/sub:latest", "example.com", "rep/rep2:latest",
297 "repository", "10.10.10.10", "10.10.10.10:8080/rep/rep2:latest",
298 }
299
300 for _, scope := range invalidScopes {
301 policyDoc.TrustPolicies[0].RegistryScopes = []string{scope}
302 err := policyDoc.Validate()
303 if err == nil || err.Error() != "registry scope \""+scope+"\" is not valid, make sure it is a fully qualified repository without the scheme, protocol or tag. For example domain.com/my/repository or a local scope like local/myOCILayout" {
304 t.Fatalf("invalid registry scope should return error. Error : %q", err)
305 }
306 }
307
308 // Test invalid scope with wild card suffix
309 invalidWildCardScopes := []string{"example.com/*", "*/", "example*/", "ex*test"}
310 for _, scope := range invalidWildCardScopes {
311 policyDoc.TrustPolicies[0].RegistryScopes = []string{scope}
312 err := policyDoc.Validate()
313 if err == nil || err.Error() != "registry scope \""+scope+"\" with wild card(s) is not valid, make sure it is a fully qualified repository without the scheme, protocol or tag. For example domain.com/my/repository or a local scope like local/myOCILayout" {
314 t.Fatalf("invalid registry scope should return error. Error : %q", err)
315 }
316 }
317 }
318
319 // TestValidateValidPolicyDocument tests a happy policy document
320 func TestValidateValidPolicyDocument(t *testing.T) {
321 policyDoc := dummyPolicyDocument()
322
323 policyStatement1 := policyDoc.TrustPolicies[0].clone()
324
325 policyStatement2 := policyStatement1.clone()
326 policyStatement2.Name = "test-statement-name-2"
327 policyStatement2.RegistryScopes = []string{"registry.wabbit-networks.io/software/unsigned/net-utils"}
328 policyStatement2.SignatureVerification = SignatureVerification{VerificationLevel: "permissive"}
329
330 policyStatement3 := policyStatement1.clone()
331 policyStatement3.Name = "test-statement-name-3"
332 policyStatement3.RegistryScopes = []string{"registry.acme-rockets.io/software/legacy/metrics"}
333 policyStatement3.TrustStores = []string{}
334 policyStatement3.TrustedIdentities = []string{}
335 policyStatement3.SignatureVerification = SignatureVerification{VerificationLevel: "skip"}
336
337 policyStatement4 := policyStatement1.clone()
338 policyStatement4.Name = "test-statement-name-4"
339 policyStatement4.RegistryScopes = []string{"*"}
340 policyStatement4.TrustStores = []string{"ca:valid-trust-store", "signingAuthority:valid-trust-store-2"}
341 policyStatement4.SignatureVerification = SignatureVerification{VerificationLevel: "audit"}
342
343 policyStatement5 := policyStatement1.clone()
344 policyStatement5.Name = "test-statement-name-5"
345 policyStatement5.RegistryScopes = []string{"registry.acme-rockets2.io/software"}
346 policyStatement5.TrustedIdentities = []string{"*"}
347 policyStatement5.SignatureVerification = SignatureVerification{VerificationLevel: "strict"}
348
349 policyStatement6 := policyStatement1.clone()
350 policyStatement6.Name = "test-statement-name-6"
351 policyStatement6.RegistryScopes = []string{"registry.acme-rockets.io/software/net-monitor6"}
352 policyStatement6.SignatureVerification.VerifyTimestamp = ""
353
354 policyStatement7 := policyStatement1.clone()
355 policyStatement7.Name = "test-statement-name-7"
356 policyStatement7.RegistryScopes = []string{"registry.acme-rockets.io/software/net-monitor7"}
357 policyStatement7.SignatureVerification.VerifyTimestamp = OptionAlways
358
359 policyStatement8 := policyStatement1.clone()
360 policyStatement8.Name = "test-statement-name-8"
361 policyStatement8.RegistryScopes = []string{"registry.acme-rockets.io/software/net-monitor8"}
362 policyStatement8.SignatureVerification.VerifyTimestamp = OptionAfterCertExpiry
363
364 policyDoc.TrustPolicies = []TrustPolicy{
365 *policyStatement1,
366 *policyStatement2,
367 *policyStatement3,
368 *policyStatement4,
369 *policyStatement5,
370 *policyStatement6,
371 *policyStatement7,
372 *policyStatement8,
373 }
374
375 err := policyDoc.Validate()
376 if err != nil {
377 t.Fatalf("validation failed on a good policy document. Error : %q", err)
378 }
379 }