Debian Codegraph @

example_signWithTimestmap_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 "crypto/x509"
19 "encoding/pem"
20 "fmt"
21
22 "oras.land/oras-go/v2/registry/remote"
23
24 "github.com/notaryproject/notation-core-go/revocation"
25 "github.com/notaryproject/notation-core-go/revocation/purpose"
26 "github.com/notaryproject/notation-core-go/testhelper"
27 "github.com/notaryproject/notation-go"
28 "github.com/notaryproject/notation-go/registry"
29 "github.com/notaryproject/notation-go/signer"
30 "github.com/notaryproject/tspclient-go"
31 )
32
33 // Example_signWithTimestamp demonstrates how to use notation.Sign to sign an
34 // artifact with a RFC 3161 compliant timestamp countersignature and
35 // user trusted TSA root certificate
36 func Example_signWithTimestamp() {
37 // exampleArtifactReference is an example of the target artifact reference
38 var exampleArtifactReference = "localhost:5000/software@sha256:60043cf45eaebc4c0867fea485a039b598f52fd09fd5b07b0b2d2f88fad9d74e"
39
40 // exampleCertTuple contains a RSA privateKey and a self-signed X509
41 // certificate generated for demo purpose ONLY.
42 exampleCertTuple := testhelper.GetRSASelfSignedSigningCertTuple("Notation Example self-signed")
43 exampleCerts := []*x509.Certificate{exampleCertTuple.Cert}
44
45 // exampleSigner is a notation.Signer given key and X509 certificate chain.
46 // Users should replace `exampleCertTuple.PrivateKey` with their own private
47 // key and replace `exampleCerts` with the corresponding full certificate
48 // chain, following the Notary certificate requirements:
49 // https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/signature-specification.md#certificate-requirements
50 exampleSigner, err := signer.NewGenericSigner(exampleCertTuple.PrivateKey, exampleCerts)
51 if err != nil {
52 panic(err) // Handle error
53 }
54
55 // exampleRepo is an example of registry.Repository.
56 remoteRepo, err := remote.NewRepository(exampleArtifactReference)
57 if err != nil {
58 panic(err) // Handle error
59 }
60 exampleRepo := registry.NewRepository(remoteRepo)
61
62 // replace exampleRFC3161TSAServer with your trusted TSA server URL.
63 exampleRFC3161TSAServer := "<TSA server URL>"
64 httpTimestamper, err := tspclient.NewHTTPTimestamper(nil, exampleRFC3161TSAServer)
65 if err != nil {
66 panic(err) // Handle error
67 }
68
69 // replace exampleTSARootCertPem with your trusted TSA root cert.
70 exampleTSARootCertPem := "<TSA root cert>"
71 block, _ := pem.Decode([]byte(exampleTSARootCertPem))
72 if block == nil {
73 panic("failed to parse tsa root certificate PEM")
74 }
75 tsaRootCert, err := x509.ParseCertificate(block.Bytes)
76 if err != nil {
77 panic("failed to parse tsa root certificate: " + err.Error())
78 }
79 tsaRootCAs := x509.NewCertPool()
80 tsaRootCAs.AddCert(tsaRootCert)
81
82 // enable timestamping certificate chain revocation check
83 tsaRevocationValidator, err := revocation.NewWithOptions(revocation.Options{
84 CertChainPurpose: purpose.Timestamping,
85 })
86 if err != nil {
87 panic(err) // Handle error
88 }
89
90 // exampleSignOptions is an example of notation.SignOptions.
91 exampleSignOptions := notation.SignOptions{
92 SignerSignOptions: notation.SignerSignOptions{
93 SignatureMediaType: exampleSignatureMediaType,
94 Timestamper: httpTimestamper,
95 TSARootCAs: tsaRootCAs,
96 TSARevocationValidator: tsaRevocationValidator,
97 },
98 ArtifactReference: exampleArtifactReference,
99 }
100
101 targetDesc, err := notation.Sign(context.Background(), exampleSigner, exampleRepo, exampleSignOptions)
102 if err != nil {
103 panic(err) // Handle error
104 }
105
106 fmt.Println("Successfully signed")
107 fmt.Println("targetDesc MediaType:", targetDesc.MediaType)
108 fmt.Println("targetDesc Digest:", targetDesc.Digest)
109 fmt.Println("targetDesc Size:", targetDesc.Size)
110 }