2024-05-02 13:42:35 -05:00
|
|
|
package attest_test
|
2024-05-02 13:35:57 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2024-09-02 16:17:50 +01:00
|
|
|
"github.com/docker/attest"
|
|
|
|
|
"github.com/docker/attest/attestation"
|
|
|
|
|
"github.com/docker/attest/oci"
|
|
|
|
|
"github.com/docker/attest/signerverifier"
|
2024-05-02 13:35:57 -05:00
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
|
|
|
"github.com/google/go-containerregistry/pkg/v1/empty"
|
|
|
|
|
"github.com/google/go-containerregistry/pkg/v1/mutate"
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-16 12:52:33 +01:00
|
|
|
func ExampleSignStatements_remote() {
|
2024-05-02 13:35:57 -05:00
|
|
|
// configure signerverifier
|
|
|
|
|
// local signer (unsafe for production)
|
|
|
|
|
signer, err := signerverifier.GenKeyPair()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
// example using AWS KMS signer
|
|
|
|
|
// aws_arn := "arn:aws:kms:us-west-2:123456789012:key/12345678-1234-1234-1234-123456789012"
|
|
|
|
|
// aws_region := "us-west-2"
|
|
|
|
|
// signer, err := signerverifier.GetAWSSigner(cmd.Context(), aws_arn, aws_region)
|
|
|
|
|
|
|
|
|
|
// configure signing options
|
2024-06-06 09:59:32 +01:00
|
|
|
opts := &attestation.SigningOptions{
|
2024-07-16 10:05:17 +01:00
|
|
|
SkipTL: true, // skip trust logging to a transparency log
|
2024-05-02 13:35:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load image index with unsigned attestation-manifests
|
|
|
|
|
ref := "docker/image-signer-verifier:latest"
|
2024-07-05 09:29:14 +01:00
|
|
|
attIdx, err := oci.IndexFromRemote(ref)
|
2024-05-02 13:35:57 -05:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
// example for local image index
|
|
|
|
|
// path := "/myimage"
|
2024-07-05 09:29:14 +01:00
|
|
|
// attIdx, err = oci.IndexFromPath(path)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
2024-05-02 13:35:57 -05:00
|
|
|
|
2024-07-05 09:29:14 +01:00
|
|
|
// sign all attestations in an image index
|
|
|
|
|
signedManifests, err := attest.SignStatements(context.Background(), attIdx.Index, signer, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
signedIndex := attIdx.Index
|
2024-07-16 10:05:17 +01:00
|
|
|
signedIndex, err = attestation.UpdateIndexImages(signedIndex, signedManifests)
|
2024-05-02 13:35:57 -05:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// push image index with signed attestation-manifests
|
2024-08-08 14:23:46 -05:00
|
|
|
err = oci.PushIndexToRegistry(signedIndex, ref)
|
2024-05-02 13:35:57 -05:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
// output image index to filesystem (optional)
|
|
|
|
|
path := "/myimage"
|
|
|
|
|
idx := v1.ImageIndex(empty.Index)
|
|
|
|
|
idx = mutate.AppendManifests(idx, mutate.IndexAddendum{
|
2024-07-05 09:29:14 +01:00
|
|
|
Add: signedIndex,
|
2024-05-02 13:35:57 -05:00
|
|
|
Descriptor: v1.Descriptor{
|
|
|
|
|
Annotations: map[string]string{
|
2024-08-01 15:35:15 +01:00
|
|
|
oci.OCIReferenceTarget: attIdx.Name,
|
2024-05-02 13:35:57 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
2024-08-08 14:23:46 -05:00
|
|
|
err = oci.SaveIndexAsOCILayout(idx, path)
|
2024-05-02 13:35:57 -05:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|