diff --git a/extractor/image/image.go b/extractor/image/image.go index 52c50129..ef48c98b 100644 --- a/extractor/image/image.go +++ b/extractor/image/image.go @@ -26,6 +26,7 @@ type ImageCloser interface { LayerInfos() (layerInfos []imageTypes.BlobInfo) ConfigInfo() imageTypes.BlobInfo ConfigBlob(context.Context) ([]byte, error) + Inspect(context.Context) (*imageTypes.ImageInspectInfo, error) Close() error } @@ -93,6 +94,22 @@ func NewImage(ctx context.Context, image Reference, transports []string, option return RealImage{}, xerrors.Errorf("failed to initialize source: %w", err) } + // Validate platform if variant was requested + if variantChoice != "" { + inspectInfo, err := src.Inspect(ctx) + if err != nil { + return RealImage{}, xerrors.Errorf("failed to inspect image: %w", err) + } + + // Check if the actual platform matches the requested one + if inspectInfo.Architecture != archChoice || inspectInfo.Variant != variantChoice { + return RealImage{}, xerrors.Errorf( + "requested platform %s/%s/%s not available", + osChoice, archChoice, variantChoice, + ) + } + } + return RealImage{ name: originalName, blobInfoCache: blobinfocache.DefaultCache(sys), diff --git a/extractor/image/image_test.go b/extractor/image/image_test.go index 32565855..ca05c0fc 100644 --- a/extractor/image/image_test.go +++ b/extractor/image/image_test.go @@ -154,6 +154,20 @@ func TestNewImage(t *testing.T) { }, wantErr: `failed to initialize: invalid character`, }, + { + name: "sad path: invalid platform variant", + args: args{ + image: Reference{ + Name: "testdata/alpine-310.tar.gz", + IsFile: true, + }, + transports: []string{"docker-archive:"}, + option: types.DockerOption{ + Platform: "linux/amd64/v99", + }, + }, + wantErr: `requested platform linux/amd64/v99 not available`, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {