Skip to content

Commit 5d0a589

Browse files
committed
Add validation to reject old OCI package format
Signed-off-by: Radoslav Dimitrov <[email protected]>
1 parent 5d3544a commit 5d0a589

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

internal/validators/registries/oci.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ func ValidateOCI(ctx context.Context, pkg model.Package, serverName string) erro
8989
return ErrMissingIdentifierForOCI
9090
}
9191

92+
// Validate that old format fields are not present
93+
if pkg.RegistryBaseURL != "" {
94+
return fmt.Errorf("OCI packages must not have 'registryBaseUrl' field - use canonical reference in 'identifier' instead (e.g., 'docker.io/owner/image:1.0.0')")
95+
}
96+
if pkg.Version != "" {
97+
return fmt.Errorf("OCI packages must not have 'version' field - include version in 'identifier' instead (e.g., 'docker.io/owner/image:1.0.0')")
98+
}
99+
92100
// Parse the canonical OCI reference from the identifier
93101
ociRef, err := ParseOCIReference(pkg.Identifier)
94102
if err != nil {

internal/validators/registries/oci_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,67 @@ func TestValidateOCI_SupportedRegistries(t *testing.T) {
196196
})
197197
}
198198
}
199+
200+
func TestValidateOCI_RejectsOldFormat(t *testing.T) {
201+
ctx := context.Background()
202+
203+
tests := []struct {
204+
name string
205+
pkg model.Package
206+
errorMessage string
207+
}{
208+
{
209+
name: "OCI package with registryBaseUrl should be rejected",
210+
pkg: model.Package{
211+
RegistryType: model.RegistryTypeOCI,
212+
RegistryBaseURL: "https://docker.io",
213+
Identifier: "docker.io/test/image:latest",
214+
},
215+
errorMessage: "OCI packages must not have 'registryBaseUrl' field",
216+
},
217+
{
218+
name: "OCI package with version field should be rejected",
219+
pkg: model.Package{
220+
RegistryType: model.RegistryTypeOCI,
221+
Identifier: "docker.io/test/image:latest",
222+
Version: "1.0.0",
223+
},
224+
errorMessage: "OCI packages must not have 'version' field",
225+
},
226+
{
227+
name: "OCI package with both old format fields should fail on registryBaseUrl first",
228+
pkg: model.Package{
229+
RegistryType: model.RegistryTypeOCI,
230+
RegistryBaseURL: "https://docker.io",
231+
Identifier: "test/image",
232+
Version: "1.0.0",
233+
},
234+
errorMessage: "OCI packages must not have 'registryBaseUrl' field",
235+
},
236+
{
237+
name: "OCI package with canonical format should pass old format validation",
238+
pkg: model.Package{
239+
RegistryType: model.RegistryTypeOCI,
240+
Identifier: "docker.io/test/image:latest",
241+
},
242+
errorMessage: "", // Should pass old format check (will fail later due to image not existing)
243+
},
244+
}
245+
246+
for _, tt := range tests {
247+
t.Run(tt.name, func(t *testing.T) {
248+
err := registries.ValidateOCI(ctx, tt.pkg, "com.example/test")
249+
250+
if tt.errorMessage != "" {
251+
assert.Error(t, err)
252+
assert.Contains(t, err.Error(), tt.errorMessage)
253+
} else {
254+
// Should not fail with old format error (may fail with other errors like image not found)
255+
if err != nil {
256+
assert.NotContains(t, err.Error(), "must not have 'registryBaseUrl'")
257+
assert.NotContains(t, err.Error(), "must not have 'version'")
258+
}
259+
}
260+
})
261+
}
262+
}

0 commit comments

Comments
 (0)