2022-05-12 20:29:52 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2022-05-19 20:27:59 +00:00
|
|
|
"fmt"
|
2022-05-12 20:29:52 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2022-05-19 20:27:59 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-07-07 17:48:38 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2022-05-19 20:27:59 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2022-05-12 20:29:52 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// Keyring endpoint serves RPCs for secure variables key management
|
|
|
|
type Keyring struct {
|
2022-05-12 20:29:52 +00:00
|
|
|
srv *Server
|
|
|
|
logger hclog.Logger
|
|
|
|
encrypter *Encrypter
|
2022-05-19 20:27:59 +00:00
|
|
|
ctx *RPCContext // context for connection, to check TLS role
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
func (k *Keyring) Rotate(args *structs.KeyringRotateRootKeyRequest, reply *structs.KeyringRotateRootKeyResponse) error {
|
|
|
|
if done, err := k.srv.forward("Keyring.Rotate", args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "keyring", "rotate"}, time.Now())
|
|
|
|
|
|
|
|
if aclObj, err := k.srv.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
if args.Algorithm == "" {
|
2022-06-02 17:41:59 +00:00
|
|
|
args.Algorithm = structs.EncryptionAlgorithmAES256GCM
|
2022-05-19 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 19:05:30 +00:00
|
|
|
rootKey, err := structs.NewRootKey(args.Algorithm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-19 20:27:59 +00:00
|
|
|
|
2022-07-07 17:48:38 +00:00
|
|
|
rootKey.Meta.SetActive()
|
2022-05-25 19:05:30 +00:00
|
|
|
|
|
|
|
// make sure it's been added to the local keystore before we write
|
|
|
|
// it to raft, so that followers don't try to Get a key that
|
|
|
|
// hasn't yet been written to disk
|
|
|
|
err = k.encrypter.AddKey(rootKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
2022-05-19 20:27:59 +00:00
|
|
|
|
|
|
|
// Update metadata via Raft so followers can retrieve this key
|
|
|
|
req := structs.KeyringUpdateRootKeyMetaRequest{
|
2022-05-25 19:05:30 +00:00
|
|
|
RootKeyMeta: rootKey.Meta,
|
2022-07-07 17:48:38 +00:00
|
|
|
Rekey: args.Full,
|
2022-05-19 20:27:59 +00:00
|
|
|
WriteRequest: args.WriteRequest,
|
|
|
|
}
|
|
|
|
out, index, err := k.srv.raftApply(structs.RootKeyMetaUpsertRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err, ok := out.(error); ok && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-25 19:05:30 +00:00
|
|
|
reply.Key = rootKey.Meta
|
2022-05-19 20:27:59 +00:00
|
|
|
reply.Index = index
|
2022-07-07 17:48:38 +00:00
|
|
|
|
|
|
|
if args.Full {
|
|
|
|
// like most core jobs, we don't commit this to raft b/c it's not
|
|
|
|
// going to be periodically recreated and the ACL is from this leader
|
|
|
|
eval := &structs.Evaluation{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Namespace: "-",
|
|
|
|
Priority: structs.CoreJobPriority,
|
|
|
|
Type: structs.JobTypeCore,
|
|
|
|
TriggeredBy: structs.EvalTriggerJobRegister,
|
|
|
|
JobID: structs.CoreJobSecureVariablesRekey,
|
|
|
|
Status: structs.EvalStatusPending,
|
|
|
|
ModifyIndex: index,
|
|
|
|
LeaderACL: k.srv.getLeaderAcl(),
|
|
|
|
}
|
|
|
|
k.srv.evalBroker.Enqueue(eval)
|
|
|
|
}
|
|
|
|
|
2022-05-12 20:29:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
func (k *Keyring) List(args *structs.KeyringListRootKeyMetaRequest, reply *structs.KeyringListRootKeyMetaResponse) error {
|
|
|
|
if done, err := k.srv.forward("Keyring.List", args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "keyring", "list"}, time.Now())
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// we need to allow both humans with management tokens and
|
|
|
|
// non-leader servers to list keys, in order to support
|
|
|
|
// replication
|
|
|
|
err := validateTLSCertificateLevel(k.srv, k.ctx, tlsCertificateLevelServer)
|
|
|
|
if err != nil {
|
|
|
|
if aclObj, err := k.srv.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, s *state.StateStore) error {
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// retrieve all the key metadata
|
|
|
|
snap, err := k.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
iter, err := snap.RootKeyMetas(ws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-20 15:48:13 +00:00
|
|
|
keys := []*structs.RootKeyMeta{}
|
2022-05-19 20:27:59 +00:00
|
|
|
for {
|
|
|
|
raw := iter.Next()
|
|
|
|
if raw == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
keyMeta := raw.(*structs.RootKeyMeta)
|
2022-06-20 15:48:13 +00:00
|
|
|
keys = append(keys, keyMeta)
|
2022-05-19 20:27:59 +00:00
|
|
|
}
|
2022-06-20 15:48:13 +00:00
|
|
|
reply.Keys = keys
|
2022-05-19 20:27:59 +00:00
|
|
|
return k.srv.replySetIndex(state.TableRootKeyMeta, &reply.QueryMeta)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return k.srv.blockingRPC(&opts)
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// Update updates an existing key in the keyring, including both the
|
|
|
|
// key material and metadata.
|
|
|
|
func (k *Keyring) Update(args *structs.KeyringUpdateRootKeyRequest, reply *structs.KeyringUpdateRootKeyResponse) error {
|
|
|
|
if done, err := k.srv.forward("Keyring.Update", args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "keyring", "update"}, time.Now())
|
|
|
|
|
|
|
|
if aclObj, err := k.srv.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
err := k.validateUpdate(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:05:30 +00:00
|
|
|
// make sure it's been added to the local keystore before we write
|
|
|
|
// it to raft, so that followers don't try to Get a key that
|
|
|
|
// hasn't yet been written to disk
|
|
|
|
err = k.encrypter.AddKey(args.RootKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// unwrap the request to turn it into a meta update only
|
|
|
|
metaReq := &structs.KeyringUpdateRootKeyMetaRequest{
|
|
|
|
RootKeyMeta: args.RootKey.Meta,
|
|
|
|
WriteRequest: args.WriteRequest,
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:05:30 +00:00
|
|
|
// update the metadata via Raft
|
2022-05-19 20:27:59 +00:00
|
|
|
out, index, err := k.srv.raftApply(structs.RootKeyMetaUpsertRequestType, metaReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err, ok := out.(error); ok && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-25 19:05:30 +00:00
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
reply.Index = index
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateUpdate validates both the request and that any change to an
|
|
|
|
// existing key is valid
|
|
|
|
func (k *Keyring) validateUpdate(args *structs.KeyringUpdateRootKeyRequest) error {
|
|
|
|
|
2022-05-25 19:05:30 +00:00
|
|
|
err := args.RootKey.Meta.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-19 20:27:59 +00:00
|
|
|
}
|
2022-05-25 19:05:30 +00:00
|
|
|
if len(args.RootKey.Key) == 0 {
|
|
|
|
return fmt.Errorf("root key material is required")
|
2022-05-19 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lookup any existing key and validate the update
|
|
|
|
snap, err := k.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
keyMeta, err := snap.RootKeyMetaByID(ws, args.RootKey.Meta.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if keyMeta != nil && keyMeta.Algorithm != args.RootKey.Meta.Algorithm {
|
|
|
|
return fmt.Errorf("root key algorithm cannot be changed after a key is created")
|
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// Get retrieves an existing key from the keyring, including both the
|
|
|
|
// key material and metadata. It is used only for replication.
|
|
|
|
func (k *Keyring) Get(args *structs.KeyringGetRootKeyRequest, reply *structs.KeyringGetRootKeyResponse) error {
|
|
|
|
// ensure that only another server can make this request
|
|
|
|
err := validateTLSCertificateLevel(k.srv, k.ctx, tlsCertificateLevelServer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := k.srv.forward("Keyring.Get", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "keyring", "get"}, time.Now())
|
|
|
|
|
|
|
|
if args.KeyID == "" {
|
|
|
|
return fmt.Errorf("root key ID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, s *state.StateStore) error {
|
|
|
|
|
|
|
|
// retrieve the key metadata
|
|
|
|
snap, err := k.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keyMeta, err := snap.RootKeyMetaByID(ws, args.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if keyMeta == nil {
|
|
|
|
return k.srv.replySetIndex(state.TableRootKeyMeta, &reply.QueryMeta)
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:05:30 +00:00
|
|
|
// retrieve the key material from the keyring
|
|
|
|
key, err := k.encrypter.GetKey(keyMeta.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rootKey := &structs.RootKey{
|
2022-05-19 20:27:59 +00:00
|
|
|
Meta: keyMeta,
|
2022-05-25 19:05:30 +00:00
|
|
|
Key: key,
|
2022-05-19 20:27:59 +00:00
|
|
|
}
|
2022-05-25 19:05:30 +00:00
|
|
|
reply.Key = rootKey
|
2022-05-19 20:27:59 +00:00
|
|
|
reply.Index = keyMeta.ModifyIndex
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return k.srv.blockingRPC(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Keyring) Delete(args *structs.KeyringDeleteRootKeyRequest, reply *structs.KeyringDeleteRootKeyResponse) error {
|
|
|
|
if done, err := k.srv.forward("Keyring.Delete", args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "keyring", "delete"}, time.Now())
|
|
|
|
|
|
|
|
if aclObj, err := k.srv.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
if args.KeyID == "" {
|
|
|
|
return fmt.Errorf("root key ID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup any existing key and validate the delete
|
|
|
|
snap, err := k.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
keyMeta, err := snap.RootKeyMetaByID(ws, args.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if keyMeta == nil {
|
|
|
|
return nil // safe to bail out early
|
|
|
|
}
|
2022-07-07 17:48:38 +00:00
|
|
|
if keyMeta.Active() {
|
2022-05-19 20:27:59 +00:00
|
|
|
return fmt.Errorf("active root key cannot be deleted - call rotate first")
|
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// update via Raft
|
|
|
|
out, index, err := k.srv.raftApply(structs.RootKeyMetaDeleteRequestType, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err, ok := out.(error); ok && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-25 19:05:30 +00:00
|
|
|
|
|
|
|
// remove the key from the keyring too
|
|
|
|
k.encrypter.RemoveKey(args.KeyID)
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
reply.Index = index
|
2022-05-12 20:29:52 +00:00
|
|
|
return nil
|
|
|
|
}
|