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"
|
|
|
|
|
|
2024-09-02 16:17:50 +01:00
|
|
|
"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")
|
2024-08-23 09:33:30 +01:00
|
|
|
tufClientOpts := tuf.NewDockerDefaultClientOptions(tufOutputPath)
|
2024-05-02 13:35:57 -05:00
|
|
|
|
|
|
|
|
// create a resolver for remote attestations
|
2024-05-02 15:57:19 -05:00
|
|
|
image := "registry-1.docker.io/library/notary:server"
|
2024-05-02 13:35:57 -05:00
|
|
|
platform := "linux/amd64"
|
|
|
|
|
|
|
|
|
|
// configure policy options
|
2024-08-01 15:35:15 +01:00
|
|
|
opts := &policy.Options{
|
2024-08-23 09:33:30 +01:00
|
|
|
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
|
2024-08-28 09:53:52 +01:00
|
|
|
DisableTUF: false, // set to disable TUF and rely on local policy files
|
2024-05-02 13:35:57 -05:00
|
|
|
}
|
|
|
|
|
|
2024-06-21 11:29:16 +01:00
|
|
|
src, err := oci.ParseImageSpec(image, oci.WithPlatform(platform))
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2024-07-29 16:43:31 +01:00
|
|
|
// verify attestations
|
2024-06-21 11:29:16 +01:00
|
|
|
result, err := attest.Verify(context.Background(), src, opts)
|
2024-05-02 13:35:57 -05:00
|
|
|
if err != nil {
|
2024-05-22 14:49:23 +01:00
|
|
|
panic(err)
|
2024-05-02 13:35:57 -05:00
|
|
|
}
|
2024-05-22 14:49:23 +01: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
|
|
|
}
|
|
|
|
|
}
|