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 <[email protected]>
  • Loading branch information
shizhMSFT committed Mar 19, 2024
1 parent 8d139f0 commit c4548d8
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions registry/remote/credentials/store.go
Original file line number Diff line number Diff line change
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 c4548d8

Please sign in to comment.