Files
attest/pkg/oci/oci.go

150 lines
4.1 KiB
Go
Raw Permalink Normal View History

2024-04-22 12:22:15 -05:00
package oci
import (
"context"
"fmt"
"strings"
2024-07-16 12:52:33 +01:00
"github.com/containerd/platforms"
2024-04-22 12:22:15 -05:00
"github.com/distribution/reference"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/common"
2024-04-22 12:22:15 -05:00
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/package-url/packageurl-go"
)
2024-05-31 11:02:14 +01:00
// ParsePlatform parses the provided platform string or attempts to obtain
// the platform of the current host system.
2024-05-31 11:02:14 +01:00
func ParsePlatform(platformStr string) (*v1.Platform, error) {
2024-04-22 12:22:15 -05:00
if platformStr == "" {
cdp := platforms.Normalize(platforms.DefaultSpec())
if cdp.OS != "windows" {
cdp.OS = "linux"
}
return &v1.Platform{
OS: cdp.OS,
Architecture: cdp.Architecture,
Variant: cdp.Variant,
}, nil
}
return v1.ParsePlatform(platformStr)
2024-04-22 12:22:15 -05:00
}
func WithOptions(ctx context.Context, platform *v1.Platform) []remote.Option {
2024-04-22 12:22:15 -05:00
// prepare options
options := []remote.Option{MultiKeychainOption(), remote.WithTransport(HTTPTransport()), remote.WithContext(ctx)}
2024-04-22 12:22:15 -05:00
// add in platform into remote Get operation; this might conflict with an explicit digest, but we are trying anyway
if platform != nil {
options = append(options, remote.WithPlatform(*platform))
}
return options
}
2024-08-12 14:49:52 -05:00
func ImageDescriptor(ix *v1.IndexManifest, platform *v1.Platform) (*v1.Descriptor, error) {
for i := range ix.Manifests {
m := &ix.Manifests[i]
if (m.MediaType == ocispec.MediaTypeImageManifest || m.MediaType == "application/vnd.docker.distribution.manifest.v2+json") && m.Platform.Equals(*platform) {
return m, nil
2024-04-22 12:22:15 -05:00
}
}
return nil, fmt.Errorf("no image found for platform %v", platform)
2024-04-22 12:22:15 -05:00
}
func RefToPURL(named reference.Named, platform *v1.Platform) (string, bool, error) {
2024-04-22 12:22:15 -05:00
var isCanonical bool
var qualifiers []packageurl.Qualifier
if canonical, ok := named.(reference.Canonical); ok {
qualifiers = append(qualifiers, packageurl.Qualifier{
Key: "digest",
Value: canonical.Digest().String(),
})
isCanonical = true
} else {
named = reference.TagNameOnly(named)
}
version := ""
if tagged, ok := named.(reference.Tagged); ok {
version = tagged.Tag()
}
name := reference.FamiliarName(named)
ns := ""
parts := strings.Split(name, "/")
if len(parts) > 1 {
ns = strings.Join(parts[:len(parts)-1], "/")
}
name = parts[len(parts)-1]
if platform != nil {
2024-04-22 12:22:15 -05:00
qualifiers = append(qualifiers, packageurl.Qualifier{
Key: "platform",
Value: platform.String(),
2024-04-22 12:22:15 -05:00
})
}
p := packageurl.NewPackageURL("docker", ns, name, version, qualifiers, "")
return p.ToString(), isCanonical, nil
}
2024-06-17 17:24:29 +01:00
func SplitDigest(digest string) (common.DigestSet, error) {
parts := strings.SplitN(digest, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid digest %q", digest)
}
2024-06-17 17:24:29 +01:00
return common.DigestSet{
parts[0]: parts[1],
}, nil
}
func ReplaceTagInSpec(src *ImageSpec, digest v1.Hash) (*ImageSpec, error) {
2024-08-12 14:49:52 -05:00
newName, err := ReplaceTag(src.Identifier, digest)
if err != nil {
return nil, fmt.Errorf("failed to parse repo name: %w", err)
}
return &ImageSpec{
Identifier: newName,
Type: src.Type,
Platform: src.Platform,
}, nil
}
// so that the index tag is replaced with a tag unique to the image digest and doesn't overwrite it.
2024-08-12 14:49:52 -05:00
func ReplaceTag(image string, digest v1.Hash) (string, error) {
if strings.HasPrefix(image, LocalPrefix) {
return image, nil
}
notag, err := WithoutTag(image)
if err != nil {
return "", nil
}
return fmt.Sprintf("%s:%s-%s.att", notag, digest.Algorithm, digest.Hex), nil
}
func ReplaceDigestInSpec(src *ImageSpec, digest v1.Hash) (*ImageSpec, error) {
newName, err := replaceDigest(src.Identifier, digest)
if err != nil {
return nil, fmt.Errorf("failed to parse repo name: %w", err)
}
return &ImageSpec{
Identifier: newName,
Type: src.Type,
Platform: src.Platform,
}, nil
}
func replaceDigest(image string, digest v1.Hash) (string, error) {
if strings.HasPrefix(image, LocalPrefix) {
return image, nil
}
notag, err := WithoutTag(image)
if err != nil {
return "", nil
}
return fmt.Sprintf("%s@%s:%s", notag, digest.Algorithm, digest.Hex), nil
}