versionsapi: canonicalize ref in version constructors

Signed-off-by: Paul Meyer <katexochen0@gmail.com>
This commit is contained in:
Paul Meyer 2025-02-17 13:59:37 +01:00
parent d208251df1
commit 1d138f40a8
2 changed files with 130 additions and 4 deletions

View File

@ -41,7 +41,7 @@ type Version struct {
// NewVersion creates a new Version object and validates it.
func NewVersion(ref, stream, version string, kind VersionKind) (Version, error) {
ver := Version{
ref: ref,
ref: CanonicalizeRef(ref),
stream: stream,
version: version,
kind: kind,
@ -62,7 +62,7 @@ func NewVersionFromShortPath(shortPath string, kind VersionKind) (Version, error
}
ver := Version{
ref: ref,
ref: ref, // Canonicalized by parseShortPath.
stream: stream,
version: version,
kind: kind,
@ -331,7 +331,7 @@ func CanonicalizeRef(ref string) string {
canRef := notAZ09Regexp.ReplaceAllString(ref, "-")
if canRef == ReleaseRef {
return "" // No ref should be cannonicalized to the release ref.
return "" // No ref should be canonicalized to the release ref.
}
return canRef
@ -401,7 +401,7 @@ func MeasurementURL(version Version) (measurementURL, signatureURL *url.URL, err
}
var (
shortPathRegex = regexp.MustCompile(`^ref/([a-zA-Z0-9-]+)/stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
shortPathRegex = regexp.MustCompile(`^ref/([^/]+)/stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
shortPathReleaseRegex = regexp.MustCompile(`^stream/([a-zA-Z0-9-]+)/([a-zA-Z0-9.-]+)$`)
)
@ -422,6 +422,7 @@ func parseShortPath(shortPath string) (ref, stream, version string, err error) {
if shortPathRegex.MatchString(shortPath) {
matches := shortPathRegex.FindStringSubmatch(shortPath)
ref := matches[1]
ref = CanonicalizeRef(ref)
if err := ValidateRef(ref); err != nil {
return "", "", "", err
}

View File

@ -16,6 +16,111 @@ import (
"github.com/edgelesssys/constellation/v2/internal/constants"
)
func TestNewVersion(t *testing.T) {
testCases := map[string]struct {
ref string
stream string
version string
kind VersionKind
wantVer Version
wantErr bool
}{
"stable release image": {
ref: ReleaseRef,
stream: "stable",
version: "v9.9.9",
kind: VersionKindImage,
wantVer: Version{
ref: ReleaseRef,
stream: "stable",
version: "v9.9.9",
kind: VersionKindImage,
},
},
"release debug image": {
ref: ReleaseRef,
stream: "debug",
version: "v9.9.9",
kind: VersionKindImage,
wantVer: Version{
ref: ReleaseRef,
stream: "debug",
version: "v9.9.9",
kind: VersionKindImage,
},
},
"stable release cli": {
ref: ReleaseRef,
stream: "stable",
version: "v9.9.9",
kind: VersionKindCLI,
wantVer: Version{
ref: ReleaseRef,
stream: "stable",
version: "v9.9.9",
kind: VersionKindCLI,
},
},
"release debug cli": {
ref: ReleaseRef,
stream: "debug",
version: "v9.9.9",
kind: VersionKindCLI,
wantVer: Version{
ref: ReleaseRef,
stream: "debug",
version: "v9.9.9",
kind: VersionKindCLI,
},
},
"unknown kind": {
ref: ReleaseRef,
stream: "debug",
version: "v9.9.9",
kind: VersionKindUnknown,
wantErr: true,
},
"non-release ref as input": {
ref: "working-branch",
stream: "debug",
version: "v9.9.9",
kind: VersionKindImage,
wantVer: Version{
ref: "working-branch",
stream: "debug",
version: "v9.9.9",
kind: VersionKindImage,
},
},
"non-canonical ref as input": {
ref: "testing-1.23",
stream: "debug",
version: "v9.9.9",
kind: VersionKindImage,
wantVer: Version{
ref: "testing-1-23",
stream: "debug",
version: "v9.9.9",
kind: VersionKindImage,
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
ver, err := NewVersion(tc.ref, tc.stream, tc.version, tc.kind)
if tc.wantErr {
assert.Error(err)
return
}
assert.NoError(err)
assert.Equal(tc.wantVer, ver)
})
}
}
func TestNewVersionFromShortPath(t *testing.T) {
testCases := map[string]struct {
path string
@ -78,6 +183,26 @@ func TestNewVersionFromShortPath(t *testing.T) {
kind: VersionKindCLI,
wantErr: true,
},
"non-release ref as input": {
path: "ref/working-branch/stream/debug/v9.9.9",
kind: VersionKindImage,
wantVer: Version{
ref: "working-branch",
stream: "debug",
version: "v9.9.9",
kind: VersionKindImage,
},
},
"non-canonical ref as input": {
path: "ref/testing-1.23/stream/debug/v9.9.9",
kind: VersionKindImage,
wantVer: Version{
ref: "testing-1-23",
stream: "debug",
version: "v9.9.9",
kind: VersionKindImage,
},
},
}
for name, tc := range testCases {