2022-05-12 20:29:52 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2022-05-27 13:33:41 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-06-10 13:41:54 +00:00
|
|
|
"strings"
|
2022-05-12 20:29:52 +00:00
|
|
|
"time"
|
|
|
|
|
2022-12-01 15:05:15 +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-08-15 15:19:53 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
|
|
|
"github.com/hashicorp/nomad/nomad/state/paginator"
|
2022-05-12 20:29:52 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Variables encapsulates the variables RPC endpoint which is
|
|
|
|
// callable via the Variables RPCs and externally via the "/v1/var{s}"
|
2022-05-27 13:33:41 +00:00
|
|
|
// HTTP API.
|
2022-08-26 18:03:56 +00:00
|
|
|
type Variables 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 NewVariablesEndpoint(srv *Server, ctx *RPCContext, enc *Encrypter) *Variables {
|
|
|
|
return &Variables{srv: srv, ctx: ctx, logger: srv.logger.Named("variables"), encrypter: enc}
|
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
// Apply is used to apply a SV update request to the data store.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) Apply(args *structs.VariablesApplyRequest, reply *structs.VariablesApplyResponse) error {
|
2023-01-24 15:52:07 +00:00
|
|
|
|
|
|
|
authErr := sv.srv.Authenticate(sv.ctx, args)
|
2022-08-26 18:03:56 +00:00
|
|
|
if done, err := sv.srv.forward(structs.VariablesApplyRPCMethod, args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-24 15:52:07 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return authErr
|
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
defer metrics.MeasureSince([]string{
|
2022-08-26 18:03:56 +00:00
|
|
|
"nomad", "variables", "apply", string(args.Op)}, time.Now())
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Check if the Namespace is explicitly set on the variable. If
|
2022-08-15 15:19:53 +00:00
|
|
|
// not, use the RequestNamespace
|
|
|
|
if args.Var == nil {
|
|
|
|
return fmt.Errorf("variable must not be nil")
|
|
|
|
}
|
|
|
|
targetNS := args.Var.Namespace
|
|
|
|
if targetNS == "" {
|
|
|
|
targetNS = args.RequestNamespace()
|
|
|
|
args.Var.Namespace = targetNS
|
2022-06-14 17:28:10 +00:00
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-10-17 20:23:51 +00:00
|
|
|
if !ServersMeetMinimumVersion(
|
|
|
|
sv.srv.serf.Members(), sv.srv.Region(), minVersionKeyring, true) {
|
|
|
|
return fmt.Errorf("all servers must be running version %v or later to apply variables", minVersionKeyring)
|
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
canRead, err := svePreApply(sv, args, args.Var)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-12 15:15:57 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var ev *structs.VariableEncrypted
|
2022-08-02 13:32:09 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
switch args.Op {
|
2022-08-26 18:03:56 +00:00
|
|
|
case structs.VarOpSet, structs.VarOpCAS:
|
2022-08-15 15:19:53 +00:00
|
|
|
ev, err = sv.encrypt(args.Var)
|
2022-08-02 13:32:09 +00:00
|
|
|
if err != nil {
|
2022-08-26 18:03:56 +00:00
|
|
|
return fmt.Errorf("variable error: encrypt: %w", err)
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
now := time.Now().UnixNano()
|
|
|
|
ev.CreateTime = now // existing will override if it exists
|
|
|
|
ev.ModifyTime = now
|
2022-08-26 18:03:56 +00:00
|
|
|
case structs.VarOpDelete, structs.VarOpDeleteCAS:
|
|
|
|
ev = &structs.VariableEncrypted{
|
|
|
|
VariableMetadata: structs.VariableMetadata{
|
2022-08-15 15:19:53 +00:00
|
|
|
Namespace: args.Var.Namespace,
|
|
|
|
Path: args.Var.Path,
|
|
|
|
ModifyIndex: args.Var.ModifyIndex,
|
|
|
|
},
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// Make a SVEArgs
|
2022-08-26 18:03:56 +00:00
|
|
|
sveArgs := structs.VarApplyStateRequest{
|
2022-08-15 15:19:53 +00:00
|
|
|
Op: args.Op,
|
|
|
|
Var: ev,
|
|
|
|
WriteRequest: args.WriteRequest,
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
2022-05-13 17:11:27 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
// Apply the update.
|
2022-08-26 18:03:56 +00:00
|
|
|
out, index, err := sv.srv.raftApply(structs.VarApplyStateRequestType, sveArgs)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
2022-08-15 15:19:53 +00:00
|
|
|
return fmt.Errorf("raft apply failed: %w", err)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
r, err := sv.makeVariablesApplyResponse(args, out.(*structs.VarApplyStateResponse), canRead)
|
2022-08-15 15:19:53 +00:00
|
|
|
if err != nil {
|
2022-05-27 13:33:41 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
*reply = *r
|
2022-05-27 13:33:41 +00:00
|
|
|
reply.Index = index
|
2022-05-12 20:29:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func svePreApply(sv *Variables, args *structs.VariablesApplyRequest, vd *structs.VariableDecrypted) (canRead bool, err error) {
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
canRead = false
|
|
|
|
var aclObj *acl.ACL
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
// Perform the ACL token resolution.
|
2022-08-15 15:19:53 +00:00
|
|
|
if aclObj, err = sv.srv.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return
|
2022-05-27 13:33:41 +00:00
|
|
|
} else if aclObj != nil {
|
2022-08-15 15:19:53 +00:00
|
|
|
hasPerm := func(perm string) bool {
|
2022-08-26 18:03:56 +00:00
|
|
|
return aclObj.AllowVariableOperation(args.Var.Namespace,
|
2022-08-15 15:19:53 +00:00
|
|
|
args.Var.Path, perm)
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
canRead = hasPerm(acl.VariablesCapabilityRead)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
switch args.Op {
|
2022-08-26 18:03:56 +00:00
|
|
|
case structs.VarOpSet, structs.VarOpCAS:
|
|
|
|
if !hasPerm(acl.VariablesCapabilityWrite) {
|
2022-08-15 15:19:53 +00:00
|
|
|
err = structs.ErrPermissionDenied
|
|
|
|
return
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
case structs.VarOpDelete, structs.VarOpDeleteCAS:
|
|
|
|
if !hasPerm(acl.VariablesCapabilityDestroy) {
|
2022-08-15 15:19:53 +00:00
|
|
|
err = structs.ErrPermissionDenied
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
2022-08-26 18:03:56 +00:00
|
|
|
err = fmt.Errorf("svPreApply: unexpected VarOp received: %q", args.Op)
|
2022-08-15 15:19:53 +00:00
|
|
|
return
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
} else {
|
|
|
|
// ACLs are not enabled.
|
|
|
|
canRead = true
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
switch args.Op {
|
2022-08-26 18:03:56 +00:00
|
|
|
case structs.VarOpSet, structs.VarOpCAS:
|
2022-08-15 15:19:53 +00:00
|
|
|
args.Var.Canonicalize()
|
|
|
|
if err = args.Var.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-06-27 19:51:01 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
case structs.VarOpDelete, structs.VarOpDeleteCAS:
|
2022-08-15 15:19:53 +00:00
|
|
|
if args.Var == nil || args.Var.Path == "" {
|
|
|
|
err = fmt.Errorf("delete requires a Path")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 19:51:01 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// MakeVariablesApplyResponse merges the output of this VarApplyStateResponse with the
|
|
|
|
// VariableDataItems
|
|
|
|
func (sv *Variables) makeVariablesApplyResponse(
|
|
|
|
req *structs.VariablesApplyRequest, eResp *structs.VarApplyStateResponse,
|
|
|
|
canRead bool) (*structs.VariablesApplyResponse, error) {
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
out := structs.VariablesApplyResponse{
|
2022-08-15 15:19:53 +00:00
|
|
|
Op: eResp.Op,
|
|
|
|
Input: req.Var,
|
|
|
|
Result: eResp.Result,
|
|
|
|
Error: eResp.Error,
|
|
|
|
WriteMeta: eResp.WriteMeta,
|
|
|
|
}
|
|
|
|
|
|
|
|
if eResp.IsOk() {
|
|
|
|
if eResp.WrittenSVMeta != nil {
|
|
|
|
// The writer is allowed to read their own write
|
2022-08-26 18:03:56 +00:00
|
|
|
out.Output = &structs.VariableDecrypted{
|
|
|
|
VariableMetadata: *eResp.WrittenSVMeta,
|
|
|
|
Items: req.Var.Items.Copy(),
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
return &out, nil
|
2022-06-27 19:51:01 +00:00
|
|
|
}
|
2022-09-14 17:14:17 +00:00
|
|
|
if eResp.IsError() {
|
|
|
|
return &out, eResp.Error
|
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// At this point, the response is necessarily a conflict.
|
|
|
|
// Prime output from the encrypted responses metadata
|
2022-08-26 18:03:56 +00:00
|
|
|
out.Conflict = &structs.VariableDecrypted{
|
|
|
|
VariableMetadata: eResp.Conflict.VariableMetadata,
|
|
|
|
Items: nil,
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
// If the caller can't read the conflicting value, return the
|
|
|
|
// metadata, but no items and flag it as redacted
|
|
|
|
if !canRead {
|
2022-08-26 18:03:56 +00:00
|
|
|
out.Result = structs.VarOpResultRedacted
|
2022-08-15 15:19:53 +00:00
|
|
|
return &out, nil
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
if eResp.Conflict == nil || eResp.Conflict.KeyID == "" {
|
|
|
|
// zero-value conflicts can be returned for delete-if-set
|
2022-08-26 18:03:56 +00:00
|
|
|
dv := &structs.VariableDecrypted{}
|
2022-08-15 15:19:53 +00:00
|
|
|
dv.Namespace = eResp.Conflict.Namespace
|
|
|
|
dv.Path = eResp.Conflict.Path
|
|
|
|
out.Conflict = dv
|
|
|
|
} else {
|
|
|
|
// At this point, the caller has read access to the conflicting
|
|
|
|
// value so we can return it in the output; decrypt it.
|
|
|
|
dv, err := sv.decrypt(eResp.Conflict)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out.Conflict = dv
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Read is used to get a specific variable
|
|
|
|
func (sv *Variables) Read(args *structs.VariablesReadRequest, reply *structs.VariablesReadResponse) error {
|
2023-01-24 15:52:07 +00:00
|
|
|
|
|
|
|
authErr := sv.srv.Authenticate(sv.ctx, args)
|
2022-08-26 18:03:56 +00:00
|
|
|
if done, err := sv.srv.forward(structs.VariablesReadRPCMethod, args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-24 15:52:07 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return authErr
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "variables", "read"}, time.Now())
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-10-18 20:43:59 +00:00
|
|
|
_, _, err := sv.handleMixedAuthEndpoint(args.QueryOptions,
|
2022-06-20 15:21:03 +00:00
|
|
|
acl.PolicyRead, args.Path)
|
2022-06-10 13:41:54 +00:00
|
|
|
if err != nil {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, s *state.StateStore) error {
|
2022-08-26 18:03:56 +00:00
|
|
|
out, err := s.GetVariable(ws, args.RequestNamespace(), args.Path)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
// Setup the output
|
|
|
|
reply.Data = nil
|
|
|
|
if out != nil {
|
2022-06-14 17:28:10 +00:00
|
|
|
dv, err := sv.decrypt(out)
|
|
|
|
if err != nil {
|
2022-05-27 13:33:41 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-06-14 17:28:10 +00:00
|
|
|
ov := dv.Copy()
|
|
|
|
reply.Data = &ov
|
2022-05-27 13:33:41 +00:00
|
|
|
reply.Index = out.ModifyIndex
|
|
|
|
} else {
|
2022-09-09 21:55:20 +00:00
|
|
|
sv.srv.setReplyQueryMeta(s, state.TableVariables, &reply.QueryMeta)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
return sv.srv.blockingRPC(&opts)
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// List is used to list variables held within state. It supports single
|
2022-05-27 13:33:41 +00:00
|
|
|
// and wildcard namespace listings.
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) List(
|
|
|
|
args *structs.VariablesListRequest,
|
|
|
|
reply *structs.VariablesListResponse) error {
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2023-01-24 15:52:07 +00:00
|
|
|
authErr := sv.srv.Authenticate(sv.ctx, args)
|
2022-08-26 18:03:56 +00:00
|
|
|
if done, err := sv.srv.forward(structs.VariablesListRPCMethod, args, args, reply); done {
|
2022-05-12 20:29:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-24 15:52:07 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return authErr
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "variables", "list"}, time.Now())
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// If the caller has requested to list variables across all namespaces, use
|
2022-05-27 13:33:41 +00:00
|
|
|
// the custom function to perform this.
|
|
|
|
if args.RequestNamespace() == structs.AllNamespacesSentinel {
|
2022-08-26 18:03:56 +00:00
|
|
|
return sv.listAllVariables(args, reply)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2023-01-24 15:52:07 +00:00
|
|
|
var aclObj *acl.ACL
|
|
|
|
var err error
|
|
|
|
aclToken := args.GetIdentity().GetACLToken()
|
|
|
|
if aclToken != nil {
|
|
|
|
aclObj, err = sv.srv.ResolveACL(args.GetIdentity().GetACLToken())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-10 13:41:54 +00:00
|
|
|
}
|
2023-01-24 15:52:07 +00:00
|
|
|
claims := args.GetIdentity().GetClaims()
|
2022-05-12 20:29:52 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
// Set up and return the blocking query.
|
|
|
|
return sv.srv.blockingRPC(&blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, stateStore *state.StateStore) error {
|
2022-05-13 17:11:27 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
// Perform the state query to get an iterator.
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := stateStore.GetVariablesByNamespaceAndPrefix(ws, args.RequestNamespace(), args.Prefix)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the tokenizer to use for pagination using namespace and
|
|
|
|
// ID to ensure complete uniqueness.
|
|
|
|
tokenizer := paginator.NewStructsTokenizer(iter,
|
|
|
|
paginator.StructsTokenizerOptions{
|
|
|
|
WithNamespace: true,
|
|
|
|
WithID: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-08-15 15:38:20 +00:00
|
|
|
filters := []paginator.Filter{
|
|
|
|
paginator.GenericFilter{
|
|
|
|
Allow: func(raw interface{}) (bool, error) {
|
2022-10-18 20:43:59 +00:00
|
|
|
v := raw.(*structs.VariableEncrypted)
|
|
|
|
if !strings.HasPrefix(v.Path, args.Prefix) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
err := sv.authorize(aclObj, claims, v.Namespace, acl.PolicyList, v.Path)
|
|
|
|
return err == nil, nil
|
2022-08-15 15:38:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
// Set up our output after we have checked the error.
|
2022-08-26 18:03:56 +00:00
|
|
|
var svs []*structs.VariableMetadata
|
2022-05-27 13:33:41 +00:00
|
|
|
|
|
|
|
// Build the paginator. This includes the function that is
|
2022-08-26 18:03:56 +00:00
|
|
|
// responsible for appending a variable to the variables
|
2022-05-27 13:33:41 +00:00
|
|
|
// stubs slice.
|
2022-08-15 15:38:20 +00:00
|
|
|
paginatorImpl, err := paginator.NewPaginator(iter, tokenizer, filters, args.QueryOptions,
|
2022-05-27 13:33:41 +00:00
|
|
|
func(raw interface{}) error {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
|
|
|
svStub := sv.VariableMetadata
|
2022-05-27 13:33:41 +00:00
|
|
|
svs = append(svs, &svStub)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCodedf(
|
|
|
|
http.StatusBadRequest, "failed to create result paginator: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling page populates our output variable stub array as well as
|
|
|
|
// returns the next token.
|
|
|
|
nextToken, err := paginatorImpl.Page()
|
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCodedf(
|
|
|
|
http.StatusBadRequest, "failed to read result page: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the reply.
|
|
|
|
reply.Data = svs
|
|
|
|
reply.NextToken = nextToken
|
|
|
|
|
|
|
|
// Use the index table to populate the query meta as we have no way
|
|
|
|
// of tracking the max index on deletes.
|
2022-08-26 18:03:56 +00:00
|
|
|
return sv.srv.setReplyQueryMeta(stateStore, state.TableVariables, &reply.QueryMeta)
|
2022-05-27 13:33:41 +00:00
|
|
|
},
|
|
|
|
})
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// listAllVariables is used to list variables held within
|
2022-05-27 13:33:41 +00:00
|
|
|
// state where the caller has used the namespace wildcard identifier.
|
2022-10-18 20:43:59 +00:00
|
|
|
func (sv *Variables) listAllVariables(
|
2022-08-26 18:03:56 +00:00
|
|
|
args *structs.VariablesListRequest,
|
|
|
|
reply *structs.VariablesListResponse) error {
|
2022-05-27 13:33:41 +00:00
|
|
|
|
|
|
|
// Perform token resolution. The request already goes through forwarding
|
|
|
|
// and metrics setup before being called.
|
2023-01-24 15:52:07 +00:00
|
|
|
var aclObj *acl.ACL
|
|
|
|
var err error
|
|
|
|
aclToken := args.GetIdentity().GetACLToken()
|
|
|
|
if aclToken != nil {
|
|
|
|
aclObj, err = sv.srv.ResolveACL(args.GetIdentity().GetACLToken())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
2023-01-24 15:52:07 +00:00
|
|
|
claims := args.GetIdentity().GetClaims()
|
2022-05-13 17:11:27 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
// Set up and return the blocking query.
|
2022-10-18 20:43:59 +00:00
|
|
|
return sv.srv.blockingRPC(&blockingOptions{
|
2022-05-27 13:33:41 +00:00
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, stateStore *state.StateStore) error {
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Get all the variables stored within state.
|
|
|
|
iter, err := stateStore.Variables(ws)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var svs []*structs.VariableMetadata
|
2022-05-27 13:33:41 +00:00
|
|
|
|
|
|
|
// Generate the tokenizer to use for pagination using namespace and
|
|
|
|
// ID to ensure complete uniqueness.
|
|
|
|
tokenizer := paginator.NewStructsTokenizer(iter,
|
|
|
|
paginator.StructsTokenizerOptions{
|
|
|
|
WithNamespace: true,
|
|
|
|
WithID: true,
|
2022-10-18 20:43:59 +00:00
|
|
|
})
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-15 15:38:20 +00:00
|
|
|
filters := []paginator.Filter{
|
|
|
|
paginator.GenericFilter{
|
|
|
|
Allow: func(raw interface{}) (bool, error) {
|
2022-10-18 20:43:59 +00:00
|
|
|
v := raw.(*structs.VariableEncrypted)
|
|
|
|
if !strings.HasPrefix(v.Path, args.Prefix) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
err := sv.authorize(aclObj, claims, v.Namespace, acl.PolicyList, v.Path)
|
|
|
|
return err == nil, nil
|
2022-08-15 15:38:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-07-12 16:49:39 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
// Build the paginator. This includes the function that is
|
|
|
|
// responsible for appending a variable to the stubs array.
|
2022-08-15 15:38:20 +00:00
|
|
|
paginatorImpl, err := paginator.NewPaginator(iter, tokenizer, filters, args.QueryOptions,
|
2022-05-27 13:33:41 +00:00
|
|
|
func(raw interface{}) error {
|
2022-10-18 20:43:59 +00:00
|
|
|
v := raw.(*structs.VariableEncrypted)
|
|
|
|
svStub := v.VariableMetadata
|
2022-05-27 13:33:41 +00:00
|
|
|
svs = append(svs, &svStub)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCodedf(
|
|
|
|
http.StatusBadRequest, "failed to create result paginator: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling page populates our output variable stubs array as well as
|
|
|
|
// returns the next token.
|
|
|
|
nextToken, err := paginatorImpl.Page()
|
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCodedf(
|
|
|
|
http.StatusBadRequest, "failed to read result page: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the reply.
|
|
|
|
reply.Data = svs
|
|
|
|
reply.NextToken = nextToken
|
|
|
|
|
|
|
|
// Use the index table to populate the query meta as we have no way
|
|
|
|
// of tracking the max index on deletes.
|
2022-10-18 20:43:59 +00:00
|
|
|
return sv.srv.setReplyQueryMeta(stateStore, state.TableVariables, &reply.QueryMeta)
|
2022-05-27 13:33:41 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) encrypt(v *structs.VariableDecrypted) (*structs.VariableEncrypted, error) {
|
2022-06-14 17:28:10 +00:00
|
|
|
b, err := json.Marshal(v.Items)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
2022-06-14 17:28:10 +00:00
|
|
|
return nil, err
|
2022-05-13 17:11:27 +00:00
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
ev := structs.VariableEncrypted{
|
|
|
|
VariableMetadata: v.VariableMetadata,
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-06-20 15:06:44 +00:00
|
|
|
ev.Data, ev.KeyID, err = sv.encrypter.Encrypt(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-14 17:28:10 +00:00
|
|
|
return &ev, nil
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-05-13 17:11:27 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) decrypt(v *structs.VariableEncrypted) (*structs.VariableDecrypted, error) {
|
2022-06-14 17:28:10 +00:00
|
|
|
b, err := sv.encrypter.Decrypt(v.Data, v.KeyID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
dv := structs.VariableDecrypted{
|
|
|
|
VariableMetadata: v.VariableMetadata,
|
2022-06-14 17:28:10 +00:00
|
|
|
}
|
|
|
|
dv.Items = make(map[string]string)
|
|
|
|
err = json.Unmarshal(b, &dv.Items)
|
2022-05-27 13:33:41 +00:00
|
|
|
if err != nil {
|
2022-06-14 17:28:10 +00:00
|
|
|
return nil, err
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-06-14 17:28:10 +00:00
|
|
|
return &dv, nil
|
2022-05-12 20:29:52 +00:00
|
|
|
}
|
2022-06-10 13:41:54 +00:00
|
|
|
|
|
|
|
// handleMixedAuthEndpoint is a helper to handle auth on RPC endpoints that can
|
|
|
|
// either be called by external clients or by workload identity
|
2022-10-18 20:43:59 +00:00
|
|
|
func (sv *Variables) handleMixedAuthEndpoint(args structs.QueryOptions, cap, pathOrPrefix string) (*acl.ACL, *structs.IdentityClaims, error) {
|
|
|
|
|
2023-01-24 15:52:07 +00:00
|
|
|
var aclObj *acl.ACL
|
|
|
|
var err error
|
|
|
|
aclToken := args.GetIdentity().GetACLToken()
|
|
|
|
if aclToken != nil {
|
|
|
|
aclObj, err = sv.srv.ResolveACL(args.GetIdentity().GetACLToken())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-10-18 20:43:59 +00:00
|
|
|
}
|
2023-01-24 15:52:07 +00:00
|
|
|
claims := args.GetIdentity().GetClaims()
|
|
|
|
|
2022-10-18 20:43:59 +00:00
|
|
|
err = sv.authorize(aclObj, claims, args.RequestNamespace(), cap, pathOrPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return aclObj, claims, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return aclObj, claims, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sv *Variables) authorize(aclObj *acl.ACL, claims *structs.IdentityClaims, ns, cap, pathOrPrefix string) error {
|
|
|
|
|
|
|
|
if aclObj == nil && claims == nil {
|
|
|
|
return nil // ACLs aren't enabled
|
2022-06-10 13:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 20:43:59 +00:00
|
|
|
// Perform normal ACL validation. If the ACL object is nil, that means we're
|
|
|
|
// working with an identity claim.
|
|
|
|
if aclObj != nil {
|
|
|
|
if !aclObj.AllowVariableOperation(ns, pathOrPrefix, cap) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
return nil
|
2022-06-20 15:21:03 +00:00
|
|
|
}
|
2022-10-18 20:43:59 +00:00
|
|
|
|
|
|
|
if claims != nil {
|
|
|
|
// The workload identity gets access to paths that match its
|
|
|
|
// identity, without having to go thru the ACL system
|
|
|
|
err := sv.authValidatePrefix(claims, ns, pathOrPrefix)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the workload identity doesn't match the implicit permissions
|
|
|
|
// given to paths, check for its attached ACL policies
|
|
|
|
aclObj, err = sv.srv.ResolveClaims(claims)
|
|
|
|
if err != nil {
|
|
|
|
return err // this only returns an error when the state store has gone wrong
|
|
|
|
}
|
|
|
|
if aclObj != nil && aclObj.AllowVariableOperation(
|
|
|
|
ns, pathOrPrefix, cap) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-20 15:21:03 +00:00
|
|
|
}
|
2022-10-18 20:43:59 +00:00
|
|
|
return structs.ErrPermissionDenied
|
2022-06-10 13:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 15:21:03 +00:00
|
|
|
// authValidatePrefix asserts that the requested path is valid for
|
|
|
|
// this allocation
|
2022-08-26 18:03:56 +00:00
|
|
|
func (sv *Variables) authValidatePrefix(claims *structs.IdentityClaims, ns, pathOrPrefix string) error {
|
2022-06-10 13:41:54 +00:00
|
|
|
|
|
|
|
store, err := sv.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
2022-06-20 15:21:03 +00:00
|
|
|
return err
|
2022-06-10 13:41:54 +00:00
|
|
|
}
|
2022-06-20 15:21:03 +00:00
|
|
|
alloc, err := store.AllocByID(nil, claims.AllocationID)
|
2022-06-10 13:41:54 +00:00
|
|
|
if err != nil {
|
2022-06-20 15:21:03 +00:00
|
|
|
return err
|
2022-06-10 13:41:54 +00:00
|
|
|
}
|
|
|
|
if alloc == nil || alloc.Job == nil {
|
2022-06-20 15:21:03 +00:00
|
|
|
return fmt.Errorf("allocation does not exist")
|
2022-06-10 13:41:54 +00:00
|
|
|
}
|
|
|
|
if alloc.Job.Namespace != ns {
|
2022-06-20 15:21:03 +00:00
|
|
|
return fmt.Errorf("allocation is in another namespace")
|
2022-06-10 13:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(pathOrPrefix, "/")
|
2022-07-21 13:05:54 +00:00
|
|
|
expect := []string{"nomad", "jobs", claims.JobID, alloc.TaskGroup, claims.TaskName}
|
2022-06-10 13:41:54 +00:00
|
|
|
if len(parts) > len(expect) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, part := range parts {
|
|
|
|
if part != expect[idx] {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|