1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package asserts
import (
"sync"
)
type memoryKeypairManager struct {
pairs map[string]PrivateKey
mu sync.RWMutex
}
// NewMemoryKeypairManager creates a new key pair manager with a memory backstore.
func NewMemoryKeypairManager() KeypairManager {
return &memoryKeypairManager{
pairs: make(map[string]PrivateKey),
}
}
func (mkm *memoryKeypairManager) Put(privKey PrivateKey) error {
mkm.mu.Lock()
defer mkm.mu.Unlock()
keyID := privKey.PublicKey().ID()
if mkm.pairs[keyID] != nil {
return errKeypairAlreadyExists
}
mkm.pairs[keyID] = privKey
return nil
}
func (mkm *memoryKeypairManager) Get(keyID string) (PrivateKey, error) {
mkm.mu.RLock()
defer mkm.mu.RUnlock()
privKey := mkm.pairs[keyID]
if privKey == nil {
return nil, errKeypairNotFound
}
return privKey, nil
}
func (mkm *memoryKeypairManager) Delete(keyID string) error {
mkm.mu.RLock()
defer mkm.mu.RUnlock()
_, ok := mkm.pairs[keyID]
if !ok {
return errKeypairNotFound
}
delete(mkm.pairs, keyID)
return nil
}
|