From 216c8308d84584585210b85e597b1ddaef01c623 Mon Sep 17 00:00:00 2001 From: jackgr Date: Mon, 21 Mar 2016 15:27:10 -0700 Subject: [PATCH] Fix race condition --- pkg/repo/inmem_credential_provider.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/repo/inmem_credential_provider.go b/pkg/repo/inmem_credential_provider.go index 93d36fbc9..453966b4a 100644 --- a/pkg/repo/inmem_credential_provider.go +++ b/pkg/repo/inmem_credential_provider.go @@ -18,10 +18,12 @@ package repo import ( "fmt" + "sync" ) // InmemCredentialProvider is a memory based credential provider. type InmemCredentialProvider struct { + sync.RWMutex credentials map[string]*RepoCredential } @@ -32,14 +34,21 @@ func NewInmemCredentialProvider() CredentialProvider { // GetCredential returns a credential by name. func (fcp *InmemCredentialProvider) GetCredential(name string) (*RepoCredential, error) { + fcp.RLock() + defer fcp.RUnlock() + if val, ok := fcp.credentials[name]; ok { return val, nil } + return nil, fmt.Errorf("no such credential: %s", name) } // SetCredential sets a credential by name. 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} return nil }