Debian Codegraph @

example_localVerify_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 notation_test
15
16 import (
17 "context"
18 "encoding/pem"
19 "errors"
20 "fmt"
21 "os"
22
23 "github.com/notaryproject/notation-core-go/signature/cose"
24 "github.com/notaryproject/notation-go"
25 "github.com/notaryproject/notation-go/dir"
26 "github.com/notaryproject/notation-go/verifier"
27 "github.com/notaryproject/notation-go/verifier/trustpolicy"
28 "github.com/notaryproject/notation-go/verifier/truststore"
29 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
30 )
31
32 // examplePolicyDocument is an example of a valid trust policy document.
33 // trust policy document should follow this spec:
34 // https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/trust-store-trust-policy.md#trust-policy
35 var examplePolicyDocument = trustpolicy.Document{
36 Version: "1.0",
37 TrustPolicies: []trustpolicy.TrustPolicy{
38 {
39 Name: "test-statement-name",
40 RegistryScopes: []string{"example/software"},
41 SignatureVerification: trustpolicy.SignatureVerification{VerificationLevel: trustpolicy.LevelStrict.Name, Override: map[trustpolicy.ValidationType]trustpolicy.ValidationAction{trustpolicy.TypeRevocation: trustpolicy.ActionSkip}},
42 TrustStores: []string{"ca:valid-trust-store"},
43 TrustedIdentities: []string{"*"},
44 },
45 },
46 }
47
48 // ExampleLocalVerify demonstrates how to use verifier.Verify to verify a
49 // signature of the target artifact at local (without using a
50 // registry.Repository).
51 func Example_localVerify() {
52 // exampleArtifactReference is an example of the target artifact reference
53 exampleArtifactReference := "example/software@sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c"
54
55 // Both COSE ("application/cose") and JWS ("application/jose+json")
56 // signature mediaTypes are supported.
57 exampleSignatureMediaType := cose.MediaTypeEnvelope
58
59 // exampleTargetDescriptor is an example of the target manifest descriptor.
60 exampleTargetDescriptor := ocispec.Descriptor{
61 MediaType: "application/vnd.docker.distribution.manifest.v2+json",
62 Digest: "sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c",
63 Size: 528,
64 }
65
66 // exampleSignatureEnvelope is a valid signature envelope.
67 exampleSignatureEnvelope := generateExampleSignatureEnvelope()
68
69 // exampleVerifyOptions is an example of notation.VerifierVerifyOptions
70 exampleVerifyOptions := notation.VerifierVerifyOptions{
71 ArtifactReference: exampleArtifactReference,
72 SignatureMediaType: exampleSignatureMediaType,
73 }
74
75 // createTrustStore creates a trust store directory for demo purpose.
76 // Users could use the default trust store from Notary and add trusted
77 // certificates into it following the trust store spec:
78 // https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/trust-store-trust-policy.md#trust-store
79 if err := createTrustStore(); err != nil {
80 panic(err) // Handle error
81 }
82
83 // exampleVerifier is an example of notation.Verifier given
84 // trust policy document and X509 trust store.
85 exampleVerifier, err := verifier.New(&examplePolicyDocument, truststore.NewX509TrustStore(dir.ConfigFS()), nil)
86 if err != nil {
87 panic(err) // Handle error
88 }
89
90 // local verify core process
91 // upon successful verification, the signature verification outcome is
92 // returned.
93 outcome, err := exampleVerifier.Verify(context.Background(), exampleTargetDescriptor, exampleSignatureEnvelope, exampleVerifyOptions)
94 if err != nil {
95 panic(err) // Handle error
96 }
97
98 fmt.Println("Successfully verified")
99
100 // a peek of the payload inside the signature envelope
101 fmt.Println("payload ContentType:", outcome.EnvelopeContent.Payload.ContentType)
102
103 // Note, upon successful verification, payload.TargetArtifact from the
104 // signature envelope matches exactly with our exampleTargetDescriptor.
105 // (This check has been done for the user inside verifier.Verify.)
106 fmt.Println("payload Content:", string(outcome.EnvelopeContent.Payload.Content))
107
108 // Output:
109 // Successfully verified
110 // payload ContentType: application/vnd.cncf.notary.payload.v1+json
111 // payload Content: {"targetArtifact":{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","digest":"sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c","size":528}}
112 }
113
114 func generateExampleSignatureEnvelope() []byte {
115 // exampleSignatureEnvelopePem is a valid signature envelope in COSE format
116 // wrapped by a PEM block.
117 // The signature envelope is generated in a previous Sign process.
118 // Users should replace it with their own signature envelope.
119 // Regarding how to generate such signature envelopes, users could refer to
120 // `example_localSign_test.go`.
121 exampleSignatureEnvelopePem := `-----BEGIN EXAMPLE SIGNATURE ENVELOPE-----
122 0oRYnqUBOCQCgXgcaW8uY25jZi5ub3Rhcnkuc2lnbmluZ1NjaGVtZQN4K2FwcGxp
123 Y2F0aW9uL3ZuZC5jbmNmLm5vdGFyeS5wYXlsb2FkLnYxK2pzb254GmlvLmNuY2Yu
124 bm90YXJ5LnNpZ25pbmdUaW1lwRpju22GeBxpby5jbmNmLm5vdGFyeS5zaWduaW5n
125 U2NoZW1la25vdGFyeS54NTA5ohghgVkDRDCCA0AwggIooAMCAQICAVEwDQYJKoZI
126 hvcNAQELBQAwTjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdT
127 ZWF0dGxlMQ8wDQYDVQQKEwZOb3RhcnkxDzANBgNVBAMTBmFscGluZTAgFw0wMDA4
128 MjkxMzUwMDBaGA8yMTIzMDgyOTEzNTAwMFowTjELMAkGA1UEBhMCVVMxCzAJBgNV
129 BAgTAldBMRAwDgYDVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZOb3RhcnkxDzANBgNV
130 BAMTBmFscGluZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKHIN6hL
131 MjQwy3wfDhw+HYYvjNTTytQLMuTV/OHPoL2nGbqDy08JqTn5upz7exjzwfRu0usc
132 YRTW0cU2H2FIyvpGgo9/F4wUX+ZRnsG0iSlZMUPNv2sV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2Bq+tA9C7BXHoeCMcqnAHa8qs
149 ZR/fcMa9FrEPI6Pl8QVE/6QRMkT+Drn+9CSFxzHk6CU9S1vRsUVYNcibyejnuVEE
150 RPYaORrnfTc5wIs4XxeqprmrLimMMNn+u82Uadtc57tbHbY8Vh4XEKP++hBJJNvQ
151 E60X5aWKIS2RnOEc4n9T7LdN0bOL1OoM1lW4iTFMhzfcy/VmF8PrOStFS9LllX3J
152 69V0WwHbmD33cjtVBDCF44UXRWgLQGbE6yaaVmdxEUBGKqSUeHf8Gp7WoZ/YaFmz
153 xQr/
154 -----END EXAMPLE SIGNATURE ENVELOPE-----`
155
156 block, _ := pem.Decode([]byte(exampleSignatureEnvelopePem))
157 if block == nil {
158 panic(errors.New("invalid signature envelope pem block"))
159 }
160
161 // block.Bytes contains the binary of the signature envelope.
162 return block.Bytes
163 }
164
165 func createTrustStore() error {
166 // changing the path of the trust store for demo purpose.
167 // Users could keep the default value, i.e. os.UserConfigDir.
168 dir.UserConfigDir = "tmp"
169
170 // an example of a valid X509 self-signed certificate for demo purpose ONLY.
171 // (This self-signed cert is paired with the private key used to
172 // generate the `exampleSignatureEnvelopePem` above.)
173 // Users should replace `exampleX509Certificate` with their own trusted
174 // certificate and add to the trust store, following the
175 // Notary certificate requirements:
176 // https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/signature-specification.md#certificate-requirements
177 exampleX509Certificate := `-----BEGIN CERTIFICATE-----
178 MIIDQDCCAiigAwIBAgIBUTANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzEL
179 MAkGA1UECBMCV0ExEDAOBgNVBAcTB1NlYXR0bGUxDzANBgNVBAoTBk5vdGFyeTEP
180 MA0GA1UEAxMGYWxwaW5lMCAXDTAwMDgyOTEzNTAwMFoYDzIxMjMwODI5MTM1MDAw
181 WjBOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCV0ExEDAOBgNVBAcTB1NlYXR0bGUx
182 DzANBgNVBAoTBk5vdGFyeTEPMA0GA1UEAxMGYWxwaW5lMIIBIjANBgkqhkiG9w0B
183 AQEFAAOCAQ8AMIIBCgKCAQEAocg3qEsyNDDLfB8OHD4dhi+M1NPK1Asy5NX84c+g
184 vacZuoPLTwmpOfm6nPt7GPPB9G7S6xxhFNbRxTYfYUjK+kaCj38XjBRf5lGewbSJ
185 KVkxQ82/axU70ceSW3JpazrageN9JUTZ/Jfi4MfnipITwcmMoiij8eGrHskjyVyZ
186 bJd0WMMKRDWVhLPUiPMVWt/4d7YtZItzacaQKtXmXgsTCTWpIols3gftNYjrQoMs
187 UelUdD8vOAWN9J28/SyC+uSh/K1KfyUlbqufn4di8DEBxntP5wnXYbJL1jtjsUgE
188 xAVjQxT1zI59X36m3t3YKqCQh1cud02L5onObY6zj57N6QIDAQABoycwJTAOBgNV
189 HQ8BAf8EBAMCB4AwEwYDVR0lBAwwCgYIKwYBBQUHAwMwDQYJKoZIhvcNAQELBQAD
190 ggEBAC8AjBLy7EsRpi6oguCdFSb6nRGjvF17N+b6mDb3sARnB8T1pxvzTT26ya+A
191 yWR+jjodEwbMIS+13lV+9qT2LwqlbOUNY519Pa2GRRY72JjeowWI3iKkKaMzfZUB
192 7lRTGXdEuZApLbTO/3JVcR9ffu00N1UaAP9YGElSt4JDJYA9M+d/Qto+HiIsE0Kj
193 +jdnwIYovPPOlryKOLfFb/r1GEq7n63xFZz83iyWNaZdsJ5N3YHxdOpkbBbCalOE
194 BDJTjQKqeAYBLoANNU0OBslmqHCSBTEnhbqJHN6QKyF09ScOl5LwM1QsTl0UY5si
195 GLAfj/jSf9OH9VLTPHOS8/N0Ka4=
196 -----END CERTIFICATE-----`
197
198 // Adding the certificate into the trust store.
199 if err := os.MkdirAll("tmp/truststore/x509/ca/valid-trust-store", 0700); err != nil {
200 return err
201 }
202 return os.WriteFile("tmp/truststore/x509/ca/valid-trust-store/NotationLocalExample.pem", []byte(exampleX509Certificate), 0600)
203 }