Skip to content

Commit

Permalink
fix correct matcher usage + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Nov 13, 2023
1 parent 4051d89 commit 0769cff
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
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")
}

0 comments on commit 0769cff

Please sign in to comment.