Skip to content

Commit b3a3f09

Browse files
authored
fix: use oci.ParseRef for ociArtifact input validation (#1927)
<!-- markdownlint-disable MD041 --> #### What this PR does / why we need it The ociArtifact input type validated its path using docker.ParseGenericRef, which splits on ":" and rejects references with more than two colon-separated parts. This broke OCI references containing digests (e.g. repo:tag@sha256:...) because the digest introduces a third colon. Replace with oci.ParseRef, which is the same parser used at runtime by the ociArtifact access method. #### Which issue(s) this PR is related to <!-- Usage: `Related to #<issue number>`, or `Related to (paste link of issue)`. --> --------- Signed-off-by: Fabian Burth <fabian.burth@sap.com>
1 parent f5af619 commit b3a3f09

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

cmds/ocm/commands/ocmcmds/common/inputs/types/ociartifact/input_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
. "ocm.software/ocm/cmds/ocm/testhelper"
1212

1313
"github.com/spf13/pflag"
14+
"k8s.io/apimachinery/pkg/util/validation/field"
1415

1516
"ocm.software/ocm/api/oci"
1617
"ocm.software/ocm/api/oci/extensions/repositories/artifactset"
@@ -141,6 +142,31 @@ var _ = Describe("Test Environment", func() {
141142
})
142143
})
143144

145+
Context("validation", func() {
146+
DescribeTable("accepts valid OCI references",
147+
func(ref string) {
148+
spec := me.New(ref)
149+
errs := spec.Validate(field.NewPath("input"), nil, "")
150+
Expect(errs).To(BeEmpty())
151+
},
152+
Entry("domain/repo:tag", "ghcr.io/open-component-model/image:v1.0"),
153+
Entry("domain/repo:tag@digest", "ghcr.io/stefanprodan/podinfo:6.11.2@sha256:187803cdf611a19d4fffbdf6a4260a01be4c09ffe9924b28902424fc0639ceb8"),
154+
Entry("domain/repo@digest", "ghcr.io/stefanprodan/podinfo@sha256:187803cdf611a19d4fffbdf6a4260a01be4c09ffe9924b28902424fc0639ceb8"),
155+
Entry("docker library shorthand", "alpine:3.19"),
156+
Entry("docker org shorthand", "library/alpine:3.19"),
157+
)
158+
159+
DescribeTable("rejects invalid OCI references",
160+
func(ref string) {
161+
spec := me.New(ref)
162+
errs := spec.Validate(field.NewPath("input"), nil, "")
163+
Expect(errs).NotTo(BeEmpty())
164+
},
165+
Entry("empty string", ""),
166+
Entry("just a tag", ":latest"),
167+
)
168+
})
169+
144170
Context("scenario", func() {
145171
var env *TestEnv
146172
var rname string

cmds/ocm/commands/ocmcmds/common/inputs/types/ociartifact/spec.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"k8s.io/apimachinery/pkg/util/validation/field"
77

8-
"ocm.software/ocm/api/oci/extensions/repositories/docker"
8+
"ocm.software/ocm/api/oci"
99
"ocm.software/ocm/api/oci/grammar"
1010
"ocm.software/ocm/api/oci/tools/transfer/filters"
1111
"ocm.software/ocm/api/ocm/extensions/accessmethods/ociartifact"
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
type Spec struct {
19-
// PathSpec holds the repository path and tag of the image in the docker daemon
19+
// PathSpec holds the OCI image reference.
2020
cpi.PathSpec
2121
// Repository is the repository hint for the index artifact
2222
Repository string `json:"repository,omitempty"`
@@ -39,8 +39,7 @@ func (s *Spec) Validate(fldPath *field.Path, ctx inputs.Context, inputFilePath s
3939
allErrs = ValidateRepository(fldPath.Child("repository"), allErrs, s.Repository)
4040
if s.Path != "" {
4141
pathField := fldPath.Child("path")
42-
_, _, err := docker.ParseGenericRef(s.Path)
43-
if err != nil {
42+
if _, err := oci.ParseRef(s.Path); err != nil {
4443
allErrs = append(allErrs, field.Invalid(pathField, s.Path, err.Error()))
4544
}
4645
}

0 commit comments

Comments
 (0)