2014-01-04 01:15:51 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2021-06-14 22:37:05 +00:00
|
|
|
"encoding/json"
|
2014-01-30 23:35:38 +00:00
|
|
|
"fmt"
|
2014-01-04 01:15:51 +00:00
|
|
|
"net/http"
|
2015-01-21 17:53:31 +00:00
|
|
|
"strconv"
|
2014-01-04 01:15:51 +00:00
|
|
|
"strings"
|
2021-06-15 16:05:52 +00:00
|
|
|
"time"
|
2018-04-19 10:15:32 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-10-04 18:37:34 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
"github.com/mitchellh/hashstructure"
|
|
|
|
|
2020-11-30 17:53:46 +00:00
|
|
|
"github.com/hashicorp/go-bexpr"
|
|
|
|
"github.com/hashicorp/serf/coordinate"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
|
2017-08-23 14:52:48 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2019-03-06 17:13:28 +00:00
|
|
|
cachetype "github.com/hashicorp/consul/agent/cache-types"
|
2021-10-26 20:08:55 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul"
|
2018-10-17 20:20:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent/debug"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-02-27 19:28:31 +00:00
|
|
|
token_store "github.com/hashicorp/consul/agent/token"
|
2020-08-26 15:04:11 +00:00
|
|
|
"github.com/hashicorp/consul/agent/xds/proxysupport"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2017-05-15 20:10:36 +00:00
|
|
|
"github.com/hashicorp/consul/ipaddr"
|
2017-08-14 14:36:07 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/logging"
|
|
|
|
"github.com/hashicorp/consul/logging/monitor"
|
2016-06-06 20:19:31 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-01-04 01:15:51 +00:00
|
|
|
)
|
|
|
|
|
2017-04-21 00:46:29 +00:00
|
|
|
type Self struct {
|
2017-10-04 17:43:17 +00:00
|
|
|
Config interface{}
|
|
|
|
DebugConfig map[string]interface{}
|
|
|
|
Coord *coordinate.Coordinate
|
|
|
|
Member serf.Member
|
|
|
|
Stats map[string]map[string]string
|
|
|
|
Meta map[string]string
|
2021-07-09 19:17:29 +00:00
|
|
|
XDS *XDSSelf `json:"xDS,omitempty"`
|
2020-08-26 15:04:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 19:17:29 +00:00
|
|
|
type XDSSelf struct {
|
2020-08-26 15:04:11 +00:00
|
|
|
SupportedProxies map[string][]string
|
2021-07-09 19:17:29 +00:00
|
|
|
Port int
|
2014-05-27 22:09:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentSelf(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-12-14 17:33:57 +00:00
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-26 19:20:57 +00:00
|
|
|
|
|
|
|
// Authorize using the agent's own enterprise meta, not the token.
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-10-26 20:08:55 +00:00
|
|
|
s.agent.AgentEnterpriseMeta().FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().AgentReadAllowed(s.agent.config.NodeName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 23:25:22 +00:00
|
|
|
var cs lib.CoordinateSet
|
|
|
|
if !s.agent.config.DisableCoordinates {
|
|
|
|
var err error
|
|
|
|
if cs, err = s.agent.GetLANCoordinate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:17:29 +00:00
|
|
|
var xds *XDSSelf
|
2022-03-22 12:40:24 +00:00
|
|
|
if s.agent.xdsServer != nil {
|
2021-07-09 19:17:29 +00:00
|
|
|
xds = &XDSSelf{
|
2020-08-26 15:04:11 +00:00
|
|
|
SupportedProxies: map[string][]string{
|
|
|
|
"envoy": proxysupport.EnvoyVersions,
|
|
|
|
},
|
2021-07-09 19:17:29 +00:00
|
|
|
Port: s.agent.config.GRPCPort,
|
2020-08-26 15:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:43:17 +00:00
|
|
|
config := struct {
|
2021-06-10 21:19:16 +00:00
|
|
|
Datacenter string
|
|
|
|
PrimaryDatacenter string
|
|
|
|
NodeName string
|
|
|
|
NodeID string
|
2021-08-19 20:09:42 +00:00
|
|
|
Partition string `json:",omitempty"`
|
2021-06-10 21:19:16 +00:00
|
|
|
Revision string
|
|
|
|
Server bool
|
|
|
|
Version string
|
2017-10-04 17:43:17 +00:00
|
|
|
}{
|
2021-06-10 21:19:16 +00:00
|
|
|
Datacenter: s.agent.config.Datacenter,
|
|
|
|
PrimaryDatacenter: s.agent.config.PrimaryDatacenter,
|
|
|
|
NodeName: s.agent.config.NodeName,
|
|
|
|
NodeID: string(s.agent.config.NodeID),
|
2021-08-19 20:09:42 +00:00
|
|
|
Partition: s.agent.config.PartitionOrEmpty(),
|
2021-06-10 21:19:16 +00:00
|
|
|
Revision: s.agent.config.Revision,
|
|
|
|
Server: s.agent.config.ServerMode,
|
2022-05-05 02:16:18 +00:00
|
|
|
// We expect the ent version to be part of the reported version string, and that's now part of the metadata, not the actual version.
|
|
|
|
Version: s.agent.config.VersionWithMetadata(),
|
2017-10-04 17:43:17 +00:00
|
|
|
}
|
2017-04-21 00:46:29 +00:00
|
|
|
return Self{
|
2017-10-04 17:43:17 +00:00
|
|
|
Config: config,
|
2021-09-08 15:48:41 +00:00
|
|
|
DebugConfig: s.agent.config.Sanitized(),
|
2017-10-04 17:43:17 +00:00
|
|
|
Coord: cs[s.agent.config.SegmentName],
|
2021-10-26 20:08:55 +00:00
|
|
|
Member: s.agent.AgentLocalMember(),
|
2017-10-04 17:43:17 +00:00
|
|
|
Stats: s.agent.Stats(),
|
2017-08-28 12:17:13 +00:00
|
|
|
Meta: s.agent.State.Metadata(),
|
2020-08-26 15:04:11 +00:00
|
|
|
XDS: xds,
|
2014-05-27 22:09:28 +00:00
|
|
|
}, nil
|
2014-05-25 23:59:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 13:18:19 +00:00
|
|
|
// acceptsOpenMetricsMimeType returns true if mime type is Prometheus-compatible
|
|
|
|
func acceptsOpenMetricsMimeType(acceptHeader string) bool {
|
|
|
|
mimeTypes := strings.Split(acceptHeader, ",")
|
|
|
|
for _, v := range mimeTypes {
|
|
|
|
mimeInfo := strings.Split(v, ";")
|
|
|
|
if len(mimeInfo) > 0 {
|
|
|
|
rawMime := strings.ToLower(strings.Trim(mimeInfo[0], " "))
|
|
|
|
if rawMime == "application/openmetrics-text" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if rawMime == "text/plain" && (len(mimeInfo) > 1 && strings.Trim(mimeInfo[1], " ") == "version=0.4.0") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-09 11:16:03 +00:00
|
|
|
// enablePrometheusOutput will look for Prometheus mime-type or format Query parameter the same way as Nomad
|
|
|
|
func enablePrometheusOutput(req *http.Request) bool {
|
|
|
|
if format := req.URL.Query().Get("format"); format == "prometheus" {
|
|
|
|
return true
|
|
|
|
}
|
2020-03-03 13:18:19 +00:00
|
|
|
return acceptsOpenMetricsMimeType(req.Header.Get("Accept"))
|
2018-04-09 11:16:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentMetrics(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-08-08 20:05:38 +00:00
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2017-08-08 20:05:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-26 19:20:57 +00:00
|
|
|
|
|
|
|
// Authorize using the agent's own enterprise meta, not the token.
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-10-26 20:08:55 +00:00
|
|
|
s.agent.AgentEnterpriseMeta().FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().AgentReadAllowed(s.agent.config.NodeName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2017-08-08 20:05:38 +00:00
|
|
|
}
|
2018-04-09 11:16:03 +00:00
|
|
|
if enablePrometheusOutput(req) {
|
2020-11-16 20:44:47 +00:00
|
|
|
if s.agent.config.Telemetry.PrometheusOpts.Expiration < 1 {
|
2022-01-05 17:11:03 +00:00
|
|
|
return nil, CodeWithPayloadError{
|
|
|
|
StatusCode: http.StatusUnsupportedMediaType,
|
|
|
|
Reason: "Prometheus is not enabled since its retention time is not positive",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
}
|
2018-04-06 12:21:05 +00:00
|
|
|
}
|
2018-04-06 06:55:49 +00:00
|
|
|
handlerOptions := promhttp.HandlerOpts{
|
2020-01-28 23:50:41 +00:00
|
|
|
ErrorLog: s.agent.logger.StandardLogger(&hclog.StandardLoggerOptions{
|
|
|
|
InferLevels: true,
|
|
|
|
}),
|
2018-04-09 11:16:03 +00:00
|
|
|
ErrorHandling: promhttp.ContinueOnError,
|
2018-04-06 06:55:49 +00:00
|
|
|
}
|
2017-08-08 20:05:38 +00:00
|
|
|
|
2018-04-06 06:55:49 +00:00
|
|
|
handler := promhttp.HandlerFor(prometheus.DefaultGatherer, handlerOptions)
|
|
|
|
handler.ServeHTTP(resp, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-05-19 20:03:46 +00:00
|
|
|
return s.agent.baseDeps.MetricsConfig.Handler.DisplayMetrics(resp, req)
|
2017-08-08 20:05:38 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 22:37:05 +00:00
|
|
|
func (s *HTTPHandlers) AgentMetricsStream(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2021-10-26 19:20:57 +00:00
|
|
|
if err != nil {
|
2021-06-14 22:37:05 +00:00
|
|
|
return nil, err
|
2021-10-26 19:20:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Authorize using the agent's own enterprise meta, not the token.
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-10-26 20:08:55 +00:00
|
|
|
s.agent.AgentEnterpriseMeta().FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().AgentReadAllowed(s.agent.config.NodeName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2021-06-14 22:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flusher, ok := resp.(http.Flusher)
|
|
|
|
if !ok {
|
2021-07-14 22:52:57 +00:00
|
|
|
return nil, fmt.Errorf("streaming not supported")
|
2021-06-14 22:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
// 0 byte write is needed before the Flush call so that if we are using
|
|
|
|
// a gzip stream it will go ahead and write out the HTTP response header
|
|
|
|
resp.Write([]byte(""))
|
|
|
|
flusher.Flush()
|
|
|
|
|
|
|
|
enc := metricsEncoder{
|
|
|
|
logger: s.agent.logger,
|
|
|
|
encoder: json.NewEncoder(resp),
|
|
|
|
flusher: flusher,
|
|
|
|
}
|
2021-07-14 22:52:57 +00:00
|
|
|
enc.encoder.SetIndent("", " ")
|
2022-05-19 20:03:46 +00:00
|
|
|
s.agent.baseDeps.MetricsConfig.Handler.Stream(req.Context(), enc)
|
2021-06-14 22:37:05 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type metricsEncoder struct {
|
|
|
|
logger hclog.Logger
|
|
|
|
encoder *json.Encoder
|
|
|
|
flusher http.Flusher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m metricsEncoder) Encode(summary interface{}) error {
|
|
|
|
if err := m.encoder.Encode(summary); err != nil {
|
|
|
|
m.logger.Error("failed to encode metrics summary", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.flusher.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentReload(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-12-14 17:33:57 +00:00
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-26 19:20:57 +00:00
|
|
|
|
|
|
|
// Authorize using the agent's own enterprise meta, not the token.
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-10-26 20:08:55 +00:00
|
|
|
s.agent.AgentEnterpriseMeta().FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().AgentWriteAllowed(s.agent.config.NodeName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
2016-11-30 18:29:42 +00:00
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
return nil, s.agent.ReloadConfig()
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 21:27:31 +00:00
|
|
|
func buildAgentService(s *structs.NodeService, dc string) api.AgentService {
|
2019-01-07 14:39:23 +00:00
|
|
|
weights := api.AgentWeights{Passing: 1, Warning: 1}
|
|
|
|
if s.Weights != nil {
|
|
|
|
if s.Weights.Passing > 0 {
|
|
|
|
weights.Passing = s.Weights.Passing
|
|
|
|
}
|
|
|
|
weights.Warning = s.Weights.Warning
|
|
|
|
}
|
2019-06-17 14:51:50 +00:00
|
|
|
|
|
|
|
var taggedAddrs map[string]api.ServiceAddress
|
|
|
|
if len(s.TaggedAddresses) > 0 {
|
|
|
|
taggedAddrs = make(map[string]api.ServiceAddress)
|
|
|
|
for k, v := range s.TaggedAddresses {
|
|
|
|
taggedAddrs[k] = v.ToAPIServiceAddress()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:39:23 +00:00
|
|
|
as := api.AgentService{
|
|
|
|
Kind: api.ServiceKind(s.Kind),
|
|
|
|
ID: s.ID,
|
|
|
|
Service: s.Service,
|
|
|
|
Tags: s.Tags,
|
|
|
|
Meta: s.Meta,
|
|
|
|
Port: s.Port,
|
|
|
|
Address: s.Address,
|
2021-08-12 17:05:22 +00:00
|
|
|
SocketPath: s.SocketPath,
|
2019-06-17 14:51:50 +00:00
|
|
|
TaggedAddresses: taggedAddrs,
|
2019-01-07 14:39:23 +00:00
|
|
|
EnableTagOverride: s.EnableTagOverride,
|
|
|
|
CreateIndex: s.CreateIndex,
|
|
|
|
ModifyIndex: s.ModifyIndex,
|
|
|
|
Weights: weights,
|
2020-11-19 21:27:31 +00:00
|
|
|
Datacenter: dc,
|
2019-01-07 14:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if as.Tags == nil {
|
|
|
|
as.Tags = []string{}
|
|
|
|
}
|
|
|
|
if as.Meta == nil {
|
|
|
|
as.Meta = map[string]string{}
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
// Attach Proxy config if exists
|
2020-03-26 16:20:56 +00:00
|
|
|
if s.Kind == structs.ServiceKindConnectProxy || s.IsGateway() {
|
2019-01-07 14:39:23 +00:00
|
|
|
as.Proxy = s.Proxy.ToAPI()
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:19:30 +00:00
|
|
|
// Attach Connect configs if they exist.
|
|
|
|
if s.Connect.Native {
|
2019-01-07 14:39:23 +00:00
|
|
|
as.Connect = &api.AgentServiceConnect{
|
|
|
|
Native: true,
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
fillAgentServiceEnterpriseMeta(&as, &s.EnterpriseMeta)
|
2019-01-07 14:39:23 +00:00
|
|
|
return as
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentServices(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-12-14 22:16:46 +00:00
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
var entMeta acl.EnterpriseMeta
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
var filterExpression string
|
|
|
|
s.parseFilter(req, &filterExpression)
|
|
|
|
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &entMeta, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &entMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: we're explicitly fetching things in the requested partition and
|
|
|
|
// namespace here.
|
2019-12-10 02:26:41 +00:00
|
|
|
services := s.agent.State.Services(&entMeta)
|
2017-04-28 01:22:07 +00:00
|
|
|
|
2018-04-20 13:24:24 +00:00
|
|
|
// Convert into api.AgentService since that includes Connect config but so far
|
|
|
|
// NodeService doesn't need to internally. They are otherwise identical since
|
|
|
|
// that is the struct used in client for reading the one we output here
|
|
|
|
// anyway.
|
|
|
|
agentSvcs := make(map[string]*api.AgentService)
|
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
for id, svc := range services {
|
|
|
|
agentService := buildAgentService(svc, s.agent.config.Datacenter)
|
2019-12-10 02:26:41 +00:00
|
|
|
agentSvcs[id.ID] = &agentService
|
2017-04-28 01:22:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
filter, err := bexpr.CreateFilter(filterExpression, nil, agentSvcs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
raw, err := filter.Execute(agentSvcs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
agentSvcs = raw.(map[string]*api.AgentService)
|
|
|
|
|
|
|
|
// Note: we filter the results with ACLs *after* applying the user-supplied
|
|
|
|
// bexpr filter, to ensure total (and the filter-by-acls header we set below)
|
|
|
|
// do not include results that would be filtered out even if the user did have
|
|
|
|
// permission.
|
|
|
|
total := len(agentSvcs)
|
|
|
|
if err := s.agent.filterServicesWithAuthorizer(authz, agentSvcs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the X-Consul-Results-Filtered-By-ACLs header, but only if the user is
|
|
|
|
// authenticated (to prevent information leaking).
|
|
|
|
//
|
|
|
|
// This is done automatically for HTTP endpoints that proxy to an RPC endpoint
|
|
|
|
// that sets QueryMeta.ResultsFilteredByACLs, but must be done manually for
|
|
|
|
// agent-local endpoints.
|
|
|
|
//
|
|
|
|
// For more information see the comment on: Server.maskResultsFilteredByACLs.
|
|
|
|
if token != "" {
|
|
|
|
setResultsFilteredByACLs(resp, total != len(agentSvcs))
|
|
|
|
}
|
|
|
|
|
|
|
|
return agentSvcs, nil
|
2014-01-21 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
// GET /v1/agent/service/:service_id
|
|
|
|
//
|
|
|
|
// Returns the service definition for a single local services and allows
|
|
|
|
// blocking watch using hash-based blocking.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentService(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2018-09-27 14:00:51 +00:00
|
|
|
// Get the proxy ID. Note that this is the ID of a proxy's service instance.
|
2021-10-15 19:05:58 +00:00
|
|
|
id, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/service/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-27 14:00:51 +00:00
|
|
|
|
|
|
|
// Maybe block
|
|
|
|
var queryOpts structs.QueryOptions
|
|
|
|
if parseWait(resp, req, &queryOpts) {
|
|
|
|
// parseWait returns an error itself
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the token
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
var entMeta acl.EnterpriseMeta
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
// need to resolve to default the meta
|
2021-10-15 19:05:58 +00:00
|
|
|
_, err = s.agent.delegate.ResolveTokenAndDefaultMeta(token, &entMeta, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
// Parse hash specially. Eventually this should happen in parseWait and end up
|
|
|
|
// in QueryOptions but I didn't want to make very general changes right away.
|
|
|
|
hash := req.URL.Query().Get("hash")
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
sid := structs.NewServiceID(id, &entMeta)
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &entMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:27:31 +00:00
|
|
|
dc := s.agent.config.Datacenter
|
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
resultHash, service, err := s.agent.LocalBlockingQuery(false, hash, queryOpts.MaxQueryTime,
|
2018-09-27 14:00:51 +00:00
|
|
|
func(ws memdb.WatchSet) (string, interface{}, error) {
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
svcState := s.agent.State.ServiceState(sid)
|
2018-09-27 14:00:51 +00:00
|
|
|
if svcState == nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return "", nil, HTTPError{StatusCode: http.StatusNotFound, Reason: fmt.Sprintf("unknown service ID: %s", sid.String())}
|
2018-09-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
svc := svcState.Service
|
|
|
|
|
|
|
|
// Setup watch on the service
|
|
|
|
ws.Add(svcState.WatchCh)
|
|
|
|
|
|
|
|
// Check ACLs.
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2018-09-27 14:00:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
2019-12-18 18:43:24 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2019-12-10 02:26:41 +00:00
|
|
|
svc.FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().ServiceReadAllowed(svc.Service, &authzContext); err != nil {
|
|
|
|
return "", nil, err
|
2018-09-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the content hash over the response, minus the hash field
|
2020-11-19 21:27:31 +00:00
|
|
|
aSvc := buildAgentService(svc, dc)
|
2019-08-10 13:15:19 +00:00
|
|
|
reply := &aSvc
|
2018-09-27 14:00:51 +00:00
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
// TODO(partitions): do we need to do anything here?
|
2018-09-27 14:00:51 +00:00
|
|
|
rawHash, err := hashstructure.Hash(reply, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include the ContentHash in the response body
|
|
|
|
reply.ContentHash = fmt.Sprintf("%x", rawHash)
|
|
|
|
|
|
|
|
return reply.ContentHash, reply, nil
|
2019-09-26 02:55:52 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if resultHash != "" {
|
|
|
|
resp.Header().Set("X-Consul-ContentHash", resultHash)
|
|
|
|
}
|
|
|
|
return service, err
|
2018-09-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentChecks(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-12-14 22:16:46 +00:00
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
var entMeta acl.EnterpriseMeta
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &entMeta, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &entMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
var filterExpression string
|
|
|
|
s.parseFilter(req, &filterExpression)
|
|
|
|
filter, err := bexpr.CreateFilter(filterExpression, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
// NOTE(partitions): this works because nodes exist in ONE partition
|
2019-12-10 02:26:41 +00:00
|
|
|
checks := s.agent.State.Checks(&entMeta)
|
2017-04-28 01:22:07 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
agentChecks := make(map[types.CheckID]*structs.HealthCheck)
|
2018-02-07 04:35:55 +00:00
|
|
|
for id, c := range checks {
|
2017-04-28 01:22:07 +00:00
|
|
|
if c.ServiceTags == nil {
|
2018-02-07 04:35:55 +00:00
|
|
|
clone := *c
|
|
|
|
clone.ServiceTags = make([]string, 0)
|
2019-12-10 02:26:41 +00:00
|
|
|
agentChecks[id.ID] = &clone
|
|
|
|
} else {
|
|
|
|
agentChecks[id.ID] = c
|
2017-04-28 01:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
raw, err := filter.Execute(agentChecks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
agentChecks = raw.(map[types.CheckID]*structs.HealthCheck)
|
|
|
|
|
|
|
|
// Note: we filter the results with ACLs *after* applying the user-supplied
|
|
|
|
// bexpr filter, to ensure total (and the filter-by-acls header we set below)
|
|
|
|
// do not include results that would be filtered out even if the user did have
|
|
|
|
// permission.
|
|
|
|
total := len(agentChecks)
|
|
|
|
if err := s.agent.filterChecksWithAuthorizer(authz, agentChecks); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the X-Consul-Results-Filtered-By-ACLs header, but only if the user is
|
|
|
|
// authenticated (to prevent information leaking).
|
|
|
|
//
|
|
|
|
// This is done automatically for HTTP endpoints that proxy to an RPC endpoint
|
|
|
|
// that sets QueryMeta.ResultsFilteredByACLs, but must be done manually for
|
|
|
|
// agent-local endpoints.
|
|
|
|
//
|
|
|
|
// For more information see the comment on: Server.maskResultsFilteredByACLs.
|
|
|
|
if token != "" {
|
|
|
|
setResultsFilteredByACLs(resp, total != len(agentChecks))
|
|
|
|
}
|
|
|
|
|
|
|
|
return agentChecks, nil
|
2014-01-04 01:15:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentMembers(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-12-14 22:16:46 +00:00
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2014-01-04 01:15:51 +00:00
|
|
|
// Check if the WAN is being queried
|
|
|
|
wan := false
|
|
|
|
if other := req.URL.Query().Get("wan"); other != "" {
|
|
|
|
wan = true
|
|
|
|
}
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
segment := req.URL.Query().Get("segment")
|
2017-09-05 20:40:19 +00:00
|
|
|
if wan {
|
|
|
|
switch segment {
|
|
|
|
case "", api.AllSegments:
|
|
|
|
// The zero value and the special "give me all members"
|
|
|
|
// key are ok, otherwise the argument doesn't apply to
|
|
|
|
// the WAN.
|
|
|
|
default:
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Cannot provide a segment with wan=true"}
|
2017-09-05 20:40:19 +00:00
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 21:44:38 +00:00
|
|
|
// Get the request partition and default to that of the agent.
|
|
|
|
entMeta := s.agent.AgentEnterpriseMeta()
|
|
|
|
if err := s.parseEntMetaPartition(req, entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-19 20:09:42 +00:00
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
var members []serf.Member
|
2014-01-04 01:15:51 +00:00
|
|
|
if wan {
|
2016-12-14 22:16:46 +00:00
|
|
|
members = s.agent.WANMembers()
|
2017-08-14 14:36:07 +00:00
|
|
|
} else {
|
2021-10-26 20:08:55 +00:00
|
|
|
filter := consul.LANMemberFilter{
|
2021-10-28 21:44:38 +00:00
|
|
|
Partition: entMeta.PartitionOrDefault(),
|
2021-10-26 20:08:55 +00:00
|
|
|
}
|
2017-09-05 19:22:20 +00:00
|
|
|
if segment == api.AllSegments {
|
2021-10-28 21:44:38 +00:00
|
|
|
// Older 'consul members' calls will default to adding segment=_all
|
|
|
|
// so we only choose to use that request argument in the case where
|
|
|
|
// the partition is also the default and ignore it the rest of the time.
|
2022-04-05 21:10:06 +00:00
|
|
|
if acl.IsDefaultPartition(filter.Partition) {
|
2021-10-28 21:44:38 +00:00
|
|
|
filter.AllSegments = true
|
|
|
|
}
|
2017-09-05 19:22:20 +00:00
|
|
|
} else {
|
2021-10-26 20:08:55 +00:00
|
|
|
filter.Segment = segment
|
2017-09-05 19:22:20 +00:00
|
|
|
}
|
2021-10-26 20:08:55 +00:00
|
|
|
var err error
|
|
|
|
members, err = s.agent.delegate.LANMembers(filter)
|
2017-08-14 14:36:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
2021-12-03 20:36:28 +00:00
|
|
|
|
|
|
|
total := len(members)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err := s.agent.filterMembers(token, &members); err != nil {
|
|
|
|
return nil, err
|
2014-01-04 01:15:51 +00:00
|
|
|
}
|
2021-12-03 20:36:28 +00:00
|
|
|
|
|
|
|
// Set the X-Consul-Results-Filtered-By-ACLs header, but only if the user is
|
|
|
|
// authenticated (to prevent information leaking).
|
|
|
|
//
|
|
|
|
// This is done automatically for HTTP endpoints that proxy to an RPC endpoint
|
|
|
|
// that sets QueryMeta.ResultsFilteredByACLs, but must be done manually for
|
|
|
|
// agent-local endpoints.
|
|
|
|
//
|
|
|
|
// For more information see the comment on: Server.maskResultsFilteredByACLs.
|
|
|
|
if token != "" {
|
|
|
|
setResultsFilteredByACLs(resp, total != len(members))
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
return members, nil
|
2014-01-04 01:15:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentJoin(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-12-14 17:33:57 +00:00
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-26 19:20:57 +00:00
|
|
|
|
|
|
|
// Authorize using the agent's own enterprise meta, not the token.
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-10-26 20:08:55 +00:00
|
|
|
s.agent.AgentEnterpriseMeta().FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
|
|
|
|
if err := authz.ToAllowAuthorizer().AgentWriteAllowed(s.agent.config.NodeName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 21:44:38 +00:00
|
|
|
// Get the request partition and default to that of the agent.
|
|
|
|
entMeta := s.agent.AgentEnterpriseMeta()
|
|
|
|
if err := s.parseEntMetaPartition(req, entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-01-04 01:15:51 +00:00
|
|
|
// Check if the WAN is being queried
|
|
|
|
wan := false
|
|
|
|
if other := req.URL.Query().Get("wan"); other != "" {
|
|
|
|
wan = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the address
|
2021-10-15 19:05:58 +00:00
|
|
|
addr, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/join/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2014-01-04 01:15:51 +00:00
|
|
|
if wan {
|
2020-03-09 20:59:02 +00:00
|
|
|
if s.agent.config.ConnectMeshGatewayWANFederationEnabled {
|
|
|
|
return nil, fmt.Errorf("WAN join is disabled when wan federation via mesh gateways is enabled")
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
_, err = s.agent.JoinWAN([]string{addr})
|
2014-01-04 01:15:51 +00:00
|
|
|
} else {
|
2021-10-28 21:44:38 +00:00
|
|
|
_, err = s.agent.JoinLAN([]string{addr}, entMeta)
|
2014-01-04 01:15:51 +00:00
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
return nil, err
|
2014-01-04 01:15:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentLeave(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-12-14 17:33:57 +00:00
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-26 19:20:57 +00:00
|
|
|
|
|
|
|
// Authorize using the agent's own enterprise meta, not the token.
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-10-26 20:08:55 +00:00
|
|
|
s.agent.AgentEnterpriseMeta().FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().AgentWriteAllowed(s.agent.config.NodeName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 18:29:42 +00:00
|
|
|
if err := s.agent.Leave(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-20 07:29:20 +00:00
|
|
|
return nil, s.agent.ShutdownAgent()
|
2016-11-30 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentForceLeave(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-12-14 17:33:57 +00:00
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-20 22:11:01 +00:00
|
|
|
// TODO(partitions): should this be possible in a partition?
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().OperatorWriteAllowed(nil); err != nil {
|
|
|
|
return nil, err
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 21:44:38 +00:00
|
|
|
// Get the request partition and default to that of the agent.
|
|
|
|
entMeta := s.agent.AgentEnterpriseMeta()
|
|
|
|
if err := s.parseEntMetaPartition(req, entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-26 19:20:57 +00:00
|
|
|
// Check the value of the prune query
|
2019-10-04 21:10:02 +00:00
|
|
|
_, prune := req.URL.Query()["prune"]
|
|
|
|
|
2021-12-02 23:15:10 +00:00
|
|
|
// Check if the WAN is being queried
|
|
|
|
_, wan := req.URL.Query()["wan"]
|
|
|
|
|
2021-10-15 19:05:58 +00:00
|
|
|
addr, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/force-leave/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-02 23:15:10 +00:00
|
|
|
if wan {
|
|
|
|
return nil, s.agent.ForceLeaveWAN(addr, prune, entMeta)
|
|
|
|
} else {
|
|
|
|
return nil, s.agent.ForceLeave(addr, prune, entMeta)
|
|
|
|
}
|
2014-01-04 01:15:51 +00:00
|
|
|
}
|
2014-01-30 22:58:36 +00:00
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
// syncChanges is a helper function which wraps a blocking call to sync
|
|
|
|
// services and checks to the server. If the operation fails, we only
|
|
|
|
// only warn because the write did succeed and anti-entropy will sync later.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) syncChanges() {
|
2017-08-28 12:17:13 +00:00
|
|
|
if err := s.agent.State.SyncChanges(); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.agent.logger.Error("failed to sync changes", "error", err)
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentRegisterCheck(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-12-18 18:46:53 +00:00
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2017-06-15 16:46:06 +00:00
|
|
|
var args structs.CheckDefinition
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-29 18:13:36 +00:00
|
|
|
|
|
|
|
if err := decodeBody(req.Body, &args); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Request decode failed: %v", err)}
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Verify the check has a name.
|
2014-01-30 23:35:38 +00:00
|
|
|
if args.Name == "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing check name"}
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-12 00:53:48 +00:00
|
|
|
if args.Status != "" && !structs.ValidStatus(args.Status) {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Bad check status"}
|
2015-04-12 00:53:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &args.EnterpriseMeta, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &args.EnterpriseMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Construct the health check.
|
2014-02-03 23:15:35 +00:00
|
|
|
health := args.HealthCheck(s.agent.config.NodeName)
|
2014-01-30 23:35:38 +00:00
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Verify the check type.
|
2017-05-15 19:49:13 +00:00
|
|
|
chkType := args.CheckType()
|
2019-12-18 18:46:53 +00:00
|
|
|
err = chkType.Validate()
|
2017-10-10 23:54:06 +00:00
|
|
|
if err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid check: %v", err)}
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 18:33:11 +00:00
|
|
|
// Store the type of check based on the definition
|
|
|
|
health.Type = chkType.Type()
|
|
|
|
|
2019-04-30 23:00:57 +00:00
|
|
|
if health.ServiceID != "" {
|
|
|
|
// fixup the service name so that vetCheckRegister requires the right ACLs
|
2022-01-05 17:11:03 +00:00
|
|
|
cid := health.CompoundServiceID()
|
2021-11-19 16:50:44 +00:00
|
|
|
service := s.agent.State.Service(cid)
|
2019-04-30 23:00:57 +00:00
|
|
|
if service != nil {
|
|
|
|
health.ServiceName = service.Service
|
2021-11-19 16:50:44 +00:00
|
|
|
} else {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: fmt.Sprintf("ServiceID %q does not exist", cid.String())}
|
2019-04-30 23:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Get the provided token, if any, and vet against any ACL policies.
|
2019-12-18 18:46:53 +00:00
|
|
|
if err := s.agent.vetCheckRegisterWithAuthorizer(authz, health); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-28 01:26:23 +00:00
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Add the check.
|
2018-10-11 12:22:11 +00:00
|
|
|
if err := s.agent.AddCheck(health, chkType, true, token, ConfigSourceRemote); err != nil {
|
2015-02-20 23:45:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentDeregisterCheck(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2021-10-15 19:05:58 +00:00
|
|
|
ID, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/check/deregister/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
checkID := structs.NewCheckID(types.CheckID(ID), nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
// Get the provided token, if any, and vet against any ACL policies.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
if err := s.parseEntMetaNoWildcard(req, &checkID.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &checkID.EnterpriseMeta, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
checkID.Normalize()
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &checkID.EnterpriseMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 16:50:44 +00:00
|
|
|
if err := s.agent.vetCheckUpdateWithAuthorizer(authz, checkID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-20 23:45:06 +00:00
|
|
|
if err := s.agent.RemoveCheck(checkID, true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentCheckPass(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2021-10-15 19:05:58 +00:00
|
|
|
ID, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/check/pass/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
checkID := types.CheckID(ID)
|
2014-01-30 23:18:05 +00:00
|
|
|
note := req.URL.Query().Get("note")
|
2019-12-10 02:26:41 +00:00
|
|
|
return s.agentCheckUpdate(resp, req, checkID, api.HealthPassing, note)
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentCheckWarn(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2021-10-15 19:05:58 +00:00
|
|
|
ID, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/check/warn/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
checkID := types.CheckID(ID)
|
2014-01-30 23:18:05 +00:00
|
|
|
note := req.URL.Query().Get("note")
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
return s.agentCheckUpdate(resp, req, checkID, api.HealthWarning, note)
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentCheckFail(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2021-10-15 19:05:58 +00:00
|
|
|
ID, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/check/fail/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
checkID := types.CheckID(ID)
|
2014-01-30 23:18:05 +00:00
|
|
|
note := req.URL.Query().Get("note")
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
return s.agentCheckUpdate(resp, req, checkID, api.HealthCritical, note)
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-03 01:08:06 +00:00
|
|
|
// checkUpdate is the payload for a PUT to AgentCheckUpdate.
|
|
|
|
type checkUpdate struct {
|
2017-04-19 23:00:11 +00:00
|
|
|
// Status us one of the api.Health* states, "passing", "warning", or
|
2016-03-03 01:08:06 +00:00
|
|
|
// "critical".
|
|
|
|
Status string
|
|
|
|
|
|
|
|
// Output is the information to post to the UI for operators as the
|
|
|
|
// output of the process that decided to hit the TTL check. This is
|
|
|
|
// different from the note field that's associated with the check
|
|
|
|
// itself.
|
|
|
|
Output string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentCheckUpdate is a PUT-based alternative to the GET-based Pass/Warn/Fail
|
|
|
|
// APIs.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentCheckUpdate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-03-03 01:08:06 +00:00
|
|
|
var update checkUpdate
|
2019-10-29 18:13:36 +00:00
|
|
|
if err := decodeBody(req.Body, &update); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Request decode failed: %v", err)}
|
2016-03-03 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch update.Status {
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.HealthPassing:
|
|
|
|
case api.HealthWarning:
|
|
|
|
case api.HealthCritical:
|
2016-03-03 01:08:06 +00:00
|
|
|
default:
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid check status: '%s'", update.Status)}
|
2016-03-03 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 19:05:58 +00:00
|
|
|
ID, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/check/update/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
checkID := types.CheckID(ID)
|
2016-12-14 22:16:46 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
return s.agentCheckUpdate(resp, req, checkID, update.Status, update.Output)
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
func (s *HTTPHandlers) agentCheckUpdate(resp http.ResponseWriter, req *http.Request, checkID types.CheckID, status string, output string) (interface{}, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
cid := structs.NewCheckID(checkID, nil)
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Get the provided token, if any, and vet against any ACL policies.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
if err := s.parseEntMetaNoWildcard(req, &cid.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &cid.EnterpriseMeta, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
cid.Normalize()
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if err := s.agent.vetCheckUpdateWithAuthorizer(authz, cid); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &cid.EnterpriseMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := s.agent.updateTTLCheck(cid, status, output); err != nil {
|
2016-03-03 01:08:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:39:23 +00:00
|
|
|
// agentHealthService Returns Health for a given service ID
|
2020-09-04 18:42:15 +00:00
|
|
|
func agentHealthService(serviceID structs.ServiceID, s *HTTPHandlers) (int, string, api.HealthChecks) {
|
2019-12-10 02:26:41 +00:00
|
|
|
checks := s.agent.State.ChecksForService(serviceID, true)
|
2019-01-07 14:39:23 +00:00
|
|
|
serviceChecks := make(api.HealthChecks, 0)
|
|
|
|
for _, c := range checks {
|
2019-12-10 02:26:41 +00:00
|
|
|
// TODO: harmonize struct.HealthCheck and api.HealthCheck (or at least extract conversion function)
|
|
|
|
healthCheck := &api.HealthCheck{
|
|
|
|
Node: c.Node,
|
|
|
|
CheckID: string(c.CheckID),
|
|
|
|
Name: c.Name,
|
|
|
|
Status: c.Status,
|
|
|
|
Notes: c.Notes,
|
|
|
|
Output: c.Output,
|
|
|
|
ServiceID: c.ServiceID,
|
|
|
|
ServiceName: c.ServiceName,
|
|
|
|
ServiceTags: c.ServiceTags,
|
2019-01-07 14:39:23 +00:00
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
fillHealthCheckEnterpriseMeta(healthCheck, &c.EnterpriseMeta)
|
|
|
|
serviceChecks = append(serviceChecks, healthCheck)
|
2019-01-07 14:39:23 +00:00
|
|
|
}
|
|
|
|
status := serviceChecks.AggregatedStatus()
|
|
|
|
switch status {
|
|
|
|
case api.HealthWarning:
|
|
|
|
return http.StatusTooManyRequests, status, serviceChecks
|
|
|
|
case api.HealthPassing:
|
|
|
|
return http.StatusOK, status, serviceChecks
|
|
|
|
default:
|
|
|
|
return http.StatusServiceUnavailable, status, serviceChecks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func returnTextPlain(req *http.Request) bool {
|
|
|
|
if contentType := req.Header.Get("Accept"); strings.HasPrefix(contentType, "text/plain") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if format := req.URL.Query().Get("format"); format != "" {
|
|
|
|
return format == "text"
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentHealthServiceByID return the local Service Health given its ID
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentHealthServiceByID(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-01-07 14:39:23 +00:00
|
|
|
// Pull out the service id (service id since there may be several instance of the same service on this host)
|
2021-10-15 19:05:58 +00:00
|
|
|
serviceID, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/health/service/id/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-07 14:39:23 +00:00
|
|
|
if serviceID == "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, &HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing serviceID"}
|
2019-01-07 14:39:23 +00:00
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
var entMeta acl.EnterpriseMeta
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:57:38 +00:00
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
|
|
|
// need to resolve to default the meta
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &entMeta, &authzContext)
|
2020-01-31 14:57:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &entMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-04-15 16:03:29 +00:00
|
|
|
sid := structs.NewServiceID(serviceID, &entMeta)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2020-11-19 21:27:31 +00:00
|
|
|
dc := s.agent.config.Datacenter
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
if service := s.agent.State.Service(sid); service != nil {
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().ServiceReadAllowed(service.Service, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2020-01-31 14:57:38 +00:00
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
code, status, healthChecks := agentHealthService(sid, s)
|
|
|
|
if returnTextPlain(req) {
|
|
|
|
return status, CodeWithPayloadError{StatusCode: code, Reason: status, ContentType: "text/plain"}
|
|
|
|
}
|
2020-11-19 21:27:31 +00:00
|
|
|
serviceInfo := buildAgentService(service, dc)
|
2019-12-10 02:26:41 +00:00
|
|
|
result := &api.AgentServiceChecksInfo{
|
|
|
|
AggregatedStatus: status,
|
|
|
|
Checks: healthChecks,
|
|
|
|
Service: &serviceInfo,
|
2019-01-07 14:39:23 +00:00
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
return result, CodeWithPayloadError{StatusCode: code, Reason: status, ContentType: "application/json"}
|
2019-01-07 14:39:23 +00:00
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
notFoundReason := fmt.Sprintf("ServiceId %s not found", sid.String())
|
2019-01-07 14:39:23 +00:00
|
|
|
if returnTextPlain(req) {
|
2021-12-07 14:44:03 +00:00
|
|
|
return notFoundReason, CodeWithPayloadError{StatusCode: http.StatusNotFound, Reason: notFoundReason, ContentType: "text/plain"}
|
2019-01-07 14:39:23 +00:00
|
|
|
}
|
|
|
|
return &api.AgentServiceChecksInfo{
|
|
|
|
AggregatedStatus: api.HealthCritical,
|
|
|
|
Checks: nil,
|
|
|
|
Service: nil,
|
|
|
|
}, CodeWithPayloadError{StatusCode: http.StatusNotFound, Reason: notFoundReason, ContentType: "application/json"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentHealthServiceByName return the worse status of all the services with given name on an agent
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentHealthServiceByName(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-01-07 14:39:23 +00:00
|
|
|
// Pull out the service name
|
2021-10-15 19:05:58 +00:00
|
|
|
serviceName, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/health/service/name/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:39:23 +00:00
|
|
|
if serviceName == "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, &HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing service Name"}
|
2019-01-07 14:39:23 +00:00
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
var entMeta acl.EnterpriseMeta
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:57:38 +00:00
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
|
|
|
// need to resolve to default the meta
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &entMeta, &authzContext)
|
2020-01-31 14:57:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().ServiceReadAllowed(serviceName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2020-01-31 14:57:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &entMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:27:31 +00:00
|
|
|
dc := s.agent.config.Datacenter
|
|
|
|
|
2019-01-07 14:39:23 +00:00
|
|
|
code := http.StatusNotFound
|
|
|
|
status := fmt.Sprintf("ServiceName %s Not Found", serviceName)
|
2021-08-19 20:09:42 +00:00
|
|
|
|
|
|
|
services := s.agent.State.ServicesByName(structs.NewServiceName(serviceName, &entMeta))
|
2019-01-07 14:39:23 +00:00
|
|
|
result := make([]api.AgentServiceChecksInfo, 0, 16)
|
|
|
|
for _, service := range services {
|
2021-08-19 20:09:42 +00:00
|
|
|
sid := structs.NewServiceID(service.ID, &entMeta)
|
|
|
|
|
|
|
|
scode, sstatus, healthChecks := agentHealthService(sid, s)
|
|
|
|
serviceInfo := buildAgentService(service, dc)
|
|
|
|
res := api.AgentServiceChecksInfo{
|
|
|
|
AggregatedStatus: sstatus,
|
|
|
|
Checks: healthChecks,
|
|
|
|
Service: &serviceInfo,
|
|
|
|
}
|
|
|
|
result = append(result, res)
|
|
|
|
// When service is not found, we ignore it and keep existing HTTP status
|
|
|
|
if code == http.StatusNotFound {
|
|
|
|
code = scode
|
|
|
|
status = sstatus
|
|
|
|
}
|
|
|
|
// We take the worst of all statuses, so we keep iterating
|
|
|
|
// passing: 200 < warning: 429 < critical: 503
|
|
|
|
if code < scode {
|
|
|
|
code = scode
|
|
|
|
status = sstatus
|
2019-01-07 14:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if returnTextPlain(req) {
|
|
|
|
return status, CodeWithPayloadError{StatusCode: code, Reason: status, ContentType: "text/plain"}
|
|
|
|
}
|
|
|
|
return result, CodeWithPayloadError{StatusCode: code, Reason: status, ContentType: "application/json"}
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentRegisterService(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-06-15 16:46:06 +00:00
|
|
|
var args structs.ServiceDefinition
|
2016-12-14 22:16:46 +00:00
|
|
|
// Fixup the type decode of TTL or Interval if a check if provided.
|
2014-04-25 02:44:27 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:13:36 +00:00
|
|
|
if err := decodeBody(req.Body, &args); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Request decode failed: %v", err)}
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Verify the service has a name.
|
2014-01-30 23:35:38 +00:00
|
|
|
if args.Name == "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing service name"}
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 16:34:45 +00:00
|
|
|
// Check the service address here and in the catalog RPC endpoint
|
2018-03-19 16:56:00 +00:00
|
|
|
// since service registration isn't synchronous.
|
2017-05-15 20:10:36 +00:00
|
|
|
if ipaddr.IsAny(args.Address) {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Invalid service address"}
|
2017-05-08 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &args.EnterpriseMeta, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &args.EnterpriseMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Get the node service.
|
2014-02-03 23:15:35 +00:00
|
|
|
ns := args.NodeService()
|
2018-09-07 14:30:47 +00:00
|
|
|
if ns.Weights != nil {
|
|
|
|
if err := structs.ValidateWeights(ns.Weights); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid Weights: %v", err)}
|
2018-09-07 14:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
if err := structs.ValidateServiceMetadata(ns.Kind, ns.Meta, false); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid Service Meta: %v", err)}
|
2018-02-07 00:54:42 +00:00
|
|
|
}
|
2014-01-30 23:35:38 +00:00
|
|
|
|
2018-03-11 01:42:30 +00:00
|
|
|
// Run validation. This is the same validation that would happen on
|
|
|
|
// the catalog endpoint so it helps ensure the sync will work properly.
|
|
|
|
if err := ns.Validate(); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Validation failed: %v", err.Error())}
|
2018-03-11 01:42:30 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Verify the check type.
|
2017-10-10 23:54:06 +00:00
|
|
|
chkTypes, err := args.CheckTypes()
|
|
|
|
if err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid check: %v", err)}
|
2017-10-10 23:54:06 +00:00
|
|
|
}
|
2015-01-14 01:52:17 +00:00
|
|
|
for _, check := range chkTypes {
|
2015-04-12 00:53:48 +00:00
|
|
|
if check.Status != "" && !structs.ValidStatus(check.Status) {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Status for checks must 'passing', 'warning', 'critical'"}
|
2015-04-12 00:53:48 +00:00
|
|
|
}
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 13:33:12 +00:00
|
|
|
// Verify the sidecar check types
|
|
|
|
if args.Connect != nil && args.Connect.SidecarService != nil {
|
|
|
|
chkTypes, err := args.Connect.SidecarService.CheckTypes()
|
|
|
|
if err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid check in sidecar_service: %v", err)}
|
2018-09-27 13:33:12 +00:00
|
|
|
}
|
|
|
|
for _, check := range chkTypes {
|
|
|
|
if check.Status != "" && !structs.ValidStatus(check.Status) {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Status for checks must 'passing', 'warning', 'critical'"}
|
2018-09-27 13:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Get the provided token, if any, and vet against any ACL policies.
|
2019-12-18 18:46:53 +00:00
|
|
|
if err := s.agent.vetServiceRegisterWithAuthorizer(authz, ns); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-28 01:26:23 +00:00
|
|
|
|
2018-09-27 13:33:12 +00:00
|
|
|
// See if we have a sidecar to register too
|
|
|
|
sidecar, sidecarChecks, sidecarToken, err := s.agent.sidecarServiceFromNodeService(ns, token)
|
|
|
|
if err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid SidecarService: %s", err)}
|
2018-09-27 13:33:12 +00:00
|
|
|
}
|
|
|
|
if sidecar != nil {
|
2021-09-07 20:29:32 +00:00
|
|
|
if err := sidecar.Validate(); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Failed Validation: %v", err.Error())}
|
2021-09-07 20:29:32 +00:00
|
|
|
}
|
2018-10-09 16:57:26 +00:00
|
|
|
// Make sure we are allowed to register the sidecar using the token
|
2018-09-27 13:33:12 +00:00
|
|
|
// specified (might be specific to sidecar or the same one as the overall
|
|
|
|
// request).
|
|
|
|
if err := s.agent.vetServiceRegister(sidecarToken, sidecar); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// We parsed the sidecar registration, now remove it from the NodeService
|
|
|
|
// for the actual service since it's done it's job and we don't want to
|
|
|
|
// persist it in the actual state/catalog. SidecarService is meant to be a
|
|
|
|
// registration syntax sugar so don't propagate it any further.
|
|
|
|
ns.Connect.SidecarService = nil
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Add the service.
|
2019-09-02 15:38:29 +00:00
|
|
|
replaceExistingChecks := false
|
|
|
|
|
|
|
|
query := req.URL.Query()
|
|
|
|
if len(query["replace-existing-checks"]) > 0 && (query.Get("replace-existing-checks") == "" || query.Get("replace-existing-checks") == "true") {
|
|
|
|
replaceExistingChecks = true
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:26:58 +00:00
|
|
|
addReq := AddServiceRequest{
|
|
|
|
Service: ns,
|
2020-11-30 18:14:15 +00:00
|
|
|
chkTypes: chkTypes,
|
|
|
|
persist: true,
|
|
|
|
token: token,
|
2020-11-30 18:26:58 +00:00
|
|
|
Source: ConfigSourceRemote,
|
2020-11-30 18:14:15 +00:00
|
|
|
replaceExistingChecks: replaceExistingChecks,
|
|
|
|
}
|
|
|
|
if err := s.agent.AddService(addReq); err != nil {
|
|
|
|
return nil, err
|
2015-02-20 23:45:06 +00:00
|
|
|
}
|
2020-11-30 18:14:15 +00:00
|
|
|
|
2018-09-27 13:33:12 +00:00
|
|
|
if sidecar != nil {
|
2020-11-30 18:26:58 +00:00
|
|
|
addReq := AddServiceRequest{
|
|
|
|
Service: sidecar,
|
2020-11-30 18:14:15 +00:00
|
|
|
chkTypes: sidecarChecks,
|
|
|
|
persist: true,
|
|
|
|
token: sidecarToken,
|
2020-11-30 18:26:58 +00:00
|
|
|
Source: ConfigSourceRemote,
|
2020-11-30 18:14:15 +00:00
|
|
|
replaceExistingChecks: replaceExistingChecks,
|
|
|
|
}
|
|
|
|
if err := s.agent.AddService(addReq); err != nil {
|
|
|
|
return nil, err
|
2018-09-27 13:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-20 23:45:06 +00:00
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentDeregisterService(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2021-10-15 19:05:58 +00:00
|
|
|
serviceID, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/service/deregister/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sid := structs.NewServiceID(serviceID, nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
|
|
|
|
// Get the provided token, if any, and vet against any ACL policies.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
if err := s.parseEntMetaNoWildcard(req, &sid.EnterpriseMeta); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &sid.EnterpriseMeta, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
sid.Normalize()
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &sid.EnterpriseMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 16:50:44 +00:00
|
|
|
if err := s.agent.vetServiceUpdateWithAuthorizer(authz, sid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := s.agent.RemoveService(sid); err != nil {
|
2015-02-20 23:45:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-13 07:57:48 +00:00
|
|
|
|
2015-02-20 23:45:06 +00:00
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
2015-01-15 08:16:34 +00:00
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentServiceMaintenance(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2015-01-15 08:16:34 +00:00
|
|
|
// Ensure we have a service ID
|
2021-10-15 19:05:58 +00:00
|
|
|
serviceID, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/service/maintenance/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sid := structs.NewServiceID(serviceID, nil)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
if sid.ID == "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing service ID"}
|
2015-01-15 08:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have some action
|
|
|
|
params := req.URL.Query()
|
|
|
|
if _, ok := params["enable"]; !ok {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing value for enable"}
|
2015-01-15 08:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
raw := params.Get("enable")
|
2015-01-21 17:53:31 +00:00
|
|
|
enable, err := strconv.ParseBool(raw)
|
|
|
|
if err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid value for enable: %q", raw)}
|
2015-01-15 08:16:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Get the provided token, if any, and vet against any ACL policies.
|
2015-09-10 18:43:59 +00:00
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
if err := s.parseEntMetaNoWildcard(req, &sid.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, &sid.EnterpriseMeta, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
sid.Normalize()
|
|
|
|
|
2021-12-09 16:05:02 +00:00
|
|
|
if !s.validateRequestPartition(resp, &sid.EnterpriseMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
if err := s.agent.vetServiceUpdateWithAuthorizer(authz, sid); err != nil {
|
2016-12-14 22:16:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-10 18:43:59 +00:00
|
|
|
|
2015-01-15 08:16:34 +00:00
|
|
|
if enable {
|
2015-01-21 20:21:57 +00:00
|
|
|
reason := params.Get("reason")
|
2019-12-10 02:26:41 +00:00
|
|
|
if err = s.agent.EnableServiceMaintenance(sid, reason, token); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: err.Error()}
|
2015-01-15 09:17:35 +00:00
|
|
|
}
|
2015-01-15 08:16:34 +00:00
|
|
|
} else {
|
2019-12-10 02:26:41 +00:00
|
|
|
if err = s.agent.DisableServiceMaintenance(sid); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: err.Error()}
|
2015-01-15 09:17:35 +00:00
|
|
|
}
|
2015-01-15 08:16:34 +00:00
|
|
|
}
|
2015-02-20 23:45:06 +00:00
|
|
|
s.syncChanges()
|
2015-01-21 21:28:26 +00:00
|
|
|
return nil, nil
|
2015-01-15 08:16:34 +00:00
|
|
|
}
|
2015-01-15 19:20:22 +00:00
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentNodeMaintenance(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2015-01-15 19:20:22 +00:00
|
|
|
// Ensure we have some action
|
|
|
|
params := req.URL.Query()
|
|
|
|
if _, ok := params["enable"]; !ok {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing value for enable"}
|
2015-01-15 19:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
raw := params.Get("enable")
|
2015-01-21 17:53:31 +00:00
|
|
|
enable, err := strconv.ParseBool(raw)
|
|
|
|
if err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Invalid value for enable: %q", raw)}
|
2015-01-15 19:20:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 22:16:46 +00:00
|
|
|
// Get the provided token, if any, and vet against any ACL policies.
|
2015-09-10 18:43:59 +00:00
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-12-13 17:43:33 +00:00
|
|
|
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2016-12-14 22:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-25 18:43:11 +00:00
|
|
|
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-10-26 20:08:55 +00:00
|
|
|
s.agent.AgentEnterpriseMeta().FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().NodeWriteAllowed(s.agent.config.NodeName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2016-12-14 22:16:46 +00:00
|
|
|
}
|
2015-09-10 18:43:59 +00:00
|
|
|
|
2015-01-15 19:20:22 +00:00
|
|
|
if enable {
|
2015-09-10 18:43:59 +00:00
|
|
|
s.agent.EnableNodeMaintenance(params.Get("reason"), token)
|
2015-01-15 19:20:22 +00:00
|
|
|
} else {
|
|
|
|
s.agent.DisableNodeMaintenance()
|
|
|
|
}
|
2015-02-20 23:45:06 +00:00
|
|
|
s.syncChanges()
|
2015-01-15 19:20:22 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-02-20 23:45:06 +00:00
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentMonitor(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-12-14 17:33:57 +00:00
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2016-12-14 17:33:57 +00:00
|
|
|
if err != nil {
|
2016-11-28 21:08:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-26 19:20:57 +00:00
|
|
|
|
|
|
|
// Authorize using the agent's own enterprise meta, not the token.
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-10-26 20:08:55 +00:00
|
|
|
s.agent.AgentEnterpriseMeta().FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().AgentReadAllowed(s.agent.config.NodeName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2016-12-14 17:33:57 +00:00
|
|
|
}
|
2016-11-28 21:08:31 +00:00
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
// Get the provided loglevel.
|
2016-11-16 21:45:26 +00:00
|
|
|
logLevel := req.URL.Query().Get("loglevel")
|
|
|
|
if logLevel == "" {
|
|
|
|
logLevel = "INFO"
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
var logJSON bool
|
|
|
|
if _, ok := req.URL.Query()["logjson"]; ok {
|
|
|
|
logJSON = true
|
|
|
|
}
|
2016-11-16 21:45:26 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
if !logging.ValidateLogLevel(logLevel) {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Unknown log level: %s", logLevel)}
|
2016-11-16 21:45:26 +00:00
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2016-11-16 21:45:26 +00:00
|
|
|
flusher, ok := resp.(http.Flusher)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Streaming not supported")
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
monitor := monitor.New(monitor.Config{
|
|
|
|
BufferSize: 512,
|
|
|
|
Logger: s.agent.logger,
|
|
|
|
LoggerOptions: &hclog.LoggerOptions{
|
|
|
|
Level: logging.LevelFromString(logLevel),
|
|
|
|
JSONFormat: logJSON,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
logsCh := monitor.Start()
|
2016-11-16 21:45:26 +00:00
|
|
|
|
2018-02-19 21:53:10 +00:00
|
|
|
// Send header so client can start streaming body
|
|
|
|
resp.WriteHeader(http.StatusOK)
|
2018-04-03 20:33:13 +00:00
|
|
|
|
|
|
|
// 0 byte write is needed before the Flush call so that if we are using
|
|
|
|
// a gzip stream it will go ahead and write out the HTTP response header
|
|
|
|
resp.Write([]byte(""))
|
2018-02-19 21:53:10 +00:00
|
|
|
flusher.Flush()
|
2021-06-15 16:05:52 +00:00
|
|
|
const flushDelay = 200 * time.Millisecond
|
|
|
|
flushTicker := time.NewTicker(flushDelay)
|
|
|
|
defer flushTicker.Stop()
|
2018-02-19 21:53:10 +00:00
|
|
|
|
2016-12-14 17:33:57 +00:00
|
|
|
// Stream logs until the connection is closed.
|
2016-11-16 21:45:26 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-01-28 23:50:41 +00:00
|
|
|
case <-req.Context().Done():
|
|
|
|
droppedCount := monitor.Stop()
|
|
|
|
if droppedCount > 0 {
|
|
|
|
s.agent.logger.Warn("Dropped logs during monitor request", "dropped_count", droppedCount)
|
2016-11-28 21:08:31 +00:00
|
|
|
}
|
2021-06-15 16:05:52 +00:00
|
|
|
flusher.Flush()
|
2016-11-16 21:45:26 +00:00
|
|
|
return nil, nil
|
2021-06-15 16:05:52 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
case log := <-logsCh:
|
|
|
|
fmt.Fprint(resp, string(log))
|
2021-06-15 16:05:52 +00:00
|
|
|
|
|
|
|
case <-flushTicker.C:
|
2016-11-16 21:45:26 +00:00
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentToken(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2022-01-05 17:11:03 +00:00
|
|
|
if s.checkACLDisabled() {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusUnauthorized, Reason: "ACL support disabled"}
|
2017-11-28 21:47:30 +00:00
|
|
|
}
|
2017-07-26 18:03:43 +00:00
|
|
|
|
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2017-07-26 18:03:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-26 19:20:57 +00:00
|
|
|
|
|
|
|
// Authorize using the agent's own enterprise meta, not the token.
|
|
|
|
var authzContext acl.AuthorizerContext
|
2021-10-26 20:08:55 +00:00
|
|
|
s.agent.AgentEnterpriseMeta().FillAuthzContext(&authzContext)
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().AgentWriteAllowed(s.agent.config.NodeName, &authzContext); err != nil {
|
|
|
|
return nil, err
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The body is just the token, but it's in a JSON object so we can add
|
|
|
|
// fields to this later if needed.
|
|
|
|
var args api.AgentToken
|
2019-10-29 18:13:36 +00:00
|
|
|
if err := decodeBody(req.Body, &args); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Request decode failed: %v", err)}
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out the target token.
|
2021-10-15 19:05:58 +00:00
|
|
|
target, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/token/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-04 18:37:34 +00:00
|
|
|
|
2020-08-17 23:30:25 +00:00
|
|
|
err = s.agent.tokens.WithPersistenceLock(func() error {
|
|
|
|
triggerAntiEntropySync := false
|
|
|
|
switch target {
|
|
|
|
case "acl_token", "default":
|
|
|
|
changed := s.agent.tokens.UpdateUserToken(args.Token, token_store.TokenSourceAPI)
|
|
|
|
if changed {
|
|
|
|
triggerAntiEntropySync = true
|
|
|
|
}
|
2019-02-27 19:28:31 +00:00
|
|
|
|
2020-08-17 23:30:25 +00:00
|
|
|
case "acl_agent_token", "agent":
|
|
|
|
changed := s.agent.tokens.UpdateAgentToken(args.Token, token_store.TokenSourceAPI)
|
|
|
|
if changed {
|
|
|
|
triggerAntiEntropySync = true
|
|
|
|
}
|
2019-02-27 19:28:31 +00:00
|
|
|
|
2021-12-02 17:05:27 +00:00
|
|
|
case "acl_agent_master_token", "agent_master", "agent_recovery":
|
2021-12-07 12:12:47 +00:00
|
|
|
s.agent.tokens.UpdateAgentRecoveryToken(args.Token, token_store.TokenSourceAPI)
|
2019-02-27 19:28:31 +00:00
|
|
|
|
2020-08-17 23:30:25 +00:00
|
|
|
case "acl_replication_token", "replication":
|
|
|
|
s.agent.tokens.UpdateReplicationToken(args.Token, token_store.TokenSourceAPI)
|
2019-02-27 19:28:31 +00:00
|
|
|
|
2020-08-17 23:30:25 +00:00
|
|
|
default:
|
2022-04-29 17:42:49 +00:00
|
|
|
return HTTPError{StatusCode: http.StatusNotFound, Reason: fmt.Sprintf("Token %q is unknown", target)}
|
2019-02-27 19:28:31 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 23:30:25 +00:00
|
|
|
// TODO: is it safe to move this out of WithPersistenceLock?
|
|
|
|
if triggerAntiEntropySync {
|
|
|
|
s.agent.sync.SyncFull.Trigger()
|
2019-02-27 19:28:31 +00:00
|
|
|
}
|
2020-08-17 23:30:25 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-02-27 19:28:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
s.agent.logger.Info("Updated agent's ACL token", "token", target)
|
2017-07-26 18:03:43 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2018-03-17 04:39:26 +00:00
|
|
|
|
|
|
|
// AgentConnectCARoots returns the trusted CA roots.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentConnectCARoots(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2018-04-11 08:52:51 +00:00
|
|
|
var args structs.DCSpecificRequest
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 15:01:25 +00:00
|
|
|
raw, m, err := s.agent.cache.Get(req.Context(), cachetype.ConnectCARootName, &args)
|
2018-04-11 08:52:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-15 12:13:54 +00:00
|
|
|
defer setCacheMeta(resp, &m)
|
|
|
|
|
|
|
|
// Add cache hit
|
2018-04-11 08:52:51 +00:00
|
|
|
|
|
|
|
reply, ok := raw.(*structs.IndexedCARoots)
|
|
|
|
if !ok {
|
|
|
|
// This should never happen, but we want to protect against panics
|
|
|
|
return nil, fmt.Errorf("internal error: response type not correct")
|
|
|
|
}
|
|
|
|
defer setMeta(resp, &reply.QueryMeta)
|
|
|
|
|
|
|
|
return *reply, nil
|
2018-03-17 04:39:26 +00:00
|
|
|
}
|
2018-03-21 17:55:39 +00:00
|
|
|
|
|
|
|
// AgentConnectCALeafCert returns the certificate bundle for a service
|
2021-12-02 19:32:15 +00:00
|
|
|
// instance. This endpoint ignores all "Cache-Control" attributes.
|
|
|
|
// This supports blocking queries to update the returned bundle.
|
|
|
|
// Non-blocking queries will always verify that the cache entry is still valid.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentConnectCALeafCert(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-03-06 17:13:28 +00:00
|
|
|
// Get the service name. Note that this is the name of the service,
|
2018-05-22 17:33:14 +00:00
|
|
|
// not the ID of the service instance.
|
2021-10-15 19:05:58 +00:00
|
|
|
serviceName, err := getPathSuffixUnescaped(req.URL.Path, "/v1/agent/connect/ca/leaf/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-21 17:55:39 +00:00
|
|
|
|
2018-04-30 21:23:49 +00:00
|
|
|
args := cachetype.ConnectCALeafRequest{
|
2019-08-27 21:45:58 +00:00
|
|
|
Service: serviceName, // Need name not ID
|
2018-04-30 21:23:49 +00:00
|
|
|
}
|
|
|
|
var qOpts structs.QueryOptions
|
2018-07-30 13:11:51 +00:00
|
|
|
|
2020-02-10 15:40:44 +00:00
|
|
|
if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-30 21:23:49 +00:00
|
|
|
// Store DC in the ConnectCALeafRequest but query opts separately
|
2019-08-09 19:19:30 +00:00
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &qOpts); done {
|
2018-04-30 21:23:49 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
args.MinQueryIndex = qOpts.MinQueryIndex
|
2019-01-10 16:23:37 +00:00
|
|
|
args.MaxQueryTime = qOpts.MaxQueryTime
|
2019-08-09 19:19:30 +00:00
|
|
|
args.Token = qOpts.Token
|
2018-04-30 21:23:49 +00:00
|
|
|
|
2021-12-02 19:32:15 +00:00
|
|
|
// TODO(ffmmmm): maybe set MustRevalidate in ConnectCALeafRequest (as part of CacheInfo())
|
|
|
|
// We don't want non-blocking queries to return expired leaf certs
|
|
|
|
// or leaf certs not valid under the current CA. So always revalidate
|
|
|
|
// the leaf cert on non-blocking queries (ie when MinQueryIndex == 0)
|
|
|
|
if args.MinQueryIndex == 0 {
|
|
|
|
args.MustRevalidate = true
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &args.EnterpriseMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 15:01:25 +00:00
|
|
|
raw, m, err := s.agent.cache.Get(req.Context(), cachetype.ConnectCALeafName, &args)
|
2018-04-30 21:23:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-15 12:13:54 +00:00
|
|
|
defer setCacheMeta(resp, &m)
|
2018-04-30 21:23:49 +00:00
|
|
|
|
|
|
|
reply, ok := raw.(*structs.IssuedCert)
|
|
|
|
if !ok {
|
|
|
|
// This should never happen, but we want to protect against panics
|
|
|
|
return nil, fmt.Errorf("internal error: response type not correct")
|
|
|
|
}
|
|
|
|
setIndex(resp, reply.ModifyIndex)
|
2018-03-21 17:55:39 +00:00
|
|
|
|
2018-04-30 21:23:49 +00:00
|
|
|
return reply, nil
|
2018-03-21 17:55:39 +00:00
|
|
|
}
|
2018-03-21 23:02:46 +00:00
|
|
|
|
|
|
|
// AgentConnectAuthorize
|
|
|
|
//
|
|
|
|
// POST /v1/agent/connect/authorize
|
2018-05-11 05:37:02 +00:00
|
|
|
//
|
2020-10-06 22:09:13 +00:00
|
|
|
// NOTE: This endpoint treats any L7 intentions as DENY.
|
|
|
|
//
|
2018-05-19 04:03:10 +00:00
|
|
|
// Note: when this logic changes, consider if the Intention.Check RPC method
|
2018-05-11 05:37:02 +00:00
|
|
|
// also needs to be updated.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentConnectAuthorize(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2018-03-26 01:50:05 +00:00
|
|
|
// Fetch the token
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2018-03-26 00:52:26 +00:00
|
|
|
var authReq structs.ConnectAuthorizeRequest
|
2020-01-13 20:51:40 +00:00
|
|
|
|
|
|
|
if err := s.parseEntMetaNoWildcard(req, &authReq.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:13:36 +00:00
|
|
|
if err := decodeBody(req.Body, &authReq); err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Request decode failed: %v", err)}
|
2018-05-09 19:30:43 +00:00
|
|
|
}
|
2018-03-27 17:09:13 +00:00
|
|
|
|
2021-08-19 20:09:42 +00:00
|
|
|
if !s.validateRequestPartition(resp, &authReq.EnterpriseMeta) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-10-03 19:37:53 +00:00
|
|
|
authz, reason, cacheMeta, err := s.agent.ConnectAuthorize(token, &authReq)
|
2018-04-17 23:26:58 +00:00
|
|
|
if err != nil {
|
2018-03-26 00:52:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-03 19:37:53 +00:00
|
|
|
setCacheMeta(resp, cacheMeta)
|
2018-03-26 01:50:05 +00:00
|
|
|
|
2018-03-26 00:52:26 +00:00
|
|
|
return &connectAuthorizeResp{
|
2018-03-26 01:50:05 +00:00
|
|
|
Authorized: authz,
|
|
|
|
Reason: reason,
|
2018-03-26 00:52:26 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-03-26 01:02:25 +00:00
|
|
|
// connectAuthorizeResp is the response format/structure for the
|
|
|
|
// /v1/agent/connect/authorize endpoint.
|
2018-03-26 00:52:26 +00:00
|
|
|
type connectAuthorizeResp struct {
|
2018-03-26 01:02:25 +00:00
|
|
|
Authorized bool // True if authorized, false if not
|
|
|
|
Reason string // Reason for the Authorized value (whether true or false)
|
2018-03-21 23:02:46 +00:00
|
|
|
}
|
2018-10-17 20:20:35 +00:00
|
|
|
|
|
|
|
// AgentHost
|
|
|
|
//
|
|
|
|
// GET /v1/agent/host
|
|
|
|
//
|
|
|
|
// Retrieves information about resources available and in-use for the
|
|
|
|
// host the agent is running on such as CPU, memory, and disk usage. Requires
|
|
|
|
// a operator:read ACL token.
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) AgentHost(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2018-10-17 20:20:35 +00:00
|
|
|
// Fetch the ACL token, if any, and enforce agent policy.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
2021-08-04 21:51:19 +00:00
|
|
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
2018-10-17 20:20:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-13 13:43:53 +00:00
|
|
|
|
2021-08-20 22:11:01 +00:00
|
|
|
// TODO(partitions): should this be possible in a partition?
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().OperatorReadAllowed(nil); err != nil {
|
|
|
|
return nil, err
|
2018-10-17 20:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return debug.CollectHostInfo(), nil
|
|
|
|
}
|