2015-03-05 21:27:35 +00:00
|
|
|
package vault
|
|
|
|
|
2015-03-15 20:52:43 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2015-05-28 00:25:36 +00:00
|
|
|
"time"
|
2015-03-15 20:52:43 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
2015-03-05 21:27:35 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrBarrierSealed is returned if an operation is performed on
|
|
|
|
// a sealed barrier. No operation is expected to succeed before unsealing
|
|
|
|
ErrBarrierSealed = errors.New("Vault is sealed")
|
|
|
|
|
|
|
|
// ErrBarrierAlreadyInit is returned if the barrier is already
|
|
|
|
// initialized. This prevents a re-initialization.
|
|
|
|
ErrBarrierAlreadyInit = errors.New("Vault is already initialized")
|
|
|
|
|
|
|
|
// ErrBarrierNotInit is returned if a non-initialized barrier
|
|
|
|
// is attempted to be unsealed.
|
|
|
|
ErrBarrierNotInit = errors.New("Vault is not initialized")
|
2015-03-12 18:27:32 +00:00
|
|
|
|
|
|
|
// ErrBarrierInvalidKey is returned if the Unseal key is invalid
|
|
|
|
ErrBarrierInvalidKey = errors.New("Unseal failed, invalid key")
|
2015-03-05 21:27:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// barrierInitPath is the path used to store our init sentinel file
|
|
|
|
barrierInitPath = "barrier/init"
|
2015-05-27 22:23:43 +00:00
|
|
|
|
|
|
|
// keyringPath is the location of the keyring data. This is encrypted
|
|
|
|
// by the master key.
|
|
|
|
keyringPath = "core/keyring"
|
2015-05-28 22:52:26 +00:00
|
|
|
|
|
|
|
// keyringUpgradePrefix is the path used to store keyring update entries.
|
|
|
|
// When running in HA mode, the active instance will install the new key
|
|
|
|
// and re-write the keyring. For standby instances, they need an upgrade
|
|
|
|
// path from key N to N+1. They cannot just use the master key because
|
|
|
|
// in the event of a rekey, that master key can no longer decrypt the keyring.
|
|
|
|
// When key N+1 is installed, we create an entry at "prefix/N" which uses
|
|
|
|
// encryption key N to provide the N+1 key. The standby instances scan
|
|
|
|
// for this periodically and refresh their keyring. The upgrade keys
|
|
|
|
// are deleted after a few minutes, but this provides enough time for the
|
|
|
|
// standby instances to upgrade without causing any disruption.
|
|
|
|
keyringUpgradePrefix = "core/upgrade/"
|
2015-05-29 21:29:55 +00:00
|
|
|
|
|
|
|
// masterKeyPath is the location of the master key. This is encrypted
|
|
|
|
// by the latest key in the keyring. This is only used by standby instances
|
|
|
|
// to handle the case of a rekey. If the active instance does a rekey,
|
|
|
|
// the standby instances can no longer reload the keyring since they
|
|
|
|
// have the old master key. This key can be decrypted if you have the
|
|
|
|
// keyring to discover the new master key. The new master key is then
|
|
|
|
// used to reload the keyring itself.
|
|
|
|
masterKeyPath = "core/master"
|
2015-03-05 21:27:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SecurityBarrier is a critical component of Vault. It is used to wrap
|
|
|
|
// an untrusted physical backend and provide a single point of encryption,
|
|
|
|
// decryption and checksum verification. The goal is to ensure that any
|
|
|
|
// data written to the barrier is confidential and that integrity is preserved.
|
|
|
|
// As a real-world analogy, this is the steel and concrete wrapper around
|
|
|
|
// a Vault. The barrier should only be Unlockable given its key.
|
|
|
|
type SecurityBarrier interface {
|
|
|
|
// Initialized checks if the barrier has been initialized
|
|
|
|
// and has a master key set.
|
|
|
|
Initialized() (bool, error)
|
|
|
|
|
|
|
|
// Initialize works only if the barrier has not been initialized
|
|
|
|
// and makes use of the given master key.
|
|
|
|
Initialize([]byte) error
|
|
|
|
|
|
|
|
// GenerateKey is used to generate a new key
|
|
|
|
GenerateKey() ([]byte, error)
|
|
|
|
|
2015-03-12 18:20:27 +00:00
|
|
|
// KeyLength is used to sanity check a key
|
|
|
|
KeyLength() (int, int)
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Sealed checks if the barrier has been unlocked yet. The Barrier
|
|
|
|
// is not expected to be able to perform any CRUD until it is unsealed.
|
|
|
|
Sealed() (bool, error)
|
|
|
|
|
|
|
|
// Unseal is used to provide the master key which permits the barrier
|
|
|
|
// to be unsealed. If the key is not correct, the barrier remains sealed.
|
|
|
|
Unseal(key []byte) error
|
|
|
|
|
2015-05-28 18:28:33 +00:00
|
|
|
// VerifyMaster is used to check if the given key matches the master key
|
|
|
|
VerifyMaster(key []byte) error
|
|
|
|
|
2015-05-28 23:09:15 +00:00
|
|
|
// ReloadKeyring is used to re-read the underlying keyring.
|
|
|
|
// This is used for HA deployments to ensure the latest keyring
|
|
|
|
// is present in the leader.
|
|
|
|
ReloadKeyring() error
|
|
|
|
|
2015-05-29 21:29:55 +00:00
|
|
|
// ReloadMasterKey is used to re-read the underlying masterkey.
|
|
|
|
// This is used for HA deployments to ensure the latest master key
|
|
|
|
// is available for keyring reloading.
|
|
|
|
ReloadMasterKey() error
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Seal is used to re-seal the barrier. This requires the barrier to
|
|
|
|
// be unsealed again to perform any further operations.
|
|
|
|
Seal() error
|
|
|
|
|
2015-05-28 00:05:02 +00:00
|
|
|
// Rotate is used to create a new encryption key. All future writes
|
|
|
|
// should use the new key, while old values should still be decryptable.
|
2015-05-28 23:42:32 +00:00
|
|
|
Rotate() (uint32, error)
|
2015-05-28 00:05:02 +00:00
|
|
|
|
2015-05-28 23:42:32 +00:00
|
|
|
// CreateUpgrade creates an upgrade path key to the given term from the previous term
|
|
|
|
CreateUpgrade(term uint32) error
|
|
|
|
|
|
|
|
// DestroyUpgrade destroys the upgrade path key to the given term
|
|
|
|
DestroyUpgrade(term uint32) error
|
|
|
|
|
|
|
|
// CheckUpgrade looks for an upgrade to the current term and installs it
|
|
|
|
CheckUpgrade() (bool, uint32, error)
|
2015-05-28 22:52:26 +00:00
|
|
|
|
2015-05-28 00:25:36 +00:00
|
|
|
// ActiveKeyInfo is used to inform details about the active key
|
|
|
|
ActiveKeyInfo() (*KeyInfo, error)
|
|
|
|
|
2015-05-28 00:13:40 +00:00
|
|
|
// Rekey is used to change the master key used to protect the keyring
|
|
|
|
Rekey([]byte) error
|
|
|
|
|
2015-04-10 23:43:35 +00:00
|
|
|
// SecurityBarrier must provide the storage APIs
|
|
|
|
BarrierStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
// BarrierStorage is the storage only interface required for a Barrier.
|
|
|
|
type BarrierStorage interface {
|
2015-03-05 21:27:35 +00:00
|
|
|
// Put is used to insert or update an entry
|
|
|
|
Put(entry *Entry) error
|
|
|
|
|
|
|
|
// Get is used to fetch an entry
|
|
|
|
Get(key string) (*Entry, error)
|
|
|
|
|
|
|
|
// Delete is used to permanently delete an entry
|
|
|
|
Delete(key string) error
|
|
|
|
|
|
|
|
// List is used ot list all the keys under a given
|
|
|
|
// prefix, up to the next prefix.
|
|
|
|
List(prefix string) ([]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry is used to represent data stored by the security barrier
|
|
|
|
type Entry struct {
|
|
|
|
Key string
|
|
|
|
Value []byte
|
|
|
|
}
|
2015-03-15 20:52:43 +00:00
|
|
|
|
|
|
|
// Logical turns the Entry into a logical storage entry.
|
|
|
|
func (e *Entry) Logical() *logical.StorageEntry {
|
|
|
|
return &logical.StorageEntry{
|
|
|
|
Key: e.Key,
|
|
|
|
Value: e.Value,
|
|
|
|
}
|
|
|
|
}
|
2015-05-28 00:25:36 +00:00
|
|
|
|
|
|
|
// KeyInfo is used to convey information about the encryption key
|
|
|
|
type KeyInfo struct {
|
|
|
|
Term int
|
|
|
|
InstallTime time.Time
|
|
|
|
}
|