2014-08-06 00:05:59 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
|
2019-04-08 18:19:09 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
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"
|
2019-04-26 17:49:28 +00:00
|
|
|
"github.com/hashicorp/go-bexpr"
|
2019-04-08 18:19:09 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
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 {
|
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
a.srv.logger.Printf("[ERR] acl.bootstrap: failed to read %q: %v", path, err)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to parse the file
|
|
|
|
var resetIdx uint64
|
|
|
|
if _, err := fmt.Sscanf(string(raw), "%d", &resetIdx); err != nil {
|
|
|
|
a.srv.logger.Printf("[ERR] acl.bootstrap: failed to parse %q: %v", path, err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the reset index
|
|
|
|
a.srv.logger.Printf("[DEBUG] acl.bootstrap: parsed %q: reset index %d", path, resetIdx)
|
|
|
|
return resetIdx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) removeBootstrapResetFile() {
|
|
|
|
if err := os.Remove(filepath.Join(a.srv.config.DataDir, aclBootstrapReset)); err != nil {
|
|
|
|
a.srv.logger.Printf("[WARN] acl.bootstrap: failed to remove bootstrap file: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) aclPreCheck() error {
|
|
|
|
if !a.srv.ACLsEnabled() {
|
|
|
|
return acl.ErrDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.srv.UseLegacyACLs() {
|
|
|
|
return fmt.Errorf("The ACL system is currently in legacy mode.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
// Bootstrap is used to perform a one-time ACL bootstrap operation on
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
if done, err := a.srv.forward("ACL.BootstrapTokens", args, args, reply); done {
|
2017-08-03 00:05:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
Type: structs.ACLTokenTypeManagement,
|
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)
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLBootstrapRequestType, &req)
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if err, ok := resp.(error); ok {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if _, token, err := state.ACLTokenGetByAccessor(nil, accessor); err == nil {
|
|
|
|
*reply = *token
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a.srv.logger.Printf("[INFO] consul.acl: ACL bootstrap completed")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.TokenRead", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rule acl.Authorizer
|
|
|
|
if args.TokenIDType == structs.ACLTokenAccessor {
|
|
|
|
var err error
|
|
|
|
// Only ACLRead privileges are required to list tokens
|
|
|
|
// However if you do not have ACLWrite as well the token
|
|
|
|
// secrets will be redacted
|
|
|
|
if rule, err = a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
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 {
|
|
|
|
index, token, err = state.ACLTokenGetByAccessor(ws, args.TokenID)
|
|
|
|
if token != nil {
|
|
|
|
a.srv.filterACLWithAuthorizer(rule, &token)
|
2019-03-04 14:52:45 +00:00
|
|
|
if !rule.ACLWrite() {
|
|
|
|
reply.Redacted = true
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index, token, err = state.ACLTokenGetBySecret(ws, args.TokenID)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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() {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.TokenClone", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "token", "clone"}, time.Now())
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
_, token, err := a.srv.fsm.State().ACLTokenGetByAccessor(nil, args.ACLToken.AccessorID)
|
|
|
|
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
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
return a.srv.forwardDC("ACL.TokenClone", a.srv.config.ACLDatacenter, args, reply)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
ServiceIdentities: token.ServiceIdentities,
|
|
|
|
Local: token.Local,
|
|
|
|
Description: token.Description,
|
|
|
|
ExpirationTime: token.ExpirationTime,
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global token creation/modification always goes to the ACL DC
|
|
|
|
if !args.ACLToken.Local {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
} else if !a.srv.LocalTokensEnabled() {
|
|
|
|
return fmt.Errorf("Local tokens are disabled")
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
if done, err := a.srv.forward("ACL.TokenSet", args, 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
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
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 != "" {
|
|
|
|
_, accessorMatch, err = state.ACLTokenGetByAccessor(nil, token.AccessorID)
|
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 != "" {
|
|
|
|
_, secretMatch, err = state.ACLTokenGetBySecret(nil, token.SecretID)
|
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")
|
|
|
|
} else if _, match, err := state.ACLTokenGetBySecret(nil, token.AccessorID); err != nil || match != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the AccessorID if not specified
|
|
|
|
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")
|
|
|
|
} else if _, match, err := state.ACLTokenGetByAccessor(nil, token.SecretID); err != nil || match != nil {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
if !token.Local {
|
|
|
|
return fmt.Errorf("Cannot create Global token via 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 == "" {
|
|
|
|
_, policy, err := state.ACLPolicyGetByName(nil, link.Name)
|
|
|
|
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{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 == "" {
|
|
|
|
_, role, err := state.ACLRoleGetByName(nil, link.Name)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
resp, 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
|
|
|
|
a.srv.acls.cache.RemoveIdentity(token.SecretID)
|
|
|
|
|
2014-08-06 00:05:59 +00:00
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:05:51 +00:00
|
|
|
// Don't check expiration times here as it doesn't really matter.
|
2018-10-19 16:04:07 +00:00
|
|
|
if _, updatedToken, err := a.srv.fsm.State().ACLTokenGetByAccessor(nil, token.AccessorID); err == nil && token != nil {
|
|
|
|
*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
|
|
|
|
// using the verified fields.
|
|
|
|
//
|
|
|
|
// - 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.
|
|
|
|
func computeBindingRuleBindName(bindType, bindName string, verifiedFields map[string]string) (string, bool, error) {
|
|
|
|
bindName, err := InterpolateHIL(bindName, verifiedFields)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
valid := false
|
|
|
|
|
|
|
|
switch bindType {
|
|
|
|
case structs.BindingRuleBindTypeService:
|
|
|
|
valid = isValidServiceIdentityName(bindName)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
2016-08-03 05:04:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if done, err := a.srv.forward("ACL.TokenDelete", args, args, reply); done {
|
|
|
|
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
|
2018-10-19 16:04:07 +00:00
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
2016-08-03 05:04:11 +00:00
|
|
|
return err
|
2018-10-19 16:04:07 +00:00
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
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
|
|
|
|
_, token, err := a.srv.fsm.State().ACLTokenGetByAccessor(nil, args.TokenID)
|
|
|
|
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-01-10 14:22:51 +00:00
|
|
|
if !a.srv.InACLDatacenter() && !token.Local {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
return a.srv.forwardDC("ACL.TokenDelete", a.srv.config.ACLDatacenter, args, reply)
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := &structs.ACLTokenBatchDeleteRequest{
|
|
|
|
TokenIDs: []string{args.TokenID},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLTokenDeleteRequestType, req)
|
|
|
|
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
|
|
|
|
if token != nil {
|
|
|
|
a.srv.acls.cache.RemoveIdentity(token.SecretID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if reply != nil && token != nil {
|
|
|
|
*reply = token.AccessorID
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) TokenList(args *structs.ACLTokenListRequest, reply *structs.ACLTokenListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
if args.Datacenter != a.srv.config.ACLDatacenter {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
args.IncludeLocal = false
|
|
|
|
args.IncludeGlobal = true
|
|
|
|
}
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.TokenList", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rule, err := a.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-04-26 17:49:28 +00:00
|
|
|
index, tokens, err := state.ACLTokenList(ws, args.IncludeLocal, args.IncludeGlobal, args.Policy, args.Role, args.AuthMethod)
|
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())
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.TokenBatchRead", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rule, err := a.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
a.srv.filterACLWithAuthorizer(rule, &tokens)
|
|
|
|
|
|
|
|
reply.Index, reply.Tokens = index, tokens
|
2019-03-04 14:52:45 +00:00
|
|
|
reply.Redacted = !rule.ACLWrite()
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.PolicyRead", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, policy, err := state.ACLPolicyGetByID(ws, args.PolicyID)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.PolicyBatchRead", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.InACLDatacenter() {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
if done, err := a.srv.forward("ACL.PolicySet", args, 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
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, idMatch, err = state.ACLPolicyGetByID(nil, policy.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("acl policy lookup by id failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, nameMatch, err = state.ACLPolicyGetByName(nil, policy.Name)
|
|
|
|
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
|
2019-04-30 15:45:36 +00:00
|
|
|
_, err = acl.NewPolicyFromSource("", 0, policy.Rules, policy.Syntax, a.srv.sentinel)
|
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},
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
resp, 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)
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, policy, err := a.srv.fsm.State().ACLPolicyGetByID(nil, policy.ID); err == nil && policy != nil {
|
|
|
|
*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
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if !a.srv.InACLDatacenter() {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.PolicyDelete", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "policy", "delete"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
_, policy, err := a.srv.fsm.State().ACLPolicyGetByID(nil, args.PolicyID)
|
|
|
|
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},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLPolicyDeleteRequestType, &req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply policy delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.srv.acls.cache.RemovePolicy(policy.ID)
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if policy != nil {
|
|
|
|
*reply = policy.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) PolicyList(args *structs.ACLPolicyListRequest, reply *structs.ACLPolicyListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.PolicyList", args, args, reply); done {
|
|
|
|
return err
|
2014-08-12 22:32:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
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 {
|
2018-10-31 20:00:46 +00:00
|
|
|
index, policies, err := state.ACLPolicyList(ws)
|
2015-10-14 02:18:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.PolicyResolve", args, args, reply); done {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
idMap := make(map[string]*structs.ACLPolicy)
|
2019-01-22 18:14:43 +00:00
|
|
|
for _, policyID := range identity.PolicyIDs() {
|
|
|
|
idMap[policyID] = nil
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
for _, policy := range policies {
|
|
|
|
idMap[policy.ID] = policy
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2014-08-08 22:32:43 +00:00
|
|
|
if done, err := a.srv.forward("ACL.GetPolicy", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-12 22:32:44 +00:00
|
|
|
// Verify we are allowed to serve this request
|
|
|
|
if a.srv.config.ACLDatacenter != 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
|
2018-10-19 16:04:07 +00:00
|
|
|
parent := a.srv.config.ACLDefaultPolicy
|
|
|
|
|
|
|
|
policy, err := a.srv.acls.GetMergedPolicyForToken(args.ACL)
|
2014-08-08 22:32:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
2018-10-19 16:04:07 +00:00
|
|
|
reply.TTL = a.srv.config.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
|
|
|
|
if done, err := a.srv.forward("ACL.ReplicationStatus", args, args, reply); done {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.RoleRead", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
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 != "" {
|
|
|
|
index, role, err = state.ACLRoleGetByID(ws, args.RoleID)
|
|
|
|
} else {
|
|
|
|
index, role, err = state.ACLRoleGetByName(ws, args.RoleName)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.RoleBatchRead", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.InACLDatacenter() {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.RoleSet", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "role", "upsert"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if role.ID == "" {
|
|
|
|
// with no role ID one will be generated
|
|
|
|
var err error
|
|
|
|
|
|
|
|
role.ID, err = lib.GenerateUUID(a.srv.checkRoleUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the name is unique
|
|
|
|
if _, existing, err := state.ACLRoleGetByName(nil, role.Name); err != nil {
|
|
|
|
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
|
|
|
|
_, existing, err := state.ACLRoleGetByID(nil, role.ID)
|
|
|
|
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 {
|
|
|
|
if _, nameMatch, err := state.ACLRoleGetByName(nil, role.Name); err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 == "" {
|
|
|
|
_, policy, err := state.ACLPolicyGetByName(nil, link.Name)
|
|
|
|
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
|
|
|
|
|
|
|
// calculate the hash for this role
|
|
|
|
role.SetHash(true)
|
|
|
|
|
|
|
|
req := &structs.ACLRoleBatchSetRequest{
|
|
|
|
Roles: structs.ACLRoles{role},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLRoleSetRequestType, req)
|
|
|
|
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)
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, role, err := a.srv.fsm.State().ACLRoleGetByID(nil, role.ID); err == nil && role != nil {
|
|
|
|
*reply = *role
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) RoleDelete(args *structs.ACLRoleDeleteRequest, reply *string) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.InACLDatacenter() {
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.RoleDelete", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "role", "delete"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
_, role, err := a.srv.fsm.State().ACLRoleGetByID(nil, args.RoleID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if role == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.ACLRoleBatchDeleteRequest{
|
|
|
|
RoleIDs: []string{args.RoleID},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLRoleDeleteRequestType, &req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply role delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.srv.acls.cache.RemoveRole(role.ID)
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if role != nil {
|
|
|
|
*reply = role.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) RoleList(args *structs.ACLRoleListRequest, reply *structs.ACLRoleListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.RoleList", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, roles, err := state.ACLRoleList(ws, args.Policy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.RoleResolve", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get full list of roles for this token
|
|
|
|
identity, roles, err := a.srv.acls.resolveTokenToIdentityAndRoles(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
idMap := make(map[string]*structs.ACLRole)
|
|
|
|
for _, roleID := range identity.RoleIDs() {
|
|
|
|
idMap[roleID] = nil
|
|
|
|
}
|
|
|
|
for _, role := range roles {
|
|
|
|
idMap[role.ID] = role
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.BindingRuleRead", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, rule, err := state.ACLBindingRuleGetByID(ws, args.BindingRuleID)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.BindingRuleSet", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "bindingrule", "upsert"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
_, existing, err := state.ACLBindingRuleGetByID(nil, rule.ID)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
methodIdx, method, err := state.ACLAuthMethodGetByName(nil, rule.AuthMethod)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule.Selector != "" {
|
|
|
|
selectableVars := validator.MakeFieldMapSelectable(map[string]string{})
|
|
|
|
_, err := bexpr.CreateEvaluatorForType(rule.Selector, nil, selectableVars)
|
|
|
|
if err != nil {
|
|
|
|
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:
|
|
|
|
case structs.BindingRuleBindTypeRole:
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid Binding Rule: unknown BindType %q", rule.BindType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if valid, err := validateBindingRuleBindName(rule.BindType, rule.BindName, validator.AvailableFields()); err != nil {
|
|
|
|
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},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLBindingRuleSetRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply binding rule upsert request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, rule, err := a.srv.fsm.State().ACLBindingRuleGetByID(nil, rule.ID); err == nil && rule != nil {
|
|
|
|
*reply = *rule
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) BindingRuleDelete(args *structs.ACLBindingRuleDeleteRequest, reply *bool) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.BindingRuleDelete", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "bindingrule", "delete"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
_, rule, err := a.srv.fsm.State().ACLBindingRuleGetByID(nil, args.BindingRuleID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.ACLBindingRuleBatchDeleteRequest{
|
|
|
|
BindingRuleIDs: []string{args.BindingRuleID},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLBindingRuleDeleteRequestType, &req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply binding rule delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) BindingRuleList(args *structs.ACLBindingRuleListRequest, reply *structs.ACLBindingRuleListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.BindingRuleList", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, rules, err := state.ACLBindingRuleList(ws, args.AuthMethod)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.AuthMethodRead", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, method, err := state.ACLAuthMethodGetByName(ws, args.AuthMethodName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.AuthMethodSet", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "authmethod", "upsert"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if the method exists first.
|
|
|
|
_, existing, err := state.ACLAuthMethodGetByName(nil, method.Name)
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate a validator but do not cache it yet. This will validate the
|
|
|
|
// configuration.
|
|
|
|
if _, err := authmethod.NewValidator(method); err != nil {
|
|
|
|
return fmt.Errorf("Invalid Auth Method: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &structs.ACLAuthMethodBatchSetRequest{
|
|
|
|
AuthMethods: structs.ACLAuthMethods{method},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLAuthMethodSetRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply auth method upsert request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, method, err := a.srv.fsm.State().ACLAuthMethodGetByName(nil, method.Name); err == nil && method != nil {
|
|
|
|
*reply = *method
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) AuthMethodDelete(args *structs.ACLAuthMethodDeleteRequest, reply *bool) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.AuthMethodDelete", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "authmethod", "delete"}, time.Now())
|
|
|
|
|
|
|
|
// Verify token is permitted to modify ACLs
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLWrite() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
_, method, err := a.srv.fsm.State().ACLAuthMethodGetByName(nil, args.AuthMethodName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if method == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.ACLAuthMethodBatchDeleteRequest{
|
|
|
|
AuthMethodNames: []string{args.AuthMethodName},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLAuthMethodDeleteRequestType, &req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to apply auth method delete request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) AuthMethodList(args *structs.ACLAuthMethodListRequest, reply *structs.ACLAuthMethodListResponse) error {
|
|
|
|
if err := a.aclPreCheck(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.srv.LocalTokensEnabled() {
|
|
|
|
return errAuthMethodsRequireTokenReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.AuthMethodList", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule, err := a.srv.ResolveToken(args.Token); err != nil {
|
|
|
|
return err
|
|
|
|
} else if rule == nil || !rule.ACLRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.srv.blockingQuery(&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, methods, err := state.ACLAuthMethodList(ws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stubs structs.ACLAuthMethodListStubs
|
|
|
|
for _, method := range methods {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Token != "" { // This shouldn't happen.
|
|
|
|
return errors.New("do not provide a token when logging in")
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.Login", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "login"}, time.Now())
|
|
|
|
|
|
|
|
auth := args.Auth
|
|
|
|
|
|
|
|
// 1. take args.Data.AuthMethod to get an AuthMethod Validator
|
|
|
|
idx, method, err := a.srv.fsm.State().ACLAuthMethodGetByName(nil, auth.AuthMethod)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if method == nil {
|
|
|
|
return acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
verifiedFields, err := validator.ValidateLogin(auth.BearerToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. send map through role bindings
|
|
|
|
serviceIdentities, roleLinks, err := a.srv.evaluateRoleBindings(validator, verifiedFields)
|
|
|
|
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.
|
2019-04-26 17:49:28 +00:00
|
|
|
if len(serviceIdentities) == 0 && len(roleLinks) == 0 {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
description := "token created via login"
|
|
|
|
loginMeta, err := encodeLoginMeta(auth.Meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if loginMeta != "" {
|
|
|
|
description += ": " + loginMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. create token
|
|
|
|
createReq := structs.ACLTokenSetRequest{
|
|
|
|
Datacenter: args.Datacenter,
|
|
|
|
ACLToken: structs.ACLToken{
|
|
|
|
Description: description,
|
|
|
|
Local: true,
|
|
|
|
AuthMethod: auth.AuthMethod,
|
|
|
|
ServiceIdentities: serviceIdentities,
|
|
|
|
Roles: roleLinks,
|
|
|
|
},
|
|
|
|
WriteRequest: args.WriteRequest,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. return token information like a TokenCreate would
|
2019-05-03 19:22:44 +00:00
|
|
|
err = a.tokenSetInternal(&createReq, reply, true)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
if done, err := a.srv.forward("ACL.Logout", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer metrics.MeasureSince([]string{"acl", "logout"}, time.Now())
|
|
|
|
|
|
|
|
_, token, err := a.srv.fsm.State().ACLTokenGetBySecret(nil, args.Token)
|
|
|
|
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
|
|
|
|
args.Datacenter = a.srv.config.ACLDatacenter
|
|
|
|
return a.srv.forwardDC("ACL.Logout", a.srv.config.ACLDatacenter, args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to check expiration time because it's being deleted.
|
|
|
|
|
|
|
|
req := &structs.ACLTokenBatchDeleteRequest{
|
|
|
|
TokenIDs: []string{token.AccessorID},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := a.srv.raftApply(structs.ACLTokenDeleteRequestType, req)
|
|
|
|
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
|
|
|
|
if token != nil {
|
|
|
|
a.srv.acls.cache.RemoveIdentity(token.SecretID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|