Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OCI repository credential handling for downloading Helm charts #578

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/helm/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"helm.sh/helm/v3/pkg/repo"

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/credentials/repositories/directcreds"
"github.com/open-component-model/ocm/pkg/contexts/oci"
ocihelm "github.com/open-component-model/ocm/pkg/contexts/ocm/download/handlers/helm"
Expand Down Expand Up @@ -73,7 +74,12 @@ func DownloadChart(out common.Printer, ctx oci.ContextProvider, ref, version, re
if registry.IsOCI(repourl) {
fs := osfs.New()
chart = vfs.Join(fs, dl.root, filepath.Base(ref)+".tgz")
creds := directcreds.NewCredentials(dl.creds)

var creds credentials.CredentialsSource
if dl.creds != nil {
creds = directcreds.NewCredentials(dl.creds)
}

chart, prov, aset, err = ocihelm.Download2(out, ctx.OCIContext(), identity.OCIRepoURL(repourl, ref)+":"+version, chart, osfs.New(), true, creds)
if prov != "" && dl.Verify > downloader.VerifyNever && dl.Verify != downloader.VerifyLater {
_, err = downloader.VerifyChart(chart, dl.Keyring)
Expand Down
6 changes: 5 additions & 1 deletion pkg/helm/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func OCIRepoURL(repourl string, chartname string) string {
}

func GetConsumerId(repourl string, chartname string) cpi.ConsumerIdentity {
i := strings.LastIndex(chartname, ":")
if i >= 0 {
chartname = chartname[:i]
}
if registry.IsOCI(repourl) {
repourl = strings.TrimSuffix(repourl, "/")
return ociidentity.GetConsumerId(OCIRepoURL(repourl, ""), chartname)
Expand All @@ -89,7 +93,7 @@ func GetCredentials(ctx credentials.ContextProvider, repourl string, chartname s
if id == nil {
return nil
}
creds, err := credentials.CredentialsForConsumer(ctx.CredentialsContext(), id, identityMatcher)
creds, err := credentials.CredentialsForConsumer(ctx.CredentialsContext(), id)
if creds == nil || err != nil {
return nil
}
Expand Down
80 changes: 80 additions & 0 deletions pkg/helm/identity/identity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package identity

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/pkg/contexts/oci"
ociidentity "github.com/open-component-model/ocm/pkg/contexts/oci/identity"
)

var _ = Describe("consumer id handling", func() {
Context("id deternation", func() {
It("handles helm repos", func() {
id := GetConsumerId("https://acme.org/charts", "demo:v1")
Expect(id).To(Equal(credentials.NewConsumerIdentity(CONSUMER_TYPE,
"pathprefix", "charts",
"port", "443",
"hostname", "acme.org",
"scheme", "https",
)))
})

It("handles oci repos", func() {
id := GetConsumerId("oci://acme.org/charts", "demo:v1")
Expect(id).To(Equal(credentials.NewConsumerIdentity(ociidentity.CONSUMER_TYPE,
"pathprefix", "charts/demo",
"hostname", "acme.org",
)))
})
})

Context("query credentials", func() {
var ctx oci.Context
var credctx credentials.Context

BeforeEach(func() {
ctx = oci.New(datacontext.MODE_EXTENDED)
credctx = ctx.CredentialsContext()
})

It("queries helm credentials", func() {
id := GetConsumerId("https://acme.org/charts", "demo:v1")
credctx.SetCredentialsForConsumer(id,
credentials.DirectCredentials{
ATTR_USERNAME: "helm",
ATTR_PASSWORD: "helmpass",
},
)

creds := GetCredentials(ctx, "https://acme.org/charts", "demo:v1")
Expect(creds).To(Equal(common.Properties{
ATTR_USERNAME: "helm",
ATTR_PASSWORD: "helmpass",
}))
})

It("queries oci credentials", func() {
id := GetConsumerId("oci://acme.org/charts", "demo:v1")
credctx.SetCredentialsForConsumer(id,
credentials.DirectCredentials{
ATTR_USERNAME: "oci",
ATTR_PASSWORD: "ocipass",
},
)

creds := GetCredentials(ctx, "oci://acme.org/charts", "demo:v1")
Expect(creds).To(Equal(common.Properties{
ATTR_USERNAME: "oci",
ATTR_PASSWORD: "ocipass",
}))
})
})
})
17 changes: 17 additions & 0 deletions pkg/helm/identity/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package identity_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Helm Identity Suite")
}