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