Fix race condition

pull/423/head
jackgr 9 years ago
parent 64f9fb66ca
commit 216c8308d8

@ -18,10 +18,12 @@ package repo
import ( import (
"fmt" "fmt"
"sync"
) )
// InmemCredentialProvider is a memory based credential provider. // InmemCredentialProvider is a memory based credential provider.
type InmemCredentialProvider struct { type InmemCredentialProvider struct {
sync.RWMutex
credentials map[string]*RepoCredential credentials map[string]*RepoCredential
} }
@ -32,14 +34,21 @@ func NewInmemCredentialProvider() CredentialProvider {
// GetCredential returns a credential by name. // GetCredential returns a credential by name.
func (fcp *InmemCredentialProvider) GetCredential(name string) (*RepoCredential, error) { func (fcp *InmemCredentialProvider) GetCredential(name string) (*RepoCredential, error) {
fcp.RLock()
defer fcp.RUnlock()
if val, ok := fcp.credentials[name]; ok { if val, ok := fcp.credentials[name]; ok {
return val, nil return val, nil
} }
return nil, fmt.Errorf("no such credential: %s", name) return nil, fmt.Errorf("no such credential: %s", name)
} }
// SetCredential sets a credential by name. // SetCredential sets a credential by name.
func (fcp *InmemCredentialProvider) SetCredential(name string, credential *RepoCredential) error { func (fcp *InmemCredentialProvider) SetCredential(name string, credential *RepoCredential) error {
fcp.Lock()
defer fcp.Unlock()
fcp.credentials[name] = &RepoCredential{APIToken: credential.APIToken, BasicAuth: credential.BasicAuth, ServiceAccount: credential.ServiceAccount} fcp.credentials[name] = &RepoCredential{APIToken: credential.APIToken, BasicAuth: credential.BasicAuth, ServiceAccount: credential.ServiceAccount}
return nil return nil
} }

Loading…
Cancel
Save