Skip to content

Commit

Permalink
fix: retry setCredsStore on next call
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <shizh@microsoft.com>
  • Loading branch information
shizhMSFT committed Mar 19, 2024
1 parent a1d93c0 commit 7cb6519
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions registry/remote/credentials/store.go
Expand Up @@ -53,7 +53,7 @@ type DynamicStore struct {
config *config.Config
options StoreOptions
detectedCredsStore string
setCredsStoreOnce sync.Once
setCredsStoreOnce func() error
}

// StoreOptions provides options for NewStore.
Expand Down Expand Up @@ -107,6 +107,18 @@ func NewStore(configPath string, opts StoreOptions) (*DynamicStore, error) {
// no authentication configured, detect the default credentials store
ds.detectedCredsStore = getDefaultHelperSuffix()
}
var setCredsStore func() error
setCredsStore = func() error {
if ds.detectedCredsStore != "" {
if err := ds.config.SetCredentialsStore(ds.detectedCredsStore); err != nil {
// retry on next call
ds.setCredsStoreOnce = sync.OnceValue(setCredsStore)
return fmt.Errorf("failed to set credsStore: %w", err)
}

Check warning on line 117 in registry/remote/credentials/store.go

View check run for this annotation

Codecov / codecov/patch

registry/remote/credentials/store.go#L113-L117

Added lines #L113 - L117 were not covered by tests
}
return nil
}
ds.setCredsStoreOnce = sync.OnceValue(setCredsStore)
return ds, nil
}

Expand Down Expand Up @@ -136,19 +148,12 @@ func (ds *DynamicStore) Get(ctx context.Context, serverAddress string) (auth.Cre
// Put saves credentials into the store for the given server address.
// Put returns ErrPlaintextPutDisabled if native store is not available and
// [StoreOptions].AllowPlaintextPut is set to false.
func (ds *DynamicStore) Put(ctx context.Context, serverAddress string, cred auth.Credential) (returnErr error) {
func (ds *DynamicStore) Put(ctx context.Context, serverAddress string, cred auth.Credential) error {
if err := ds.getStore(serverAddress).Put(ctx, serverAddress, cred); err != nil {
return err
}
// save the detected creds store back to the config file on first put
ds.setCredsStoreOnce.Do(func() {
if ds.detectedCredsStore != "" {
if err := ds.config.SetCredentialsStore(ds.detectedCredsStore); err != nil {
returnErr = fmt.Errorf("failed to set credsStore: %w", err)
}
}
})
return returnErr
return ds.setCredsStoreOnce()
}

// Delete removes credentials from the store for the given server address.
Expand Down

0 comments on commit 7cb6519

Please sign in to comment.