2014-08-06 00:50:36 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2015-05-06 02:25:10 +00:00
|
|
|
|
2017-08-23 14:52:48 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-12-06 16:14:56 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2014-08-06 00:50:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// aclCreateResponse is used to wrap the ACL ID
|
2018-10-19 16:04:07 +00:00
|
|
|
type aclBootstrapResponse struct {
|
2014-08-06 00:50:36 +00:00
|
|
|
ID string
|
2018-10-19 16:04:07 +00:00
|
|
|
structs.ACLToken
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 21:47:30 +00:00
|
|
|
// checkACLDisabled will return a standard response if ACLs are disabled. This
|
|
|
|
// returns true if they are disabled and we should not continue.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) checkACLDisabled(resp http.ResponseWriter, _req *http.Request) bool {
|
2020-07-03 20:52:08 +00:00
|
|
|
if s.agent.config.ACLsEnabled {
|
2017-11-28 21:47:30 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusUnauthorized)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, "ACL support disabled")
|
2017-11-28 21:47:30 +00:00
|
|
|
return true
|
2014-08-12 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
// ACLBootstrap is used to perform a one-time ACL bootstrap operation on
|
|
|
|
// a cluster to get the first management token.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLBootstrap(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 21:47:30 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
}
|
2021-08-13 22:31:32 +00:00
|
|
|
var out structs.ACLToken
|
|
|
|
err := s.agent.RPC("ACL.BootstrapTokens", &args, &out)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), structs.ACLBootstrapNotAllowedErr.Error()) {
|
|
|
|
resp.WriteHeader(http.StatusForbidden)
|
|
|
|
fmt.Fprint(resp, acl.PermissionDeniedError{Cause: err.Error()}.Error())
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-13 22:31:32 +00:00
|
|
|
return &aclBootstrapResponse{ID: out.SecretID, ACLToken: out}, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLReplicationStatus(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we do not forward to the ACL DC here. This is a query for
|
|
|
|
// any DC that's doing replication.
|
|
|
|
args := structs.DCSpecificRequest{}
|
|
|
|
s.parseSource(req, &args.Source)
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the request.
|
|
|
|
var out structs.ACLReplicationStatus
|
|
|
|
if err := s.agent.RPC("ACL.ReplicationStatus", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLPolicyList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 21:47:30 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
var args structs.ACLPolicyListRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLPolicyListResponse
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("ACL.PolicyList", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we return an array and not nil
|
|
|
|
if out.Policies == nil {
|
|
|
|
out.Policies = make(structs.ACLPolicyListStubs, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Policies, nil
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLPolicyCRUD(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 21:47:30 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
var fn func(resp http.ResponseWriter, req *http.Request, policyID string) (interface{}, error)
|
|
|
|
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
2020-03-25 14:34:24 +00:00
|
|
|
fn = s.ACLPolicyReadByID
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
fn = s.ACLPolicyWrite
|
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
fn = s.ACLPolicyDelete
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT", "DELETE"}}
|
|
|
|
}
|
|
|
|
|
|
|
|
policyID := strings.TrimPrefix(req.URL.Path, "/v1/acl/policy/")
|
|
|
|
if policyID == "" && req.Method != "PUT" {
|
|
|
|
return nil, BadRequestError{Reason: "Missing policy ID"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(resp, req, policyID)
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLPolicyRead(resp http.ResponseWriter, req *http.Request, policyID, policyName string) (interface{}, error) {
|
2018-10-31 20:00:46 +00:00
|
|
|
args := structs.ACLPolicyGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
PolicyID: policyID,
|
2020-03-25 14:34:24 +00:00
|
|
|
PolicyName: policyName,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
var out structs.ACLPolicyResponse
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("ACL.PolicyRead", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Policy == nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
// TODO(rb): should this return a normal 404?
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Policy, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLPolicyReadByName(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2020-03-25 14:34:24 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
policyName := strings.TrimPrefix(req.URL.Path, "/v1/acl/policy/name/")
|
|
|
|
if policyName == "" {
|
|
|
|
return nil, BadRequestError{Reason: "Missing policy Name"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.ACLPolicyRead(resp, req, "", policyName)
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLPolicyReadByID(resp http.ResponseWriter, req *http.Request, policyID string) (interface{}, error) {
|
2020-03-25 14:34:24 +00:00
|
|
|
return s.ACLPolicyRead(resp, req, policyID, "")
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLPolicyCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
return s.aclPolicyWriteInternal(resp, req, "", true)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLPolicyWrite(resp http.ResponseWriter, req *http.Request, policyID string) (interface{}, error) {
|
2019-04-30 15:45:36 +00:00
|
|
|
return s.aclPolicyWriteInternal(resp, req, policyID, false)
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) aclPolicyWriteInternal(_resp http.ResponseWriter, req *http.Request, policyID string, create bool) (interface{}, error) {
|
2018-10-31 20:00:46 +00:00
|
|
|
args := structs.ACLPolicySetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.Policy.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.Policy)); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Policy decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Policy.Syntax = acl.SyntaxCurrent
|
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
if create {
|
|
|
|
if args.Policy.ID != "" {
|
|
|
|
return nil, BadRequestError{Reason: "Cannot specify the ID when creating a new policy"}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if args.Policy.ID != "" && args.Policy.ID != policyID {
|
|
|
|
return nil, BadRequestError{Reason: "Policy ID in URL and payload do not match"}
|
|
|
|
} else if args.Policy.ID == "" {
|
|
|
|
args.Policy.ID = policyID
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLPolicy
|
2018-10-31 20:00:46 +00:00
|
|
|
if err := s.agent.RPC("ACL.PolicySet", args, &out); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLPolicyDelete(resp http.ResponseWriter, req *http.Request, policyID string) (interface{}, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
args := structs.ACLPolicyDeleteRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
PolicyID: policyID,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-02-13 15:16:30 +00:00
|
|
|
var ignored string
|
|
|
|
if err := s.agent.RPC("ACL.PolicyDelete", args, &ignored); err != nil {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
return true, nil
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 21:47:30 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2014-08-19 21:28:34 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
args := &structs.ACLTokenListRequest{
|
|
|
|
IncludeLocal: true,
|
|
|
|
IncludeGlobal: true,
|
2014-08-12 18:35:22 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
args.Policy = req.URL.Query().Get("policy")
|
2019-04-15 20:43:19 +00:00
|
|
|
args.Role = req.URL.Query().Get("role")
|
2019-04-26 17:49:28 +00:00
|
|
|
args.AuthMethod = req.URL.Query().Get("authmethod")
|
2020-01-14 15:09:29 +00:00
|
|
|
if err := parseACLAuthMethodEnterpriseMeta(req, &args.ACLAuthMethodEnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
var out structs.ACLTokenListResponse
|
2014-08-06 00:50:36 +00:00
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := s.agent.RPC("ACL.TokenList", &args, &out); err != nil {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
return out.Tokens, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenCRUD(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
var fn func(resp http.ResponseWriter, req *http.Request, tokenID string) (interface{}, error)
|
|
|
|
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
2018-10-31 20:00:46 +00:00
|
|
|
fn = s.ACLTokenGet
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
case "PUT":
|
2018-10-31 20:00:46 +00:00
|
|
|
fn = s.ACLTokenSet
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
fn = s.ACLTokenDelete
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT", "DELETE"}}
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
tokenID := strings.TrimPrefix(req.URL.Path, "/v1/acl/token/")
|
|
|
|
if strings.HasSuffix(tokenID, "/clone") && req.Method == "PUT" {
|
|
|
|
tokenID = tokenID[:len(tokenID)-6]
|
|
|
|
fn = s.ACLTokenClone
|
|
|
|
}
|
|
|
|
if tokenID == "" && req.Method != "PUT" {
|
|
|
|
return nil, BadRequestError{Reason: "Missing token ID"}
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
return fn(resp, req, tokenID)
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenSelf(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 21:47:30 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-09-26 06:11:19 +00:00
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
args := structs.ACLTokenGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
TokenIDType: structs.ACLTokenSecret,
|
2014-08-12 18:35:22 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// copy the token parameter to the ID
|
|
|
|
args.TokenID = args.Token
|
|
|
|
|
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
var out structs.ACLTokenResponse
|
2014-08-06 00:50:36 +00:00
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := s.agent.RPC("ACL.TokenRead", &args, &out); err != nil {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if out.Token == nil {
|
|
|
|
return nil, acl.ErrNotFound
|
2015-11-15 05:05:37 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return out.Token, nil
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-11-28 21:47:30 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-09-26 06:11:19 +00:00
|
|
|
|
2021-07-20 21:52:34 +00:00
|
|
|
return s.aclTokenSetInternal(req, "", true)
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenGet(resp http.ResponseWriter, req *http.Request, tokenID string) (interface{}, error) {
|
2018-10-31 20:00:46 +00:00
|
|
|
args := structs.ACLTokenGetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
TokenID: tokenID,
|
|
|
|
TokenIDType: structs.ACLTokenAccessor,
|
2014-08-12 18:35:22 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLTokenResponse
|
2014-08-06 00:50:36 +00:00
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := s.agent.RPC("ACL.TokenRead", &args, &out); err != nil {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if out.Token == nil {
|
|
|
|
return nil, acl.ErrNotFound
|
2015-11-15 05:05:37 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return out.Token, nil
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
2016-08-05 04:32:36 +00:00
|
|
|
|
2021-07-20 21:52:34 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenSet(_ http.ResponseWriter, req *http.Request, tokenID string) (interface{}, error) {
|
|
|
|
return s.aclTokenSetInternal(req, tokenID, false)
|
2019-04-30 15:45:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 21:52:34 +00:00
|
|
|
func (s *HTTPHandlers) aclTokenSetInternal(req *http.Request, tokenID string, create bool) (interface{}, error) {
|
2018-10-31 20:00:46 +00:00
|
|
|
args := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.agent.config.Datacenter,
|
2019-04-30 15:45:36 +00:00
|
|
|
Create: create,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.ACLToken.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.ACLToken)); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Token decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
if !create {
|
|
|
|
if args.ACLToken.AccessorID != "" && args.ACLToken.AccessorID != tokenID {
|
|
|
|
return nil, BadRequestError{Reason: "Token Accessor ID in URL and payload do not match"}
|
|
|
|
} else if args.ACLToken.AccessorID == "" {
|
|
|
|
args.ACLToken.AccessorID = tokenID
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLToken
|
2018-10-31 20:00:46 +00:00
|
|
|
if err := s.agent.RPC("ACL.TokenSet", args, &out); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenDelete(resp http.ResponseWriter, req *http.Request, tokenID string) (interface{}, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
args := structs.ACLTokenDeleteRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
TokenID: tokenID,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-02-13 15:16:30 +00:00
|
|
|
var ignored string
|
|
|
|
if err := s.agent.RPC("ACL.TokenDelete", args, &ignored); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenClone(resp http.ResponseWriter, req *http.Request, tokenID string) (interface{}, error) {
|
2017-11-28 21:47:30 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-09-26 06:11:19 +00:00
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
args := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.agent.config.Datacenter,
|
2019-04-30 15:45:36 +00:00
|
|
|
Create: true,
|
2016-08-05 04:32:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.ACLToken.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.ACLToken)); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Token decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
|
|
|
// Set this for the ID to clone
|
|
|
|
args.ACLToken.AccessorID = tokenID
|
|
|
|
|
|
|
|
var out structs.ACLToken
|
|
|
|
if err := s.agent.RPC("ACL.TokenClone", args, &out); err != nil {
|
2016-08-05 04:32:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
return &out, nil
|
2016-08-05 04:32:36 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLRoleList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var args structs.ACLRoleListRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
args.Policy = req.URL.Query().Get("policy")
|
|
|
|
|
|
|
|
var out structs.ACLRoleListResponse
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("ACL.RoleList", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we return an array and not nil
|
|
|
|
if out.Roles == nil {
|
|
|
|
out.Roles = make(structs.ACLRoles, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Roles, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLRoleCRUD(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var fn func(resp http.ResponseWriter, req *http.Request, roleID string) (interface{}, error)
|
|
|
|
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
fn = s.ACLRoleReadByID
|
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
fn = s.ACLRoleWrite
|
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
fn = s.ACLRoleDelete
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT", "DELETE"}}
|
|
|
|
}
|
|
|
|
|
|
|
|
roleID := strings.TrimPrefix(req.URL.Path, "/v1/acl/role/")
|
|
|
|
if roleID == "" && req.Method != "PUT" {
|
|
|
|
return nil, BadRequestError{Reason: "Missing role ID"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(resp, req, roleID)
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLRoleReadByName(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
roleName := strings.TrimPrefix(req.URL.Path, "/v1/acl/role/name/")
|
|
|
|
if roleName == "" {
|
|
|
|
return nil, BadRequestError{Reason: "Missing role Name"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.ACLRoleRead(resp, req, "", roleName)
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLRoleReadByID(resp http.ResponseWriter, req *http.Request, roleID string) (interface{}, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
return s.ACLRoleRead(resp, req, roleID, "")
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLRoleRead(resp http.ResponseWriter, req *http.Request, roleID, roleName string) (interface{}, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
args := structs.ACLRoleGetRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
RoleID: roleID,
|
|
|
|
RoleName: roleName,
|
|
|
|
}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLRoleResponse
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("ACL.RoleRead", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Role == nil {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Role, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLRoleCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.ACLRoleWrite(resp, req, "")
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLRoleWrite(resp http.ResponseWriter, req *http.Request, roleID string) (interface{}, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
args := structs.ACLRoleSetRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.Role.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.Role)); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Role decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Role.ID != "" && args.Role.ID != roleID {
|
|
|
|
return nil, BadRequestError{Reason: "Role ID in URL and payload do not match"}
|
|
|
|
} else if args.Role.ID == "" {
|
|
|
|
args.Role.ID = roleID
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLRole
|
|
|
|
if err := s.agent.RPC("ACL.RoleSet", args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLRoleDelete(resp http.ResponseWriter, req *http.Request, roleID string) (interface{}, error) {
|
2019-04-15 20:43:19 +00:00
|
|
|
args := structs.ACLRoleDeleteRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
RoleID: roleID,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
var ignored string
|
|
|
|
if err := s.agent.RPC("ACL.RoleDelete", args, &ignored); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLBindingRuleList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var args structs.ACLBindingRuleListRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
args.AuthMethod = req.URL.Query().Get("authmethod")
|
|
|
|
|
|
|
|
var out structs.ACLBindingRuleListResponse
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("ACL.BindingRuleList", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we return an array and not nil
|
|
|
|
if out.BindingRules == nil {
|
|
|
|
out.BindingRules = make(structs.ACLBindingRules, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.BindingRules, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLBindingRuleCRUD(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var fn func(resp http.ResponseWriter, req *http.Request, bindingRuleID string) (interface{}, error)
|
|
|
|
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
fn = s.ACLBindingRuleRead
|
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
fn = s.ACLBindingRuleWrite
|
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
fn = s.ACLBindingRuleDelete
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT", "DELETE"}}
|
|
|
|
}
|
|
|
|
|
|
|
|
bindingRuleID := strings.TrimPrefix(req.URL.Path, "/v1/acl/binding-rule/")
|
|
|
|
if bindingRuleID == "" && req.Method != "PUT" {
|
|
|
|
return nil, BadRequestError{Reason: "Missing binding rule ID"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(resp, req, bindingRuleID)
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLBindingRuleRead(resp http.ResponseWriter, req *http.Request, bindingRuleID string) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
args := structs.ACLBindingRuleGetRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
BindingRuleID: bindingRuleID,
|
|
|
|
}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLBindingRuleResponse
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("ACL.BindingRuleRead", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.BindingRule == nil {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.BindingRule, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLBindingRuleCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.ACLBindingRuleWrite(resp, req, "")
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLBindingRuleWrite(resp http.ResponseWriter, req *http.Request, bindingRuleID string) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
args := structs.ACLBindingRuleSetRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.BindingRule.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.BindingRule)); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("BindingRule decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.BindingRule.ID != "" && args.BindingRule.ID != bindingRuleID {
|
|
|
|
return nil, BadRequestError{Reason: "BindingRule ID in URL and payload do not match"}
|
|
|
|
} else if args.BindingRule.ID == "" {
|
|
|
|
args.BindingRule.ID = bindingRuleID
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLBindingRule
|
|
|
|
if err := s.agent.RPC("ACL.BindingRuleSet", args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLBindingRuleDelete(resp http.ResponseWriter, req *http.Request, bindingRuleID string) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
args := structs.ACLBindingRuleDeleteRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
BindingRuleID: bindingRuleID,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
var ignored bool
|
|
|
|
if err := s.agent.RPC("ACL.BindingRuleDelete", args, &ignored); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLAuthMethodList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var args structs.ACLAuthMethodListRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLAuthMethodListResponse
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("ACL.AuthMethodList", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we return an array and not nil
|
|
|
|
if out.AuthMethods == nil {
|
|
|
|
out.AuthMethods = make(structs.ACLAuthMethodListStubs, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.AuthMethods, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLAuthMethodCRUD(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var fn func(resp http.ResponseWriter, req *http.Request, methodName string) (interface{}, error)
|
|
|
|
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
fn = s.ACLAuthMethodRead
|
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
fn = s.ACLAuthMethodWrite
|
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
fn = s.ACLAuthMethodDelete
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT", "DELETE"}}
|
|
|
|
}
|
|
|
|
|
|
|
|
methodName := strings.TrimPrefix(req.URL.Path, "/v1/acl/auth-method/")
|
|
|
|
if methodName == "" && req.Method != "PUT" {
|
|
|
|
return nil, BadRequestError{Reason: "Missing auth method name"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(resp, req, methodName)
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLAuthMethodRead(resp http.ResponseWriter, req *http.Request, methodName string) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
args := structs.ACLAuthMethodGetRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
AuthMethodName: methodName,
|
|
|
|
}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLAuthMethodResponse
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("ACL.AuthMethodRead", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.AuthMethod == nil {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fixupAuthMethodConfig(out.AuthMethod)
|
|
|
|
return out.AuthMethod, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLAuthMethodCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.ACLAuthMethodWrite(resp, req, "")
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLAuthMethodWrite(resp http.ResponseWriter, req *http.Request, methodName string) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
args := structs.ACLAuthMethodSetRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.AuthMethod.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.AuthMethod)); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("AuthMethod decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if methodName != "" {
|
|
|
|
if args.AuthMethod.Name != "" && args.AuthMethod.Name != methodName {
|
|
|
|
return nil, BadRequestError{Reason: "AuthMethod Name in URL and payload do not match"}
|
|
|
|
} else if args.AuthMethod.Name == "" {
|
|
|
|
args.AuthMethod.Name = methodName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLAuthMethod
|
|
|
|
if err := s.agent.RPC("ACL.AuthMethodSet", args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fixupAuthMethodConfig(&out)
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLAuthMethodDelete(resp http.ResponseWriter, req *http.Request, methodName string) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
args := structs.ACLAuthMethodDeleteRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
AuthMethodName: methodName,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
|
|
|
var ignored bool
|
|
|
|
if err := s.agent.RPC("ACL.AuthMethodDelete", args, &ignored); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLLogin(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
args := &structs.ACLLoginRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
2019-10-24 18:38:09 +00:00
|
|
|
Auth: &structs.ACLLoginParams{},
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.Auth.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.Auth)); err != nil {
|
2020-05-01 19:52:15 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Failed to decode request body: %v", err)}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLToken
|
|
|
|
if err := s.agent.RPC("ACL.Login", args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLLogout(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-26 17:49:28 +00:00
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.ACLLogoutRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
|
|
|
if args.Token == "" {
|
|
|
|
return nil, acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
var ignored bool
|
|
|
|
if err := s.agent.RPC("ACL.Logout", &args, &ignored); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A hack to fix up the config types inside of the map[string]interface{}
|
|
|
|
// so that they get formatted correctly during json.Marshal. Without this,
|
|
|
|
// string values that get converted to []uint8 end up getting output back
|
|
|
|
// to the user in base64-encoded form.
|
|
|
|
func fixupAuthMethodConfig(method *structs.ACLAuthMethod) {
|
|
|
|
for k, v := range method.Config {
|
|
|
|
if raw, ok := v.([]uint8); ok {
|
|
|
|
strVal := structs.Uint8ToString(raw)
|
|
|
|
method.Config[k] = strVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 14:25:26 +00:00
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLAuthorize(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-12-06 14:25:26 +00:00
|
|
|
// At first glance it may appear like this endpoint is going to leak security relevant information.
|
|
|
|
// There are a number of reason why this is okay.
|
|
|
|
//
|
|
|
|
// 1. The authorizations performed here are the same as what would be done if other HTTP APIs
|
|
|
|
// were used. This is just a way to see if it would be allowed. In the future when we have
|
|
|
|
// audit logging, these authorization checks will be logged along with those from the real
|
|
|
|
// endpoints. In that respect, you can figure out if you have access just as easily by
|
|
|
|
// attempting to perform the requested operation.
|
|
|
|
// 2. In order to use this API you must have a valid ACL token secret.
|
|
|
|
// 3. Along with #2 you can use the ACL.GetPolicy RPC endpoint which will return a rolled up
|
|
|
|
// set of policy rules showing your tokens effective policy. This RPC endpoint exposes
|
|
|
|
// more information than this one and has been around since before v1.0.0. With that other
|
|
|
|
// endpoint you get to see all things possible rather than having to have a list of things
|
|
|
|
// you may want to do and to request authorizations for each one.
|
|
|
|
// 4. In addition to the legacy ACL.GetPolicy RPC endpoint we have an ACL.PolicyResolve and
|
|
|
|
// ACL.RoleResolve endpoints. These RPC endpoints allow reading roles and policies so long
|
|
|
|
// as the token used for the request is linked with them. This is needed to allow client
|
|
|
|
// agents to pull the policy and roles for a token that they are resolving. The only
|
|
|
|
// alternative to this style of access would be to make every agent use a token
|
|
|
|
// with acl:read privileges for all policy and role resolution requests. Once you have
|
|
|
|
// all the associated policies and roles it would be easy enough to recreate the effective
|
|
|
|
// policy.
|
|
|
|
const maxRequests = 64
|
|
|
|
|
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
request := structs.RemoteACLAuthorizationRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
AllowStale: true,
|
|
|
|
RequireConsistent: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var responses []structs.ACLAuthorizationResponse
|
|
|
|
|
|
|
|
s.parseToken(req, &request.Token)
|
|
|
|
s.parseDC(req, &request.Datacenter)
|
|
|
|
|
|
|
|
if err := decodeBody(req.Body, &request.Requests); err != nil {
|
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Failed to decode request body: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(request.Requests) > maxRequests {
|
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Refusing to process more than %d authorizations at once", maxRequests)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(request.Requests) == 0 {
|
|
|
|
return make([]structs.ACLAuthorizationResponse, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Datacenter != "" && request.Datacenter != s.agent.config.Datacenter {
|
|
|
|
// when we are targeting a datacenter other than our own then we must issue an RPC
|
|
|
|
// to perform the resolution as it may involve a local token
|
|
|
|
if err := s.agent.RPC("ACL.Authorize", &request, &responses); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(request.Token, nil, nil)
|
2019-12-06 14:25:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
responses, err = structs.CreateACLAuthorizationResponses(authz, request.Requests)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BadRequestError{Reason: err.Error()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if responses == nil {
|
|
|
|
responses = make([]structs.ACLAuthorizationResponse, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return responses, nil
|
|
|
|
}
|