| 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 |
"fmt" |
| 19 |
"os" |
| 20 |
|
| 21 |
_ "github.com/notaryproject/notation-core-go/signature/cose" |
| 22 |
_ "github.com/notaryproject/notation-core-go/signature/jws" |
| 23 |
"github.com/notaryproject/notation-go" |
| 24 |
"github.com/notaryproject/notation-go/dir" |
| 25 |
"github.com/notaryproject/notation-go/registry" |
| 26 |
"github.com/notaryproject/notation-go/verifier" |
| 27 |
"github.com/notaryproject/notation-go/verifier/trustpolicy" |
| 28 |
"github.com/notaryproject/notation-go/verifier/truststore" |
| 29 |
"oras.land/oras-go/v2/registry/remote" |
| 30 |
) |
| 31 |
|
| 32 |
// ExampleRemoteVerify demonstrates how to use notation.Verify to verify |
| 33 |
// signatures of an artifact in the remote registry. |
| 34 |
func Example_remoteVerify() { |
| 35 |
// exampleArtifactReference is an example of the target artifact reference |
| 36 |
exampleArtifactReference := "localhost:5000/software@sha256:60043cf45eaebc4c0867fea485a039b598f52fd09fd5b07b0b2d2f88fad9d74e" |
| 37 |
|
| 38 |
// examplePolicyDocument is an example of a valid trust policy document. |
| 39 |
// trust policy document should follow this spec: |
| 40 |
// https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/trust-store-trust-policy.md#trust-policy |
| 41 |
examplePolicyDocument := trustpolicy.Document{ |
| 42 |
Version: "1.0", |
| 43 |
TrustPolicies: []trustpolicy.TrustPolicy{ |
| 44 |
{ |
| 45 |
Name: "test-statement-name", |
| 46 |
RegistryScopes: []string{"*"}, |
| 47 |
SignatureVerification: trustpolicy.SignatureVerification{VerificationLevel: trustpolicy.LevelStrict.Name}, |
| 48 |
TrustStores: []string{"ca:valid-trust-store"}, |
| 49 |
TrustedIdentities: []string{"*"}, |
| 50 |
}, |
| 51 |
}, |
| 52 |
} |
| 53 |
|
| 54 |
// generateTrustStore generates a trust store directory for demo purpose. |
| 55 |
// Users should configure their own trust store and add trusted certificates |
| 56 |
// into it following the trust store spec: |
| 57 |
// https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/trust-store-trust-policy.md#trust-store |
| 58 |
if err := generateTrustStore(); err != nil { |
| 59 |
panic(err) // Handle error |
| 60 |
} |
| 61 |
|
| 62 |
// exampleVerifier is an example of notation.Verifier given |
| 63 |
// trust policy document and X509 trust store. |
| 64 |
exampleVerifier, err := verifier.New(&examplePolicyDocument, truststore.NewX509TrustStore(dir.ConfigFS()), nil) |
| 65 |
if err != nil { |
| 66 |
panic(err) // Handle error |
| 67 |
} |
| 68 |
|
| 69 |
// exampleRepo is an example of registry.Repository. |
| 70 |
remoteRepo, err := remote.NewRepository(exampleArtifactReference) |
| 71 |
if err != nil { |
| 72 |
panic(err) // Handle error |
| 73 |
} |
| 74 |
exampleRepo := registry.NewRepository(remoteRepo) |
| 75 |
|
| 76 |
// exampleVerifyOptions is an example of notation.VerifyOptions. |
| 77 |
exampleVerifyOptions := notation.VerifyOptions{ |
| 78 |
ArtifactReference: exampleArtifactReference, |
| 79 |
MaxSignatureAttempts: 50, |
| 80 |
} |
| 81 |
|
| 82 |
// remote verify core process |
| 83 |
// upon successful verification, the target manifest descriptor |
| 84 |
// and signature verification outcome are returned. |
| 85 |
targetDesc, _, err := notation.Verify(context.Background(), exampleVerifier, exampleRepo, exampleVerifyOptions) |
| 86 |
if err != nil { |
| 87 |
panic(err) // Handle error |
| 88 |
} |
| 89 |
|
| 90 |
fmt.Println("Successfully verified") |
| 91 |
fmt.Println("targetDesc MediaType:", targetDesc.MediaType) |
| 92 |
fmt.Println("targetDesc Digest:", targetDesc.Digest) |
| 93 |
fmt.Println("targetDesc Size:", targetDesc.Size) |
| 94 |
} |
| 95 |
|
| 96 |
func generateTrustStore() error { |
| 97 |
// changing the path of the trust store for demo purpose. |
| 98 |
// Users could keep the default value, i.e. os.UserConfigDir. |
| 99 |
dir.UserConfigDir = "tmp" |
| 100 |
|
| 101 |
// an example of a valid X509 self-signed certificate for demo purpose ONLY. |
| 102 |
// Users should replace `exampleX509Certificate` with their own trusted |
| 103 |
// certificate and add to the trust store, following the |
| 104 |
// Notary certificate requirements: |
| 105 |
// https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/signature-specification.md#certificate-requirements |
| 106 |
exampleX509Certificate := `-----BEGIN CERTIFICATE----- |
| 107 |
MIIDQDCCAiigAwIBAgIBUTANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzEL |
| 108 |
MAkGA1UECBMCV0ExEDAOBgNVBAcTB1NlYXR0bGUxDzANBgNVBAoTBk5vdGFyeTEP |
| 109 |
MA0GA1UEAxMGYWxwaW5lMCAXDTAwMDgyOTEzNTAwMFoYDzIxMjMwODI5MTM1MDAw |
| 110 |
WjBOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCV0ExEDAOBgNVBAcTB1NlYXR0bGUx |
| 111 |
DzANBgNVBAoTBk5vdGFyeTEPMA0GA1UEAxMGYWxwaW5lMIIBIjANBgkqhkiG9w0B |
| 112 |
AQEFAAOCAQ8AMIIBCgKCAQEAocg3qEsyNDDLfB8OHD4dhi+M1NPK1Asy5NX84c+g |
| 113 |
vacZuoPLTwmpOfm6nPt7GPPB9G7S6xxhFNbRxTYfYUjK+kaCj38XjBRf5lGewbSJ |
| 114 |
KVkxQ82/axU70ceSW3JpazrageN9JUTZ/Jfi4MfnipITwcmMoiij8eGrHskjyVyZ |
| 115 |
bJd0WMMKRDWVhLPUiPMVWt/4d7YtZItzacaQKtXmXgsTCTWpIols3gftNYjrQoMs |
| 116 |
UelUdD8vOAWN9J28/SyC+uSh/K1KfyUlbqufn4di8DEBxntP5wnXYbJL1jtjsUgE |
| 117 |
xAVjQxT1zI59X36m3t3YKqCQh1cud02L5onObY6zj57N6QIDAQABoycwJTAOBgNV |
| 118 |
HQ8BAf8EBAMCB4AwEwYDVR0lBAwwCgYIKwYBBQUHAwMwDQYJKoZIhvcNAQELBQAD |
| 119 |
ggEBAC8AjBLy7EsRpi6oguCdFSb6nRGjvF17N+b6mDb3sARnB8T1pxvzTT26ya+A |
| 120 |
yWR+jjodEwbMIS+13lV+9qT2LwqlbOUNY519Pa2GRRY72JjeowWI3iKkKaMzfZUB |
| 121 |
7lRTGXdEuZApLbTO/3JVcR9ffu00N1UaAP9YGElSt4JDJYA9M+d/Qto+HiIsE0Kj |
| 122 |
+jdnwIYovPPOlryKOLfFb/r1GEq7n63xFZz83iyWNaZdsJ5N3YHxdOpkbBbCalOE |
| 123 |
BDJTjQKqeAYBLoANNU0OBslmqHCSBTEnhbqJHN6QKyF09ScOl5LwM1QsTl0UY5si |
| 124 |
GLAfj/jSf9OH9VLTPHOS8/N0Ka4= |
| 125 |
-----END CERTIFICATE-----` |
| 126 |
|
| 127 |
// Adding the certificate into the trust store. |
| 128 |
if err := os.MkdirAll("tmp/truststore/x509/ca/valid-trust-store", 0700); err != nil { |
| 129 |
return err |
| 130 |
} |
| 131 |
return os.WriteFile("tmp/truststore/x509/ca/valid-trust-store/NotationExample.pem", []byte(exampleX509Certificate), 0600) |
| 132 |
} |