mirror of https://github.com/helm/helm
Signed-off-by: Terry Howe <terrylhowe@gmail.com>pull/12690/head
parent
0f5c33e340
commit
ad9fb68fa3
@ -0,0 +1,71 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package registry
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
orasregistry "oras.land/oras-go/pkg/registry"
|
||||
)
|
||||
|
||||
type Reference struct {
|
||||
OrasReference orasregistry.Reference
|
||||
Registry string
|
||||
Repository string
|
||||
Tag string
|
||||
Digest string
|
||||
}
|
||||
|
||||
// NewReference will parse and validate the reference, and clean tags when
|
||||
// applicable tags are only cleaned when plus (+) signs are present, and are
|
||||
// converted to underscores (_) before pushing
|
||||
// See https://github.com/helm/helm/issues/10166
|
||||
func NewReference(raw string) (result Reference, err error) {
|
||||
// Remove oci:// prefix if it is there
|
||||
raw = strings.TrimPrefix(raw, OCIScheme+"://")
|
||||
|
||||
// The sole possible reference modification is replacing plus (+) signs
|
||||
// present in tags with underscores (_). To do this properly, we first
|
||||
// need to identify a tag, and then pass it on to the reference parser
|
||||
// NOTE: Passing immediately to the reference parser will fail since (+)
|
||||
// signs are an invalid tag character, and simply replacing all plus (+)
|
||||
// occurrences could invalidate other portions of the URI
|
||||
lastIndex := strings.LastIndex(raw, "@")
|
||||
if lastIndex >= 0 {
|
||||
result.Digest = raw[(lastIndex + 1):]
|
||||
raw = raw[:lastIndex]
|
||||
}
|
||||
parts := strings.Split(raw, ":")
|
||||
if len(parts) > 1 && !strings.Contains(parts[len(parts)-1], "/") {
|
||||
tag := parts[len(parts)-1]
|
||||
|
||||
if tag != "" {
|
||||
// Replace any plus (+) signs with known underscore (_) conversion
|
||||
newTag := strings.ReplaceAll(tag, "+", "_")
|
||||
raw = strings.ReplaceAll(raw, tag, newTag)
|
||||
}
|
||||
}
|
||||
|
||||
result.OrasReference, err = orasregistry.ParseReference(raw)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Registry = result.OrasReference.Registry
|
||||
result.Repository = result.OrasReference.Repository
|
||||
result.Tag = result.OrasReference.Reference
|
||||
return result, nil
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package registry
|
||||
|
||||
import "testing"
|
||||
|
||||
func verify(t *testing.T, actual Reference, registry, repository, tag, digest string) {
|
||||
if registry != actual.OrasReference.Registry {
|
||||
t.Errorf("Oras Reference registry expected %v actual %v", registry, actual.Registry)
|
||||
}
|
||||
if repository != actual.OrasReference.Repository {
|
||||
t.Errorf("Oras Reference repository expected %v actual %v", repository, actual.Repository)
|
||||
}
|
||||
if tag != actual.OrasReference.Reference {
|
||||
t.Errorf("Oras Reference reference expected %v actual %v", tag, actual.Tag)
|
||||
}
|
||||
if registry != actual.Registry {
|
||||
t.Errorf("Registry expected %v actual %v", registry, actual.Registry)
|
||||
}
|
||||
if repository != actual.Repository {
|
||||
t.Errorf("Repository expected %v actual %v", repository, actual.Repository)
|
||||
}
|
||||
if tag != actual.Tag {
|
||||
t.Errorf("Tag expected %v actual %v", tag, actual.Tag)
|
||||
}
|
||||
if digest != actual.Digest {
|
||||
t.Errorf("Digest expected %v actual %v", digest, actual.Digest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewReference(t *testing.T) {
|
||||
actual, err := NewReference("registry.example.com/repository:1.0@sha256:c6841b3a895f1444a6738b5d04564a57e860ce42f8519c3be807fb6d9bee7888")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
}
|
||||
verify(t, actual, "registry.example.com", "repository", "1.0", "sha256:c6841b3a895f1444a6738b5d04564a57e860ce42f8519c3be807fb6d9bee7888")
|
||||
|
||||
actual, err = NewReference("oci://registry.example.com/repository:1.0@sha256:c6841b3a895f1444a6738b5d04564a57e860ce42f8519c3be807fb6d9bee7888")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
}
|
||||
verify(t, actual, "registry.example.com", "repository", "1.0", "sha256:c6841b3a895f1444a6738b5d04564a57e860ce42f8519c3be807fb6d9bee7888")
|
||||
|
||||
actual, err = NewReference("a/b:1@c")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
}
|
||||
verify(t, actual, "a", "b", "1", "c")
|
||||
|
||||
actual, err = NewReference("a/b:@")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
}
|
||||
verify(t, actual, "a", "b", "", "")
|
||||
|
||||
actual, err = NewReference("registry.example.com/repository:1.0+001")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
}
|
||||
verify(t, actual, "registry.example.com", "repository", "1.0_001", "")
|
||||
|
||||
actual, err = NewReference("thing:1.0")
|
||||
if err == nil {
|
||||
t.Errorf("Expect error error %v", err)
|
||||
}
|
||||
verify(t, actual, "", "", "", "")
|
||||
|
||||
actual, err = NewReference("registry.example.com/the/repository@sha256:c6841b3a895f1444a6738b5d04564a57e860ce42f8519c3be807fb6d9bee7888")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
}
|
||||
verify(t, actual, "registry.example.com", "the/repository", "", "sha256:c6841b3a895f1444a6738b5d04564a57e860ce42f8519c3be807fb6d9bee7888")
|
||||
}
|
Loading…
Reference in new issue