2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2014-08-06 00:50:36 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-08-03 20:21:43 +00:00
|
|
|
"net/url"
|
2014-08-06 00:50:36 +00:00
|
|
|
"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"
|
2023-01-04 20:19:33 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
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
|
|
|
}
|
|
|
|
|
2022-04-29 17:42:49 +00:00
|
|
|
var aclDisabled = HTTPError{StatusCode: http.StatusUnauthorized, Reason: "ACL support disabled"}
|
2022-01-05 17:11:03 +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.
|
2022-01-05 17:11:03 +00:00
|
|
|
func (s *HTTPHandlers) checkACLDisabled() bool {
|
2020-07-03 20:52:08 +00:00
|
|
|
if s.agent.config.ACLsEnabled {
|
2017-11-28 21:47:30 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2017-11-28 21:47:30 +00:00
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
|
2023-01-04 20:19:33 +00:00
|
|
|
args := structs.ACLInitialTokenBootstrapRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
}
|
2023-01-04 20:19:33 +00:00
|
|
|
|
|
|
|
// Handle optional request body
|
|
|
|
if req.ContentLength > 0 {
|
|
|
|
var bootstrapSecretRequest api.BootstrapRequest
|
|
|
|
if err := lib.DecodeJSON(req.Body, &bootstrapSecretRequest); err != nil {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Request decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
args.BootstrapSecret = bootstrapSecretRequest.BootstrapSecret
|
|
|
|
}
|
|
|
|
|
2021-08-13 22:31:32 +00:00
|
|
|
var out structs.ACLToken
|
2022-12-14 15:24:22 +00:00
|
|
|
err := s.agent.RPC(req.Context(), "ACL.BootstrapTokens", &args, &out)
|
2021-08-13 22:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), structs.ACLBootstrapNotAllowedErr.Error()) {
|
2022-01-05 17:11:03 +00:00
|
|
|
return nil, acl.PermissionDeniedError{Cause: err.Error()}
|
2021-08-13 22:31:32 +00:00
|
|
|
} 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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.ReplicationStatus", &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) ACLPolicyList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2017-11-28 21:47:30 +00:00
|
|
|
}
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.PolicyList", &args, &out); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2017-11-28 21:47:30 +00:00
|
|
|
}
|
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"}}
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:17:14 +00:00
|
|
|
policyID := strings.TrimPrefix(req.URL.Path, "/v1/acl/policy/")
|
2018-10-19 16:04:07 +00:00
|
|
|
if policyID == "" && req.Method != "PUT" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing policy ID"}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-08-03 20:21:43 +00:00
|
|
|
// policy name needs to be unescaped in case there were `/` characters
|
|
|
|
policyName, err := url.QueryUnescape(policyName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.PolicyRead", &args, &out); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
// should return permission denied error if missing permissions
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Policy == nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
// if no error was returned above, the policy does not exist
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
msg := acl.ACLResourceNotExistError("policy", args.EnterpriseMeta)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: msg.Error()}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out.Policy, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLPolicyReadByName(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2020-03-25 14:34:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:17:14 +00:00
|
|
|
policyName := strings.TrimPrefix(req.URL.Path, "/v1/acl/policy/name/")
|
2020-03-25 14:34:24 +00:00
|
|
|
if policyName == "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing policy Name"}
|
2020-03-25 14:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Policy decoding failed: %v", err)}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:45:36 +00:00
|
|
|
if create {
|
|
|
|
if args.Policy.ID != "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Cannot specify the ID when creating a new policy"}
|
2019-04-30 15:45:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if args.Policy.ID != "" && args.Policy.ID != policyID {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Policy ID in URL and payload do not match"}
|
2019-04-30 15:45:36 +00:00
|
|
|
} else if args.Policy.ID == "" {
|
|
|
|
args.Policy.ID = policyID
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLPolicy
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.PolicyDelete", args, &ignored); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
if strings.Contains(err.Error(), acl.ErrNotFound.Error()) {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: "Cannot find policy to delete"}
|
|
|
|
}
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2017-11-28 21:47:30 +00:00
|
|
|
}
|
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")
|
2023-09-06 15:52:45 +00:00
|
|
|
args.ServiceName = req.URL.Query().Get("servicename")
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 18:26:30 +00:00
|
|
|
var fn func(resp http.ResponseWriter, req *http.Request, tokenAccessorID string) (interface{}, error)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-07 18:26:30 +00:00
|
|
|
tokenAccessorID := strings.TrimPrefix(req.URL.Path, "/v1/acl/token/")
|
|
|
|
if strings.HasSuffix(tokenAccessorID, "/clone") && req.Method == "PUT" {
|
|
|
|
tokenAccessorID = tokenAccessorID[:len(tokenAccessorID)-6]
|
2018-10-19 16:04:07 +00:00
|
|
|
fn = s.ACLTokenClone
|
|
|
|
}
|
2023-02-07 18:26:30 +00:00
|
|
|
if tokenAccessorID == "" && req.Method != "PUT" {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing token AccessorID"}
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 18:26:30 +00:00
|
|
|
return fn(resp, req, tokenAccessorID)
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2017-11-28 21:47:30 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:26:30 +00:00
|
|
|
// copy the token secret parameter to the ID
|
2018-10-19 16:04:07 +00:00
|
|
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.TokenRead", &args, &out); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
// should return permission denied error if missing permissions
|
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 {
|
2023-02-08 23:49:44 +00:00
|
|
|
// if no error was returned above, the token does not exist
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: "Supplied token does not exist"}
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2017-11-28 21:47:30 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-02-07 18:26:30 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenGet(resp http.ResponseWriter, req *http.Request, tokenAccessorID 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,
|
2023-02-07 18:26:30 +00:00
|
|
|
TokenID: tokenAccessorID,
|
2018-10-19 16:04:07 +00:00
|
|
|
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
|
|
|
|
}
|
2022-03-31 16:49:11 +00:00
|
|
|
if _, ok := req.URL.Query()["expanded"]; ok {
|
|
|
|
args.Expanded = true
|
|
|
|
}
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "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 {
|
2023-02-08 23:49:44 +00:00
|
|
|
// if no error was returned above, the token does not exist
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
msg := acl.ACLResourceNotExistError("token", args.EnterpriseMeta)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: msg.Error()}
|
2015-11-15 05:05:37 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2022-03-31 16:49:11 +00:00
|
|
|
if args.Expanded {
|
|
|
|
expanded := &structs.ACLTokenExpanded{
|
|
|
|
ACLToken: out.Token,
|
|
|
|
ExpandedTokenInfo: out.ExpandedTokenInfo,
|
|
|
|
}
|
|
|
|
return expanded, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-02-07 18:26:30 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenSet(_ http.ResponseWriter, req *http.Request, tokenAccessorID string) (interface{}, error) {
|
|
|
|
return s.aclTokenSetInternal(req, tokenAccessorID, false)
|
2019-04-30 15:45:36 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 18:26:30 +00:00
|
|
|
func (s *HTTPHandlers) aclTokenSetInternal(req *http.Request, tokenAccessorID 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 {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Token decoding failed: %v", err)}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 18:31:40 +00:00
|
|
|
if !create {
|
|
|
|
// NOTE: AccessorID in the request body is optional when not creating a new token.
|
|
|
|
// If not present in the body and only in the URL then it will be filled in by Consul.
|
|
|
|
if args.ACLToken.AccessorID == "" {
|
|
|
|
args.ACLToken.AccessorID = tokenAccessorID
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.ACLToken.AccessorID != tokenAccessorID {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Token Accessor ID in URL and payload do not match"}
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLToken
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.TokenSet", args, &out); err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:26:30 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenDelete(resp http.ResponseWriter, req *http.Request, tokenAccessorID string) (interface{}, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
args := structs.ACLTokenDeleteRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
2023-02-07 18:26:30 +00:00
|
|
|
TokenID: tokenAccessorID,
|
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.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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.TokenDelete", args, &ignored); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
if strings.Contains(err.Error(), acl.ErrNotFound.Error()) {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: "Cannot find token to delete"}
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:26:30 +00:00
|
|
|
func (s *HTTPHandlers) ACLTokenClone(resp http.ResponseWriter, req *http.Request, tokenAccessorID string) (interface{}, error) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2017-11-28 21:47:30 +00:00
|
|
|
}
|
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 {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Token decoding failed: %v", err)}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
|
|
|
// Set this for the ID to clone
|
2023-02-07 18:26:30 +00:00
|
|
|
args.ACLToken.AccessorID = tokenAccessorID
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
var out structs.ACLToken
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.RoleList", &args, &out); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"}}
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:17:14 +00:00
|
|
|
roleID := strings.TrimPrefix(req.URL.Path, "/v1/acl/role/")
|
2019-04-15 20:43:19 +00:00
|
|
|
if roleID == "" && req.Method != "PUT" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing role ID"}
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:17:14 +00:00
|
|
|
roleName := strings.TrimPrefix(req.URL.Path, "/v1/acl/role/name/")
|
2019-04-15 20:43:19 +00:00
|
|
|
if roleName == "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing role Name"}
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.RoleRead", &args, &out); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
// should return permission denied error if missing permissions
|
2019-04-15 20:43:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Role == nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
// if not permission denied error is returned above, role does not exist
|
2019-04-15 20:43:19 +00:00
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
2023-02-08 23:49:44 +00:00
|
|
|
msg := acl.ACLResourceNotExistError("role", args.EnterpriseMeta)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: msg.Error()}
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out.Role, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLRoleCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Role decoding failed: %v", err)}
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if args.Role.ID != "" && args.Role.ID != roleID {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Role ID in URL and payload do not match"}
|
2019-04-15 20:43:19 +00:00
|
|
|
} else if args.Role.ID == "" {
|
|
|
|
args.Role.ID = roleID
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLRole
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.RoleSet", args, &out); err != nil {
|
2019-04-15 20:43:19 +00:00
|
|
|
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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.RoleDelete", args, &ignored); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
if strings.Contains(err.Error(), acl.ErrNotFound.Error()) {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: "Cannot find role to delete"}
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.BindingRuleList", &args, &out); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"}}
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:17:14 +00:00
|
|
|
bindingRuleID := strings.TrimPrefix(req.URL.Path, "/v1/acl/binding-rule/")
|
2019-04-26 17:49:28 +00:00
|
|
|
if bindingRuleID == "" && req.Method != "PUT" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing binding rule ID"}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.BindingRuleRead", &args, &out); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
// should return permission denied error if missing permissions
|
2019-04-26 17:49:28 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.BindingRule == nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
// if no error was returned above, the binding rule does not exist
|
2019-04-26 17:49:28 +00:00
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
2023-02-08 23:49:44 +00:00
|
|
|
msg := acl.ACLResourceNotExistError("binding rule", args.EnterpriseMeta)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: msg.Error()}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out.BindingRule, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ACLBindingRuleCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-02-17 21:41:27 +00:00
|
|
|
args := structs.ACLBindingRuleSetRequest{}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
2019-04-26 17:49:28 +00:00
|
|
|
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 {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("BindingRule decoding failed: %v", err)}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if args.BindingRule.ID != "" && args.BindingRule.ID != bindingRuleID {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "BindingRule ID in URL and payload do not match"}
|
2019-04-26 17:49:28 +00:00
|
|
|
} else if args.BindingRule.ID == "" {
|
|
|
|
args.BindingRule.ID = bindingRuleID
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLBindingRule
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.BindingRuleSet", args, &out); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
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{
|
|
|
|
BindingRuleID: bindingRuleID,
|
|
|
|
}
|
2022-02-17 21:41:27 +00:00
|
|
|
s.parseDC(req, &args.Datacenter)
|
2019-04-26 17:49:28 +00:00
|
|
|
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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.BindingRuleDelete", args, &ignored); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
if strings.Contains(err.Error(), acl.ErrNotFound.Error()) {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: "Cannot find binding rule to delete"}
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.AuthMethodList", &args, &out); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"}}
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:17:14 +00:00
|
|
|
methodName := strings.TrimPrefix(req.URL.Path, "/v1/acl/auth-method/")
|
2019-04-26 17:49:28 +00:00
|
|
|
if methodName == "" && req.Method != "PUT" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing auth method name"}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.AuthMethodRead", &args, &out); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
// should return permission denied if missing permissions
|
2019-04-26 17:49:28 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.AuthMethod == nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
// if no error was returned above, the auth method does not exist
|
2019-04-26 17:49:28 +00:00
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
2023-02-08 23:49:44 +00:00
|
|
|
msg := acl.ACLResourceNotExistError("auth method", args.EnterpriseMeta)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: msg.Error()}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-02-17 21:41:27 +00:00
|
|
|
args := structs.ACLAuthMethodSetRequest{}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
2019-04-26 17:49:28 +00:00
|
|
|
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 {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("AuthMethod decoding failed: %v", err)}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if methodName != "" {
|
|
|
|
if args.AuthMethod.Name != "" && args.AuthMethod.Name != methodName {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "AuthMethod Name in URL and payload do not match"}
|
2019-04-26 17:49:28 +00:00
|
|
|
} else if args.AuthMethod.Name == "" {
|
|
|
|
args.AuthMethod.Name = methodName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLAuthMethod
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.AuthMethodSet", args, &out); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
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{
|
|
|
|
AuthMethodName: methodName,
|
|
|
|
}
|
2022-02-17 21:41:27 +00:00
|
|
|
s.parseDC(req, &args.Datacenter)
|
2019-04-26 17:49:28 +00:00
|
|
|
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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.AuthMethodDelete", args, &ignored); err != nil {
|
2023-02-08 23:49:44 +00:00
|
|
|
if strings.Contains(err.Error(), acl.ErrNotFound.Error()) {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: "Cannot find auth method to delete"}
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Failed to decode request body: %v", err)}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ACLToken
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.Login", args, &out); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
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) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.ACLLogoutRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
|
|
|
if args.Token == "" {
|
2023-02-08 23:49:44 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusUnauthorized, Reason: "Supplied token does not exist"}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ignored bool
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.Logout", &args, &ignored); err != nil {
|
2019-04-26 17:49:28 +00:00
|
|
|
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
|
2023-02-06 15:35:52 +00:00
|
|
|
// were used. This is just a way to see if it would be allowed. 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.
|
2019-12-06 14:25:26 +00:00
|
|
|
// 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
|
|
|
|
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
2019-12-06 14:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Failed to decode request body: %v", err)}
|
2019-12-06 14:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(request.Requests) > maxRequests {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Refusing to process more than %d authorizations at once", maxRequests)}
|
2019-12-06 14:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := s.agent.RPC(req.Context(), "ACL.Authorize", &request, &responses); err != nil {
|
2019-12-06 14:25:26 +00:00
|
|
|
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 {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: err.Error()}
|
2019-12-06 14:25:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if responses == nil {
|
|
|
|
responses = make([]structs.ACLAuthorizationResponse, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return responses, nil
|
|
|
|
}
|
2023-09-11 18:11:37 +00:00
|
|
|
|
|
|
|
type ACLTemplatedPolicyResponse struct {
|
|
|
|
TemplateName string
|
|
|
|
Schema string
|
|
|
|
Template string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPHandlers) ACLTemplatedPoliciesList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
|
|
|
var entMeta acl.EnterpriseMeta
|
|
|
|
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.defaultMetaPartitionToAgent(&entMeta)
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &entMeta, &authzContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only ACLRead privileges are required to list templated policies
|
|
|
|
if err := authz.ToAllowAuthorizer().ACLReadAllowed(&authzContext); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
templatedPolicies := make(map[string]ACLTemplatedPolicyResponse)
|
|
|
|
|
|
|
|
for tp, tmpBase := range structs.GetACLTemplatedPolicyList() {
|
|
|
|
templatedPolicies[tp] = ACLTemplatedPolicyResponse{
|
|
|
|
TemplateName: tmpBase.TemplateName,
|
|
|
|
Schema: tmpBase.Schema,
|
|
|
|
Template: tmpBase.Template,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return templatedPolicies, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPHandlers) ACLTemplatedPolicyRead(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
templateName := strings.TrimPrefix(req.URL.Path, "/v1/acl/templated-policy/name/")
|
|
|
|
if templateName == "" {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing templated policy Name"}
|
|
|
|
}
|
|
|
|
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
|
|
|
var entMeta acl.EnterpriseMeta
|
|
|
|
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.defaultMetaPartitionToAgent(&entMeta)
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &entMeta, &authzContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only ACLRead privileges are required to read templated policies
|
|
|
|
if err := authz.ToAllowAuthorizer().ACLReadAllowed(&authzContext); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
baseTemplate, ok := structs.GetACLTemplatedPolicyBase(templateName)
|
|
|
|
if !ok {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid templated policy Name: %s", templateName)}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ACLTemplatedPolicyResponse{
|
|
|
|
TemplateName: baseTemplate.TemplateName,
|
|
|
|
Schema: baseTemplate.Schema,
|
|
|
|
Template: baseTemplate.Template,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPHandlers) ACLTemplatedPolicyPreview(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if s.checkACLDisabled() {
|
|
|
|
return nil, aclDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
templateName := strings.TrimPrefix(req.URL.Path, "/v1/acl/templated-policy/preview/")
|
|
|
|
if templateName == "" {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing templated policy Name"}
|
|
|
|
}
|
|
|
|
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
|
|
|
var entMeta acl.EnterpriseMeta
|
|
|
|
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.defaultMetaPartitionToAgent(&entMeta)
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &entMeta, &authzContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only ACLRead privileges are required to read/preview templated policies
|
|
|
|
if err := authz.ToAllowAuthorizer().ACLReadAllowed(&authzContext); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
baseTemplate, ok := structs.GetACLTemplatedPolicyBase(templateName)
|
|
|
|
if !ok {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("templated policy %q does not exist", templateName)}
|
|
|
|
}
|
|
|
|
|
|
|
|
var tpRequest structs.ACLTemplatedPolicyVariables
|
|
|
|
|
|
|
|
if err := decodeBody(req.Body, &tpRequest); err != nil {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Failed to decode request body: %s", err.Error())}
|
|
|
|
}
|
|
|
|
|
|
|
|
templatedPolicy := structs.ACLTemplatedPolicy{
|
|
|
|
TemplateID: baseTemplate.TemplateID,
|
|
|
|
TemplateName: baseTemplate.TemplateName,
|
|
|
|
TemplateVariables: &tpRequest,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = templatedPolicy.ValidateTemplatedPolicy(baseTemplate.Schema)
|
|
|
|
if err != nil {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("validation error for templated policy: %q: %s", templatedPolicy.TemplateName, err.Error())}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderedPolicy, err := templatedPolicy.SyntheticPolicy(&entMeta)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, HTTPError{StatusCode: http.StatusInternalServerError, Reason: fmt.Sprintf("Failed to generate synthetic policy: %q: %s", templatedPolicy.TemplateName, err.Error())}
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderedPolicy, nil
|
|
|
|
}
|