From 8f0f0cd12d59ae30cb1dc17daf99ae40923d6389 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Sun, 3 Nov 2024 21:09:53 +0800 Subject: [PATCH] fix: force config media type when setting platform (#1519) Signed-off-by: Billy Zha Co-authored-by: Terry Howe --- cmd/oras/root/push.go | 8 +++++--- test/e2e/suite/command/push.go | 18 +++--------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/cmd/oras/root/push.go b/cmd/oras/root/push.go index 323468d39..b5ae07e14 100644 --- a/cmd/oras/root/push.go +++ b/cmd/oras/root/push.go @@ -197,11 +197,13 @@ func runPush(cmd *cobra.Command, opts *pushOptions) error { if err != nil { return err } - mediaType := ocispec.MediaTypeImageConfig if opts.Flag == option.ImageSpecV1_0 && opts.artifactType != "" { - mediaType = opts.artifactType + return &oerrors.Error{ + Err: errors.New(`artifact type cannot be customized for OCI image-spec v1.0 when platform is specified`), + Recommendation: "consider using image spec v1.1 or remove --artifact-type", + } } - desc := content.NewDescriptorFromBytes(mediaType, blob) + desc := content.NewDescriptorFromBytes(ocispec.MediaTypeImageConfig, blob) err = store.Push(ctx, desc, bytes.NewReader(blob)) if err != nil { return err diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index 7dba0b3a2..516a56629 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -640,24 +640,12 @@ var _ = Describe("OCI image layout users:", func() { })) }) - It("should push files with platform with mediaType as artifactType for v1.0", func() { + It("should fail to customize config mediaType when baking config blob with platform for v1.0", func() { tempDir := PrepareTempFiles() ref := LayoutRef(tempDir, tag) ORAS("push", Flags.Layout, ref, "--image-spec", "v1.0", "--artifact-type", "test/artifact+json", "--artifact-platform", "darwin/arm64", foobar.FileBarName, "-v"). - MatchStatus([]match.StateKey{ - foobar.PlatformV10ConfigStateKey, - foobar.FileBarStateKey, - }, true, 2). - WithWorkDir(tempDir).Exec() - // validate - fetched := ORAS("manifest", "fetch", Flags.Layout, ref).Exec().Out.Contents() - var manifest ocispec.Manifest - Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) - Expect(manifest.Config).Should(Equal(ocispec.Descriptor{ - MediaType: "test/artifact+json", - Size: int64(foobar.PlatformV10ConfigSize), - Digest: foobar.PlatformV10ConfigDigest, - })) + ExpectFailure(). + Exec() }) It("should push files with platform with no artifactType for v1.0", func() {