*Breaking* Parse platform earlier (#43)

* *Breaking* Parse platform earlier

* Use constructors and hide fields to avoid confusion
This commit is contained in:
James Carnegie
2024-05-30 17:38:58 +01:00
committed by GitHub
parent e81016fc31
commit a334599635
7 changed files with 70 additions and 60 deletions

View File

@@ -38,7 +38,7 @@ func parsePlatform(platformStr string) (*v1.Platform, error) {
}
}
func attestationManifestFromOCILayout(path string, platformStr string) (*AttestationManifest, error) {
func attestationManifestFromOCILayout(path string, platform *v1.Platform) (*AttestationManifest, error) {
idx, err := layout.ImageIndexFromPath(path)
if err != nil {
return nil, fmt.Errorf("failed to load image index: %w", err)
@@ -61,10 +61,6 @@ func attestationManifestFromOCILayout(path string, platformStr string) (*Attesta
if err != nil {
return nil, fmt.Errorf("failed to extract IndexManifest from ImageIndex: %w", err)
}
platform, err := parsePlatform(platformStr)
if err != nil {
return nil, fmt.Errorf("failed to parse platform: %w", err)
}
var imageDigest string
for _, mf := range mfs2.Manifests {
if mf.Platform.Equals(*platform) {
@@ -104,17 +100,28 @@ func attestationManifestFromOCILayout(path string, platformStr string) (*Attesta
// implementation of AttestationResolver that closes over attestations from an oci layout
type OCILayoutResolver struct {
Path string
Platform string
path string
platform *v1.Platform
*AttestationManifest
}
func (r *OCILayoutResolver) ImagePlatformStr() string {
return r.Platform
func NewOCILayoutAttestationResolver(path string, platform string) (*OCILayoutResolver, error) {
p, err := parsePlatform(platform)
if err != nil {
return nil, err
}
return &OCILayoutResolver{
path: path,
platform: p,
}, nil
}
func (r *OCILayoutResolver) ImagePlatform() (*v1.Platform, error) {
return r.platform, nil
}
func (r *OCILayoutResolver) fetchAttestationManifest() (*AttestationManifest, error) {
if r.AttestationManifest == nil {
m, err := attestationManifestFromOCILayout(r.Path, r.Platform)
m, err := attestationManifestFromOCILayout(r.path, r.platform)
if err != nil {
return nil, fmt.Errorf("failed to get attestation manifest: %w", err)
}
@@ -189,22 +196,33 @@ func (r *OCILayoutResolver) ImageDigest(ctx context.Context) (string, error) {
}
type RegistryResolver struct {
Image string
Platform string
image string
platform *v1.Platform
*AttestationManifest
}
func (r *RegistryResolver) ImageName(ctx context.Context) (string, error) {
return r.Image, nil
func NewRegistryAttestationResolver(image string, platform string) (*RegistryResolver, error) {
p, err := parsePlatform(platform)
if err != nil {
return nil, err
}
return &RegistryResolver{
image: image,
platform: p,
}, nil
}
func (r *RegistryResolver) ImagePlatformStr() string {
return r.Platform
func (r *RegistryResolver) ImageName(ctx context.Context) (string, error) {
return r.image, nil
}
func (r *RegistryResolver) ImagePlatform() (*v1.Platform, error) {
return r.platform, nil
}
func (r *RegistryResolver) ImageDigest(ctx context.Context) (string, error) {
if r.AttestationManifest == nil {
attest, err := FetchAttestationManifest(ctx, r.Image, r.Platform)
attest, err := FetchAttestationManifest(ctx, r.image, r.platform)
if err != nil {
return "", fmt.Errorf("failed to get attestation manifest: %w", err)
}
@@ -215,7 +233,7 @@ func (r *RegistryResolver) ImageDigest(ctx context.Context) (string, error) {
func (r *RegistryResolver) Attestations(ctx context.Context, predicateType string) ([]*att.Envelope, error) {
if r.AttestationManifest == nil {
attest, err := FetchAttestationManifest(ctx, r.Image, r.Platform)
attest, err := FetchAttestationManifest(ctx, r.image, r.platform)
if err != nil {
return nil, fmt.Errorf("failed to get attestation manifest: %w", err)
}
@@ -224,19 +242,13 @@ func (r *RegistryResolver) Attestations(ctx context.Context, predicateType strin
return ExtractEnvelopes(r.AttestationManifest, predicateType)
}
func FetchAttestationManifest(ctx context.Context, image, platformStr string) (*AttestationManifest, error) {
platform, err := parsePlatform(platformStr)
if err != nil {
return nil, fmt.Errorf("failed to parse platform %s: %w", platform, err)
}
func FetchAttestationManifest(ctx context.Context, image string, platform *v1.Platform) (*AttestationManifest, error) {
// we want to get to the image index, so ignoring platform for now
options := withOptions(ctx, nil)
ref, err := name.ParseReference(image)
if err != nil {
return nil, fmt.Errorf("failed to parse reference: %w", err)
}
desc, err := remote.Index(ref, options...)
if err != nil {
return nil, fmt.Errorf("failed to obtain index manifest: %w", err)
@@ -348,7 +360,7 @@ func attestationDigestForDigest(ix *v1.IndexManifest, imageDigest string, attest
return "", errors.New(fmt.Sprintf("no attestation found for image %s", imageDigest))
}
func RefToPURL(ref string, platform string) (string, bool, error) {
func RefToPURL(ref string, platform *v1.Platform) (string, bool, error) {
var isCanonical bool
named, err := reference.ParseNormalizedNamed(ref)
if err != nil {
@@ -380,14 +392,10 @@ func RefToPURL(ref string, platform string) (string, bool, error) {
}
name = parts[len(parts)-1]
pf, err := parsePlatform(platform)
if err != nil {
return "", false, fmt.Errorf("failed to parse platform %q: %w", platform, err)
}
if pf != nil {
if platform != nil {
qualifiers = append(qualifiers, packageurl.Qualifier{
Key: "platform",
Value: pf.String(),
Value: platform.String(),
})
}