-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix cred names + harmonize propagation handling
- Loading branch information
1 parent
a9477ab
commit b6058b0
Showing
8 changed files
with
150 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package npm | ||
|
||
import ( | ||
"github.com/open-component-model/ocm/pkg/listformat" | ||
) | ||
|
||
var usage = ` | ||
This repository type can be used to access credentials stored in a file | ||
following the NPM npmrc format (~/.npmrc). It take into account the | ||
credentials helper section, also. If enabled, the described | ||
credentials will be automatically assigned to appropriate consumer ids. | ||
` | ||
|
||
var format = `The repository specification supports the following fields: | ||
` + listformat.FormatListElements("", listformat.StringElementDescriptionList{ | ||
"npmrcFile", "*string*: the file path to a NPM npmrc file", | ||
"propagateConsumerIdentity", "*bool*(optional): enable consumer id propagation", | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,42 @@ | ||
package npm | ||
package npm_test | ||
|
||
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/credentials/builtin/npm/identity" | ||
"github.com/open-component-model/ocm/pkg/contexts/credentials/repositories/npm" | ||
. "github.com/open-component-model/ocm/pkg/testutils" | ||
) | ||
|
||
var _ = Describe("Config deserialization Test Environment", func() { | ||
It("read .npmrc", func() { | ||
cfg, err := readNpmConfigFile("testdata/.npmrc") | ||
Expect(err).To(BeNil()) | ||
Expect(cfg).ToNot(BeNil()) | ||
Expect(cfg).ToNot(BeEmpty()) | ||
Expect(cfg["https://registry.npmjs.org/"]).To(Equal("npm_TOKEN")) | ||
Expect(cfg["https://npm.registry.acme.com/api/npm/"]).To(Equal("bearer_TOKEN")) | ||
ctx := credentials.New() | ||
|
||
repo := Must(npm.NewRepository(ctx, "testdata/.npmrc")) | ||
Expect(Must(repo.LookupCredentials("https://registry.npmjs.org")).Properties()).To(Equal(common.Properties{identity.ATTR_TOKEN: "npm_TOKEN"})) | ||
Expect(Must(repo.LookupCredentials("https://npm.registry.acme.com/api/npm")).Properties()).To(Equal(common.Properties{identity.ATTR_TOKEN: "bearer_TOKEN"})) | ||
}) | ||
|
||
It("propagates credentials", func() { | ||
ctx := credentials.New() | ||
|
||
spec := npm.NewRepositorySpec("testdata/.npmrc") | ||
|
||
_ = Must(ctx.RepositoryForSpec(spec)) | ||
id := identity.GetConsumerId("https://registry.npmjs.org", "pkg") | ||
|
||
creds := Must(credentials.CredentialsForConsumer(ctx, id)) | ||
Expect(creds).NotTo(BeNil()) | ||
Expect(creds.GetProperty(identity.ATTR_TOKEN)).To(Equal("npm_TOKEN")) | ||
}) | ||
|
||
It("has description", func() { | ||
ctx := credentials.New() | ||
t := ctx.RepositoryTypes().GetType(npm.TypeV1) | ||
Expect(t).NotTo(BeNil()) | ||
Expect(t.Description()).NotTo(Equal("")) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package npm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/open-component-model/ocm/pkg/contexts/credentials/cpi" | ||
"github.com/open-component-model/ocm/pkg/runtime" | ||
"github.com/open-component-model/ocm/pkg/utils" | ||
) | ||
|
||
const ( | ||
// Type is the type of the NPMConfig. | ||
Type = "NPMConfig" | ||
TypeV1 = Type + runtime.VersionSeparator + "v1" | ||
) | ||
|
||
func init() { | ||
cpi.RegisterRepositoryType(cpi.NewRepositoryType[*RepositorySpec](Type)) | ||
cpi.RegisterRepositoryType(cpi.NewRepositoryType[*RepositorySpec](TypeV1, cpi.WithDescription(usage), cpi.WithFormatSpec(format))) | ||
} | ||
|
||
// RepositorySpec describes a docker npmrc based credential repository interface. | ||
type RepositorySpec struct { | ||
runtime.ObjectVersionedType `json:",inline"` | ||
NpmrcFile string `json:"npmrcFile,omitempty"` | ||
PropgateConsumerIdentity *bool `json:"propagateConsumerIdentity,omitempty"` | ||
} | ||
|
||
// NewRepositorySpec creates a new memory RepositorySpec. | ||
func NewRepositorySpec(path string) *RepositorySpec { | ||
if path == "" { | ||
path = "~/.npmrc" | ||
} | ||
return &RepositorySpec{ | ||
ObjectVersionedType: runtime.NewVersionedTypedObject(Type), | ||
NpmrcFile: path, | ||
} | ||
} | ||
|
||
func (rs *RepositorySpec) GetType() string { | ||
return Type | ||
} | ||
|
||
func (rs *RepositorySpec) Repository(ctx cpi.Context, _ cpi.Credentials) (cpi.Repository, error) { | ||
r := ctx.GetAttributes().GetOrCreateAttribute(".npmrc", createCache) | ||
cache, ok := r.(*Cache) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to assert type %T to Cache", r) | ||
} | ||
return cache.GetRepository(ctx, rs.NpmrcFile, utils.AsBool(rs.PropgateConsumerIdentity, true)) | ||
} |