2022-05-12 20:29:52 +00:00
|
|
|
package structs
|
|
|
|
|
2022-05-19 18:19:56 +00:00
|
|
|
import (
|
2022-05-25 19:05:30 +00:00
|
|
|
"fmt"
|
2022-05-27 13:33:41 +00:00
|
|
|
"reflect"
|
2022-05-19 18:19:56 +00:00
|
|
|
"time"
|
|
|
|
|
2022-05-25 19:05:30 +00:00
|
|
|
// note: this is aliased so that it's more noticeable if someone
|
|
|
|
// accidentally swaps it out for math/rand via running goimports
|
|
|
|
cryptorand "crypto/rand"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/helper"
|
2022-05-19 18:19:56 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
)
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
const (
|
|
|
|
// SecureVariablesUpsertRPCMethod is the RPC method for upserting
|
|
|
|
// secure variables into Nomad state.
|
|
|
|
//
|
|
|
|
// Args: SecureVariablesUpsertRequest
|
|
|
|
// Reply: SecureVariablesUpsertResponse
|
|
|
|
SecureVariablesUpsertRPCMethod = "SecureVariables.Upsert"
|
|
|
|
|
|
|
|
// SecureVariablesDeleteRPCMethod is the RPC method for deleting
|
|
|
|
// a secure variable by its namespace and path.
|
|
|
|
//
|
|
|
|
// Args: SecureVariablesDeleteRequest
|
|
|
|
// Reply: SecureVariablesDeleteResponse
|
|
|
|
SecureVariablesDeleteRPCMethod = "SecureVariables.Delete"
|
|
|
|
|
|
|
|
// SecureVariablesListRPCMethod is the RPC method for listing secure
|
|
|
|
// variables within Nomad.
|
|
|
|
//
|
|
|
|
// Args: SecureVariablesListRequest
|
|
|
|
// Reply: SecureVariablesListResponse
|
|
|
|
SecureVariablesListRPCMethod = "SecureVariables.List"
|
|
|
|
|
|
|
|
// SecureVariablesGetServiceRPCMethod is the RPC method for fetching a
|
|
|
|
// secure variable according to its namepace and path.
|
|
|
|
//
|
|
|
|
// Args: SecureVariablesByNameRequest
|
|
|
|
// Reply: SecureVariablesByNameResponse
|
|
|
|
SecureVariablesReadRPCMethod = "SecureVariables.Read"
|
|
|
|
)
|
|
|
|
|
2022-05-12 20:29:52 +00:00
|
|
|
// SecureVariable is the metadata envelope for a Secure Variable
|
|
|
|
type SecureVariable struct {
|
|
|
|
Namespace string
|
|
|
|
Path string
|
|
|
|
CreateTime time.Time
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
ModifyTime time.Time
|
|
|
|
|
|
|
|
// reserved for post-1.4.0 work
|
|
|
|
// LockIndex uint64
|
|
|
|
// Session string
|
|
|
|
// DeletedAt time.Time
|
|
|
|
// Version uint64
|
|
|
|
// CustomMetaData map[string]string
|
|
|
|
|
2022-05-13 17:11:27 +00:00
|
|
|
EncryptedData *SecureVariableData `json:"-"` // removed during serialization
|
|
|
|
UnencryptedData map[string]string `json:"Items"` // empty until serialized
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SecureVariableData is the secret data for a Secure Variable
|
|
|
|
type SecureVariableData struct {
|
|
|
|
Data []byte // includes nonce
|
|
|
|
KeyID string // ID of root key used to encrypt this entry
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:11:27 +00:00
|
|
|
func (sv SecureVariableData) Copy() *SecureVariableData {
|
2022-05-27 13:33:41 +00:00
|
|
|
out := make([]byte, len(sv.Data))
|
2022-05-13 17:11:27 +00:00
|
|
|
copy(out, sv.Data)
|
|
|
|
return &SecureVariableData{
|
|
|
|
Data: out,
|
|
|
|
KeyID: sv.KeyID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *SecureVariable) Copy() *SecureVariable {
|
|
|
|
if sv == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out := *sv
|
|
|
|
if sv.UnencryptedData != nil {
|
|
|
|
out.UnencryptedData = make(map[string]string, len(sv.UnencryptedData))
|
|
|
|
for k, v := range sv.UnencryptedData {
|
|
|
|
out.UnencryptedData[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sv.EncryptedData != nil {
|
|
|
|
out.EncryptedData = sv.EncryptedData.Copy()
|
|
|
|
}
|
|
|
|
return &out
|
|
|
|
}
|
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
func (sv SecureVariable) Equals(sv2 SecureVariable) bool {
|
|
|
|
// FIXME: This should be a smarter equality check
|
|
|
|
return reflect.DeepEqual(sv, sv2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv SecureVariable) Validate() error {
|
|
|
|
// FIXME: Placeholder for more extensive validation
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *SecureVariable) Canonicalize() {
|
|
|
|
if sv.Namespace == "" {
|
|
|
|
sv.Namespace = DefaultNamespace
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNamespace returns the secure variable's namespace. Used for pagination.
|
|
|
|
func (sv SecureVariable) GetNamespace() string {
|
|
|
|
return sv.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the secure variable's path. Used for pagination.
|
|
|
|
func (sv SecureVariable) GetID() string {
|
|
|
|
return sv.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCreateIndex returns the secure variable's create index. Used for pagination.
|
|
|
|
func (sv SecureVariable) GetCreateIndex() uint64 {
|
|
|
|
return sv.CreateIndex
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:11:27 +00:00
|
|
|
func (sv SecureVariable) Stub() SecureVariableStub {
|
|
|
|
return SecureVariableStub{
|
|
|
|
Namespace: sv.Namespace,
|
|
|
|
Path: sv.Path,
|
|
|
|
CreateIndex: sv.CreateIndex,
|
|
|
|
CreateTime: sv.CreateTime,
|
|
|
|
ModifyIndex: sv.ModifyIndex,
|
|
|
|
ModifyTime: sv.ModifyTime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SecureVariableStub is the metadata envelope for a Secure Variable omitting
|
|
|
|
// the actual data. Intended to be used in list operations.
|
|
|
|
type SecureVariableStub struct {
|
|
|
|
Namespace string
|
|
|
|
Path string
|
|
|
|
CreateTime time.Time
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
ModifyTime time.Time
|
|
|
|
|
|
|
|
// reserved for post-1.4.0 work
|
|
|
|
// LockIndex uint64
|
|
|
|
// Session string
|
|
|
|
// DeletedAt time.Time
|
|
|
|
// Version uint64
|
|
|
|
// CustomMetaData map[string]string
|
|
|
|
}
|
|
|
|
|
2022-05-12 20:29:52 +00:00
|
|
|
// SecureVariablesQuota is used to track the total size of secure
|
|
|
|
// variables entries per namespace. The total length of
|
|
|
|
// SecureVariable.EncryptedData will be added to the SecureVariablesQuota
|
|
|
|
// table in the same transaction as a write, update, or delete.
|
|
|
|
type SecureVariablesQuota struct {
|
|
|
|
Namespace string
|
|
|
|
Size uint64
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecureVariablesUpsertRequest struct {
|
2022-05-27 13:33:41 +00:00
|
|
|
Data []*SecureVariable
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecureVariableUpsertRequest struct {
|
2022-05-12 20:29:52 +00:00
|
|
|
Data *SecureVariable
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecureVariablesUpsertResponse struct {
|
|
|
|
WriteMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecureVariablesListRequest struct {
|
|
|
|
// TODO: do we need any fields here?
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecureVariablesListResponse struct {
|
2022-05-13 17:11:27 +00:00
|
|
|
Data []*SecureVariableStub
|
2022-05-12 20:29:52 +00:00
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecureVariablesReadRequest struct {
|
|
|
|
Path string
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecureVariablesReadResponse struct {
|
|
|
|
Data *SecureVariable
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecureVariablesDeleteRequest struct {
|
|
|
|
Path string
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecureVariablesDeleteResponse struct {
|
|
|
|
WriteMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootKey is used to encrypt and decrypt secure variables. It is
|
|
|
|
// never stored in raft.
|
|
|
|
type RootKey struct {
|
2022-05-19 20:27:59 +00:00
|
|
|
Meta *RootKeyMeta
|
2022-05-12 20:29:52 +00:00
|
|
|
Key []byte // serialized to keystore as base64 blob
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:05:30 +00:00
|
|
|
// NewRootKey returns a new root key and its metadata.
|
|
|
|
func NewRootKey(algorithm EncryptionAlgorithm) (*RootKey, error) {
|
|
|
|
meta := NewRootKeyMeta()
|
|
|
|
meta.Algorithm = algorithm
|
|
|
|
|
|
|
|
rootKey := &RootKey{
|
|
|
|
Meta: meta,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch algorithm {
|
|
|
|
case EncryptionAlgorithmAES256GCM:
|
|
|
|
key := make([]byte, 32)
|
|
|
|
if _, err := cryptorand.Read(key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rootKey.Key = key
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootKey, nil
|
|
|
|
}
|
|
|
|
|
2022-05-12 20:29:52 +00:00
|
|
|
// RootKeyMeta is the metadata used to refer to a RootKey. It is
|
|
|
|
// stored in raft.
|
|
|
|
type RootKeyMeta struct {
|
2022-06-02 17:41:59 +00:00
|
|
|
Active bool
|
|
|
|
KeyID string // UUID
|
|
|
|
Algorithm EncryptionAlgorithm
|
|
|
|
CreateTime time.Time
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 18:19:56 +00:00
|
|
|
// NewRootKeyMeta returns a new RootKeyMeta with default values
|
|
|
|
func NewRootKeyMeta() *RootKeyMeta {
|
|
|
|
return &RootKeyMeta{
|
|
|
|
KeyID: uuid.Generate(),
|
2022-06-02 17:41:59 +00:00
|
|
|
Algorithm: EncryptionAlgorithmAES256GCM,
|
2022-05-19 18:19:56 +00:00
|
|
|
CreateTime: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:05:30 +00:00
|
|
|
// RootKeyMetaStub is for serializing root key metadata to the
|
|
|
|
// keystore, not for the List API. It excludes frequently-changing
|
2022-06-02 17:41:59 +00:00
|
|
|
// fields such as ModifyIndex so we don't have to sync them to the
|
|
|
|
// on-disk keystore when the fields are already in raft.
|
2022-05-25 19:05:30 +00:00
|
|
|
type RootKeyMetaStub struct {
|
|
|
|
KeyID string
|
|
|
|
Algorithm EncryptionAlgorithm
|
|
|
|
CreateTime time.Time
|
|
|
|
Active bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rkm *RootKeyMeta) Stub() *RootKeyMetaStub {
|
|
|
|
if rkm == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &RootKeyMetaStub{
|
|
|
|
KeyID: rkm.KeyID,
|
|
|
|
Algorithm: rkm.Algorithm,
|
|
|
|
CreateTime: rkm.CreateTime,
|
|
|
|
Active: rkm.Active,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-05-19 18:19:56 +00:00
|
|
|
func (rkm *RootKeyMeta) Copy() *RootKeyMeta {
|
|
|
|
if rkm == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out := *rkm
|
|
|
|
return &out
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:05:30 +00:00
|
|
|
func (rkm *RootKeyMeta) Validate() error {
|
|
|
|
if rkm == nil {
|
|
|
|
return fmt.Errorf("root key metadata is required")
|
|
|
|
}
|
|
|
|
if rkm.KeyID == "" || !helper.IsUUID(rkm.KeyID) {
|
|
|
|
return fmt.Errorf("root key UUID is required")
|
|
|
|
}
|
|
|
|
if rkm.Algorithm == "" {
|
|
|
|
return fmt.Errorf("root key algorithm is required")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-12 20:29:52 +00:00
|
|
|
// EncryptionAlgorithm chooses which algorithm is used for
|
|
|
|
// encrypting / decrypting entries with this key
|
|
|
|
type EncryptionAlgorithm string
|
|
|
|
|
|
|
|
const (
|
|
|
|
EncryptionAlgorithmAES256GCM EncryptionAlgorithm = "aes256-gcm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type KeyringRotateRootKeyRequest struct {
|
|
|
|
Algorithm EncryptionAlgorithm
|
|
|
|
Full bool
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyringRotateRootKeyResponse returns the full key metadata
|
|
|
|
type KeyringRotateRootKeyResponse struct {
|
|
|
|
Key *RootKeyMeta
|
|
|
|
WriteMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyringListRootKeyMetaRequest struct {
|
|
|
|
// TODO: do we need any fields here?
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyringListRootKeyMetaResponse struct {
|
|
|
|
Keys []*RootKeyMeta
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyringUpdateRootKeyRequest is used internally for key replication
|
|
|
|
// only and for keyring restores. The RootKeyMeta will be extracted
|
|
|
|
// for applying to the FSM with the KeyringUpdateRootKeyMetaRequest
|
|
|
|
// (see below)
|
|
|
|
type KeyringUpdateRootKeyRequest struct {
|
|
|
|
RootKey *RootKey
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyringUpdateRootKeyResponse struct {
|
|
|
|
WriteMeta
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// KeyringGetRootKeyRequest is used internally for key replication
|
|
|
|
// only and for keyring restores.
|
|
|
|
type KeyringGetRootKeyRequest struct {
|
|
|
|
KeyID string
|
|
|
|
QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyringGetRootKeyResponse struct {
|
|
|
|
Key *RootKey
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2022-05-12 20:29:52 +00:00
|
|
|
// KeyringUpdateRootKeyMetaRequest is used internally for key
|
|
|
|
// replication so that we have a request wrapper for writing the
|
|
|
|
// metadata to the FSM without including the key material
|
|
|
|
type KeyringUpdateRootKeyMetaRequest struct {
|
|
|
|
RootKeyMeta *RootKeyMeta
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyringUpdateRootKeyMetaResponse struct {
|
|
|
|
WriteMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyringDeleteRootKeyRequest struct {
|
|
|
|
KeyID string
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyringDeleteRootKeyResponse struct {
|
|
|
|
WriteMeta
|
|
|
|
}
|