2014-08-06 00:05:59 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-19 16:04:07 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2014-08-08 23:00:32 +00:00
|
|
|
"time"
|
|
|
|
|
2014-08-06 00:05:59 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2014-08-08 23:00:32 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
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"
|
2017-01-24 08:00:06 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2016-01-29 19:42:34 +00:00
|
|
|
"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
|
|
|
|
var validPolicyName = regexp.MustCompile(`^[A-Za-z0-9\-_]{1,128}$`)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
} else if token == nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
Policies: token.Policies,
|
|
|
|
Local: token.Local,
|
|
|
|
Description: token.Description,
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (a *ACL) tokenSetInternal(args *structs.ACLTokenSetRequest, reply *structs.ACLToken, upgrade 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()
|
|
|
|
|
|
|
|
if token.AccessorID == "" {
|
|
|
|
// Token Create
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Generate the AccessorID
|
|
|
|
token.AccessorID, err = lib.GenerateUUID(a.srv.checkTokenUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the SecretID - not supporting non-UUID secrets
|
|
|
|
token.SecretID, err = lib.GenerateUUID(a.srv.checkTokenUUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
token.CreateTime = time.Now()
|
|
|
|
} 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
|
|
|
|
_, existing, err := state.ACLTokenGetByAccessor(nil, token.AccessorID)
|
2014-08-08 23:00:32 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Failed to lookup the acl token %q: %v", token.AccessorID, err)
|
|
|
|
}
|
|
|
|
if existing == nil {
|
|
|
|
return fmt.Errorf("Cannot find token %q", token.AccessorID)
|
|
|
|
}
|
|
|
|
if token.SecretID == "" {
|
|
|
|
token.SecretID = existing.SecretID
|
|
|
|
} else if existing.SecretID != token.SecretID {
|
|
|
|
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
|
|
|
|
if token.Local != existing.Local {
|
|
|
|
return fmt.Errorf("cannot toggle local mode of %s", token.AccessorID)
|
2014-08-06 17:30:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if upgrade {
|
|
|
|
token.CreateTime = time.Now()
|
|
|
|
} else {
|
|
|
|
token.CreateTime = existing.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
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
index, tokens, err := state.ACLTokenList(ws, args.IncludeLocal, args.IncludeGlobal, args.Policy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stubs := make([]*structs.ACLTokenListStub, 0, len(tokens))
|
|
|
|
for _, token := range tokens {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if _, existing, err := state.ACLPolicyGetByName(nil, policy.Name); err != nil {
|
|
|
|
return fmt.Errorf("acl policy lookup by name failed: %v", err)
|
|
|
|
} else if existing != nil {
|
|
|
|
return fmt.Errorf("Invalid Policy: A Policy with Name %q already exists", policy.Name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := uuid.ParseUUID(policy.ID); err != nil {
|
|
|
|
return fmt.Errorf("Policy ID invalid UUID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the policy exists
|
|
|
|
_, existing, err := state.ACLPolicyGetByID(nil, policy.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("acl policy lookup failed: %v", err)
|
|
|
|
} else if existing == nil {
|
|
|
|
return fmt.Errorf("cannot find policy %s", policy.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing.Name != policy.Name {
|
|
|
|
if _, nameMatch, err := state.ACLPolicyGetByName(nil, policy.Name); err != nil {
|
|
|
|
return fmt.Errorf("acl policy lookup by name failed: %v", err)
|
|
|
|
} else if nameMatch != nil {
|
|
|
|
return fmt.Errorf("Invalid Policy: A policy with name %q already exists", policy.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if policy.Rules != existing.Rules {
|
|
|
|
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
|
|
|
|
_, err := acl.NewPolicyFromSource("", 0, policy.Rules, policy.Syntax, a.srv.sentinel)
|
|
|
|
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
|
|
|
|
}
|