2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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"
|
|
|
|
|
2023-12-08 13:47:02 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2022-05-12 20:29:52 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2022-12-01 15:05:15 +00:00
|
|
|
"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-08-26 18:03:56 +00:00
|
|
|
// Keyring endpoint serves RPCs for root key management
|
2022-05-19 20:27:59 +00:00
|
|
|
type Keyring struct {
|
2022-12-01 15:05:15 +00:00
|
|
|
srv *Server
|
|
|
|
ctx *RPCContext
|
|
|
|
logger hclog.Logger
|
|
|
|
|
2022-05-12 20:29:52 +00:00
|
|
|
encrypter *Encrypter
|
2022-12-01 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewKeyringEndpoint(srv *Server, ctx *RPCContext, enc *Encrypter) *Keyring {
|
|
|
|
return &Keyring{srv: srv, ctx: ctx, logger: srv.logger.Named("keyring"), encrypter: enc}
|
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 {
|
2023-01-25 19:33:06 +00:00
|
|
|
|
|
|
|
authErr := k.srv.Authenticate(k.ctx, args)
|
2022-05-19 20:27:59 +00:00
|
|
|
if done, err := k.srv.forward("Keyring.Rotate", args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
k.srv.MeasureRPCRate("keyring", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "keyring", "rotate"}, time.Now())
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := k.srv.ResolveACL(args); err != nil {
|
2022-05-12 20:29:52 +00:00
|
|
|
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,
|
|
|
|
}
|
2023-03-02 18:51:09 +00:00
|
|
|
_, index, err := k.srv.raftApply(structs.RootKeyMetaUpsertRequestType, req)
|
2022-05-19 20:27:59 +00:00
|
|
|
if 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,
|
2022-08-26 18:03:56 +00:00
|
|
|
JobID: structs.CoreJobVariablesRekey,
|
2022-07-07 17:48:38 +00:00
|
|
|
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 {
|
2023-01-25 19:33:06 +00:00
|
|
|
|
|
|
|
authErr := k.srv.Authenticate(k.ctx, args)
|
2022-05-19 20:27:59 +00:00
|
|
|
if done, err := k.srv.forward("Keyring.List", args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
k.srv.MeasureRPCRate("keyring", structs.RateMetricList, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
|
|
|
|
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 {
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := k.srv.ResolveACL(args); err != nil {
|
2022-05-19 20:27:59 +00:00
|
|
|
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 {
|
2023-01-25 19:33:06 +00:00
|
|
|
|
|
|
|
authErr := k.srv.Authenticate(k.ctx, args)
|
2022-05-19 20:27:59 +00:00
|
|
|
if done, err := k.srv.forward("Keyring.Update", args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
k.srv.MeasureRPCRate("keyring", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "keyring", "update"}, time.Now())
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := k.srv.ResolveACL(args); err != nil {
|
2022-05-12 20:29:52 +00:00
|
|
|
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
|
2023-03-02 18:51:09 +00:00
|
|
|
_, index, err := k.srv.raftApply(structs.RootKeyMetaUpsertRequestType, metaReq)
|
2022-05-19 20:27:59 +00:00
|
|
|
if 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 {
|
2023-01-26 20:04:25 +00:00
|
|
|
|
|
|
|
authErr := k.srv.Authenticate(k.ctx, args)
|
|
|
|
|
2022-05-19 20:27:59 +00:00
|
|
|
// 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
|
|
|
|
}
|
2023-01-26 20:04:25 +00:00
|
|
|
k.srv.MeasureRPCRate("keyring", structs.RateMetricRead, args)
|
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-05-19 20:27:59 +00:00
|
|
|
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-10-21 16:33:16 +00:00
|
|
|
|
|
|
|
// Use the last index that affected the policy table
|
|
|
|
index, err := s.Index(state.TableRootKeyMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we never set the index to zero, otherwise a blocking query
|
|
|
|
// cannot be used. We floor the index at one, since realistically
|
|
|
|
// the first write must have a higher index.
|
|
|
|
if index == 0 {
|
|
|
|
index = 1
|
|
|
|
}
|
|
|
|
reply.Index = index
|
2022-05-19 20:27:59 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return k.srv.blockingRPC(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Keyring) Delete(args *structs.KeyringDeleteRootKeyRequest, reply *structs.KeyringDeleteRootKeyResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
|
|
|
|
authErr := k.srv.Authenticate(k.ctx, args)
|
2022-05-19 20:27:59 +00:00
|
|
|
if done, err := k.srv.forward("Keyring.Delete", args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
k.srv.MeasureRPCRate("keyring", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "keyring", "delete"}, time.Now())
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := k.srv.ResolveACL(args); err != nil {
|
2022-05-12 20:29:52 +00:00
|
|
|
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
|
2023-03-02 18:51:09 +00:00
|
|
|
_, index, err := k.srv.raftApply(structs.RootKeyMetaDeleteRequestType, args)
|
2022-05-19 20:27:59 +00:00
|
|
|
if 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
|
|
|
|
}
|