gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/asserts/fskeypairmgr.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  	"errors"
    24  	"fmt"
    25  	"os"
    26  	"path/filepath"
    27  	"sync"
    28  )
    29  
    30  // the default simple filesystem based keypair manager/backstore
    31  
    32  const (
    33  	privateKeysLayoutVersion = "v1"
    34  	privateKeysRoot          = "private-keys-" + privateKeysLayoutVersion
    35  )
    36  
    37  type filesystemKeypairManager struct {
    38  	top string
    39  	mu  sync.RWMutex
    40  }
    41  
    42  // OpenFSKeypairManager opens a filesystem backed assertions backstore under path.
    43  func OpenFSKeypairManager(path string) (KeypairManager, error) {
    44  	top := filepath.Join(path, privateKeysRoot)
    45  	err := ensureTop(top)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	return &filesystemKeypairManager{top: top}, nil
    50  }
    51  
    52  var errKeypairAlreadyExists = errors.New("key pair with given key id already exists")
    53  
    54  func (fskm *filesystemKeypairManager) Put(privKey PrivateKey) error {
    55  	keyID := privKey.PublicKey().ID()
    56  	if entryExists(fskm.top, keyID) {
    57  		return errKeypairAlreadyExists
    58  	}
    59  	encoded, err := encodePrivateKey(privKey)
    60  	if err != nil {
    61  		return fmt.Errorf("cannot store private key: %v", err)
    62  	}
    63  
    64  	fskm.mu.Lock()
    65  	defer fskm.mu.Unlock()
    66  
    67  	err = atomicWriteEntry(encoded, true, fskm.top, keyID)
    68  	if err != nil {
    69  		return fmt.Errorf("cannot store private key: %v", err)
    70  	}
    71  	return nil
    72  }
    73  
    74  var errKeypairNotFound = errors.New("cannot find key pair")
    75  
    76  func (fskm *filesystemKeypairManager) Get(keyID string) (PrivateKey, error) {
    77  	fskm.mu.RLock()
    78  	defer fskm.mu.RUnlock()
    79  
    80  	encoded, err := readEntry(fskm.top, keyID)
    81  	if os.IsNotExist(err) {
    82  		return nil, errKeypairNotFound
    83  	}
    84  	if err != nil {
    85  		return nil, fmt.Errorf("cannot read key pair: %v", err)
    86  	}
    87  	privKey, err := decodePrivateKey(encoded)
    88  	if err != nil {
    89  		return nil, fmt.Errorf("cannot decode key pair: %v", err)
    90  	}
    91  	return privKey, nil
    92  }