diff --git a/pkg/action/push.go b/pkg/action/push.go index 0c7148f65..a8f0b3a7d 100644 --- a/pkg/action/push.go +++ b/pkg/action/push.go @@ -37,6 +37,7 @@ type Push struct { caFile string insecureSkipTLSVerify bool plainHTTP bool + ociNormalizeVersion bool out io.Writer } @@ -73,6 +74,14 @@ func WithPlainHTTP(plainHTTP bool) PushOpt { } } +// WithOCINormalizeVersion configures whether the OCI tag is derived from the +// canonical semver representation of the chart version. +func WithOCINormalizeVersion(ociNormalizeVersion bool) PushOpt { + return func(p *Push) { + p.ociNormalizeVersion = ociNormalizeVersion + } +} + // WithPushOptWriter sets the registryOut field on the push configuration object. func WithPushOptWriter(out io.Writer) PushOpt { return func(p *Push) { @@ -100,6 +109,7 @@ func (p *Push) Run(chartRef string, remote string) (string, error) { pusher.WithTLSClientConfig(p.certFile, p.keyFile, p.caFile), pusher.WithInsecureSkipTLSVerify(p.insecureSkipTLSVerify), pusher.WithPlainHTTP(p.plainHTTP), + pusher.WithOCINormalizeVersion(p.ociNormalizeVersion), }, } diff --git a/pkg/cmd/push.go b/pkg/cmd/push.go index df0bad643..6c2d2fed9 100644 --- a/pkg/cmd/push.go +++ b/pkg/cmd/push.go @@ -40,6 +40,7 @@ type registryPushOptions struct { caFile string insecureSkipTLSVerify bool plainHTTP bool + ociNormalizeVersion bool password string username string } @@ -83,6 +84,7 @@ func newPushCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { action.WithTLSClientConfig(o.certFile, o.keyFile, o.caFile), action.WithInsecureSkipTLSVerify(o.insecureSkipTLSVerify), action.WithPlainHTTP(o.plainHTTP), + action.WithOCINormalizeVersion(o.ociNormalizeVersion), action.WithPushOptWriter(out)) client.Settings = settings output, err := client.Run(chartRef, remote) @@ -100,6 +102,7 @@ func newPushCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f.StringVar(&o.caFile, "ca-file", "", "verify certificates of HTTPS-enabled servers using this CA bundle") f.BoolVar(&o.insecureSkipTLSVerify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the chart upload") f.BoolVar(&o.plainHTTP, "plain-http", false, "use insecure HTTP connections for the chart upload") + f.BoolVar(&o.ociNormalizeVersion, "oci-normalize-version", false, "push using the canonical semver form of the chart version as the OCI tag (e.g. v1.2.3 is pushed as 1.2.3)") f.StringVar(&o.username, "username", "", "chart repository username where to locate the requested chart") f.StringVar(&o.password, "password", "", "chart repository password where to locate the requested chart") diff --git a/pkg/pusher/ocipusher.go b/pkg/pusher/ocipusher.go index 2a12e09b4..2fbb3ed5b 100644 --- a/pkg/pusher/ocipusher.go +++ b/pkg/pusher/ocipusher.go @@ -26,6 +26,8 @@ import ( "strings" "time" + "github.com/Masterminds/semver/v3" + "helm.sh/helm/v4/internal/tlsutil" "helm.sh/helm/v4/pkg/chart/v2/loader" "helm.sh/helm/v4/pkg/registry" @@ -85,9 +87,20 @@ func (pusher *OCIPusher) push(chartRef, href string) error { pushOpts = append(pushOpts, registry.PushOptProvData(provBytes)) } - ref := fmt.Sprintf("%s:%s", - path.Join(strings.TrimPrefix(href, registry.OCIScheme+"://"), meta.Metadata.Name), - meta.Metadata.Version) + // Build the OCI reference for the chart. When --oci-normalize-version is + // set (pusher.opts.ociNormalizeVersion) the tag is the canonical semver + // form of the chart version; otherwise it is the raw chart version. + ref, relaxStrictMode, err := buildOCIReference(href, meta.Metadata.Name, meta.Metadata.Version, pusher.opts.ociNormalizeVersion) + if err != nil { + return err + } + + // The registry client's strict mode asserts that the tag equals the raw + // chart version. Once the version has been canonicalized the tag no longer + // matches, so that assertion must be disabled. + if relaxStrictMode { + pushOpts = append(pushOpts, registry.PushOptStrictMode(false)) + } // The time the chart was "created" is semantically the time the chart archive file was last written(modified) chartArchiveFileCreatedTime := stat.ModTime() @@ -97,6 +110,52 @@ func (pusher *OCIPusher) push(chartRef, href string) error { return err } +// buildOCIReference constructs the OCI reference used to push a chart and +// reports whether the registry client's strict mode must be relaxed for it. +// +// When ociNormalizeVersion is true the tag is the canonical semver form of the +// chart version (e.g. "v1.2.3" -> "1.2.3"); otherwise the raw chart version is +// used unchanged. The registry client still applies its usual tag +// transformations afterwards (e.g. replacing plus (+) signs with underscores). +// +// relaxStrictMode is true when the resulting tag differs from the raw chart +// version. The registry client's strict mode (registry.PushOptStrictMode) +// asserts the tag equals the raw chart version, so it must be disabled once the +// version has been canonicalized. This is separate from the option that drives +// ociNormalizeVersion despite the shared "strict" terminology. +func buildOCIReference(href, chartName, rawVersion string, ociNormalizeVersion bool) (ref string, relaxStrictMode bool, err error) { + version, err := resolveOCITagVersion(rawVersion, ociNormalizeVersion) + if err != nil { + return "", false, err + } + + ref = fmt.Sprintf("%s:%s", + path.Join(strings.TrimPrefix(href, registry.OCIScheme+"://"), chartName), + version) + + return ref, version != rawVersion, nil +} + +// resolveOCITagVersion returns the version string to use as the base of the OCI +// tag for a chart. When ociNormalizeVersion is false the raw chart version is +// returned unchanged. When it is true the version is parsed with semver and its +// canonical string representation is returned, so that a canonical semver tag is +// produced regardless of how the version was written in Chart.yaml. +func resolveOCITagVersion(rawVersion string, ociNormalizeVersion bool) (string, error) { + if !ociNormalizeVersion { + return rawVersion, nil + } + + parsedVersion, err := semver.NewVersion(rawVersion) + if err != nil { + // Defensive: the chart loader validates the version as semver before a + // push reaches this point, so a valid chart should never hit this path. + return "", fmt.Errorf("failed to parse chart version %q as semver: %w", rawVersion, err) + } + + return parsedVersion.String(), nil +} + // NewOCIPusher constructs a valid OCI client as a Pusher func NewOCIPusher(ops ...Option) (Pusher, error) { var client OCIPusher diff --git a/pkg/pusher/ocipusher_test.go b/pkg/pusher/ocipusher_test.go index b63077293..65bcf8fd7 100644 --- a/pkg/pusher/ocipusher_test.go +++ b/pkg/pusher/ocipusher_test.go @@ -324,6 +324,199 @@ func TestOCIPusher_Push_ChartOperations(t *testing.T) { } } +func TestWithOCINormalizeVersion(t *testing.T) { + p, err := NewOCIPusher(WithOCINormalizeVersion(true)) + if err != nil { + t.Fatal(err) + } + + op, ok := p.(*OCIPusher) + if !ok { + t.Fatal("Expected NewOCIPusher to produce an *OCIPusher") + } + + if !op.opts.ociNormalizeVersion { + t.Error("Expected WithOCINormalizeVersion(true) to set ociNormalizeVersion") + } + + // Defaults to false when the option is not supplied. + p, err = NewOCIPusher() + if err != nil { + t.Fatal(err) + } + if p.(*OCIPusher).opts.ociNormalizeVersion { + t.Error("Expected ociNormalizeVersion to default to false") + } +} + +func TestResolveOCITagVersion(t *testing.T) { + tests := []struct { + name string + rawVersion string + ociNormalizeVersion bool + want string + expectError bool + }{ + { + name: "normalize disabled returns raw version unchanged", + rawVersion: "v1.2.3", + ociNormalizeVersion: false, + want: "v1.2.3", + }, + { + name: "normalize disabled does not validate version", + rawVersion: "not-a-semver", + ociNormalizeVersion: false, + want: "not-a-semver", + }, + { + name: "normalize strips leading v", + rawVersion: "v1.2.3", + ociNormalizeVersion: true, + want: "1.2.3", + }, + { + name: "normalize leaves canonical version unchanged", + rawVersion: "1.2.3", + ociNormalizeVersion: true, + want: "1.2.3", + }, + { + name: "normalize preserves prerelease", + rawVersion: "1.2.3-alpha.1", + ociNormalizeVersion: true, + want: "1.2.3-alpha.1", + }, + { + name: "normalize preserves build metadata (sanitized to underscore later)", + rawVersion: "1.2.3+build.5", + ociNormalizeVersion: true, + want: "1.2.3+build.5", + }, + { + name: "normalize errors on non-semver version", + rawVersion: "not-a-semver", + ociNormalizeVersion: true, + expectError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := resolveOCITagVersion(tt.rawVersion, tt.ociNormalizeVersion) + if tt.expectError { + if err == nil { + t.Fatalf("Expected error for version %q but got none", tt.rawVersion) + } + return + } + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + if got != tt.want { + t.Errorf("resolveOCITagVersion(%q, %t) = %q, want %q", tt.rawVersion, tt.ociNormalizeVersion, got, tt.want) + } + }) + } +} + +func TestBuildOCIReference(t *testing.T) { + tests := []struct { + name string + href string + chartName string + rawVersion string + ociNormalizeVersion bool + wantRef string + wantRelax bool + expectError bool + }{ + { + name: "normalize disabled uses raw version and keeps strict mode", + href: "oci://localhost:5000/charts", + chartName: "mychart", + rawVersion: "v1.2.3", + ociNormalizeVersion: false, + wantRef: "localhost:5000/charts/mychart:v1.2.3", + wantRelax: false, + }, + { + name: "normalize canonicalizes v-prefixed version and relaxes strict mode", + href: "oci://localhost:5000/charts", + chartName: "mychart", + rawVersion: "v1.2.3", + ociNormalizeVersion: true, + wantRef: "localhost:5000/charts/mychart:1.2.3", + wantRelax: true, + }, + { + name: "normalize leaves canonical version and keeps strict mode", + href: "oci://localhost:5000/charts", + chartName: "mychart", + rawVersion: "1.2.3", + ociNormalizeVersion: true, + wantRef: "localhost:5000/charts/mychart:1.2.3", + wantRelax: false, + }, + { + name: "normalize completes short version and relaxes strict mode", + href: "oci://localhost:5000/charts", + chartName: "mychart", + rawVersion: "1.2", + ociNormalizeVersion: true, + wantRef: "localhost:5000/charts/mychart:1.2.0", + wantRelax: true, + }, + { + name: "normalize with build metadata keeps strict mode", + href: "oci://localhost:5000/charts", + chartName: "mychart", + rawVersion: "1.2.3+build.5", + ociNormalizeVersion: true, + wantRef: "localhost:5000/charts/mychart:1.2.3+build.5", + wantRelax: false, + }, + { + name: "oci scheme prefix is trimmed from href", + href: "oci://registry.example.com/team/charts", + chartName: "mychart", + rawVersion: "1.2.3", + ociNormalizeVersion: false, + wantRef: "registry.example.com/team/charts/mychart:1.2.3", + wantRelax: false, + }, + { + name: "normalize errors on non-semver version", + href: "oci://localhost:5000/charts", + chartName: "mychart", + rawVersion: "not-a-semver", + ociNormalizeVersion: true, + expectError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ref, relax, err := buildOCIReference(tt.href, tt.chartName, tt.rawVersion, tt.ociNormalizeVersion) + if tt.expectError { + if err == nil { + t.Fatalf("Expected error for version %q but got none", tt.rawVersion) + } + return + } + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + if ref != tt.wantRef { + t.Errorf("buildOCIReference ref = %q, want %q", ref, tt.wantRef) + } + if relax != tt.wantRelax { + t.Errorf("buildOCIReference relaxStrictMode = %t, want %t", relax, tt.wantRelax) + } + }) + } +} + func TestOCIPusher_Push_MultipleOptions(t *testing.T) { chartPath := "../../pkg/cmd/testdata/testcharts/compressedchart-0.1.0.tgz" diff --git a/pkg/pusher/pusher.go b/pkg/pusher/pusher.go index 8ce78b011..f62de63d6 100644 --- a/pkg/pusher/pusher.go +++ b/pkg/pusher/pusher.go @@ -34,6 +34,7 @@ type options struct { caFile string insecureSkipTLSVerify bool plainHTTP bool + ociNormalizeVersion bool } // Option allows specifying various settings configurable by the user for overriding the defaults @@ -69,6 +70,18 @@ func WithPlainHTTP(plainHTTP bool) Option { } } +// WithOCINormalizeVersion determines whether the OCI tag is derived from the +// canonical semver representation of the chart version rather than the raw +// version string. Note this is unrelated to the registry client's "strict +// mode" (registry.PushOptStrictMode); enabling it may in fact require that +// strict mode be relaxed, since the canonical tag can differ from the raw +// chart version. +func WithOCINormalizeVersion(ociNormalizeVersion bool) Option { + return func(opts *options) { + opts.ociNormalizeVersion = ociNormalizeVersion + } +} + // Pusher is an interface to support upload to the specified URL. type Pusher interface { // Push file content by url string