2014-08-06 00:50:36 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-19 16:04:07 +00:00
|
|
|
"io/ioutil"
|
2014-08-06 00:50:36 +00:00
|
|
|
"net/http"
|
2018-10-19 16:04:07 +00:00
|
|
|
"strconv"
|
2014-08-06 00:50:36 +00:00
|
|
|
"strings"
|
2018-10-19 16:04:07 +00:00
|
|
|
"time"
|
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"
|
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.
|
|
|
|
func (s *HTTPServer) checkACLDisabled(resp http.ResponseWriter, req *http.Request) bool {
|
2018-10-19 16:04:07 +00:00
|
|
|
if s.agent.delegate.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.
|
|
|
|
func (s *HTTPServer) 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
legacy := false
|
|
|
|
legacyStr := req.URL.Query().Get("legacy")
|
|
|
|
if legacyStr != "" {
|
|
|
|
legacy, _ = strconv.ParseBool(legacyStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if legacy && s.agent.delegate.UseLegacyACLs() {
|
|
|
|
var out structs.ACL
|
|
|
|
err := s.agent.RPC("ACL.Bootstrap", &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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &aclBootstrapResponse{ID: out.ID}, nil
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &aclBootstrapResponse{ID: out.SecretID, ACLToken: out}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) ACLReplicationStatus(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) ACLRulesTranslate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if s.checkACLDisabled(resp, req) {
|
|
|
|
return nil, nil
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 22:11:51 +00:00
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
rule, err := s.agent.resolveToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Should this require lesser permissions? Really the only reason to require authorization at all is
|
|
|
|
// to prevent external entities from DoS Consul with repeated rule translation requests
|
|
|
|
if rule != nil && !rule.ACLRead() {
|
|
|
|
return nil, acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
policyBytes, err := ioutil.ReadAll(req.Body)
|
2017-08-03 00:05:18 +00:00
|
|
|
if err != nil {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Failed to read body: %v", err)}
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
translated, err := acl.TranslateLegacyRules(policyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BadRequestError{Reason: err.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Write(translated)
|
|
|
|
return nil, nil
|
2017-08-03 00:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *HTTPServer) ACLRulesTranslateLegacyToken(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
|
|
|
tokenID := strings.TrimPrefix(req.URL.Path, "/v1/acl/rules/translate/")
|
|
|
|
if tokenID == "" {
|
|
|
|
return nil, BadRequestError{Reason: "Missing token ID"}
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
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
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not allow blocking
|
|
|
|
args.QueryOptions.MinQueryIndex = 0
|
|
|
|
|
|
|
|
var out structs.ACLTokenResponse
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("ACL.TokenRead", &args, &out); err != nil {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
if out.Token == nil {
|
|
|
|
return nil, acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Token.Rules == "" {
|
|
|
|
return nil, fmt.Errorf("The specified token does not have any rules set")
|
|
|
|
}
|
|
|
|
|
|
|
|
translated, err := acl.TranslateLegacyRules([]byte(out.Token.Rules))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse legacy rules: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Write(translated)
|
|
|
|
return nil, nil
|
2014-08-06 00:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *HTTPServer) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *HTTPServer) 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":
|
|
|
|
fn = s.ACLPolicyRead
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *HTTPServer) ACLPolicyRead(resp http.ResponseWriter, req *http.Request, policyID 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,
|
|
|
|
}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
2014-08-06 00:50:36 +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 {
|
|
|
|
return nil, acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Policy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) ACLPolicyCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if s.checkACLDisabled(resp, req) {
|
2014-08-06 00:50:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
return s.ACLPolicyWrite(resp, req, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// fixCreateTimeAndHash is used to help in decoding the CreateTime and Hash
|
2018-10-25 15:09:46 +00:00
|
|
|
// attributes from the ACL Token/Policy create/update requests. It is needed
|
2018-10-19 16:04:07 +00:00
|
|
|
// to help mapstructure decode things properly when decodeBody is used.
|
|
|
|
func fixCreateTimeAndHash(raw interface{}) error {
|
|
|
|
rawMap, ok := raw.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := rawMap["CreateTime"]; ok {
|
|
|
|
if sval, ok := val.(string); ok {
|
|
|
|
t, err := time.Parse(time.RFC3339, sval)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rawMap["CreateTime"] = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := rawMap["Hash"]; ok {
|
|
|
|
if sval, ok := val.(string); ok {
|
|
|
|
rawMap["Hash"] = []byte(sval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) ACLPolicyWrite(resp http.ResponseWriter, req *http.Request, policyID string) (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)
|
|
|
|
|
2018-10-25 15:09:46 +00:00
|
|
|
if err := decodeBody(req, &args.Policy, fixCreateTimeAndHash); 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
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) ACLPolicyDelete(resp http.ResponseWriter, req *http.Request, policyID string) (interface{}, error) {
|
|
|
|
args := structs.ACLPolicyDeleteRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
PolicyID: policyID,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *HTTPServer) 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
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) ACLTokenCRUD(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *HTTPServer) 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
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (s *HTTPServer) 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
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
return s.ACLTokenSet(resp, req, "")
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (s *HTTPServer) ACLTokenGet(resp http.ResponseWriter, req *http.Request, tokenID string) (interface{}, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
func (s *HTTPServer) ACLTokenSet(resp http.ResponseWriter, req *http.Request, tokenID string) (interface{}, error) {
|
|
|
|
args := structs.ACLTokenSetRequest{
|
2018-10-19 16:04:07 +00:00
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
|
|
|
if err := decodeBody(req, &args.ACLToken, fixCreateTimeAndHash); err != nil {
|
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Token decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) ACLTokenDelete(resp http.ResponseWriter, req *http.Request, tokenID string) (interface{}, error) {
|
|
|
|
args := structs.ACLTokenDeleteRequest{
|
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
TokenID: tokenID,
|
|
|
|
}
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) 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,
|
2016-08-05 04:32:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if err := decodeBody(req, &args.ACLToken, fixCreateTimeAndHash); err != nil && err.Error() != "EOF" {
|
|
|
|
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
|
|
|
}
|