| 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 |
"fmt" |
| 20 |
|
| 21 |
"oras.land/oras-go/v2/registry/remote" |
| 22 |
|
| 23 |
"github.com/notaryproject/notation-core-go/signature/cose" |
| 24 |
"github.com/notaryproject/notation-core-go/testhelper" |
| 25 |
"github.com/notaryproject/notation-go" |
| 26 |
"github.com/notaryproject/notation-go/registry" |
| 27 |
"github.com/notaryproject/notation-go/signer" |
| 28 |
) |
| 29 |
|
| 30 |
// Both COSE ("application/cose") and JWS ("application/jose+json") |
| 31 |
// signature mediaTypes are supported. |
| 32 |
var exampleSignatureMediaType = cose.MediaTypeEnvelope |
| 33 |
|
| 34 |
// ExampleRemoteSign demonstrates how to use notation.Sign to sign an artifact |
| 35 |
// in the remote registry and push the signature to the remote. |
| 36 |
func Example_remoteSign() { |
| 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 |
// exampleSignOptions is an example of notation.SignOptions. |
| 63 |
exampleSignOptions := notation.SignOptions{ |
| 64 |
SignerSignOptions: notation.SignerSignOptions{ |
| 65 |
SignatureMediaType: exampleSignatureMediaType, |
| 66 |
}, |
| 67 |
ArtifactReference: exampleArtifactReference, |
| 68 |
} |
| 69 |
|
| 70 |
// remote sign core process |
| 71 |
// upon successful signing, descriptor of the sign content is returned and |
| 72 |
// the generated signature is pushed into remote registry. |
| 73 |
targetDesc, err := notation.Sign(context.Background(), exampleSigner, exampleRepo, exampleSignOptions) |
| 74 |
if err != nil { |
| 75 |
panic(err) // Handle error |
| 76 |
} |
| 77 |
|
| 78 |
fmt.Println("Successfully signed") |
| 79 |
fmt.Println("targetDesc MediaType:", targetDesc.MediaType) |
| 80 |
fmt.Println("targetDesc Digest:", targetDesc.Digest) |
| 81 |
fmt.Println("targetDesc Size:", targetDesc.Size) |
| 82 |
} |