Skip to content

Commit

Permalink
make token caching an opt in feature
Browse files Browse the repository at this point in the history
If implemented, users have to opt in to cache a token.

Signed-off-by: Soule BA <bah.soule@gmail.com>
(cherry picked from commit 42a04c7)
  • Loading branch information
souleb authored and mattfarina committed Jul 12, 2022
1 parent c768c3e commit 1cf5bc4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/helm/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string

registryClient, err := registry.NewClient(
registry.ClientOptDebug(settings.Debug),
registry.ClientOptEnableCache(true),
registry.ClientOptWriter(out),
registry.ClientOptCredentialsFile(settings.RegistryConfig),
)
Expand Down
4 changes: 3 additions & 1 deletion pkg/getter/ocigetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func (g *OCIGetter) get(href string) (*bytes.Buffer, error) {

// NewOCIGetter constructs a valid http/https client as a Getter
func NewOCIGetter(ops ...Option) (Getter, error) {
registryClient, err := registry.NewClient()
registryClient, err := registry.NewClient(
registry.ClientOptEnableCache(true),
)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/pusher/ocipusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ func (pusher *OCIPusher) push(chartRef, href string) error {

// NewOCIPusher constructs a valid OCI client as a Pusher
func NewOCIPusher(ops ...Option) (Pusher, error) {
registryClient, err := registry.NewClient()
registryClient, err := registry.NewClient(
registry.ClientOptEnableCache(true),
)
if err != nil {
return nil, err
}
Expand Down
18 changes: 16 additions & 2 deletions pkg/registry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ a plus (+) when pulling from a registry.`
type (
// Client works with OCI-compliant registries
Client struct {
debug bool
debug bool
enableCache bool
// path to repository config file e.g. ~/.docker/config.json
credentialsFile string
out io.Writer
Expand Down Expand Up @@ -95,12 +96,18 @@ func NewClient(options ...ClientOption) (*Client, error) {
}
client.resolver = resolver
}

// allocate a cache if option is set
var cache registryauth.Cache
if client.enableCache {
cache = registryauth.DefaultCache
}
if client.registryAuthorizer == nil {
client.registryAuthorizer = &registryauth.Client{
Header: http.Header{
"User-Agent": {version.GetUserAgent()},
},
Cache: registryauth.DefaultCache,
Cache: cache,
Credential: func(ctx context.Context, reg string) (registryauth.Credential, error) {
dockerClient, ok := client.authorizer.(*dockerauth.Client)
if !ok {
Expand Down Expand Up @@ -138,6 +145,13 @@ func ClientOptDebug(debug bool) ClientOption {
}
}

// ClientOptEnableCache returns a function that sets the enableCache setting on a client options set
func ClientOptEnableCache(enableCache bool) ClientOption {
return func(client *Client) {
client.enableCache = enableCache
}
}

// ClientOptWriter returns a function that sets the writer setting on client options set
func ClientOptWriter(out io.Writer) ClientOption {
return func(client *Client) {
Expand Down
1 change: 1 addition & 0 deletions pkg/registry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (suite *RegistryClientTestSuite) SetupSuite() {
var err error
suite.RegistryClient, err = NewClient(
ClientOptDebug(true),
ClientOptEnableCache(true),
ClientOptWriter(suite.Out),
ClientOptCredentialsFile(credentialsFile),
)
Expand Down
1 change: 1 addition & 0 deletions pkg/repo/repotest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (srv *OCIServer) Run(t *testing.T, opts ...OCIServerOpt) {
// init test client
registryClient, err := ociRegistry.NewClient(
ociRegistry.ClientOptDebug(true),
ociRegistry.ClientOptEnableCache(true),
ociRegistry.ClientOptWriter(os.Stdout),
ociRegistry.ClientOptCredentialsFile(credentialsFile),
)
Expand Down

0 comments on commit 1cf5bc4

Please sign in to comment.