2016-08-30 02:09:57 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2016-11-15 02:54:37 +00:00
|
|
|
"fmt"
|
2016-08-30 02:09:57 +00:00
|
|
|
"net/http"
|
2017-02-24 04:32:13 +00:00
|
|
|
"strconv"
|
2017-03-10 00:43:07 +00:00
|
|
|
"time"
|
2016-08-30 02:09:57 +00:00
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-03-10 00:43:07 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-11-22 23:24:40 +00:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2016-08-30 02:09:57 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2020-09-25 17:46:38 +00:00
|
|
|
autopilot "github.com/hashicorp/raft-autopilot"
|
2016-08-30 02:09:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// OperatorRaftConfiguration is used to inspect the current Raft configuration.
|
|
|
|
// This supports the stale query mode in case the cluster doesn't have a leader.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) OperatorRaftConfiguration(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-08-30 02:09:57 +00:00
|
|
|
var args structs.DCSpecificRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply structs.RaftConfigurationResponse
|
|
|
|
if err := s.agent.RPC("Operator.RaftGetConfiguration", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperatorRaftPeer supports actions on Raft peers. Currently we only support
|
|
|
|
// removing peers by address.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) OperatorRaftPeer(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-03-30 01:09:41 +00:00
|
|
|
var args structs.RaftRemovePeerRequest
|
2016-08-30 02:09:57 +00:00
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
|
|
|
params := req.URL.Query()
|
2017-03-30 01:09:41 +00:00
|
|
|
_, hasID := params["id"]
|
|
|
|
if hasID {
|
|
|
|
args.ID = raft.ServerID(params.Get("id"))
|
|
|
|
}
|
|
|
|
_, hasAddress := params["address"]
|
|
|
|
if hasAddress {
|
2016-08-30 02:09:57 +00:00
|
|
|
args.Address = raft.ServerAddress(params.Get("address"))
|
2017-03-30 01:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !hasID && !hasAddress {
|
2019-08-12 18:11:11 +00:00
|
|
|
return nil, BadRequestError{Reason: "Must specify either ?id with the server's ID or ?address with IP:port of peer to remove"}
|
2017-03-30 01:09:41 +00:00
|
|
|
}
|
|
|
|
if hasID && hasAddress {
|
2019-08-12 18:11:11 +00:00
|
|
|
return nil, BadRequestError{Reason: "Must specify only one of ?id or ?address"}
|
2016-08-30 02:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var reply struct{}
|
2017-03-30 01:09:41 +00:00
|
|
|
method := "Operator.RaftRemovePeerByID"
|
|
|
|
if hasAddress {
|
|
|
|
method = "Operator.RaftRemovePeerByAddress"
|
|
|
|
}
|
|
|
|
if err := s.agent.RPC(method, &args, &reply); err != nil {
|
2016-08-30 02:09:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-30 01:09:41 +00:00
|
|
|
|
2016-08-30 02:09:57 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2016-11-15 02:54:37 +00:00
|
|
|
|
2016-11-22 23:24:40 +00:00
|
|
|
type keyringArgs struct {
|
2017-02-02 02:42:41 +00:00
|
|
|
Key string
|
|
|
|
Token string
|
|
|
|
RelayFactor uint8
|
2019-08-12 18:11:11 +00:00
|
|
|
LocalOnly bool // ?local-only; only used for GET requests
|
2016-11-22 23:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OperatorKeyringEndpoint handles keyring operations (install, list, use, remove)
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) OperatorKeyringEndpoint(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-11-22 23:24:40 +00:00
|
|
|
var args keyringArgs
|
|
|
|
if req.Method == "POST" || req.Method == "PUT" || req.Method == "DELETE" {
|
2019-10-29 18:13:36 +00:00
|
|
|
if err := decodeBody(req.Body, &args); err != nil {
|
2019-08-12 18:11:11 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Request decode failed: %v", err)}
|
2016-11-22 23:24:40 +00:00
|
|
|
}
|
2016-11-15 02:54:37 +00:00
|
|
|
}
|
2016-11-22 23:24:40 +00:00
|
|
|
s.parseToken(req, &args.Token)
|
2016-11-15 02:54:37 +00:00
|
|
|
|
2017-02-02 02:42:41 +00:00
|
|
|
// Parse relay factor
|
|
|
|
if relayFactor := req.URL.Query().Get("relay-factor"); relayFactor != "" {
|
|
|
|
n, err := strconv.Atoi(relayFactor)
|
|
|
|
if err != nil {
|
2019-08-12 18:11:11 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Error parsing relay factor: %v", err)}
|
2017-02-02 02:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args.RelayFactor, err = ParseRelayFactor(n)
|
|
|
|
if err != nil {
|
2019-08-12 18:11:11 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Invalid relay-factor: %v", err)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse local-only. local-only can only be used in GET requests.
|
|
|
|
if localOnly := req.URL.Query().Get("local-only"); localOnly != "" {
|
|
|
|
var err error
|
|
|
|
args.LocalOnly, err = strconv.ParseBool(localOnly)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Error parsing local-only: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ValidateLocalOnly(args.LocalOnly, req.Method == "GET")
|
|
|
|
if err != nil {
|
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Invalid use of local-only: %v", err)}
|
2017-02-02 02:42:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:24:40 +00:00
|
|
|
// Switch on the method
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
return s.KeyringList(resp, req, &args)
|
|
|
|
case "POST":
|
|
|
|
return s.KeyringInstall(resp, req, &args)
|
|
|
|
case "PUT":
|
|
|
|
return s.KeyringUse(resp, req, &args)
|
|
|
|
case "DELETE":
|
|
|
|
return s.KeyringRemove(resp, req, &args)
|
|
|
|
default:
|
2017-09-26 06:11:19 +00:00
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "POST", "PUT", "DELETE"}}
|
2016-11-15 02:54:37 +00:00
|
|
|
}
|
2016-11-22 23:24:40 +00:00
|
|
|
}
|
2016-11-15 02:54:37 +00:00
|
|
|
|
2016-11-22 23:24:40 +00:00
|
|
|
// KeyringInstall is used to install a new gossip encryption key into the cluster
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) KeyringInstall(resp http.ResponseWriter, req *http.Request, args *keyringArgs) (interface{}, error) {
|
2017-02-02 02:42:41 +00:00
|
|
|
responses, err := s.agent.InstallKey(args.Key, args.Token, args.RelayFactor)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:24:40 +00:00
|
|
|
return nil, keyringErrorsOrNil(responses.Responses)
|
2016-11-15 02:54:37 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:24:40 +00:00
|
|
|
// KeyringList is used to list the keys installed in the cluster
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) KeyringList(resp http.ResponseWriter, req *http.Request, args *keyringArgs) (interface{}, error) {
|
2020-08-10 20:24:15 +00:00
|
|
|
responses, err := s.agent.ListKeys(args.Token, args.LocalOnly, args.RelayFactor)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:24:40 +00:00
|
|
|
return responses.Responses, keyringErrorsOrNil(responses.Responses)
|
2016-11-15 02:54:37 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:24:40 +00:00
|
|
|
// KeyringRemove is used to list the keys installed in the cluster
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) KeyringRemove(resp http.ResponseWriter, req *http.Request, args *keyringArgs) (interface{}, error) {
|
2017-02-02 02:42:41 +00:00
|
|
|
responses, err := s.agent.RemoveKey(args.Key, args.Token, args.RelayFactor)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:24:40 +00:00
|
|
|
return nil, keyringErrorsOrNil(responses.Responses)
|
2016-11-15 02:54:37 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:24:40 +00:00
|
|
|
// KeyringUse is used to change the primary gossip encryption key
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) KeyringUse(resp http.ResponseWriter, req *http.Request, args *keyringArgs) (interface{}, error) {
|
2017-02-02 02:42:41 +00:00
|
|
|
responses, err := s.agent.UseKey(args.Key, args.Token, args.RelayFactor)
|
2016-11-15 02:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-22 23:24:40 +00:00
|
|
|
|
|
|
|
return nil, keyringErrorsOrNil(responses.Responses)
|
|
|
|
}
|
|
|
|
|
|
|
|
func keyringErrorsOrNil(responses []*structs.KeyringResponse) error {
|
|
|
|
var errs error
|
|
|
|
for _, response := range responses {
|
2016-11-15 02:54:37 +00:00
|
|
|
if response.Error != "" {
|
2017-02-08 23:25:47 +00:00
|
|
|
pool := response.Datacenter + " (LAN)"
|
|
|
|
if response.WAN {
|
|
|
|
pool = "WAN"
|
|
|
|
}
|
|
|
|
errs = multierror.Append(errs, fmt.Errorf("%s error: %s", pool, response.Error))
|
|
|
|
for key, message := range response.Messages {
|
|
|
|
errs = multierror.Append(errs, fmt.Errorf("%s: %s", key, message))
|
|
|
|
}
|
2016-11-15 02:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 23:24:40 +00:00
|
|
|
return errs
|
2016-11-15 02:54:37 +00:00
|
|
|
}
|
2017-02-24 04:32:13 +00:00
|
|
|
|
|
|
|
// OperatorAutopilotConfiguration is used to inspect the current Autopilot configuration.
|
|
|
|
// This supports the stale query mode in case the cluster doesn't have a leader.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) OperatorAutopilotConfiguration(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-02-24 04:32:13 +00:00
|
|
|
// Switch on the method
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
var args structs.DCSpecificRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
var reply structs.AutopilotConfig
|
2017-02-24 04:32:13 +00:00
|
|
|
if err := s.agent.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-10 00:43:07 +00:00
|
|
|
out := api.AutopilotConfiguration{
|
|
|
|
CleanupDeadServers: reply.CleanupDeadServers,
|
|
|
|
LastContactThreshold: api.NewReadableDuration(reply.LastContactThreshold),
|
|
|
|
MaxTrailingLogs: reply.MaxTrailingLogs,
|
2019-10-29 14:04:41 +00:00
|
|
|
MinQuorum: reply.MinQuorum,
|
2017-03-10 00:43:07 +00:00
|
|
|
ServerStabilizationTime: api.NewReadableDuration(reply.ServerStabilizationTime),
|
2017-03-21 23:36:44 +00:00
|
|
|
RedundancyZoneTag: reply.RedundancyZoneTag,
|
|
|
|
DisableUpgradeMigration: reply.DisableUpgradeMigration,
|
2017-07-20 00:39:48 +00:00
|
|
|
UpgradeVersionTag: reply.UpgradeVersionTag,
|
2017-03-10 00:43:07 +00:00
|
|
|
CreateIndex: reply.CreateIndex,
|
|
|
|
ModifyIndex: reply.ModifyIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
2017-04-21 01:59:42 +00:00
|
|
|
|
2017-02-24 04:32:13 +00:00
|
|
|
case "PUT":
|
|
|
|
var args structs.AutopilotSetConfigRequest
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
|
|
|
s.parseToken(req, &args.Token)
|
|
|
|
|
2021-07-02 15:35:09 +00:00
|
|
|
conf := api.NewAutopilotConfiguration()
|
2019-10-29 18:13:36 +00:00
|
|
|
if err := decodeBody(req.Body, &conf); err != nil {
|
2019-08-12 18:11:11 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Error parsing autopilot config: %v", err)}
|
2017-03-10 00:43:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
args.Config = structs.AutopilotConfig{
|
2017-03-10 00:43:07 +00:00
|
|
|
CleanupDeadServers: conf.CleanupDeadServers,
|
|
|
|
LastContactThreshold: conf.LastContactThreshold.Duration(),
|
|
|
|
MaxTrailingLogs: conf.MaxTrailingLogs,
|
2019-10-29 14:04:41 +00:00
|
|
|
MinQuorum: conf.MinQuorum,
|
2017-03-10 00:43:07 +00:00
|
|
|
ServerStabilizationTime: conf.ServerStabilizationTime.Duration(),
|
2017-03-21 23:36:44 +00:00
|
|
|
RedundancyZoneTag: conf.RedundancyZoneTag,
|
|
|
|
DisableUpgradeMigration: conf.DisableUpgradeMigration,
|
2017-07-20 00:39:48 +00:00
|
|
|
UpgradeVersionTag: conf.UpgradeVersionTag,
|
2017-03-10 00:43:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
// Check for cas value
|
|
|
|
params := req.URL.Query()
|
|
|
|
if _, ok := params["cas"]; ok {
|
|
|
|
casVal, err := strconv.ParseUint(params.Get("cas"), 10, 64)
|
|
|
|
if err != nil {
|
2019-08-12 18:11:11 +00:00
|
|
|
return nil, BadRequestError{Reason: fmt.Sprintf("Error parsing cas value: %v", err)}
|
2017-02-24 21:08:49 +00:00
|
|
|
}
|
|
|
|
args.Config.ModifyIndex = casVal
|
|
|
|
args.CAS = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply bool
|
2017-02-24 04:32:13 +00:00
|
|
|
if err := s.agent.RPC("Operator.AutopilotSetConfiguration", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
// Only use the out value if this was a CAS
|
|
|
|
if !args.CAS {
|
|
|
|
return true, nil
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return reply, nil
|
|
|
|
|
2017-02-24 04:32:13 +00:00
|
|
|
default:
|
2017-09-26 06:11:19 +00:00
|
|
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT"}}
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-01 22:04:40 +00:00
|
|
|
|
|
|
|
// OperatorServerHealth is used to get the health of the servers in the local DC
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) OperatorServerHealth(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-03-01 22:04:40 +00:00
|
|
|
var args structs.DCSpecificRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
var reply structs.AutopilotHealthReply
|
2017-03-01 22:04:40 +00:00
|
|
|
if err := s.agent.RPC("Operator.ServerHealth", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-07 21:58:06 +00:00
|
|
|
// Reply with status 429 if something is unhealthy
|
|
|
|
if !reply.Healthy {
|
|
|
|
resp.WriteHeader(http.StatusTooManyRequests)
|
|
|
|
}
|
|
|
|
|
2017-03-10 00:43:07 +00:00
|
|
|
out := &api.OperatorHealthReply{
|
|
|
|
Healthy: reply.Healthy,
|
|
|
|
FailureTolerance: reply.FailureTolerance,
|
|
|
|
}
|
|
|
|
for _, server := range reply.Servers {
|
|
|
|
out.Servers = append(out.Servers, api.ServerHealth{
|
|
|
|
ID: server.ID,
|
|
|
|
Name: server.Name,
|
2017-03-16 01:27:17 +00:00
|
|
|
Address: server.Address,
|
2017-03-21 23:36:44 +00:00
|
|
|
Version: server.Version,
|
|
|
|
Leader: server.Leader,
|
2017-03-10 00:43:07 +00:00
|
|
|
SerfStatus: server.SerfStatus.String(),
|
|
|
|
LastContact: api.NewReadableDuration(server.LastContact),
|
|
|
|
LastTerm: server.LastTerm,
|
|
|
|
LastIndex: server.LastIndex,
|
|
|
|
Healthy: server.Healthy,
|
2017-03-15 23:09:55 +00:00
|
|
|
Voter: server.Voter,
|
2017-03-10 00:43:07 +00:00
|
|
|
StableSince: server.StableSince.Round(time.Second).UTC(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|
2020-09-25 17:46:38 +00:00
|
|
|
|
|
|
|
func (s *HTTPHandlers) OperatorAutopilotState(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
var args structs.DCSpecificRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply autopilot.State
|
|
|
|
if err := s.agent.RPC("Operator.AutopilotState", &args, &reply); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := autopilotToAPIState(&reply)
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringIDs(ids []raft.ServerID) []string {
|
|
|
|
out := make([]string, len(ids))
|
|
|
|
for i, id := range ids {
|
|
|
|
out[i] = string(id)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func autopilotToAPIState(state *autopilot.State) *api.AutopilotState {
|
|
|
|
out := &api.AutopilotState{
|
|
|
|
Healthy: state.Healthy,
|
|
|
|
FailureTolerance: state.FailureTolerance,
|
|
|
|
Leader: string(state.Leader),
|
|
|
|
Voters: stringIDs(state.Voters),
|
|
|
|
Servers: make(map[string]api.AutopilotServer),
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, srv := range state.Servers {
|
|
|
|
out.Servers[string(id)] = autopilotToAPIServer(srv)
|
|
|
|
}
|
|
|
|
|
|
|
|
autopilotToAPIStateEnterprise(state, out)
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func autopilotToAPIServer(srv *autopilot.ServerState) api.AutopilotServer {
|
|
|
|
apiSrv := api.AutopilotServer{
|
|
|
|
ID: string(srv.Server.ID),
|
|
|
|
Name: srv.Server.Name,
|
|
|
|
Address: string(srv.Server.Address),
|
|
|
|
NodeStatus: string(srv.Server.NodeStatus),
|
|
|
|
Version: srv.Server.Version,
|
|
|
|
LastContact: api.NewReadableDuration(srv.Stats.LastContact),
|
|
|
|
LastTerm: srv.Stats.LastTerm,
|
|
|
|
LastIndex: srv.Stats.LastIndex,
|
|
|
|
Healthy: srv.Health.Healthy,
|
|
|
|
StableSince: srv.Health.StableSince,
|
|
|
|
Status: api.AutopilotServerStatus(srv.State),
|
|
|
|
Meta: srv.Server.Meta,
|
|
|
|
NodeType: api.AutopilotServerType(srv.Server.NodeType),
|
|
|
|
}
|
|
|
|
|
|
|
|
autopilotToAPIServerEnterprise(srv, &apiSrv)
|
|
|
|
|
|
|
|
return apiSrv
|
|
|
|
}
|