| 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 |
"github.com/notaryproject/notation-core-go/signature" |
| 22 |
"github.com/notaryproject/notation-core-go/signature/cose" |
| 23 |
"github.com/notaryproject/notation-core-go/testhelper" |
| 24 |
"github.com/notaryproject/notation-go" |
| 25 |
"github.com/notaryproject/notation-go/signer" |
| 26 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 27 |
) |
| 28 |
|
| 29 |
var ( |
| 30 |
// exampleDesc is an example of the target manifest descriptor. |
| 31 |
exampleDesc = ocispec.Descriptor{ |
| 32 |
MediaType: "application/vnd.docker.distribution.manifest.v2+json", |
| 33 |
Digest: "sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c", |
| 34 |
Size: 528, |
| 35 |
} |
| 36 |
|
| 37 |
// exampleCertTuple contains a RSA privateKey and a self-signed X509 |
| 38 |
// certificate generated for demo purpose ONLY. |
| 39 |
exampleCertTuple = testhelper.GetRSASelfSignedSigningCertTuple("Notation Example self-signed") |
| 40 |
exampleCerts = []*x509.Certificate{exampleCertTuple.Cert} |
| 41 |
) |
| 42 |
|
| 43 |
// ExampleLocalSign demonstrates how to use signer.Sign to sign an artifact |
| 44 |
// at local (without using a registry.Repository). |
| 45 |
func Example_localSign() { |
| 46 |
// exampleSigner is a notation.Signer given key and X509 certificate chain. |
| 47 |
// Users should replace `exampleCertTuple.PrivateKey` with their own private |
| 48 |
// key and replace `exampleCerts` with the corresponding full certificate |
| 49 |
// chain, following the Notary certificate requirements: |
| 50 |
// https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/signature-specification.md#certificate-requirements |
| 51 |
exampleSigner, err := signer.NewGenericSigner(exampleCertTuple.PrivateKey, exampleCerts) |
| 52 |
if err != nil { |
| 53 |
panic(err) // Handle error |
| 54 |
} |
| 55 |
|
| 56 |
// Both COSE ("application/cose") and JWS ("application/jose+json") |
| 57 |
// signature mediaTypes are supported. |
| 58 |
exampleSignatureMediaType := cose.MediaTypeEnvelope |
| 59 |
|
| 60 |
// exampleSignOptions is an example of notation.SignerSignOptions. |
| 61 |
exampleSignOptions := notation.SignerSignOptions{ |
| 62 |
SignatureMediaType: exampleSignatureMediaType, |
| 63 |
SigningAgent: "example signing agent", |
| 64 |
} |
| 65 |
|
| 66 |
// local sign core process |
| 67 |
// upon successful signing, signature envelope and signerInfo are returned. |
| 68 |
// signatureEnvelope can be used in a verification process later on. |
| 69 |
signatureEnvelope, signerInfo, err := exampleSigner.Sign(context.Background(), exampleDesc, exampleSignOptions) |
| 70 |
if err != nil { |
| 71 |
panic(err) // Handle error |
| 72 |
} |
| 73 |
|
| 74 |
fmt.Println("Successfully signed") |
| 75 |
|
| 76 |
// a peek of the signature envelope generated from Sign |
| 77 |
sigBlob, err := signature.ParseEnvelope(exampleSignatureMediaType, signatureEnvelope) |
| 78 |
if err != nil { |
| 79 |
panic(err) // Handle error |
| 80 |
} |
| 81 |
sigContent, err := sigBlob.Content() |
| 82 |
if err != nil { |
| 83 |
panic(err) // Handle error |
| 84 |
} |
| 85 |
fmt.Println("signature Payload ContentType:", sigContent.Payload.ContentType) |
| 86 |
fmt.Println("signature Payload Content:", string(sigContent.Payload.Content)) |
| 87 |
fmt.Println("signerInfo SigningAgent:", signerInfo.UnsignedAttributes.SigningAgent) |
| 88 |
|
| 89 |
// Output: |
| 90 |
// Successfully signed |
| 91 |
// signature Payload ContentType: application/vnd.cncf.notary.payload.v1+json |
| 92 |
// signature Payload Content: {"targetArtifact":{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","digest":"sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c","size":528}} |
| 93 |
// signerInfo SigningAgent: example signing agent |
| 94 |
} |