Rebased from master

Signed-off-by: Diogo Monica <diogo@docker.com>
This commit is contained in:
Diogo Monica 2015-07-20 13:46:01 -07:00
parent 4dfe45d64e
commit f7ea67cfab
2 changed files with 14 additions and 13 deletions

View File

@ -41,7 +41,7 @@ type PassphraseRetriever func(keyId, alias string, createNew bool, attempts int)
type KeyFileStore struct { type KeyFileStore struct {
sync.Mutex sync.Mutex
SimpleFileStore SimpleFileStore
PassphraseRetriever passphrase.Retriever
cachedKeys map[string]*cachedKey cachedKeys map[string]*cachedKey
} }
@ -49,7 +49,7 @@ type KeyFileStore struct {
type KeyMemoryStore struct { type KeyMemoryStore struct {
sync.Mutex sync.Mutex
MemoryFileStore MemoryFileStore
PassphraseRetriever passphrase.Retriever
cachedKeys map[string]*cachedKey cachedKeys map[string]*cachedKey
} }
@ -63,22 +63,22 @@ func NewKeyFileStore(baseDir string, passphraseRetriever passphrase.Retriever) (
cachedKeys := make(map[string]*cachedKey) cachedKeys := make(map[string]*cachedKey)
return &KeyFileStore{SimpleFileStore: *fileStore, return &KeyFileStore{SimpleFileStore: *fileStore,
PassphraseRetriever: passphraseRetriever, Retriever: passphraseRetriever,
cachedKeys: cachedKeys}, nil cachedKeys: cachedKeys}, nil
} }
// AddKey stores the contents of a PEM-encoded private key as a PEM block // AddKey stores the contents of a PEM-encoded private key as a PEM block
func (s *KeyFileStore) AddKey(name, alias string, privKey data.PrivateKey) error { func (s *KeyFileStore) AddKey(name, alias string, privKey data.PrivateKey) error {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
return addKey(s, s.PassphraseRetriever, s.cachedKeys, name, alias, privKey) return addKey(s, s.Retriever, s.cachedKeys, name, alias, privKey)
} }
// GetKey returns the PrivateKey given a KeyID // GetKey returns the PrivateKey given a KeyID
func (s *KeyFileStore) GetKey(name string) (data.PrivateKey, string, error) { func (s *KeyFileStore) GetKey(name string) (data.PrivateKey, string, error) {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
return getKey(s, s.PassphraseRetriever, s.cachedKeys, name) return getKey(s, s.Retriever, s.cachedKeys, name)
} }
// ListKeys returns a list of unique PublicKeys present on the KeyFileStore. // ListKeys returns a list of unique PublicKeys present on the KeyFileStore.
@ -101,22 +101,22 @@ func NewKeyMemoryStore(passphraseRetriever passphrase.Retriever) *KeyMemoryStore
cachedKeys := make(map[string]*cachedKey) cachedKeys := make(map[string]*cachedKey)
return &KeyMemoryStore{MemoryFileStore: *memStore, return &KeyMemoryStore{MemoryFileStore: *memStore,
PassphraseRetriever: passphraseRetriever, Retriever: passphraseRetriever,
cachedKeys: cachedKeys} cachedKeys: cachedKeys}
} }
// AddKey stores the contents of a PEM-encoded private key as a PEM block // AddKey stores the contents of a PEM-encoded private key as a PEM block
func (s *KeyMemoryStore) AddKey(name, alias string, privKey data.PrivateKey) error { func (s *KeyMemoryStore) AddKey(name, alias string, privKey data.PrivateKey) error {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
return addKey(s, s.PassphraseRetriever, s.cachedKeys, name, alias, privKey) return addKey(s, s.Retriever, s.cachedKeys, name, alias, privKey)
} }
// GetKey returns the PrivateKey given a KeyID // GetKey returns the PrivateKey given a KeyID
func (s *KeyMemoryStore) GetKey(name string) (data.PrivateKey, string, error) { func (s *KeyMemoryStore) GetKey(name string) (data.PrivateKey, string, error) {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
return getKey(s, s.PassphraseRetriever, s.cachedKeys, name) return getKey(s, s.Retriever, s.cachedKeys, name)
} }
// ListKeys returns a list of unique PublicKeys present on the KeyFileStore. // ListKeys returns a list of unique PublicKeys present on the KeyFileStore.
@ -133,7 +133,7 @@ func (s *KeyMemoryStore) RemoveKey(name string) error {
return removeKey(s, s.cachedKeys, name) return removeKey(s, s.cachedKeys, name)
} }
func addKey(s LimitedFileStore, passphraseRetriever PassphraseRetriever, cachedKeys map[string]*cachedKey, name, alias string, privKey data.PrivateKey) error { func addKey(s LimitedFileStore, passphraseRetriever passphrase.Retriever, cachedKeys map[string]*cachedKey, name, alias string, privKey data.PrivateKey) error {
pemPrivKey, err := KeyToPEM(privKey) pemPrivKey, err := KeyToPEM(privKey)
if err != nil { if err != nil {
return err return err
@ -186,7 +186,7 @@ func getKeyAlias(s LimitedFileStore, keyID string) (string, error) {
} }
// GetKey returns the PrivateKey given a KeyID // GetKey returns the PrivateKey given a KeyID
func getKey(s LimitedFileStore, passphraseRetriever PassphraseRetriever, cachedKeys map[string]*cachedKey, name string) (data.PrivateKey, string, error) { func getKey(s LimitedFileStore, passphraseRetriever passphrase.Retriever, cachedKeys map[string]*cachedKey, name string) (data.PrivateKey, string, error) {
cachedKeyEntry, ok := cachedKeys[name] cachedKeyEntry, ok := cachedKeys[name]
if ok { if ok {
return cachedKeyEntry.key, cachedKeyEntry.alias, nil return cachedKeyEntry.key, cachedKeyEntry.alias, nil

View File

@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/docker/notary/pkg/passphrase"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -295,7 +296,7 @@ func TestKeysAreCached(t *testing.T) {
assert.NoError(t, err, "failed to create a temporary directory") assert.NoError(t, err, "failed to create a temporary directory")
defer os.RemoveAll(tempBaseDir) defer os.RemoveAll(tempBaseDir)
var countingPassphraseRetriever PassphraseRetriever var countingPassphraseRetriever passphrase.Retriever
numTimesCalled := 0 numTimesCalled := 0
countingPassphraseRetriever = func(keyId, alias string, createNew bool, attempts int) (passphrase string, giveup bool, err error) { countingPassphraseRetriever = func(keyId, alias string, createNew bool, attempts int) (passphrase string, giveup bool, err error) {