2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2021-11-16 18:04:01 +00:00
|
|
|
//go:build !consulent
|
2019-10-24 18:38:09 +00:00
|
|
|
// +build !consulent
|
|
|
|
|
2017-11-29 00:06:26 +00:00
|
|
|
package agent
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
import (
|
2019-12-06 16:14:56 +00:00
|
|
|
"fmt"
|
2019-10-24 18:38:09 +00:00
|
|
|
"net/http"
|
2019-12-06 16:14:56 +00:00
|
|
|
"strings"
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2019-10-24 18:38:09 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
2018-02-18 01:31:24 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *HTTPHandlers) parseEntMeta(req *http.Request, entMeta *acl.EnterpriseMeta) error {
|
2019-12-06 16:14:56 +00:00
|
|
|
if headerNS := req.Header.Get("X-Consul-Namespace"); headerNS != "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return HTTPError{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Reason: "Invalid header: \"X-Consul-Namespace\" - Namespaces are a Consul Enterprise feature",
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
}
|
|
|
|
if queryNS := req.URL.Query().Get("ns"); queryNS != "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return HTTPError{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Reason: "Invalid query parameter: \"ns\" - Namespaces are a Consul Enterprise feature",
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
}
|
2021-08-19 20:09:42 +00:00
|
|
|
|
2021-09-03 12:37:23 +00:00
|
|
|
return s.parseEntMetaPartition(req, entMeta)
|
2019-12-06 16:14:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:59:30 +00:00
|
|
|
func (s *HTTPHandlers) validateEnterpriseIntentionPartition(logName, partition string) error {
|
|
|
|
if partition == "" {
|
|
|
|
return nil
|
|
|
|
} else if strings.ToLower(partition) == "default" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
// No special handling for wildcard namespaces as they are pointless in CE.
|
2021-09-08 15:59:30 +00:00
|
|
|
|
2022-04-29 17:42:49 +00:00
|
|
|
return HTTPError{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Reason: "Invalid " + logName + "(" + partition + ")" + ": Partitions is a Consul Enterprise feature",
|
|
|
|
}
|
2021-09-08 15:59:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) validateEnterpriseIntentionNamespace(logName, ns string, _ bool) error {
|
2020-06-26 21:59:15 +00:00
|
|
|
if ns == "" {
|
|
|
|
return nil
|
|
|
|
} else if strings.ToLower(ns) == structs.IntentionDefaultNamespace {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
// No special handling for wildcard namespaces as they are pointless in CE.
|
2020-06-26 21:59:15 +00:00
|
|
|
|
2022-04-29 17:42:49 +00:00
|
|
|
return HTTPError{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Reason: "Invalid " + logName + "(" + ns + ")" + ": Namespaces is a Consul Enterprise feature",
|
|
|
|
}
|
2020-06-26 21:59:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *HTTPHandlers) parseEntMetaNoWildcard(req *http.Request, _ *acl.EnterpriseMeta) error {
|
2019-12-06 19:01:34 +00:00
|
|
|
return s.parseEntMeta(req, nil)
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) rewordUnknownEnterpriseFieldError(err error) error {
|
2019-12-06 16:14:56 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := err.Error()
|
|
|
|
|
|
|
|
if strings.Contains(msg, "json: unknown field ") {
|
|
|
|
quotedField := strings.TrimPrefix(msg, "json: unknown field ")
|
|
|
|
|
|
|
|
switch quotedField {
|
|
|
|
case `"Namespace"`:
|
2020-06-23 16:57:30 +00:00
|
|
|
return fmt.Errorf("%v - Namespaces are a Consul Enterprise feature", err)
|
2019-12-06 16:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2017-11-29 00:06:26 +00:00
|
|
|
}
|
2019-12-13 19:50:07 +00:00
|
|
|
|
2020-01-14 15:09:29 +00:00
|
|
|
func parseACLAuthMethodEnterpriseMeta(req *http.Request, _ *structs.ACLAuthMethodEnterpriseMeta) error {
|
|
|
|
if methodNS := req.URL.Query().Get("authmethod-ns"); methodNS != "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return HTTPError{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Reason: "Invalid query parameter: \"authmethod-ns\" - Namespaces are a Consul Enterprise feature",
|
|
|
|
}
|
2020-01-14 15:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-16 22:07:52 +00:00
|
|
|
|
2020-05-13 22:47:05 +00:00
|
|
|
// enterpriseHandler is a noop for the enterprise implementation. we pass the original back
|
2020-09-04 18:42:15 +00:00
|
|
|
func (s *HTTPHandlers) enterpriseHandler(next http.Handler) http.Handler {
|
2020-05-13 22:47:05 +00:00
|
|
|
return next
|
2020-04-16 22:07:52 +00:00
|
|
|
}
|
2020-10-09 13:32:39 +00:00
|
|
|
|
|
|
|
// uiTemplateDataTransform returns an optional uiserver.UIDataTransform to allow
|
|
|
|
// altering UI data in enterprise.
|
2021-03-24 18:43:42 +00:00
|
|
|
func (s *HTTPHandlers) uiTemplateDataTransform(data map[string]interface{}) error {
|
2020-10-09 13:32:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-14 19:07:38 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *HTTPHandlers) parseEntMetaPartition(req *http.Request, meta *acl.EnterpriseMeta) error {
|
2021-08-19 20:09:42 +00:00
|
|
|
if headerAP := req.Header.Get("X-Consul-Partition"); headerAP != "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return HTTPError{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Reason: "Invalid header: \"X-Consul-Partition\" - Partitions are a Consul Enterprise feature",
|
|
|
|
}
|
2021-08-19 20:09:42 +00:00
|
|
|
}
|
|
|
|
if queryAP := req.URL.Query().Get("partition"); queryAP != "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return HTTPError{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Reason: "Invalid query parameter: \"partition\" - Partitions are a Consul Enterprise feature",
|
|
|
|
}
|
2021-08-19 20:09:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 19:07:38 +00:00
|
|
|
return nil
|
|
|
|
}
|