2014-08-06 00:05:59 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2020-05-01 22:35:28 +00:00
|
|
|
"context"
|
2019-04-26 17:49:28 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2014-08-06 00:05:59 +00:00
|
|
|
"fmt"
|
2018-10-19 16:04:07 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2014-08-08 23:00:32 +00:00
|
|
|
"time"
|
|
|
|
|
2020-11-13 02:12:12 +00:00
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/armon/go-metrics/prometheus"
|
2021-04-08 22:58:15 +00:00
|
|
|
"github.com/hashicorp/go-bexpr"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
|
|
|
|
2014-08-08 23:00:32 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2019-04-26 17:49:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/authmethod"
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2020-06-16 19:03:22 +00:00
|
|
|
"github.com/hashicorp/consul/lib/template"
|
2014-08-06 00:05:59 +00:00
|
|
|
)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
const (
|
|
|
|
// aclBootstrapReset is the file name to create in the data dir. It's only contents
|
|
|
|
// should be the reset index
|
|
|
|
aclBootstrapReset = "acl-bootstrap-reset"
|
|
|
|
)
|
|
|
|
|
2020-11-13 02:12:12 +00:00
|
|
|
var ACLEndpointSummaries = []prometheus.SummaryDefinition{
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "token", "clone"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "token", "upsert"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "token", "delete"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "policy", "upsert"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "policy", "delete"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "policy", "delete"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "role", "upsert"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "role", "delete"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "bindingrule", "upsert"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "bindingrule", "delete"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "authmethod", "upsert"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "authmethod", "delete"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "login"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "login"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "logout"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
{
|
2020-11-13 21:18:04 +00:00
|
|
|
Name: []string{"acl", "logout"},
|
2020-11-13 02:12:12 +00:00
|
|
|
Help: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Regex for matching
|
2019-04-08 18:19:09 +00:00
|
|
|
var (
|
|
|
|
validPolicyName = regexp.MustCompile(`^[A-Za-z0-9\-_]{1,128}$`)
|
|
|
|
validServiceIdentityName = regexp.MustCompile(`^[a-z0-9]([a-z0-9\-_]*[a-z0-9])?$`)
|
|
|
|
serviceIdentityNameMaxLength = 256
|
2020-06-16 16:54:27 +00:00
|
|
|
validNodeIdentityName = regexp.MustCompile(`^[a-z0-9]([a-z0-9\-_]*[a-z0-9])?$`)
|
|
|
|
nodeIdentityNameMaxLength = 256
|
2019-04-15 20:43:19 +00:00
|
|
|
validRoleName = regexp.MustCompile(`^[A-Za-z0-9\-_]{1,256}$`)
|
2019-04-26 17:49:28 +00:00
|
|
|
validAuthMethod = regexp.MustCompile(`^[A-Za-z0-9\-_]{1,128}$`)
|
2019-04-08 18:19:09 +00:00
|
|
|
)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2014-08-06 00:05:59 +00:00
|
|
|
// ACL endpoint is used to manipulate ACLs
|
|
|
|
type ACL struct {
|
2020-01-28 23:50:41 +00:00
|
|
|
srv *Server
|
|
|
|
logger hclog.Logger
|
2014-08-06 00:05:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 17:13:28 +00:00
|
|
|
// fileBootstrapResetIndex retrieves the reset index specified by the administrator from
|
2018-10-19 16:04:07 +00:00
|
|
|
// the file on disk.
|
|
|
|
//
|
|
|
|
// Q: What is the bootstrap reset index?
|
|
|
|
// A: If you happen to lose acess to all tokens capable of ACL management you need a way
|
|
|
|
// to get back into your system. This allows an admin to write the current
|
|
|
|
// bootstrap "index" into a special file on disk to override the mechanism preventing
|
|
|
|
// a second token bootstrap. The index will be retrieved by a API call to /v1/acl/bootstrap
|
|
|
|
// When already bootstrapped this API will return the reset index necessary within
|
|
|
|
// the error response. Once set in the file, the bootstrap API can be used again to
|
|
|
|
// get a new token.
|
|
|
|
//
|
|
|
|
// Q: Why is the reset index not in the config?
|
|
|
|
// A: We want to be able to remove the reset index once we have used it. This prevents
|
|
|
|
// accidentally allowing bootstrapping yet again after a snapshot restore.
|
|
|
|
//
|
|
|
|
func (a *ACL) fileBootstrapResetIndex() uint64 {
|
|
|
|
// Determine the file path to check
|
|
|
|
path := filepath.Join(a.srv.config.DataDir, aclBootstrapReset)
|
|
|
|
|
|
|
|
// Read the file
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2020-01-28 23:50:41 +00:00
|
|
|
a.logger.Error("bootstrap: failed to read path",
|
|
|
|
"path", path,
|
|
|
|
"error", err,
|
|
|
|
)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to parse the file
|
|
|
|
var resetIdx uint64
|
|
|
|
if _, err := fmt.Sscanf(string(raw), "%d", &resetIdx); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
a.logger.Error("failed to parse bootstrap reset index path", "path", path, "error", err)
|
2018-10-19 16:04:07 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the reset index
|
2020-01-28 23:50:41 +00:00
|
|
|
a.logger.Debug("parsed bootstrap reset index path", "path", path, "reset_index", resetIdx)
|
2018-10-19 16:04:07 +00:00
|
|
|
return resetIdx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) removeBootstrapResetFile() {
|
|
|
|
if err := os.Remove(filepath.Join(a.srv.config.DataDir, aclBootstrapReset)); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
a.logger.Warn("failed to remove bootstrap file", "error", err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) aclPreCheck() error {
|
2020-07-03 20:52:08 +00:00
|
|
|
if !a.srv.config.ACLsEnabled {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.srv.UseLegacyACLs() {
|
|
|
|
return fmt.Errorf("The ACL system is currently in legacy mode.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:49:51 +00:00
|
|
|
// BootstrapTokens is used to perform a one-time ACL bootstrap operation on
|
2017-08-03 00:05:18 +00:00
|
|
|
// a cluster to get the first management token.
|
2018-10-19 16:04:07 +00:00
|
|
|
func (a *ACL) BootstrapTokens(args *structs.DCSpecificRequest, reply *structs.ACLToken) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.BootstrapTokens", args, reply); done {
|
2017-08-03 00:05:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-14 15:45:39 +00:00
|
|
|
if err := a.srv.aclBootstrapAllowed(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
// Verify we are allowed to serve this request
|
2018-10-19 16:04:07 +00:00
|
|
|
if !a.srv.InACLDatacenter() {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrDisabled
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// By doing some pre-checks we can head off later bootstrap attempts
|
|
|
|
// without having to run them through Raft, which should curb abuse.
|
|
|
|
state := a.srv.fsm.State()
|
2018-10-19 16:04:07 +00:00
|
|
|
allowed, resetIdx, err := state.CanBootstrapACLToken()
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
var specifiedIndex uint64 = 0
|
|
|
|
if !allowed {
|
|
|
|
// Check if there is a reset index specified
|
|
|
|
specifiedIndex = a.fileBootstrapResetIndex()
|
|
|
|
if specifiedIndex == 0 {
|
|
|
|
return fmt.Errorf("ACL bootstrap no longer allowed (reset index: %d)", resetIdx)
|
|
|
|
} else if specifiedIndex != resetIdx {
|
|
|
|
return fmt.Errorf("Invalid bootstrap reset index (specified %d, reset index: %d)", specifiedIndex, resetIdx)
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// remove the bootstrap override file now that we have the index from it and it was valid.
|
|
|
|
// whether bootstrapping works or not is irrelevant as we really don't want this file hanging around
|
|
|
|
// in case a snapshot restore is done. In that case we don't want to accidentally allow re-bootstrapping
|
2018-11-02 17:00:39 +00:00
|
|
|
// just because the file was unchanged.
|
2018-10-19 16:04:07 +00:00
|
|
|
a.removeBootstrapResetFile()
|
|
|
|
|
|
|
|
accessor, err := lib.GenerateUUID(a.srv.checkTokenUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
secret, err := lib.GenerateUUID(a.srv.checkTokenUUID)
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
req := structs.ACLTokenBootstrapRequest{
|
|
|
|
Token: structs.ACLToken{
|
|
|
|
AccessorID: accessor,
|
|
|
|
SecretID: secret,
|
|
|
|
Description: "Bootstrap Token (Global Management)",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
|
|
|
{
|
|
|
|
ID: structs.ACLPolicyGlobalManagementID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CreateTime: time.Now(),
|
|
|
|
Local: false,
|
|
|
|
// DEPRECATED (ACL-Legacy-Compat) - This is used so that the bootstrap token is still visible via the v1 acl APIs
|
2019-12-06 19:01:34 +00:00
|
|
|
Type: structs.ACLTokenTypeManagement,
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2017-08-03 00:05:18 +00:00
|
|
|
},
|
2018-10-19 16:04:07 +00:00
|
|
|
ResetIndex: specifiedIndex,
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
req.Token.SetHash(true)
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLBootstrapRequestType, &req)
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:20:45 +00:00
|
|
|
if _, token, err := state.ACLTokenGetByAccessor(nil, accessor, structs.DefaultEnterpriseMetaInDefaultPartition()); err == nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
*reply = *token
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
a.logger.Info("ACL bootstrap completed")
|
2017-08-03 00:05:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (a *ACL) TokenRead(args *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// clients will not know whether the server has local token store. In the case
|
2018-11-02 17:00:39 +00:00
|
|
|
// where it doesn't we will transparently forward requests.
|
2018-10-19 16:04:07 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.TokenRead", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authz acl.Authorizer
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if args.TokenIDType == structs.ACLTokenAccessor {
|
|
|
|
var err error
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2018-10-19 16:04:07 +00:00
|
|
|
// Only ACLRead privileges are required to list tokens
|
|
|
|
// However if you do not have ACLWrite as well the token
|
|
|
|
// secrets will be redacted
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err = a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLRead(&authzContext) != acl.Allow {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2014-08-06 17:30:47 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
var index uint64
|
|
|
|
var token *structs.ACLToken
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if args.TokenIDType == structs.ACLTokenAccessor {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, token, err = state.ACLTokenGetByAccessor(ws, args.TokenID, &args.EnterpriseMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if token != nil {
|
2019-12-18 18:46:53 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(authz, &token)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
|
|
|
// token secret was redacted
|
|
|
|
if token.SecretID == redactedToken {
|
2019-03-04 14:52:45 +00:00
|
|
|
reply.Redacted = true
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, token, err = state.ACLTokenGetBySecret(ws, args.TokenID, nil)
|
|
|
|
// no extra validation is needed here. If you have the secret ID you can read it.
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:05:51 +00:00
|
|
|
if token != nil && token.IsExpired(time.Now()) {
|
|
|
|
token = nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Token = index, token
|
2020-06-09 19:13:09 +00:00
|
|
|
reply.SourceDatacenter = args.Datacenter
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (a *ACL) TokenClone(args *structs.ACLTokenSetRequest, reply *structs.ACLToken) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-06 17:30:47 +00:00
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.ACLToken.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// clients will not know whether the server has local token store. In the case
|
2018-11-02 17:00:39 +00:00
|
|
|
// where it doesn't we will transparently forward requests.
|
2018-10-19 16:04:07 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.TokenClone", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "token", "clone"}, time.Now())
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.ACLToken.EnterpriseMeta, &authzContext)
|
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
_, token, err := a.srv.fsm.State().ACLTokenGetByAccessor(nil, args.ACLToken.AccessorID, &args.ACLToken.EnterpriseMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-08 17:05:51 +00:00
|
|
|
} else if token == nil || token.IsExpired(time.Now()) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrNotFound
|
|
|
|
} else if !a.srv.InACLDatacenter() && !token.Local {
|
|
|
|
// global token writes must be forwarded to the primary DC
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
|
|
|
return a.srv.forwardDC("ACL.TokenClone", a.srv.config.PrimaryDatacenter, args, reply)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if token.AuthMethod != "" {
|
|
|
|
return fmt.Errorf("Cannot clone a token created from an auth method")
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if token.Rules != "" {
|
|
|
|
return fmt.Errorf("Cannot clone a legacy ACL with this endpoint")
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
cloneReq := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: args.Datacenter,
|
|
|
|
ACLToken: structs.ACLToken{
|
2019-04-08 18:19:09 +00:00
|
|
|
Policies: token.Policies,
|
2020-04-02 16:09:35 +00:00
|
|
|
Roles: token.Roles,
|
2019-04-08 18:19:09 +00:00
|
|
|
ServiceIdentities: token.ServiceIdentities,
|
2020-06-16 16:54:27 +00:00
|
|
|
NodeIdentities: token.NodeIdentities,
|
2019-04-08 18:19:09 +00:00
|
|
|
Local: token.Local,
|
|
|
|
Description: token.Description,
|
|
|
|
ExpirationTime: token.ExpirationTime,
|
2019-10-24 18:38:09 +00:00
|
|
|
EnterpriseMeta: args.ACLToken.EnterpriseMeta,
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
|
|
|
WriteRequest: args.WriteRequest,
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.ACLToken.Description != "" {
|
|
|
|
cloneReq.ACLToken.Description = args.ACLToken.Description
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
return a.tokenSetInternal(&cloneReq, reply, false)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (a *ACL) TokenSet(args *structs.ACLTokenSetRequest, reply *structs.ACLToken) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.ACLToken.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Global token creation/modification always goes to the ACL DC
|
|
|
|
if !args.ACLToken.Local {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2018-10-19 16:04:07 +00:00
|
|
|
} else if !a.srv.LocalTokensEnabled() {
|
|
|
|
return fmt.Errorf("Local tokens are disabled")
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.TokenSet", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "token", "upsert"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.ACLToken.EnterpriseMeta, &authzContext); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
return a.tokenSetInternal(args, reply, false)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
func (a *ACL) tokenSetInternal(args *structs.ACLTokenSetRequest, reply *structs.ACLToken, fromLogin bool) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
token := &args.ACLToken
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
// local token operations
|
|
|
|
return fmt.Errorf("Cannot upsert tokens within this datacenter")
|
|
|
|
} else if !a.srv.InACLDatacenter() && !token.Local {
|
|
|
|
return fmt.Errorf("Cannot upsert global tokens within this datacenter")
|
|
|
|
}
|
|
|
|
|
|
|
|
state := a.srv.fsm.State()
|
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
var accessorMatch *structs.ACLToken
|
|
|
|
var secretMatch *structs.ACLToken
|
|
|
|
var err error
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
if token.AccessorID != "" {
|
2019-10-24 18:38:09 +00:00
|
|
|
_, accessorMatch, err = state.ACLTokenGetByAccessor(nil, token.AccessorID, nil)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
2019-04-30 15:45:36 +00:00
|
|
|
return fmt.Errorf("Failed acl token lookup by accessor: %v", err)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-30 15:45:36 +00:00
|
|
|
}
|
|
|
|
if token.SecretID != "" {
|
2019-10-24 18:38:09 +00:00
|
|
|
_, secretMatch, err = state.ACLTokenGetBySecret(nil, token.SecretID, nil)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
2019-04-30 15:45:36 +00:00
|
|
|
return fmt.Errorf("Failed acl token lookup by secret: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if token.AccessorID == "" || args.Create {
|
|
|
|
// Token Create
|
|
|
|
|
|
|
|
// Generate the AccessorID if not specified
|
|
|
|
if token.AccessorID == "" {
|
|
|
|
token.AccessorID, err = lib.GenerateUUID(a.srv.checkTokenUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if _, err := uuid.ParseUUID(token.AccessorID); err != nil {
|
|
|
|
return fmt.Errorf("Invalid Token: AccessorID is not a valid UUID")
|
|
|
|
} else if accessorMatch != nil {
|
|
|
|
return fmt.Errorf("Invalid Token: AccessorID is already in use")
|
2019-10-24 18:38:09 +00:00
|
|
|
} else if _, match, err := state.ACLTokenGetBySecret(nil, token.AccessorID, nil); err != nil || match != nil {
|
2019-04-30 15:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to lookup the acl token: %v", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Invalid Token: AccessorID is already in use")
|
|
|
|
} else if structs.ACLIDReserved(token.AccessorID) {
|
|
|
|
return fmt.Errorf("Invalid Token: UUIDs with the prefix %q are reserved", structs.ACLReservedPrefix)
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
// Generate the SecretID if not specified
|
2019-04-30 15:45:36 +00:00
|
|
|
if token.SecretID == "" {
|
|
|
|
token.SecretID, err = lib.GenerateUUID(a.srv.checkTokenUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if _, err := uuid.ParseUUID(token.SecretID); err != nil {
|
|
|
|
return fmt.Errorf("Invalid Token: SecretID is not a valid UUID")
|
|
|
|
} else if secretMatch != nil {
|
|
|
|
return fmt.Errorf("Invalid Token: SecretID is already in use")
|
2019-10-24 18:38:09 +00:00
|
|
|
} else if _, match, err := state.ACLTokenGetByAccessor(nil, token.SecretID, nil); err != nil || match != nil {
|
2019-04-30 15:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to lookup the acl token: %v", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Invalid Token: SecretID is already in use")
|
|
|
|
} else if structs.ACLIDReserved(token.SecretID) {
|
|
|
|
return fmt.Errorf("Invalid Token: UUIDs with the prefix %q are reserved", structs.ACLReservedPrefix)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
token.CreateTime = time.Now()
|
2019-04-08 17:05:51 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if fromLogin {
|
|
|
|
if token.AuthMethod == "" {
|
|
|
|
return fmt.Errorf("AuthMethod field is required during Login")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if token.AuthMethod != "" {
|
|
|
|
return fmt.Errorf("AuthMethod field is disallowed outside of Login")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:05:51 +00:00
|
|
|
// Ensure an ExpirationTTL is valid if provided.
|
|
|
|
if token.ExpirationTTL != 0 {
|
|
|
|
if token.ExpirationTTL < 0 {
|
|
|
|
return fmt.Errorf("Token Expiration TTL '%s' should be > 0", token.ExpirationTTL)
|
|
|
|
}
|
2019-04-15 18:35:55 +00:00
|
|
|
if token.HasExpirationTime() {
|
2019-04-08 17:05:51 +00:00
|
|
|
return fmt.Errorf("Token Expiration TTL and Expiration Time cannot both be set")
|
|
|
|
}
|
|
|
|
|
2019-04-15 18:35:55 +00:00
|
|
|
token.ExpirationTime = timePointer(token.CreateTime.Add(token.ExpirationTTL))
|
2019-04-08 17:05:51 +00:00
|
|
|
token.ExpirationTTL = 0
|
|
|
|
}
|
|
|
|
|
2019-04-15 18:35:55 +00:00
|
|
|
if token.HasExpirationTime() {
|
|
|
|
if token.CreateTime.After(*token.ExpirationTime) {
|
2019-04-08 17:05:51 +00:00
|
|
|
return fmt.Errorf("ExpirationTime cannot be before CreateTime")
|
|
|
|
}
|
|
|
|
|
|
|
|
expiresIn := token.ExpirationTime.Sub(token.CreateTime)
|
|
|
|
if expiresIn > a.srv.config.ACLTokenMaxExpirationTTL {
|
|
|
|
return fmt.Errorf("ExpirationTime cannot be more than %s in the future (was %s)",
|
|
|
|
a.srv.config.ACLTokenMaxExpirationTTL, expiresIn)
|
|
|
|
} else if expiresIn < a.srv.config.ACLTokenMinExpirationTTL {
|
|
|
|
return fmt.Errorf("ExpirationTime cannot be less than %s in the future (was %s)",
|
|
|
|
a.srv.config.ACLTokenMinExpirationTTL, expiresIn)
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
} else {
|
|
|
|
// Token Update
|
|
|
|
if _, err := uuid.ParseUUID(token.AccessorID); err != nil {
|
|
|
|
return fmt.Errorf("AccessorID is not a valid UUID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED (ACL-Legacy-Compat) - maybe get rid of this in the future
|
|
|
|
// and instead do a ParseUUID check. New tokens will not have
|
|
|
|
// secrets generated by users but rather they will always be UUIDs.
|
|
|
|
// However if users just continue the upgrade cycle they may still
|
|
|
|
// have tokens using secrets that are not UUIDS
|
|
|
|
// The RootAuthorizer checks that the SecretID is not "allow", "deny"
|
|
|
|
// or "manage" as a precaution against something accidentally using
|
|
|
|
// one of these root policies by setting the secret to it.
|
|
|
|
if acl.RootAuthorizer(token.SecretID) != nil {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.PermissionDeniedError{Cause: "Cannot modify root ACL"}
|
2014-08-22 21:55:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Verify the token exists
|
2019-04-30 15:45:36 +00:00
|
|
|
if accessorMatch == nil || accessorMatch.IsExpired(time.Now()) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Cannot find token %q", token.AccessorID)
|
|
|
|
}
|
|
|
|
if token.SecretID == "" {
|
2019-04-30 15:45:36 +00:00
|
|
|
token.SecretID = accessorMatch.SecretID
|
|
|
|
} else if accessorMatch.SecretID != token.SecretID {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Changing a tokens SecretID is not permitted")
|
2014-08-08 23:00:32 +00:00
|
|
|
}
|
2014-08-06 00:05:59 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Cannot toggle the "Global" mode
|
2019-04-30 15:45:36 +00:00
|
|
|
if token.Local != accessorMatch.Local {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("cannot toggle local mode of %s", token.AccessorID)
|
2014-08-06 17:30:47 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if token.AuthMethod == "" {
|
2019-04-30 15:45:36 +00:00
|
|
|
token.AuthMethod = accessorMatch.AuthMethod
|
|
|
|
} else if token.AuthMethod != accessorMatch.AuthMethod {
|
2019-04-26 17:49:28 +00:00
|
|
|
return fmt.Errorf("Cannot change AuthMethod of %s", token.AccessorID)
|
|
|
|
}
|
|
|
|
|
2019-04-15 18:35:55 +00:00
|
|
|
if token.ExpirationTTL != 0 {
|
|
|
|
return fmt.Errorf("Cannot change expiration time of %s", token.AccessorID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !token.HasExpirationTime() {
|
2019-04-30 15:45:36 +00:00
|
|
|
token.ExpirationTime = accessorMatch.ExpirationTime
|
|
|
|
} else if !accessorMatch.HasExpirationTime() {
|
2019-04-15 18:35:55 +00:00
|
|
|
return fmt.Errorf("Cannot change expiration time of %s", token.AccessorID)
|
2019-04-30 15:45:36 +00:00
|
|
|
} else if !token.ExpirationTime.Equal(*accessorMatch.ExpirationTime) {
|
2019-04-08 17:05:51 +00:00
|
|
|
return fmt.Errorf("Cannot change expiration time of %s", token.AccessorID)
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
token.CreateTime = accessorMatch.CreateTime
|
2014-08-06 00:05:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
policyIDs := make(map[string]struct{})
|
|
|
|
var policies []structs.ACLTokenPolicyLink
|
|
|
|
|
|
|
|
// Validate all the policy names and convert them to policy IDs
|
|
|
|
for _, link := range token.Policies {
|
|
|
|
if link.ID == "" {
|
2019-10-24 18:38:09 +00:00
|
|
|
_, policy, err := state.ACLPolicyGetByName(nil, link.Name, &token.EnterpriseMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error looking up policy for name %q: %v", link.Name, err)
|
|
|
|
}
|
|
|
|
if policy == nil {
|
|
|
|
return fmt.Errorf("No such ACL policy with name %q", link.Name)
|
|
|
|
}
|
|
|
|
link.ID = policy.ID
|
2019-10-24 18:38:09 +00:00
|
|
|
} else {
|
|
|
|
_, policy, err := state.ACLPolicyGetByID(nil, link.ID, &token.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error looking up policy for id %q: %v", link.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if policy == nil {
|
|
|
|
return fmt.Errorf("No such ACL policy with ID %q", link.ID)
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not store the policy name within raft/memdb as the policy could be renamed in the future.
|
|
|
|
link.Name = ""
|
|
|
|
|
|
|
|
// dedup policy links by id
|
|
|
|
if _, ok := policyIDs[link.ID]; !ok {
|
|
|
|
policies = append(policies, link)
|
|
|
|
policyIDs[link.ID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token.Policies = policies
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
roleIDs := make(map[string]struct{})
|
|
|
|
var roles []structs.ACLTokenRoleLink
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// Validate all the role names and convert them to role IDs.
|
2019-04-15 20:43:19 +00:00
|
|
|
for _, link := range token.Roles {
|
|
|
|
if link.ID == "" {
|
2019-10-24 18:38:09 +00:00
|
|
|
_, role, err := state.ACLRoleGetByName(nil, link.Name, &token.EnterpriseMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error looking up role for name %q: %v", link.Name, err)
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return fmt.Errorf("No such ACL role with name %q", link.Name)
|
|
|
|
}
|
|
|
|
link.ID = role.ID
|
2019-10-24 18:38:09 +00:00
|
|
|
} else {
|
|
|
|
_, role, err := state.ACLRoleGetByID(nil, link.ID, &token.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error looking up role for id %q: %v", link.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if role == nil {
|
|
|
|
return fmt.Errorf("No such ACL role with ID %q", link.ID)
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not store the role name within raft/memdb as the role could be renamed in the future.
|
|
|
|
link.Name = ""
|
|
|
|
|
|
|
|
// dedup role links by id
|
|
|
|
if _, ok := roleIDs[link.ID]; !ok {
|
|
|
|
roles = append(roles, link)
|
|
|
|
roleIDs[link.ID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token.Roles = roles
|
|
|
|
|
2019-04-08 18:19:09 +00:00
|
|
|
for _, svcid := range token.ServiceIdentities {
|
|
|
|
if svcid.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Service identity is missing the service name field on this token")
|
|
|
|
}
|
|
|
|
if token.Local && len(svcid.Datacenters) > 0 {
|
|
|
|
return fmt.Errorf("Service identity %q cannot specify a list of datacenters on a local token", svcid.ServiceName)
|
|
|
|
}
|
|
|
|
if !isValidServiceIdentityName(svcid.ServiceName) {
|
|
|
|
return fmt.Errorf("Service identity %q has an invalid name. Only alphanumeric characters, '-' and '_' are allowed", svcid.ServiceName)
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
token.ServiceIdentities = dedupeServiceIdentities(token.ServiceIdentities)
|
2019-04-08 18:19:09 +00:00
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
for _, nodeid := range token.NodeIdentities {
|
|
|
|
if nodeid.NodeName == "" {
|
|
|
|
return fmt.Errorf("Node identity is missing the node name field on this token")
|
|
|
|
}
|
|
|
|
if nodeid.Datacenter == "" {
|
|
|
|
return fmt.Errorf("Node identity is missing the datacenter field on this token")
|
|
|
|
}
|
|
|
|
if !isValidNodeIdentityName(nodeid.NodeName) {
|
|
|
|
return fmt.Errorf("Node identity has an invalid name. Only alphanumeric characters, '-' and '_' are allowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token.NodeIdentities = dedupeNodeIdentities(token.NodeIdentities)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if token.Rules != "" {
|
|
|
|
return fmt.Errorf("Rules cannot be specified for this token")
|
|
|
|
}
|
|
|
|
|
|
|
|
if token.Type != "" {
|
|
|
|
return fmt.Errorf("Type cannot be specified for this token")
|
|
|
|
}
|
|
|
|
|
|
|
|
token.SetHash(true)
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
// validate the enterprise meta
|
|
|
|
err = state.ACLTokenUpsertValidateEnterprise(token, accessorMatch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
req := &structs.ACLTokenBatchSetRequest{
|
|
|
|
Tokens: structs.ACLTokens{token},
|
|
|
|
CAS: false,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 19:22:44 +00:00
|
|
|
if fromLogin {
|
|
|
|
// Logins may attempt to link to roles that do not exist. These
|
|
|
|
// may be persisted, but don't allow tokens to be created that
|
|
|
|
// have no privileges (i.e. role links that point nowhere).
|
|
|
|
req.AllowMissingLinks = true
|
|
|
|
req.ProhibitUnprivileged = true
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLTokenSetRequestType, req)
|
2014-08-06 00:05:59 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Failed to apply token write request: %v", err)
|
2014-08-06 00:05:59 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// Purge the identity from the cache to prevent using the previous definition of the identity
|
2019-10-25 17:05:43 +00:00
|
|
|
a.srv.acls.cache.RemoveIdentity(tokenSecretCacheID(token.SecretID))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-08 17:05:51 +00:00
|
|
|
// Don't check expiration times here as it doesn't really matter.
|
2019-10-24 18:38:09 +00:00
|
|
|
if _, updatedToken, err := a.srv.fsm.State().ACLTokenGetByAccessor(nil, token.AccessorID, nil); err == nil && updatedToken != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
*reply = *updatedToken
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Failed to retrieve the token after insertion")
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
func validateBindingRuleBindName(bindType, bindName string, availableFields []string) (bool, error) {
|
|
|
|
if bindType == "" || bindName == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fakeVarMap := make(map[string]string)
|
|
|
|
for _, v := range availableFields {
|
|
|
|
fakeVarMap[v] = "fake"
|
|
|
|
}
|
|
|
|
|
|
|
|
_, valid, err := computeBindingRuleBindName(bindType, bindName, fakeVarMap)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return valid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// computeBindingRuleBindName processes the HIL for the provided bind type+name
|
2020-05-01 22:35:28 +00:00
|
|
|
// using the projected variables.
|
2019-04-26 17:49:28 +00:00
|
|
|
//
|
|
|
|
// - If the HIL is invalid ("", false, AN_ERROR) is returned.
|
|
|
|
// - If the computed name is not valid for the type ("INVALID_NAME", false, nil) is returned.
|
|
|
|
// - If the computed name is valid for the type ("VALID_NAME", true, nil) is returned.
|
2020-05-01 22:35:28 +00:00
|
|
|
func computeBindingRuleBindName(bindType, bindName string, projectedVars map[string]string) (string, bool, error) {
|
2020-06-16 19:03:22 +00:00
|
|
|
bindName, err := template.InterpolateHIL(bindName, projectedVars, true)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
valid := false
|
|
|
|
|
|
|
|
switch bindType {
|
|
|
|
case structs.BindingRuleBindTypeService:
|
|
|
|
valid = isValidServiceIdentityName(bindName)
|
2020-06-16 16:54:27 +00:00
|
|
|
case structs.BindingRuleBindTypeNode:
|
|
|
|
valid = isValidNodeIdentityName(bindName)
|
2019-04-26 17:49:28 +00:00
|
|
|
case structs.BindingRuleBindTypeRole:
|
|
|
|
valid = validRoleName.MatchString(bindName)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "", false, fmt.Errorf("unknown binding rule bind type: %s", bindType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindName, valid, nil
|
|
|
|
}
|
|
|
|
|
2019-04-08 18:19:09 +00:00
|
|
|
// isValidServiceIdentityName returns true if the provided name can be used as
|
|
|
|
// an ACLServiceIdentity ServiceName. This is more restrictive than standard
|
|
|
|
// catalog registration, which basically takes the view that "everything is
|
|
|
|
// valid".
|
|
|
|
func isValidServiceIdentityName(name string) bool {
|
|
|
|
if len(name) < 1 || len(name) > serviceIdentityNameMaxLength {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return validServiceIdentityName.MatchString(name)
|
|
|
|
}
|
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
// isValidNodeIdentityName returns true if the provided name can be used as
|
|
|
|
// an ACLNodeIdentity NodeName. This is more restrictive than standard
|
|
|
|
// catalog registration, which basically takes the view that "everything is
|
|
|
|
// valid".
|
|
|
|
func isValidNodeIdentityName(name string) bool {
|
|
|
|
if len(name) < 1 || len(name) > nodeIdentityNameMaxLength {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return validNodeIdentityName.MatchString(name)
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (a *ACL) TokenDelete(args *structs.ACLTokenDeleteRequest, reply *string) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
2016-08-03 05:04:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.TokenDelete", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "token", "delete"}, time.Now())
|
|
|
|
|
2016-08-03 05:04:11 +00:00
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext); err != nil {
|
2016-08-03 05:04:11 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if _, err := uuid.ParseUUID(args.TokenID); err != nil {
|
|
|
|
return fmt.Errorf("Accessor ID is missing or an invalid UUID")
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.TokenID == structs.ACLTokenAnonymousID {
|
|
|
|
return fmt.Errorf("Delete operation not permitted on the anonymous token")
|
|
|
|
}
|
|
|
|
|
|
|
|
// grab the token here so we can invalidate our cache later on
|
2019-10-24 18:38:09 +00:00
|
|
|
_, token, err := a.srv.fsm.State().ACLTokenGetByAccessor(nil, args.TokenID, &args.EnterpriseMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-10 14:22:51 +00:00
|
|
|
if token != nil {
|
|
|
|
if args.Token == token.SecretID {
|
|
|
|
return fmt.Errorf("Deletion of the request's authorization token is not permitted")
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:05:51 +00:00
|
|
|
// No need to check expiration time because it's being deleted.
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
// token found in secondary DC but its not local so it must be deleted in the primary
|
2019-01-10 14:22:51 +00:00
|
|
|
if !a.srv.InACLDatacenter() && !token.Local {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
|
|
|
return a.srv.forwardDC("ACL.TokenDelete", a.srv.config.PrimaryDatacenter, args, reply)
|
2019-01-10 14:22:51 +00:00
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
} else if !a.srv.InACLDatacenter() {
|
|
|
|
// token not found in secondary DC - attempt to delete within the primary
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
|
|
|
return a.srv.forwardDC("ACL.TokenDelete", a.srv.config.PrimaryDatacenter, args, reply)
|
2019-10-24 18:38:09 +00:00
|
|
|
} else {
|
|
|
|
// in Primary Datacenter but the token does not exist - return early as there is nothing to do.
|
|
|
|
return nil
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := &structs.ACLTokenBatchDeleteRequest{
|
|
|
|
TokenIDs: []string{args.TokenID},
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLTokenDeleteRequestType, req)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply token delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purge the identity from the cache to prevent using the previous definition of the identity
|
2019-10-25 17:05:43 +00:00
|
|
|
a.srv.acls.cache.RemoveIdentity(tokenSecretCacheID(token.SecretID))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
if reply != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
*reply = token.AccessorID
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) TokenList(args *structs.ACLTokenListRequest, reply *structs.ACLTokenListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
2021-08-06 22:00:58 +00:00
|
|
|
if args.Datacenter != a.srv.config.PrimaryDatacenter {
|
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2018-10-19 16:04:07 +00:00
|
|
|
args.IncludeLocal = false
|
|
|
|
args.IncludeGlobal = true
|
|
|
|
}
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.TokenList", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2020-01-24 15:04:58 +00:00
|
|
|
var requestMeta structs.EnterpriseMeta
|
|
|
|
authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &requestMeta, &authzContext)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-01-24 15:04:58 +00:00
|
|
|
}
|
|
|
|
// merge the token default meta into the requests meta
|
|
|
|
args.EnterpriseMeta.Merge(&requestMeta)
|
|
|
|
args.EnterpriseMeta.FillAuthzContext(&authzContext)
|
2021-07-30 17:51:57 +00:00
|
|
|
if authz.ACLRead(&authzContext) != acl.Allow {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:09:29 +00:00
|
|
|
var methodMeta *structs.EnterpriseMeta
|
|
|
|
if args.AuthMethod != "" {
|
|
|
|
methodMeta = args.ACLAuthMethodEnterpriseMeta.ToEnterpriseMeta()
|
2020-01-24 15:04:58 +00:00
|
|
|
// attempt to merge in the overall meta, wildcards will not be merged
|
|
|
|
methodMeta.MergeNoWildcard(&args.EnterpriseMeta)
|
|
|
|
// in the event that the meta above didn't merge due to being a wildcard
|
|
|
|
// we ensure that proper token based meta inference occurs
|
|
|
|
methodMeta.Merge(&requestMeta)
|
2020-01-14 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2020-01-14 15:09:29 +00:00
|
|
|
index, tokens, err := state.ACLTokenList(ws, args.IncludeLocal, args.IncludeGlobal, args.Policy, args.Role, args.AuthMethod, methodMeta, &args.EnterpriseMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:05:51 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
stubs := make([]*structs.ACLTokenListStub, 0, len(tokens))
|
|
|
|
for _, token := range tokens {
|
2019-04-08 17:05:51 +00:00
|
|
|
if token.IsExpired(now) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
stubs = append(stubs, token.Stub())
|
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
|
|
|
|
// filter down to just the tokens that the requester has permissions to read
|
2021-07-30 21:08:58 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(authz, &stubs)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
reply.Index, reply.Tokens = index, stubs
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (a *ACL) TokenBatchRead(args *structs.ACLTokenBatchGetRequest, reply *structs.ACLTokenBatchResponse) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.TokenBatchRead", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.srv.ResolveToken(args.Token)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2018-10-31 20:00:46 +00:00
|
|
|
index, tokens, err := state.ACLTokenBatchGet(ws, args.AccessorIDs)
|
2016-08-09 07:11:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:05:51 +00:00
|
|
|
// This RPC is used for replication, so don't filter out expired tokens here.
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
// Filter the tokens down to just what we have permission to see - also redact
|
|
|
|
// secrets based on allowed permissions. We could just call filterACLWithAuthorizer
|
|
|
|
// on the whole token list but then it would require another pass through the token
|
|
|
|
// list to determine if any secrets were redacted. Its a small amount of code to
|
|
|
|
// process the loop so it was duplicated here and we instead call the filter func
|
|
|
|
// with just a single token.
|
|
|
|
ret := make(structs.ACLTokens, 0, len(tokens))
|
|
|
|
for _, token := range tokens {
|
|
|
|
final := token
|
2019-12-18 18:46:53 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(authz, &final)
|
2019-10-24 18:38:09 +00:00
|
|
|
if final != nil {
|
|
|
|
ret = append(ret, final)
|
|
|
|
if final.SecretID == redactedToken {
|
|
|
|
reply.Redacted = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reply.Removed = true
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
reply.Index, reply.Tokens = index, ret
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (a *ACL) PolicyRead(args *structs.ACLPolicyGetRequest, reply *structs.ACLPolicyResponse) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.PolicyRead", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLRead(&authzContext) != acl.Allow {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2020-03-25 14:34:24 +00:00
|
|
|
var (
|
|
|
|
index uint64
|
|
|
|
policy *structs.ACLPolicy
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if args.PolicyID != "" {
|
|
|
|
index, policy, err = state.ACLPolicyGetByID(ws, args.PolicyID, &args.EnterpriseMeta)
|
|
|
|
} else {
|
|
|
|
index, policy, err = state.ACLPolicyGetByName(ws, args.PolicyName, &args.EnterpriseMeta)
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Policy = index, policy
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (a *ACL) PolicyBatchRead(args *structs.ACLPolicyBatchGetRequest, reply *structs.ACLPolicyBatchResponse) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.PolicyBatchRead", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.srv.ResolveToken(args.Token)
|
2019-10-24 18:38:09 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2018-10-31 20:00:46 +00:00
|
|
|
index, policies, err := state.ACLPolicyBatchGet(ws, args.PolicyIDs)
|
2016-08-09 07:11:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(authz, &policies)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
reply.Index, reply.Policies = index, policies
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (a *ACL) PolicySet(args *structs.ACLPolicySetRequest, reply *structs.ACLPolicy) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.Policy.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if !a.srv.InACLDatacenter() {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.PolicySet", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "policy", "upsert"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.Policy.EnterpriseMeta, &authzContext); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := &args.Policy
|
|
|
|
state := a.srv.fsm.State()
|
|
|
|
|
|
|
|
// Almost all of the checks here are also done in the state store. However,
|
|
|
|
// we want to prevent the raft operations when we know they are going to fail
|
|
|
|
// so we still do them here.
|
|
|
|
|
|
|
|
// ensure a name is set
|
|
|
|
if policy.Name == "" {
|
|
|
|
return fmt.Errorf("Invalid Policy: no Name is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validPolicyName.MatchString(policy.Name) {
|
|
|
|
return fmt.Errorf("Invalid Policy: invalid Name. Only alphanumeric characters, '-' and '_' are allowed")
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
var idMatch *structs.ACLPolicy
|
|
|
|
var nameMatch *structs.ACLPolicy
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if policy.ID != "" {
|
|
|
|
if _, err := uuid.ParseUUID(policy.ID); err != nil {
|
|
|
|
return fmt.Errorf("Policy ID invalid UUID")
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
_, idMatch, err = state.ACLPolicyGetByID(nil, policy.ID, nil)
|
2019-04-30 15:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("acl policy lookup by id failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
_, nameMatch, err = state.ACLPolicyGetByName(nil, policy.Name, &policy.EnterpriseMeta)
|
2019-04-30 15:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("acl policy lookup by name failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if policy.ID == "" {
|
|
|
|
// with no policy ID one will be generated
|
|
|
|
var err error
|
|
|
|
policy.ID, err = lib.GenerateUUID(a.srv.checkPolicyUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the name is unique
|
2019-04-30 15:45:36 +00:00
|
|
|
if nameMatch != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Invalid Policy: A Policy with Name %q already exists", policy.Name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Verify the policy exists
|
2019-04-30 15:45:36 +00:00
|
|
|
if idMatch == nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("cannot find policy %s", policy.ID)
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
// Verify that the name isn't changing or that the name is not already used
|
|
|
|
if idMatch.Name != policy.Name && nameMatch != nil {
|
|
|
|
return fmt.Errorf("Invalid Policy: A policy with name %q already exists", policy.Name)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if policy.ID == structs.ACLPolicyGlobalManagementID {
|
|
|
|
if policy.Datacenters != nil || len(policy.Datacenters) > 0 {
|
|
|
|
return fmt.Errorf("Changing the Datacenters of the builtin global-management policy is not permitted")
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
if policy.Rules != idMatch.Rules {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Changing the Rules for the builtin global-management policy is not permitted")
|
2016-08-09 07:11:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// validate the rules
|
2020-01-13 20:51:40 +00:00
|
|
|
_, err = acl.NewPolicyFromSource("", 0, policy.Rules, policy.Syntax, a.srv.aclConfig, policy.EnterprisePolicyMeta())
|
2019-10-24 18:38:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the enterprise meta
|
|
|
|
err = state.ACLPolicyUpsertValidateEnterprise(policy, idMatch)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
2016-08-03 05:04:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:00:39 +00:00
|
|
|
// calculate the hash for this policy
|
2018-10-19 16:04:07 +00:00
|
|
|
policy.SetHash(true)
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
req := &structs.ACLPolicyBatchSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Policies: structs.ACLPolicies{policy},
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLPolicySetRequestType, req)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply policy upsert request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from the cache to prevent stale cache usage
|
|
|
|
a.srv.acls.cache.RemovePolicy(policy.ID)
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
if _, policy, err := a.srv.fsm.State().ACLPolicyGetByID(nil, policy.ID, &policy.EnterpriseMeta); err == nil && policy != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
*reply = *policy
|
2014-08-18 22:23:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-06 00:05:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (a *ACL) PolicyDelete(args *structs.ACLPolicyDeleteRequest, reply *string) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
2014-08-06 00:05:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if !a.srv.InACLDatacenter() {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.PolicyDelete", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "policy", "delete"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
_, policy, err := a.srv.fsm.State().ACLPolicyGetByID(nil, args.PolicyID, &args.EnterpriseMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if policy == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if policy.ID == structs.ACLPolicyGlobalManagementID {
|
|
|
|
return fmt.Errorf("Delete operation not permitted on the builtin global-management policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.ACLPolicyBatchDeleteRequest{
|
|
|
|
PolicyIDs: []string{args.PolicyID},
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLPolicyDeleteRequestType, &req)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply policy delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.srv.acls.cache.RemovePolicy(policy.ID)
|
|
|
|
|
2020-08-21 15:42:37 +00:00
|
|
|
*reply = policy.Name
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) PolicyList(args *structs.ACLPolicyListRequest, reply *structs.ACLPolicyListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.PolicyList", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2014-08-12 22:32:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-30 13:10:11 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
2019-10-24 18:38:09 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLRead(&authzContext) != acl.Allow {
|
2018-10-19 16:04:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, policies, err := state.ACLPolicyList(ws, &args.EnterpriseMeta)
|
2015-10-14 02:18:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
// filter down to just what the requester has permissions to see
|
2019-12-18 18:46:53 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(authz, &policies)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
var stubs structs.ACLPolicyListStubs
|
|
|
|
for _, policy := range policies {
|
|
|
|
stubs = append(stubs, policy.Stub())
|
2014-08-06 00:05:59 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
reply.Index, reply.Policies = index, stubs
|
2015-10-14 02:18:43 +00:00
|
|
|
return nil
|
2014-08-06 00:05:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// PolicyResolve is used to retrieve a subset of the policies associated with a given token
|
|
|
|
// The policy ids in the args simply act as a filter on the policy set assigned to the token
|
2018-10-31 20:00:46 +00:00
|
|
|
func (a *ACL) PolicyResolve(args *structs.ACLPolicyBatchGetRequest, reply *structs.ACLPolicyBatchResponse) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.PolicyResolve", args, reply); done {
|
2018-10-19 16:04:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get full list of policies for this token
|
2019-01-22 18:14:43 +00:00
|
|
|
identity, policies, err := a.srv.acls.resolveTokenToIdentityAndPolicies(args.Token)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-13 19:55:27 +00:00
|
|
|
entIdentity, entPolicies, err := a.srv.acls.resolveEnterpriseIdentityAndPolicies(identity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
idMap := make(map[string]*structs.ACLPolicy)
|
2019-01-22 18:14:43 +00:00
|
|
|
for _, policyID := range identity.PolicyIDs() {
|
|
|
|
idMap[policyID] = nil
|
|
|
|
}
|
2020-02-13 19:55:27 +00:00
|
|
|
if entIdentity != nil {
|
|
|
|
for _, policyID := range entIdentity.PolicyIDs() {
|
|
|
|
idMap[policyID] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
for _, policy := range policies {
|
|
|
|
idMap[policy.ID] = policy
|
|
|
|
}
|
2020-02-13 19:55:27 +00:00
|
|
|
for _, policy := range entPolicies {
|
|
|
|
idMap[policy.ID] = policy
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
for _, policyID := range args.PolicyIDs {
|
|
|
|
if policy, ok := idMap[policyID]; ok {
|
2019-01-22 18:14:43 +00:00
|
|
|
// only add non-deleted policies
|
|
|
|
if policy != nil {
|
|
|
|
reply.Policies = append(reply.Policies, policy)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// send a permission denied to indicate that the request included
|
|
|
|
// policy ids not associated with this token
|
|
|
|
return acl.ErrPermissionDenied
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-22 18:14:43 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
a.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-04 00:01:32 +00:00
|
|
|
// makeACLETag returns an ETag for the given parent and policy.
|
|
|
|
func makeACLETag(parent string, policy *acl.Policy) string {
|
|
|
|
return fmt.Sprintf("%s:%s", parent, policy.ID)
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:32:43 +00:00
|
|
|
// GetPolicy is used to retrieve a compiled policy object with a TTL. Does not
|
|
|
|
// support a blocking query.
|
2018-10-19 16:04:07 +00:00
|
|
|
func (a *ACL) GetPolicy(args *structs.ACLPolicyResolveLegacyRequest, reply *structs.ACLPolicyResolveLegacyResponse) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.GetPolicy", args, reply); done {
|
2014-08-08 22:32:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-12 22:32:44 +00:00
|
|
|
// Verify we are allowed to serve this request
|
2021-08-06 22:00:58 +00:00
|
|
|
if a.srv.config.PrimaryDatacenter != a.srv.config.Datacenter {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrDisabled
|
2014-08-12 22:32:44 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 22:32:43 +00:00
|
|
|
// Get the policy via the cache
|
2021-08-06 22:39:39 +00:00
|
|
|
parent := a.srv.config.ACLResolverSettings.ACLDefaultPolicy
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2020-06-03 15:22:22 +00:00
|
|
|
ident, policy, err := a.srv.acls.GetMergedPolicyForToken(args.ACL)
|
2014-08-08 22:32:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:22:22 +00:00
|
|
|
if token, ok := ident.(*structs.ACLToken); ok && token.Type == structs.ACLTokenTypeManagement {
|
|
|
|
parent = "manage"
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// translates the structures internals to most closely match what could be expressed in the original rule language
|
|
|
|
policy = policy.ConvertToLegacy()
|
|
|
|
|
2014-08-08 23:55:47 +00:00
|
|
|
// Generate an ETag
|
2016-08-04 00:01:32 +00:00
|
|
|
etag := makeACLETag(parent, policy)
|
2014-08-08 23:55:47 +00:00
|
|
|
|
|
|
|
// Setup the response
|
|
|
|
reply.ETag = etag
|
2021-08-06 22:39:39 +00:00
|
|
|
reply.TTL = a.srv.config.ACLResolverSettings.ACLTokenTTL
|
2014-08-08 22:32:43 +00:00
|
|
|
a.srv.setQueryMeta(&reply.QueryMeta)
|
2014-08-08 23:55:47 +00:00
|
|
|
|
|
|
|
// Only send the policy on an Etag mis-match
|
|
|
|
if args.ETag != etag {
|
2014-08-12 17:54:56 +00:00
|
|
|
reply.Parent = parent
|
2014-08-08 23:55:47 +00:00
|
|
|
reply.Policy = policy
|
|
|
|
}
|
2014-08-08 22:32:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-05 04:32:36 +00:00
|
|
|
// ReplicationStatus is used to retrieve the current ACL replication status.
|
|
|
|
func (a *ACL) ReplicationStatus(args *structs.DCSpecificRequest,
|
|
|
|
reply *structs.ACLReplicationStatus) error {
|
|
|
|
// This must be sent to the leader, so we fix the args since we are
|
|
|
|
// re-using a structure where we don't support all the options.
|
|
|
|
args.RequireConsistent = true
|
|
|
|
args.AllowStale = false
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.ReplicationStatus", args, reply); done {
|
2016-08-05 04:32:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// There's no ACL token required here since this doesn't leak any
|
|
|
|
// sensitive information, and we don't want people to have to use
|
|
|
|
// management tokens if they are querying this via a health check.
|
|
|
|
|
|
|
|
// Poll the latest status.
|
|
|
|
a.srv.aclReplicationStatusLock.RLock()
|
|
|
|
*reply = a.srv.aclReplicationStatus
|
|
|
|
a.srv.aclReplicationStatusLock.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-15 18:35:55 +00:00
|
|
|
|
|
|
|
func timePointer(t time.Time) *time.Time {
|
|
|
|
return &t
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
func (a *ACL) RoleRead(args *structs.ACLRoleGetRequest, reply *structs.ACLRoleResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.RoleRead", args, reply); done {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLRead(&authzContext) != acl.Allow {
|
2019-04-15 20:43:19 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
var (
|
|
|
|
index uint64
|
|
|
|
role *structs.ACLRole
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if args.RoleID != "" {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, role, err = state.ACLRoleGetByID(ws, args.RoleID, &args.EnterpriseMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
} else {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, role, err = state.ACLRoleGetByName(ws, args.RoleName, &args.EnterpriseMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Role = index, role
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) RoleBatchRead(args *structs.ACLRoleBatchGetRequest, reply *structs.ACLRoleBatchResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.RoleBatchRead", args, reply); done {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.srv.ResolveToken(args.Token)
|
2019-10-24 18:38:09 +00:00
|
|
|
if err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, roles, err := state.ACLRoleBatchGet(ws, args.RoleIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(authz, &roles)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
reply.Index, reply.Roles = index, roles
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) RoleSet(args *structs.ACLRoleSetRequest, reply *structs.ACLRole) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.Role.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
if !a.srv.InACLDatacenter() {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.RoleSet", args, reply); done {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "role", "upsert"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.Role.EnterpriseMeta, &authzContext); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2019-04-15 20:43:19 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
role := &args.Role
|
|
|
|
state := a.srv.fsm.State()
|
|
|
|
|
|
|
|
// Almost all of the checks here are also done in the state store. However,
|
|
|
|
// we want to prevent the raft operations when we know they are going to fail
|
|
|
|
// so we still do them here.
|
|
|
|
|
|
|
|
// ensure a name is set
|
|
|
|
if role.Name == "" {
|
|
|
|
return fmt.Errorf("Invalid Role: no Name is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validRoleName.MatchString(role.Name) {
|
|
|
|
return fmt.Errorf("Invalid Role: invalid Name. Only alphanumeric characters, '-' and '_' are allowed")
|
|
|
|
}
|
|
|
|
|
2019-11-01 20:11:44 +00:00
|
|
|
var existing *structs.ACLRole
|
|
|
|
var err error
|
2019-04-15 20:43:19 +00:00
|
|
|
if role.ID == "" {
|
|
|
|
// with no role ID one will be generated
|
|
|
|
role.ID, err = lib.GenerateUUID(a.srv.checkRoleUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the name is unique
|
2019-11-01 20:11:44 +00:00
|
|
|
if _, existing, err = state.ACLRoleGetByName(nil, role.Name, &role.EnterpriseMeta); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return fmt.Errorf("acl role lookup by name failed: %v", err)
|
|
|
|
} else if existing != nil {
|
|
|
|
return fmt.Errorf("Invalid Role: A Role with Name %q already exists", role.Name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := uuid.ParseUUID(role.ID); err != nil {
|
|
|
|
return fmt.Errorf("Role ID invalid UUID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the role exists
|
2019-11-01 20:11:44 +00:00
|
|
|
_, existing, err = state.ACLRoleGetByID(nil, role.ID, nil)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("acl role lookup failed: %v", err)
|
|
|
|
} else if existing == nil {
|
|
|
|
return fmt.Errorf("cannot find role %s", role.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing.Name != role.Name {
|
2019-10-24 18:38:09 +00:00
|
|
|
if _, nameMatch, err := state.ACLRoleGetByName(nil, role.Name, &role.EnterpriseMeta); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return fmt.Errorf("acl role lookup by name failed: %v", err)
|
|
|
|
} else if nameMatch != nil {
|
|
|
|
return fmt.Errorf("Invalid Role: A role with name %q already exists", role.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 20:11:44 +00:00
|
|
|
// validate the enterprise meta
|
|
|
|
if err := state.ACLRoleUpsertValidateEnterprise(role, existing); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
policyIDs := make(map[string]struct{})
|
|
|
|
var policies []structs.ACLRolePolicyLink
|
|
|
|
|
|
|
|
// Validate all the policy names and convert them to policy IDs
|
|
|
|
for _, link := range role.Policies {
|
|
|
|
if link.ID == "" {
|
2019-10-24 18:38:09 +00:00
|
|
|
_, policy, err := state.ACLPolicyGetByName(nil, link.Name, &role.EnterpriseMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error looking up policy for name %q: %v", link.Name, err)
|
|
|
|
}
|
|
|
|
if policy == nil {
|
|
|
|
return fmt.Errorf("No such ACL policy with name %q", link.Name)
|
|
|
|
}
|
|
|
|
link.ID = policy.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not store the policy name within raft/memdb as the policy could be renamed in the future.
|
|
|
|
link.Name = ""
|
|
|
|
|
|
|
|
// dedup policy links by id
|
|
|
|
if _, ok := policyIDs[link.ID]; !ok {
|
|
|
|
policies = append(policies, link)
|
|
|
|
policyIDs[link.ID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
role.Policies = policies
|
|
|
|
|
|
|
|
for _, svcid := range role.ServiceIdentities {
|
|
|
|
if svcid.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Service identity is missing the service name field on this role")
|
|
|
|
}
|
|
|
|
if !isValidServiceIdentityName(svcid.ServiceName) {
|
|
|
|
return fmt.Errorf("Service identity %q has an invalid name. Only alphanumeric characters, '-' and '_' are allowed", svcid.ServiceName)
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
role.ServiceIdentities = dedupeServiceIdentities(role.ServiceIdentities)
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
for _, nodeid := range role.NodeIdentities {
|
|
|
|
if nodeid.NodeName == "" {
|
|
|
|
return fmt.Errorf("Node identity is missing the node name field on this role")
|
|
|
|
}
|
|
|
|
if nodeid.Datacenter == "" {
|
|
|
|
return fmt.Errorf("Node identity is missing the datacenter field on this role")
|
|
|
|
}
|
|
|
|
if !isValidNodeIdentityName(nodeid.NodeName) {
|
|
|
|
return fmt.Errorf("Node identity has an invalid name. Only alphanumeric characters, '-' and '_' are allowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
role.NodeIdentities = dedupeNodeIdentities(role.NodeIdentities)
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
// calculate the hash for this role
|
|
|
|
role.SetHash(true)
|
|
|
|
|
|
|
|
req := &structs.ACLRoleBatchSetRequest{
|
|
|
|
Roles: structs.ACLRoles{role},
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLRoleSetRequestType, req)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply role upsert request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from the cache to prevent stale cache usage
|
|
|
|
a.srv.acls.cache.RemoveRole(role.ID)
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
if _, role, err := a.srv.fsm.State().ACLRoleGetByID(nil, role.ID, &role.EnterpriseMeta); err == nil && role != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
*reply = *role
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) RoleDelete(args *structs.ACLRoleDeleteRequest, reply *string) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
if !a.srv.InACLDatacenter() {
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.RoleDelete", args, reply); done {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "role", "delete"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2019-04-15 20:43:19 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
_, role, err := a.srv.fsm.State().ACLRoleGetByID(nil, args.RoleID, &args.EnterpriseMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if role == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.ACLRoleBatchDeleteRequest{
|
|
|
|
RoleIDs: []string{args.RoleID},
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLRoleDeleteRequestType, &req)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply role delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.srv.acls.cache.RemoveRole(role.ID)
|
|
|
|
|
2020-08-21 15:42:37 +00:00
|
|
|
*reply = role.Name
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) RoleList(args *structs.ACLRoleListRequest, reply *structs.ACLRoleListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.RoleList", args, reply); done {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-30 13:10:11 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
2019-10-24 18:38:09 +00:00
|
|
|
if err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLRead(&authzContext) != acl.Allow {
|
2019-04-15 20:43:19 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, roles, err := state.ACLRoleList(ws, args.Policy, &args.EnterpriseMeta)
|
2019-04-15 20:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(authz, &roles)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
reply.Index, reply.Roles = index, roles
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoleResolve is used to retrieve a subset of the roles associated with a given token
|
|
|
|
// The role ids in the args simply act as a filter on the role set assigned to the token
|
|
|
|
func (a *ACL) RoleResolve(args *structs.ACLRoleBatchGetRequest, reply *structs.ACLRoleBatchResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.RoleResolve", args, reply); done {
|
2019-04-15 20:43:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get full list of roles for this token
|
|
|
|
identity, roles, err := a.srv.acls.resolveTokenToIdentityAndRoles(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-13 19:55:27 +00:00
|
|
|
entIdentity, entRoles, err := a.srv.acls.resolveEnterpriseIdentityAndRoles(identity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
idMap := make(map[string]*structs.ACLRole)
|
|
|
|
for _, roleID := range identity.RoleIDs() {
|
|
|
|
idMap[roleID] = nil
|
|
|
|
}
|
2020-02-13 19:55:27 +00:00
|
|
|
if entIdentity != nil {
|
|
|
|
for _, roleID := range entIdentity.RoleIDs() {
|
|
|
|
idMap[roleID] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
for _, role := range roles {
|
|
|
|
idMap[role.ID] = role
|
|
|
|
}
|
2020-02-13 19:55:27 +00:00
|
|
|
for _, role := range entRoles {
|
|
|
|
idMap[role.ID] = role
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
for _, roleID := range args.RoleIDs {
|
|
|
|
if role, ok := idMap[roleID]; ok {
|
|
|
|
// only add non-deleted roles
|
|
|
|
if role != nil {
|
|
|
|
reply.Roles = append(reply.Roles, role)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// send a permission denied to indicate that the request included
|
|
|
|
// role ids not associated with this token
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
var errAuthMethodsRequireTokenReplication = errors.New("Token replication is required for auth methods to function")
|
|
|
|
|
|
|
|
func (a *ACL) BindingRuleRead(args *structs.ACLBindingRuleGetRequest, reply *structs.ACLBindingRuleResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.BindingRuleRead", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
2019-10-24 18:38:09 +00:00
|
|
|
if err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLRead(&authzContext) != acl.Allow {
|
2019-04-26 17:49:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, rule, err := state.ACLBindingRuleGetByID(ws, args.BindingRuleID, &args.EnterpriseMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.BindingRule = index, rule
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) BindingRuleSet(args *structs.ACLBindingRuleSetRequest, reply *structs.ACLBindingRule) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.BindingRule.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.BindingRuleSet", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "bindingrule", "upsert"}, time.Now())
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.BindingRule.EnterpriseMeta, &authzContext); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2019-04-26 17:49:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
var existing *structs.ACLBindingRule
|
2019-04-26 17:49:28 +00:00
|
|
|
rule := &args.BindingRule
|
|
|
|
state := a.srv.fsm.State()
|
|
|
|
|
|
|
|
if rule.ID == "" {
|
|
|
|
// with no binding rule ID one will be generated
|
|
|
|
var err error
|
|
|
|
|
|
|
|
rule.ID, err = lib.GenerateUUID(a.srv.checkBindingRuleUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := uuid.ParseUUID(rule.ID); err != nil {
|
|
|
|
return fmt.Errorf("Binding Rule ID invalid UUID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the role exists
|
2019-10-24 18:38:09 +00:00
|
|
|
var err error
|
2019-12-18 18:46:53 +00:00
|
|
|
// specifically disregarding the enterprise meta here
|
2019-10-24 18:38:09 +00:00
|
|
|
_, existing, err = state.ACLBindingRuleGetByID(nil, rule.ID, nil)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("acl binding rule lookup failed: %v", err)
|
|
|
|
} else if existing == nil {
|
|
|
|
return fmt.Errorf("cannot find binding rule %s", rule.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.AuthMethod == "" {
|
|
|
|
rule.AuthMethod = existing.AuthMethod
|
|
|
|
} else if existing.AuthMethod != rule.AuthMethod {
|
|
|
|
return fmt.Errorf("the AuthMethod field of an Binding Rule is immutable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.AuthMethod == "" {
|
|
|
|
return fmt.Errorf("Invalid Binding Rule: no AuthMethod is set")
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
// this is done early here to produce better errors
|
|
|
|
if err := state.ACLBindingRuleUpsertValidateEnterprise(rule, existing); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
methodIdx, method, err := state.ACLAuthMethodGetByName(nil, rule.AuthMethod, &args.BindingRule.EnterpriseMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("acl auth method lookup failed: %v", err)
|
|
|
|
} else if method == nil {
|
|
|
|
return fmt.Errorf("cannot find auth method with name %q", rule.AuthMethod)
|
|
|
|
}
|
|
|
|
validator, err := a.srv.loadAuthMethodValidator(methodIdx, method)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-01 22:35:28 +00:00
|
|
|
// Create a blank placeholder identity for use in validation below.
|
|
|
|
blankID := validator.NewIdentity()
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if rule.Selector != "" {
|
2020-05-01 22:35:28 +00:00
|
|
|
if _, err := bexpr.CreateEvaluatorForType(rule.Selector, nil, blankID.SelectableFields); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return fmt.Errorf("invalid Binding Rule: Selector is invalid: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.BindType == "" {
|
|
|
|
return fmt.Errorf("Invalid Binding Rule: no BindType is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.BindName == "" {
|
|
|
|
return fmt.Errorf("Invalid Binding Rule: no BindName is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rule.BindType {
|
|
|
|
case structs.BindingRuleBindTypeService:
|
2020-06-16 16:54:27 +00:00
|
|
|
case structs.BindingRuleBindTypeNode:
|
2019-04-26 17:49:28 +00:00
|
|
|
case structs.BindingRuleBindTypeRole:
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid Binding Rule: unknown BindType %q", rule.BindType)
|
|
|
|
}
|
|
|
|
|
2020-05-01 22:35:28 +00:00
|
|
|
if valid, err := validateBindingRuleBindName(rule.BindType, rule.BindName, blankID.ProjectedVarNames()); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return fmt.Errorf("Invalid Binding Rule: invalid BindName: %v", err)
|
|
|
|
} else if !valid {
|
|
|
|
return fmt.Errorf("Invalid Binding Rule: invalid BindName")
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &structs.ACLBindingRuleBatchSetRequest{
|
|
|
|
BindingRules: structs.ACLBindingRules{rule},
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLBindingRuleSetRequestType, req)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply binding rule upsert request: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
if _, rule, err := a.srv.fsm.State().ACLBindingRuleGetByID(nil, rule.ID, &rule.EnterpriseMeta); err == nil && rule != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
*reply = *rule
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) BindingRuleDelete(args *structs.ACLBindingRuleDeleteRequest, reply *bool) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.BindingRuleDelete", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "bindingrule", "delete"}, time.Now())
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2019-04-26 17:49:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
_, rule, err := a.srv.fsm.State().ACLBindingRuleGetByID(nil, args.BindingRuleID, &args.EnterpriseMeta)
|
2021-07-30 17:16:38 +00:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2021-07-30 17:16:38 +00:00
|
|
|
case rule == nil:
|
2019-04-26 17:49:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.ACLBindingRuleBatchDeleteRequest{
|
|
|
|
BindingRuleIDs: []string{args.BindingRuleID},
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLBindingRuleDeleteRequestType, &req)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply binding rule delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) BindingRuleList(args *structs.ACLBindingRuleListRequest, reply *structs.ACLBindingRuleListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.BindingRuleList", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-30 13:10:11 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
2019-10-24 18:38:09 +00:00
|
|
|
if err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLRead(&authzContext) != acl.Allow {
|
2019-04-26 17:49:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, rules, err := state.ACLBindingRuleList(ws, args.AuthMethod, &args.EnterpriseMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(authz, &rules)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
reply.Index, reply.BindingRules = index, rules
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) AuthMethodRead(args *structs.ACLAuthMethodGetRequest, reply *structs.ACLAuthMethodResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.AuthMethodRead", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLRead(&authzContext) != acl.Allow {
|
2019-04-26 17:49:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, method, err := state.ACLAuthMethodGetByName(ws, args.AuthMethodName, &args.EnterpriseMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:59:29 +00:00
|
|
|
if method != nil {
|
|
|
|
_ = a.enterpriseAuthMethodTypeValidation(method.Type)
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
reply.Index, reply.AuthMethod = index, method
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) AuthMethodSet(args *structs.ACLAuthMethodSetRequest, reply *structs.ACLAuthMethod) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.AuthMethod.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.AuthMethodSet", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "authmethod", "upsert"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.AuthMethod.EnterpriseMeta, &authzContext); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2019-04-26 17:49:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
method := &args.AuthMethod
|
|
|
|
state := a.srv.fsm.State()
|
|
|
|
|
|
|
|
// ensure a name is set
|
|
|
|
if method.Name == "" {
|
|
|
|
return fmt.Errorf("Invalid Auth Method: no Name is set")
|
|
|
|
}
|
|
|
|
if !validAuthMethod.MatchString(method.Name) {
|
|
|
|
return fmt.Errorf("Invalid Auth Method: invalid Name. Only alphanumeric characters, '-' and '_' are allowed")
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:59:29 +00:00
|
|
|
if err := a.enterpriseAuthMethodTypeValidation(method.Type); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// Check to see if the method exists first.
|
2019-10-24 18:38:09 +00:00
|
|
|
_, existing, err := state.ACLAuthMethodGetByName(nil, method.Name, &method.EnterpriseMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("acl auth method lookup failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing != nil {
|
|
|
|
if method.Type == "" {
|
|
|
|
method.Type = existing.Type
|
|
|
|
} else if existing.Type != method.Type {
|
|
|
|
return fmt.Errorf("the Type field of an Auth Method is immutable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !authmethod.IsRegisteredType(method.Type) {
|
|
|
|
return fmt.Errorf("Invalid Auth Method: Type should be one of: %v", authmethod.Types())
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:02:57 +00:00
|
|
|
if method.MaxTokenTTL != 0 {
|
|
|
|
if method.MaxTokenTTL > a.srv.config.ACLTokenMaxExpirationTTL {
|
|
|
|
return fmt.Errorf("MaxTokenTTL %s cannot be more than %s",
|
|
|
|
method.MaxTokenTTL, a.srv.config.ACLTokenMaxExpirationTTL)
|
|
|
|
} else if method.MaxTokenTTL < a.srv.config.ACLTokenMinExpirationTTL {
|
|
|
|
return fmt.Errorf("MaxTokenTTL %s cannot be less than %s",
|
|
|
|
method.MaxTokenTTL, a.srv.config.ACLTokenMinExpirationTTL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 16:44:47 +00:00
|
|
|
switch method.TokenLocality {
|
|
|
|
case "local", "":
|
|
|
|
case "global":
|
|
|
|
if !a.srv.InACLDatacenter() {
|
|
|
|
return fmt.Errorf("Invalid Auth Method: TokenLocality 'global' can only be used in the primary datacenter")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid Auth Method: TokenLocality should be one of 'local' or 'global'")
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// Instantiate a validator but do not cache it yet. This will validate the
|
|
|
|
// configuration.
|
2020-05-06 18:48:04 +00:00
|
|
|
validator, err := authmethod.NewValidator(a.srv.logger, method)
|
|
|
|
if err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return fmt.Errorf("Invalid Auth Method: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-06 18:48:04 +00:00
|
|
|
if err := enterpriseAuthMethodValidation(method, validator); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
if err := a.srv.fsm.State().ACLAuthMethodUpsertValidateEnterprise(method, existing); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
req := &structs.ACLAuthMethodBatchSetRequest{
|
|
|
|
AuthMethods: structs.ACLAuthMethods{method},
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLAuthMethodSetRequestType, req)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply auth method upsert request: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
if _, method, err := a.srv.fsm.State().ACLAuthMethodGetByName(nil, method.Name, &method.EnterpriseMeta); err == nil && method != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
*reply = *method
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) AuthMethodDelete(args *structs.ACLAuthMethodDeleteRequest, reply *bool) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.AuthMethodDelete", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "authmethod", "delete"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLWrite(&authzContext) != acl.Allow {
|
2019-04-26 17:49:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
_, method, err := a.srv.fsm.State().ACLAuthMethodGetByName(nil, args.AuthMethodName, &args.EnterpriseMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if method == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:59:29 +00:00
|
|
|
if err := a.enterpriseAuthMethodTypeValidation(method.Type); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
req := structs.ACLAuthMethodBatchDeleteRequest{
|
|
|
|
AuthMethodNames: []string{args.AuthMethodName},
|
2019-10-24 18:38:09 +00:00
|
|
|
EnterpriseMeta: args.EnterpriseMeta,
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLAuthMethodDeleteRequestType, &req)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply auth method delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) AuthMethodList(args *structs.ACLAuthMethodListRequest, reply *structs.ACLAuthMethodListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.EnterpriseMeta, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.AuthMethodList", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-10-30 13:10:11 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
authz, err := a.srv.ResolveTokenAndDefaultMeta(args.Token, &args.EnterpriseMeta, &authzContext)
|
2019-10-24 18:38:09 +00:00
|
|
|
if err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2021-07-30 17:51:57 +00:00
|
|
|
} else if authz.ACLRead(&authzContext) != acl.Allow {
|
2019-04-26 17:49:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-10-24 18:38:09 +00:00
|
|
|
index, methods, err := state.ACLAuthMethodList(ws, &args.EnterpriseMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(authz, &methods)
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
var stubs structs.ACLAuthMethodListStubs
|
|
|
|
for _, method := range methods {
|
2020-05-12 01:59:29 +00:00
|
|
|
_ = a.enterpriseAuthMethodTypeValidation(method.Type)
|
2019-04-26 17:49:28 +00:00
|
|
|
stubs = append(stubs, method.Stub())
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.AuthMethods = index, stubs
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) Login(args *structs.ACLLoginRequest, reply *structs.ACLToken) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:42:39 +00:00
|
|
|
if args.Auth == nil {
|
|
|
|
return fmt.Errorf("Invalid Login request: Missing auth parameters")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := a.srv.validateEnterpriseRequest(&args.Auth.EnterpriseMeta, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if args.Token != "" { // This shouldn't happen.
|
|
|
|
return errors.New("do not provide a token when logging in")
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.Login", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "login"}, time.Now())
|
|
|
|
|
|
|
|
auth := args.Auth
|
|
|
|
|
|
|
|
// 1. take args.Data.AuthMethod to get an AuthMethod Validator
|
2019-10-24 18:38:09 +00:00
|
|
|
idx, method, err := a.srv.fsm.State().ACLAuthMethodGetByName(nil, auth.AuthMethod, &auth.EnterpriseMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if method == nil {
|
2021-05-03 20:39:13 +00:00
|
|
|
return fmt.Errorf("%w: auth method %q not found", acl.ErrNotFound, auth.AuthMethod)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 01:59:29 +00:00
|
|
|
if err := a.enterpriseAuthMethodTypeValidation(method.Type); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
validator, err := a.srv.loadAuthMethodValidator(idx, method)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Send args.Data.BearerToken to method validator and get back a fields map
|
2020-05-01 22:35:28 +00:00
|
|
|
verifiedIdentity, err := validator.ValidateLogin(context.Background(), auth.BearerToken)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:59:29 +00:00
|
|
|
return a.tokenSetFromAuthMethod(
|
|
|
|
method,
|
|
|
|
&auth.EnterpriseMeta,
|
|
|
|
"token created via login",
|
|
|
|
auth.Meta,
|
|
|
|
validator,
|
|
|
|
verifiedIdentity,
|
|
|
|
&structs.ACLTokenSetRequest{
|
|
|
|
Datacenter: args.Datacenter,
|
|
|
|
WriteRequest: args.WriteRequest,
|
|
|
|
},
|
|
|
|
reply,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) tokenSetFromAuthMethod(
|
|
|
|
method *structs.ACLAuthMethod,
|
|
|
|
entMeta *structs.EnterpriseMeta,
|
|
|
|
tokenDescriptionPrefix string,
|
|
|
|
tokenMetadata map[string]string,
|
|
|
|
validator authmethod.Validator,
|
|
|
|
verifiedIdentity *authmethod.Identity,
|
|
|
|
createReq *structs.ACLTokenSetRequest, // this should be prepopulated with datacenter+writerequest
|
|
|
|
reply *structs.ACLToken,
|
|
|
|
) error {
|
2020-01-14 15:09:29 +00:00
|
|
|
// This always will return a valid pointer
|
2020-05-06 18:48:04 +00:00
|
|
|
targetMeta, err := computeTargetEnterpriseMeta(method, verifiedIdentity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-14 15:09:29 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// 3. send map through role bindings
|
2020-06-16 16:54:27 +00:00
|
|
|
bindings, err := a.srv.evaluateRoleBindings(validator, verifiedIdentity, entMeta, targetMeta)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 19:22:44 +00:00
|
|
|
// We try to prevent the creation of a useless token without taking a trip
|
|
|
|
// through the state store if we can.
|
2020-06-16 16:54:27 +00:00
|
|
|
if bindings == nil || (len(bindings.serviceIdentities) == 0 && len(bindings.nodeIdentities) == 0 && len(bindings.roles) == 0) {
|
2019-04-26 17:49:28 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:59:29 +00:00
|
|
|
// TODO(sso): add a CapturedField to ACLAuthMethod that would pluck fields from the returned identity and stuff into `auth.Meta`.
|
|
|
|
|
|
|
|
description := tokenDescriptionPrefix
|
|
|
|
loginMeta, err := encodeLoginMeta(tokenMetadata)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if loginMeta != "" {
|
|
|
|
description += ": " + loginMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. create token
|
2020-05-12 01:59:29 +00:00
|
|
|
createReq.ACLToken = structs.ACLToken{
|
|
|
|
Description: description,
|
|
|
|
Local: true,
|
|
|
|
AuthMethod: method.Name,
|
2020-06-16 16:54:27 +00:00
|
|
|
ServiceIdentities: bindings.serviceIdentities,
|
|
|
|
NodeIdentities: bindings.nodeIdentities,
|
|
|
|
Roles: bindings.roles,
|
2020-05-12 01:59:29 +00:00
|
|
|
ExpirationTTL: method.MaxTokenTTL,
|
|
|
|
EnterpriseMeta: *targetMeta,
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 16:44:47 +00:00
|
|
|
if method.TokenLocality == "global" {
|
|
|
|
if !a.srv.InACLDatacenter() {
|
|
|
|
return errors.New("creating global tokens via auth methods is only permitted in the primary datacenter")
|
|
|
|
}
|
|
|
|
createReq.ACLToken.Local = false
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:59:29 +00:00
|
|
|
createReq.ACLToken.ACLAuthMethodEnterpriseMeta.FillWithEnterpriseMeta(entMeta)
|
2020-01-14 15:09:29 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// 5. return token information like a TokenCreate would
|
2020-05-12 01:59:29 +00:00
|
|
|
err = a.tokenSetInternal(createReq, reply, true)
|
2019-05-03 19:22:44 +00:00
|
|
|
|
|
|
|
// If we were in a slight race with a role delete operation then we may
|
|
|
|
// still end up failing to insert an unprivileged token in the state
|
|
|
|
// machine instead. Return the same error as earlier so it doesn't
|
|
|
|
// actually matter which one prevents the insertion.
|
|
|
|
if err != nil && err.Error() == state.ErrTokenHasNoPrivileges.Error() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func encodeLoginMeta(meta map[string]string) (string, error) {
|
|
|
|
if len(meta) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := json.Marshal(meta)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(d), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) Logout(args *structs.ACLLogoutRequest, reply *bool) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Token == "" {
|
|
|
|
return acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.Logout", args, reply); done {
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "logout"}, time.Now())
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
_, token, err := a.srv.fsm.State().ACLTokenGetBySecret(nil, args.Token, nil)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
|
|
|
|
} else if token == nil {
|
|
|
|
return acl.ErrNotFound
|
|
|
|
|
|
|
|
} else if token.AuthMethod == "" {
|
|
|
|
// Can't "logout" of a token that wasn't a result of login.
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
|
|
|
|
} else if !a.srv.InACLDatacenter() && !token.Local {
|
|
|
|
// global token writes must be forwarded to the primary DC
|
2021-08-06 22:00:58 +00:00
|
|
|
args.Datacenter = a.srv.config.PrimaryDatacenter
|
|
|
|
return a.srv.forwardDC("ACL.Logout", a.srv.config.PrimaryDatacenter, args, reply)
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No need to check expiration time because it's being deleted.
|
|
|
|
|
|
|
|
req := &structs.ACLTokenBatchDeleteRequest{
|
|
|
|
TokenIDs: []string{token.AccessorID},
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:58:15 +00:00
|
|
|
_, err = a.srv.raftApply(structs.ACLTokenDeleteRequestType, req)
|
2019-04-26 17:49:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply token delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purge the identity from the cache to prevent using the previous definition of the identity
|
2020-08-21 15:42:37 +00:00
|
|
|
a.srv.acls.cache.RemoveIdentity(tokenSecretCacheID(token.SecretID))
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
*reply = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-06 14:25:26 +00:00
|
|
|
|
|
|
|
func (a *ACL) Authorize(args *structs.RemoteACLAuthorizationRequest, reply *[]structs.ACLAuthorizationResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("ACL.Authorize", args, reply); done {
|
2019-12-06 14:25:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
authz, err := a.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
responses, err := structs.CreateACLAuthorizationResponses(authz, args.Requests)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = responses
|
|
|
|
return nil
|
|
|
|
}
|