Files
snapd/asserts/database.go

524 lines
17 KiB
Go
Raw Normal View History

// -*- Mode: Go; indent-tabs-mode: t -*-
/*
2016-01-22 14:54:04 +01:00
* 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 implements snappy assertions and a database
// abstraction for managing and holding them.
package asserts
import (
2015-11-23 18:28:30 +01:00
"errors"
"fmt"
"regexp"
"time"
)
// A Backstore stores assertions. It can store and retrieve assertions
// by type under unique primary key headers (whose names are available
// from assertType.PrimaryKey). Plus it supports searching by headers.
type Backstore interface {
// Put stores an assertion.
// It is responsible for checking that assert is newer than a
// previously stored revision with the same primary key headers.
Put(assertType *AssertionType, assert Assertion) error
// Get returns the assertion with the given unique key for its primary key headers.
// If none is present it returns ErrNotFound.
Get(assertType *AssertionType, key []string) (Assertion, error)
2016-01-12 14:47:33 +01:00
// Search returns assertions matching the given headers.
// It invokes foundCb for each found assertion.
Search(assertType *AssertionType, headers map[string]string, foundCb func(Assertion)) error
}
type nullBackstore struct{}
func (nbs nullBackstore) Put(t *AssertionType, a Assertion) error {
return fmt.Errorf("cannot store assertions without setting a proper assertion backstore implementation")
}
func (nbs nullBackstore) Get(t *AssertionType, k []string) (Assertion, error) {
return nil, ErrNotFound
}
func (nbs nullBackstore) Search(t *AssertionType, h map[string]string, f func(Assertion)) error {
return nil
}
2016-01-12 14:47:33 +01:00
// A KeypairManager is a manager and backstore for private/public key pairs.
type KeypairManager interface {
// Put stores the given private/public key pair,
// making sure it can be later retrieved by its unique key id with Get.
// Trying to store a key with an already present key id should
// result in an error.
Put(privKey PrivateKey) error
// Get returns the private/public key pair with the given key id.
Get(keyID string) (PrivateKey, error)
}
// DatabaseConfig for an assertion database.
type DatabaseConfig struct {
// trusted set of assertions (account and account-key supported)
Trusted []Assertion
2016-01-12 23:16:49 +01:00
// backstore for assertions, left unset storing assertions will error
Backstore Backstore
// manager/backstore for keypairs, defaults to in-memory implementation
KeypairManager KeypairManager
// assertion checkers used by Database.Check, left unset DefaultCheckers will be used which is recommended
Checkers []Checker
}
2015-11-23 18:28:30 +01:00
// Well-known errors
var (
ErrNotFound = errors.New("assertion not found")
2015-11-23 18:28:30 +01:00
)
// RevisionError indicates a revision improperly used for an operation.
type RevisionError struct {
Used, Current int
}
func (e *RevisionError) Error() string {
if e.Used < 0 || e.Current < 0 {
// TODO: message may need tweaking once there's a use.
return fmt.Sprintf("assertion revision is unknown")
}
if e.Used == e.Current {
return fmt.Sprintf("revision %d is already the current revision", e.Used)
}
if e.Used < e.Current {
return fmt.Sprintf("revision %d is older than current revision %d", e.Used, e.Current)
}
return fmt.Sprintf("revision %d is more recent than current revision %d", e.Used, e.Current)
}
// A RODatabase exposes read-only access to an assertion database.
type RODatabase interface {
2016-07-14 11:56:55 +02:00
// IsTrustedAccount returns whether the account is part of the trusted set.
IsTrustedAccount(accountID string) bool
// Find an assertion based on arbitrary headers.
// Provided headers must contain the primary key for the assertion type.
// It returns ErrNotFound if the assertion cannot be found.
Find(assertionType *AssertionType, headers map[string]string) (Assertion, error)
// FindTrusted finds an assertion in the trusted set based on arbitrary headers.
// Provided headers must contain the primary key for the assertion type.
// It returns ErrNotFound if the assertion cannot be found.
FindTrusted(assertionType *AssertionType, headers map[string]string) (Assertion, error)
// FindMany finds assertions based on arbitrary headers.
// It returns ErrNotFound if no assertion can be found.
FindMany(assertionType *AssertionType, headers map[string]string) ([]Assertion, error)
// Check tests whether the assertion is properly signed and consistent with all the stored knowledge.
Check(assert Assertion) error
}
// A Checker defines a check on an assertion considering aspects such as
// the signing key, and consistency with other
// assertions in the database.
type Checker func(assert Assertion, signingKey *AccountKey, roDB RODatabase, checkTime time.Time) error
// A NoAuthorityChecker defines a check on a no-authority assertion.
type NoAuthorityChecker func(assert Assertion, roDB RODatabase, checkTime time.Time) error
// Database holds assertions and can be used to sign or check
// further assertions.
type Database struct {
2016-09-02 17:53:29 +01:00
bs Backstore
keypairMgr KeypairManager
trusted Backstore
backstores []Backstore
checkers []Checker
}
// OpenDatabase opens the assertion database based on the configuration.
func OpenDatabase(cfg *DatabaseConfig) (*Database, error) {
bs := cfg.Backstore
keypairMgr := cfg.KeypairManager
if bs == nil {
bs = nullBackstore{}
}
if keypairMgr == nil {
keypairMgr = NewMemoryKeypairManager()
}
trustedBackstore := NewMemoryBackstore()
for _, a := range cfg.Trusted {
2016-06-13 12:53:50 +02:00
switch accepted := a.(type) {
case *AccountKey:
accKey := accepted
err := trustedBackstore.Put(AccountKeyType, accKey)
if err != nil {
return nil, fmt.Errorf("error loading for use trusted account key %q for %q: %v", accKey.PublicKeyID(), accKey.AccountID(), err)
}
2016-06-13 12:53:50 +02:00
case *Account:
acct := accepted
err := trustedBackstore.Put(AccountType, acct)
if err != nil {
return nil, fmt.Errorf("error loading for use trusted account %q: %v", acct.DisplayName(), err)
}
default:
return nil, fmt.Errorf("cannot load trusted assertions that are not account-key or account: %s", a.Type().Name)
}
2015-12-09 16:25:28 +01:00
}
checkers := cfg.Checkers
if len(checkers) == 0 {
checkers = DefaultCheckers
}
dbCheckers := make([]Checker, len(checkers))
copy(dbCheckers, checkers)
return &Database{
bs: bs,
keypairMgr: keypairMgr,
trusted: trustedBackstore,
// order here is relevant, Find* precedence and
// findAccountKey depend on it, trusted should win over the
// general backstore!
2016-09-02 17:53:29 +01:00
backstores: []Backstore{trustedBackstore, bs},
checkers: dbCheckers,
}, nil
2015-11-23 18:28:30 +01:00
}
// ImportKey stores the given private/public key pair.
func (db *Database) ImportKey(privKey PrivateKey) error {
return db.keypairMgr.Put(privKey)
}
var (
// for sanity checking of base64 hash strings
base64HashLike = regexp.MustCompile("^[[:alnum:]_-]*$")
)
func (db *Database) safeGetPrivateKey(keyID string) (PrivateKey, error) {
if keyID == "" {
return nil, fmt.Errorf("key id is empty")
}
2016-08-11 16:49:56 +02:00
if !base64HashLike.MatchString(keyID) {
return nil, fmt.Errorf("key id contains unexpected chars: %q", keyID)
}
return db.keypairMgr.Get(keyID)
}
// PublicKey returns the public key part of the key pair that has the given key id.
func (db *Database) PublicKey(keyID string) (PublicKey, error) {
privKey, err := db.safeGetPrivateKey(keyID)
if err != nil {
return nil, err
}
return privKey.PublicKey(), nil
}
// Sign assembles an assertion with the provided information and signs it
// with the private key from `headers["authority-id"]` that has the provided key id.
func (db *Database) Sign(assertType *AssertionType, headers map[string]interface{}, body []byte, keyID string) (Assertion, error) {
privKey, err := db.safeGetPrivateKey(keyID)
if err != nil {
return nil, err
}
return assembleAndSign(assertType, headers, body, privKey)
}
2016-08-11 16:49:56 +02:00
// findAccountKey finds an AccountKey exactly with account id and key id.
func (db *Database) findAccountKey(authorityID, keyID string) (*AccountKey, error) {
2016-08-11 16:49:56 +02:00
key := []string{keyID}
// consider trusted account keys then disk stored account keys
for _, bs := range db.backstores {
a, err := bs.Get(AccountKeyType, key)
2016-01-29 15:40:25 +01:00
if err == nil {
hit := a.(*AccountKey)
if hit.AccountID() != authorityID {
2016-08-11 16:49:56 +02:00
return nil, fmt.Errorf("found public key %q from %q but expected it from: %s", keyID, hit.AccountID(), authorityID)
}
return hit, nil
2016-01-29 15:40:25 +01:00
}
if err != ErrNotFound {
return nil, err
}
}
return nil, ErrNotFound
}
// IsTrustedAccount returns whether the account is part of the trusted set.
func (db *Database) IsTrustedAccount(accountID string) bool {
if accountID == "" {
return false
}
_, err := db.trusted.Get(AccountType, []string{accountID})
return err == nil
}
// Check tests whether the assertion is properly signed and consistent with all the stored knowledge.
func (db *Database) Check(assert Assertion) error {
typ := assert.Type()
2016-09-02 17:53:29 +01:00
now := time.Now()
if typ.flags&noAuthority == 0 {
// TODO: later may need to consider type of assert to find candidate keys
accKey, err := db.findAccountKey(assert.AuthorityID(), assert.SignKeyID())
if err == ErrNotFound {
return fmt.Errorf("no matching public key %q for signature by %q", assert.SignKeyID(), assert.AuthorityID())
}
if err != nil {
return fmt.Errorf("error finding matching public key for signature: %v", err)
}
for _, checker := range db.checkers {
err := checker(assert, accKey, db, now)
if err != nil {
return err
}
}
} else {
if assert.AuthorityID() != "" {
return fmt.Errorf("internal error: %q assertion cannot have authority-id set", typ.Name)
}
for _, checker := range noAuthorityCheckers {
err := checker(assert, db, now)
if err != nil {
return err
}
}
}
return nil
}
2015-11-24 23:04:43 +01:00
// Add persists the assertion after ensuring it is properly signed and consistent with all the stored knowledge.
// It will return an error when trying to add an older revision of the assertion than the one currently stored.
2015-11-23 18:28:30 +01:00
func (db *Database) Add(assert Assertion) error {
ref := assert.Ref()
if len(ref.PrimaryKey) == 0 {
return fmt.Errorf("internal error: assertion type %q has no primary key", ref.Type.Name)
}
err := db.Check(assert)
if err != nil {
return err
2015-11-23 18:28:30 +01:00
}
for i, keyVal := range ref.PrimaryKey {
if keyVal == "" {
return fmt.Errorf("missing or non-string primary key header: %v", ref.Type.PrimaryKey[i])
2015-11-23 18:28:30 +01:00
}
}
// assuming trusted account keys/assertions will be managed
// through the os snap this seems the safest policy until we
// know more/better
_, err = db.trusted.Get(ref.Type, ref.PrimaryKey)
if err != ErrNotFound {
return fmt.Errorf("cannot add %q assertion with primary key clashing with a trusted assertion: %v", ref.Type.Name, ref.PrimaryKey)
}
return db.bs.Put(ref.Type, assert)
2015-11-23 18:28:30 +01:00
}
func searchMatch(assert Assertion, expectedHeaders map[string]string) bool {
// check non-primary-key headers as well
for expectedKey, expectedValue := range expectedHeaders {
if assert.Header(expectedKey) != expectedValue {
return false
}
}
return true
}
func find(backstores []Backstore, assertionType *AssertionType, headers map[string]string) (Assertion, error) {
err := checkAssertType(assertionType)
2015-11-23 18:28:30 +01:00
if err != nil {
return nil, err
}
keyValues := make([]string, len(assertionType.PrimaryKey))
for i, k := range assertionType.PrimaryKey {
2015-11-23 18:28:30 +01:00
keyVal := headers[k]
if keyVal == "" {
2015-11-24 12:22:04 +01:00
return nil, fmt.Errorf("must provide primary key: %v", k)
2015-11-23 18:28:30 +01:00
}
keyValues[i] = keyVal
2015-11-23 18:28:30 +01:00
}
var assert Assertion
for _, bs := range backstores {
a, err := bs.Get(assertionType, keyValues)
2016-01-29 15:40:25 +01:00
if err == nil {
assert = a
2016-01-29 15:40:25 +01:00
break
}
if err != ErrNotFound {
return nil, err
}
}
if assert == nil || !searchMatch(assert, headers) {
return nil, ErrNotFound
}
return assert, nil
}
// Find an assertion based on arbitrary headers.
// Provided headers must contain the primary key for the assertion type.
// It returns ErrNotFound if the assertion cannot be found.
func (db *Database) Find(assertionType *AssertionType, headers map[string]string) (Assertion, error) {
return find(db.backstores, assertionType, headers)
}
// FindTrusted finds an assertion in the trusted set based on arbitrary headers.
// Provided headers must contain the primary key for the assertion type.
// It returns ErrNotFound if the assertion cannot be found.
func (db *Database) FindTrusted(assertionType *AssertionType, headers map[string]string) (Assertion, error) {
return find([]Backstore{db.trusted}, assertionType, headers)
}
// FindMany finds assertions based on arbitrary headers.
// It returns ErrNotFound if no assertion can be found.
func (db *Database) FindMany(assertionType *AssertionType, headers map[string]string) ([]Assertion, error) {
err := checkAssertType(assertionType)
2015-11-23 18:28:30 +01:00
if err != nil {
return nil, err
}
res := []Assertion{}
foundCb := func(assert Assertion) {
res = append(res, assert)
}
for _, bs := range db.backstores {
err = bs.Search(assertionType, headers, foundCb)
if err != nil {
return nil, err
}
}
if len(res) == 0 {
return nil, ErrNotFound
}
return res, nil
2015-11-23 18:28:30 +01:00
}
// assertion checkers
// CheckSigningKeyIsNotExpired checks that the signing key is not expired.
func CheckSigningKeyIsNotExpired(assert Assertion, signingKey *AccountKey, roDB RODatabase, checkTime time.Time) error {
if !signingKey.isKeyValidAt(checkTime) {
2016-08-12 18:19:27 +02:00
return fmt.Errorf("assertion is signed with expired public key %q from %q", assert.SignKeyID(), assert.AuthorityID())
}
return nil
}
// CheckSignature checks that the signature is valid.
func CheckSignature(assert Assertion, signingKey *AccountKey, roDB RODatabase, checkTime time.Time) error {
content, encSig := assert.Signature()
signature, err := decodeSignature(encSig)
if err != nil {
return err
}
err = signingKey.publicKey().verify(content, signature)
if err != nil {
return fmt.Errorf("failed signature verification: %v", err)
}
return nil
}
type timestamped interface {
Timestamp() time.Time
}
// CheckTimestampVsSigningKeyValidity verifies that the timestamp of
// the assertion is within the signing key validity.
func CheckTimestampVsSigningKeyValidity(assert Assertion, signingKey *AccountKey, roDB RODatabase, checkTime time.Time) error {
if tstamped, ok := assert.(timestamped); ok {
if !signingKey.isKeyValidAt(tstamped.Timestamp()) {
return fmt.Errorf("%s assertion timestamp outside of signing key validity", assert.Type().Name)
}
}
return nil
}
// XXX: keeping these in this form until we know better
// A consistencyChecker performs further checks based on the full
// assertion database knowledge and its own signing key.
type consistencyChecker interface {
checkConsistency(roDB RODatabase, signingKey *AccountKey) error
}
// CheckCrossConsistency verifies that the assertion is consistent with the other statements in the database.
func CheckCrossConsistency(assert Assertion, signingKey *AccountKey, roDB RODatabase, checkTime time.Time) error {
// see if the assertion requires further checks
if checker, ok := assert.(consistencyChecker); ok {
return checker.checkConsistency(roDB, signingKey)
}
return nil
}
// DefaultCheckers lists the default and recommended assertion
// checkers used by Database if none are specified in the
// DatabaseConfig.Checkers.
var DefaultCheckers = []Checker{
CheckSigningKeyIsNotExpired,
CheckSignature,
CheckTimestampVsSigningKeyValidity,
CheckCrossConsistency,
}
// no-authority assertion checkers
// NoAuthorityCheckSignature checks that the signature is valid.
func NoAuthorityCheckSignature(assert Assertion, roDB RODatabase, checkTime time.Time) error {
selfSigned, ok := assert.(selfSignedAssertion)
if !ok {
panic(fmt.Errorf("cannot check non-self-signed assertion type %q", assert.Type().Name))
}
signingKey := selfSigned.signKey()
content, encSig := assert.Signature()
signature, err := decodeSignature(encSig)
if err != nil {
return err
}
err = signingKey.verify(content, signature)
if err != nil {
return fmt.Errorf("failed signature verification: %v", err)
}
return nil
}
// A noAuthorityConsistencyChecker performs further checks based on the full
// assertion database knowledge and its own signing key.
type noAuthorityConsistencyChecker interface {
noAuthorityCheckConsistency(roDB RODatabase) error
}
// NoAuthorityCheckCrossConsistency verifies that the assertion is consistent with the other statements in the database.
func NoAuthorityCheckCrossConsistency(assert Assertion, roDB RODatabase, checkTime time.Time) error {
// see if the assertion requires further checks
if checker, ok := assert.(noAuthorityConsistencyChecker); ok {
return checker.noAuthorityCheckConsistency(roDB)
}
return nil
}
// noAuthorityCheckers lists the assertion checkers used by Database.
var noAuthorityCheckers = []NoAuthorityChecker{
NoAuthorityCheckSignature,
NoAuthorityCheckCrossConsistency,
}