Files
attest/example_verify_test.go

55 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-05-02 13:42:35 -05:00
package attest_test
2024-05-02 13:35:57 -05:00
import (
"context"
2024-05-08 13:12:40 +01:00
"fmt"
2024-05-02 13:35:57 -05:00
"os"
"path/filepath"
"github.com/docker/attest"
"github.com/docker/attest/oci"
"github.com/docker/attest/policy"
"github.com/docker/attest/tuf"
2024-05-02 13:35:57 -05:00
)
func ExampleVerify_remote() {
// create a tuf client
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
tufOutputPath := filepath.Join(home, ".docker", "tuf")
tufClientOpts := tuf.NewDockerDefaultClientOptions(tufOutputPath)
2024-05-02 13:35:57 -05:00
// create a resolver for remote attestations
image := "registry-1.docker.io/library/notary:server"
2024-05-02 13:35:57 -05:00
platform := "linux/amd64"
// configure policy options
opts := &policy.Options{
TUFClientOptions: tufClientOpts,
LocalTargetsDir: filepath.Join(home, ".docker", "policy"), // location to store policy files downloaded from TUF
LocalPolicyDir: "", // overrides TUF policy for local policy files if set
PolicyID: "", // set to ignore policy mapping and select a policy by id
DisableTUF: false, // set to disable TUF and rely on local policy files
2024-05-02 13:35:57 -05:00
}
src, err := oci.ParseImageSpec(image, oci.WithPlatform(platform))
if err != nil {
panic(err)
}
// verify attestations
result, err := attest.Verify(context.Background(), src, opts)
2024-05-02 13:35:57 -05:00
if err != nil {
panic(err)
2024-05-02 13:35:57 -05:00
}
switch result.Outcome {
case attest.OutcomeSuccess:
fmt.Println("policy passed")
case attest.OutcomeNoPolicy:
fmt.Println("no policy for image")
case attest.OutcomeFailure:
fmt.Println("policy failed")
2024-05-02 13:35:57 -05:00
}
}