2019-04-29 22:08:09 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
2020-06-05 21:54:29 +00:00
|
|
|
const ConfigEntryNotFoundErr string = "Config entry not found"
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
// Config switches on the different CRUD operations for config entries.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) Config(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-29 22:08:09 +00:00
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
return s.configGet(resp, req)
|
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
return s.configDelete(resp, req)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "DELETE"}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// configGet gets either a specific config entry, or lists all config entries
|
|
|
|
// of a kind if no name is provided.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) configGet(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-29 22:08:09 +00:00
|
|
|
var args structs.ConfigEntryQuery
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-01-11 13:52:45 +00:00
|
|
|
kindAndName, err := getPathSuffixUnescaped(req.URL.Path, "/v1/config/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pathArgs := strings.SplitN(kindAndName, "/", 2)
|
2019-04-29 22:08:09 +00:00
|
|
|
|
2020-01-30 00:12:48 +00:00
|
|
|
switch len(pathArgs) {
|
|
|
|
case 2:
|
2019-04-29 22:08:09 +00:00
|
|
|
// Both kind/name provided.
|
|
|
|
args.Kind = pathArgs[0]
|
|
|
|
args.Name = pathArgs[1]
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if err := s.parseEntMetaForConfigEntryKind(args.Kind, req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
var reply structs.ConfigEntryResponse
|
|
|
|
if err := s.agent.RPC("ConfigEntry.Get", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-26 15:42:17 +00:00
|
|
|
setMeta(resp, &reply.QueryMeta)
|
2019-04-29 22:08:09 +00:00
|
|
|
|
|
|
|
if reply.Entry == nil {
|
2020-06-05 21:54:29 +00:00
|
|
|
return nil, NotFoundError{Reason: fmt.Sprintf("%s for %q / %q", ConfigEntryNotFoundErr, pathArgs[0], pathArgs[1])}
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return reply.Entry, nil
|
|
|
|
case 1:
|
2020-01-30 00:12:48 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-29 22:08:09 +00:00
|
|
|
// Only kind provided, list entries.
|
|
|
|
args.Kind = pathArgs[0]
|
|
|
|
|
|
|
|
var reply structs.IndexedConfigEntries
|
|
|
|
if err := s.agent.RPC("ConfigEntry.List", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-26 15:42:17 +00:00
|
|
|
setMeta(resp, &reply.QueryMeta)
|
2019-04-29 22:08:09 +00:00
|
|
|
|
|
|
|
return reply.Entries, nil
|
|
|
|
default:
|
2019-04-30 22:19:19 +00:00
|
|
|
return nil, NotFoundError{Reason: "Must provide either a kind or both kind and name"}
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// configDelete deletes the given config entry.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) configDelete(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-29 22:08:09 +00:00
|
|
|
var args structs.ConfigEntryRequest
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
2022-01-11 13:52:45 +00:00
|
|
|
kindAndName, err := getPathSuffixUnescaped(req.URL.Path, "/v1/config/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pathArgs := strings.SplitN(kindAndName, "/", 2)
|
2019-04-29 22:08:09 +00:00
|
|
|
|
|
|
|
if len(pathArgs) != 2 {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprintf(resp, "Must provide both a kind and name to delete")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := structs.MakeConfigEntry(pathArgs[0], pathArgs[1])
|
|
|
|
if err != nil {
|
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
|
|
|
fmt.Fprintf(resp, "%v", err)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
args.Entry = entry
|
2020-01-24 15:04:58 +00:00
|
|
|
// Parse enterprise meta.
|
|
|
|
meta := args.Entry.GetEnterpriseMeta()
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
if err := s.parseEntMetaForConfigEntryKind(entry.GetKind(), req, meta); err != nil {
|
2020-01-24 15:04:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-29 22:08:09 +00:00
|
|
|
|
2021-11-01 16:42:01 +00:00
|
|
|
// Check for cas value
|
|
|
|
if casStr := req.URL.Query().Get("cas"); casStr != "" {
|
|
|
|
casVal, err := strconv.ParseUint(casStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
args.Op = structs.ConfigEntryDeleteCAS
|
|
|
|
args.Entry.GetRaftIndex().ModifyIndex = casVal
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply structs.ConfigEntryDeleteResponse
|
2019-04-29 22:08:09 +00:00
|
|
|
if err := s.agent.RPC("ConfigEntry.Delete", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-01 16:42:01 +00:00
|
|
|
// Return the `deleted` boolean for CAS operations, but not normal deletions
|
|
|
|
// to maintain backwards-compatibility with existing callers.
|
|
|
|
if args.Op == structs.ConfigEntryDeleteCAS {
|
|
|
|
return reply.Deleted, nil
|
|
|
|
}
|
|
|
|
return struct{}{}, nil
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:59:30 +00:00
|
|
|
// ConfigApply applies the given config entry update.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) ConfigApply(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-04-29 22:08:09 +00:00
|
|
|
args := structs.ConfigEntryRequest{
|
|
|
|
Op: structs.ConfigEntryUpsert,
|
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
|
|
|
var raw map[string]interface{}
|
2019-10-29 18:13:36 +00:00
|
|
|
if err := decodeBodyDeprecated(req, &raw, nil); err != nil {
|
2019-04-29 22:08:09 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Request decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry, err := structs.DecodeConfigEntry(raw); err == nil {
|
|
|
|
args.Entry = entry
|
|
|
|
} else {
|
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Request decoding failed: %v", err)}
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:04:58 +00:00
|
|
|
// Parse enterprise meta.
|
|
|
|
var meta structs.EnterpriseMeta
|
2020-10-06 18:24:05 +00:00
|
|
|
if err := s.parseEntMetaForConfigEntryKind(args.Entry.GetKind(), req, &meta); err != nil {
|
2020-01-24 15:04:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
args.Entry.GetEnterpriseMeta().Merge(&meta)
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
// Check for cas value
|
|
|
|
if casStr := req.URL.Query().Get("cas"); casStr != "" {
|
|
|
|
casVal, err := strconv.ParseUint(casStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
args.Op = structs.ConfigEntryUpsertCAS
|
|
|
|
args.Entry.GetRaftIndex().ModifyIndex = casVal
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
var reply bool
|
|
|
|
if err := s.agent.RPC("ConfigEntry.Apply", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply, nil
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
func (s *HTTPHandlers) parseEntMetaForConfigEntryKind(kind string, req *http.Request, entMeta *structs.EnterpriseMeta) error {
|
|
|
|
if kind == structs.ServiceIntentions {
|
|
|
|
return s.parseEntMeta(req, entMeta)
|
|
|
|
}
|
|
|
|
return s.parseEntMetaNoWildcard(req, entMeta)
|
|
|
|
}
|