2023-08-11 13:12:13 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
2023-08-04 18:27:48 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
)
|
|
|
|
|
2023-08-15 13:11:50 +00:00
|
|
|
const (
|
|
|
|
HeaderConsulToken = "x-consul-token"
|
|
|
|
HeaderConsistencyMode = "x-consul-consistency-mode"
|
|
|
|
)
|
|
|
|
|
2024-01-25 22:18:54 +00:00
|
|
|
// NewHandler creates a new HTTP handler for the resource service.
|
|
|
|
// httpPathPrefix is the prefix to be used for all HTTP endpoints. Should start with "/" and
|
|
|
|
// end without a trailing "/".
|
|
|
|
// client is the gRPC client to be used to communicate with the resource service.
|
|
|
|
// registry is the resource registry to be used to determine the resource types.
|
|
|
|
// parseToken is a function that will be called to parse the Consul token from the request.
|
2023-08-04 18:27:48 +00:00
|
|
|
func NewHandler(
|
2024-01-25 22:18:54 +00:00
|
|
|
httpPathPrefix string,
|
2023-08-04 18:27:48 +00:00
|
|
|
client pbresource.ResourceServiceClient,
|
|
|
|
registry resource.Registry,
|
|
|
|
parseToken func(req *http.Request, token *string),
|
|
|
|
logger hclog.Logger) http.Handler {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
for _, t := range registry.Types() {
|
2023-08-15 13:11:50 +00:00
|
|
|
// List Endpoint
|
|
|
|
base := strings.ToLower(fmt.Sprintf("/%s/%s/%s", t.Type.Group, t.Type.GroupVersion, t.Type.Kind))
|
|
|
|
mux.Handle(base, http.StripPrefix(base, &listHandler{t, client, parseToken, logger}))
|
2024-01-25 22:18:54 +00:00
|
|
|
logger.Info("Registered resource endpoint", "endpoint", fmt.Sprintf("%s%s", httpPathPrefix, base))
|
2023-08-15 13:11:50 +00:00
|
|
|
|
|
|
|
// Individual Resource Endpoints
|
|
|
|
prefix := strings.ToLower(fmt.Sprintf("%s/", base))
|
2023-08-04 18:27:48 +00:00
|
|
|
mux.Handle(prefix, http.StripPrefix(prefix, &resourceHandler{t, client, parseToken, logger}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
type writeRequest struct {
|
|
|
|
Metadata map[string]string `json:"metadata"`
|
|
|
|
Data json.RawMessage `json:"data"`
|
|
|
|
Owner *pbresource.ID `json:"owner"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type resourceHandler struct {
|
|
|
|
reg resource.Registration
|
|
|
|
client pbresource.ResourceServiceClient
|
|
|
|
parseToken func(req *http.Request, token *string)
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var token string
|
|
|
|
h.parseToken(r, &token)
|
2023-08-15 13:11:50 +00:00
|
|
|
ctx := metadata.AppendToOutgoingContext(r.Context(), HeaderConsulToken, token)
|
2023-08-04 18:27:48 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPut:
|
|
|
|
h.handleWrite(w, r, ctx)
|
2023-08-11 21:11:11 +00:00
|
|
|
case http.MethodGet:
|
|
|
|
h.handleRead(w, r, ctx)
|
2023-08-11 13:22:30 +00:00
|
|
|
case http.MethodDelete:
|
|
|
|
h.handleDelete(w, r, ctx)
|
2023-08-04 18:27:48 +00:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *resourceHandler) handleWrite(w http.ResponseWriter, r *http.Request, ctx context.Context) {
|
|
|
|
var req writeRequest
|
|
|
|
// convert req body to writeRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2023-09-12 13:22:15 +00:00
|
|
|
h.logger.Error("Failed to decode request body", "error", err)
|
2023-08-04 18:27:48 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2023-09-12 13:22:15 +00:00
|
|
|
w.Write([]byte("Request body format is invalid"))
|
|
|
|
return
|
2023-08-04 18:27:48 +00:00
|
|
|
}
|
|
|
|
// convert data struct to proto message
|
|
|
|
data := h.reg.Proto.ProtoReflect().New().Interface()
|
|
|
|
if err := protojson.Unmarshal(req.Data, data); err != nil {
|
2023-09-12 13:22:15 +00:00
|
|
|
h.logger.Error("Failed to unmarshal to proto message", "error", err)
|
2023-08-04 18:27:48 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2023-09-12 13:22:15 +00:00
|
|
|
w.Write([]byte("Request body didn't follow the resource schema"))
|
|
|
|
return
|
2023-08-04 18:27:48 +00:00
|
|
|
}
|
|
|
|
// proto message to any
|
|
|
|
anyProtoMsg, err := anypb.New(data)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
h.logger.Error("Failed to convert proto message to any type", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-11 21:11:11 +00:00
|
|
|
tenancyInfo, params := parseParams(r)
|
2023-08-04 18:27:48 +00:00
|
|
|
|
|
|
|
rsp, err := h.client.Write(ctx, &pbresource.WriteRequest{
|
|
|
|
Resource: &pbresource.Resource{
|
|
|
|
Id: &pbresource.ID{
|
|
|
|
Type: h.reg.Type,
|
|
|
|
Tenancy: tenancyInfo,
|
2023-08-11 21:11:11 +00:00
|
|
|
Name: params["resourceName"],
|
2023-08-04 18:27:48 +00:00
|
|
|
},
|
|
|
|
Owner: req.Owner,
|
2023-08-11 21:11:11 +00:00
|
|
|
Version: params["version"],
|
2023-08-04 18:27:48 +00:00
|
|
|
Metadata: req.Metadata,
|
|
|
|
Data: anyProtoMsg,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-08-15 13:11:50 +00:00
|
|
|
handleResponseError(err, w, h.logger)
|
2023-08-04 18:27:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := jsonMarshal(rsp.Resource)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
h.logger.Error("Failed to unmarshal GRPC resource response", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(output)
|
|
|
|
}
|
|
|
|
|
2023-08-11 21:11:11 +00:00
|
|
|
func (h *resourceHandler) handleRead(w http.ResponseWriter, r *http.Request, ctx context.Context) {
|
|
|
|
tenancyInfo, params := parseParams(r)
|
|
|
|
if params["consistent"] != "" {
|
|
|
|
ctx = metadata.AppendToOutgoingContext(ctx, "x-consul-consistency-mode", "consistent")
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp, err := h.client.Read(ctx, &pbresource.ReadRequest{
|
|
|
|
Id: &pbresource.ID{
|
|
|
|
Type: h.reg.Type,
|
|
|
|
Tenancy: tenancyInfo,
|
|
|
|
Name: params["resourceName"],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-08-15 13:11:50 +00:00
|
|
|
handleResponseError(err, w, h.logger)
|
2023-08-11 21:11:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := jsonMarshal(rsp.Resource)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
h.logger.Error("Failed to unmarshal GRPC resource response", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: The HTTP endpoints do not accept UID since it is quite unlikely that the user will have access to it
|
|
|
|
func (h *resourceHandler) handleDelete(w http.ResponseWriter, r *http.Request, ctx context.Context) {
|
|
|
|
tenancyInfo, params := parseParams(r)
|
|
|
|
_, err := h.client.Delete(ctx, &pbresource.DeleteRequest{
|
|
|
|
Id: &pbresource.ID{
|
|
|
|
Type: h.reg.Type,
|
|
|
|
Tenancy: tenancyInfo,
|
|
|
|
Name: params["resourceName"],
|
|
|
|
},
|
|
|
|
Version: params["version"],
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-08-15 13:11:50 +00:00
|
|
|
handleResponseError(err, w, h.logger)
|
2023-08-11 21:11:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
w.Write([]byte("{}"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseParams(r *http.Request) (tenancy *pbresource.Tenancy, params map[string]string) {
|
|
|
|
query := r.URL.Query()
|
2023-09-05 16:17:19 +00:00
|
|
|
namespace := query.Get("namespace")
|
|
|
|
if namespace == "" {
|
|
|
|
namespace = query.Get("ns")
|
|
|
|
}
|
2024-01-29 20:08:31 +00:00
|
|
|
|
2023-08-04 18:27:48 +00:00
|
|
|
tenancy = &pbresource.Tenancy{
|
2023-08-11 21:11:11 +00:00
|
|
|
Partition: query.Get("partition"),
|
2023-09-05 16:17:19 +00:00
|
|
|
Namespace: namespace,
|
2023-08-04 18:27:48 +00:00
|
|
|
}
|
2023-08-11 21:11:11 +00:00
|
|
|
|
2024-01-29 20:08:31 +00:00
|
|
|
// TODO(peering/v2) handle parsing peer tenancy
|
|
|
|
|
2023-08-11 21:11:11 +00:00
|
|
|
resourceName := path.Base(r.URL.Path)
|
2023-08-04 18:27:48 +00:00
|
|
|
if resourceName == "." || resourceName == "/" {
|
|
|
|
resourceName = ""
|
|
|
|
}
|
2023-08-11 21:11:11 +00:00
|
|
|
|
|
|
|
params = make(map[string]string)
|
|
|
|
params["resourceName"] = resourceName
|
|
|
|
params["version"] = query.Get("version")
|
2023-08-15 13:11:50 +00:00
|
|
|
params["namePrefix"] = query.Get("name_prefix")
|
2023-09-05 16:17:19 +00:00
|
|
|
// coming from command line
|
|
|
|
params["consistent"] = query.Get("RequireConsistent")
|
|
|
|
// coming from http client
|
2023-08-11 21:11:11 +00:00
|
|
|
if _, ok := query["consistent"]; ok {
|
|
|
|
params["consistent"] = "true"
|
|
|
|
}
|
2023-08-04 18:27:48 +00:00
|
|
|
|
2023-08-15 13:11:50 +00:00
|
|
|
return tenancy, params
|
2023-08-04 18:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func jsonMarshal(res *pbresource.Resource) ([]byte, error) {
|
|
|
|
output, err := protojson.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stuff map[string]any
|
|
|
|
if err := json.Unmarshal(output, &stuff); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(stuff["data"].(map[string]any), "@type")
|
|
|
|
return json.MarshalIndent(stuff, "", " ")
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:11:50 +00:00
|
|
|
func handleResponseError(err error, w http.ResponseWriter, logger hclog.Logger) {
|
2023-08-04 18:27:48 +00:00
|
|
|
if e, ok := status.FromError(err); ok {
|
|
|
|
switch e.Code() {
|
|
|
|
case codes.InvalidArgument:
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2023-08-15 13:11:50 +00:00
|
|
|
logger.Info("User has mal-formed request", "error", err)
|
2023-08-04 18:27:48 +00:00
|
|
|
case codes.NotFound:
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2023-08-15 13:11:50 +00:00
|
|
|
logger.Info("Received error from resource service: Not found", "error", err)
|
2023-08-04 18:27:48 +00:00
|
|
|
case codes.PermissionDenied:
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2023-08-15 13:11:50 +00:00
|
|
|
logger.Info("Received error from resource service: User not authenticated", "error", err)
|
2023-08-04 18:27:48 +00:00
|
|
|
case codes.Aborted:
|
|
|
|
w.WriteHeader(http.StatusConflict)
|
2023-08-15 13:11:50 +00:00
|
|
|
logger.Info("Received error from resource service: the request conflict with the current state of the target resource", "error", err)
|
2023-08-04 18:27:48 +00:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-08-15 13:11:50 +00:00
|
|
|
logger.Error("Received error from resource service", "error", err)
|
2023-08-04 18:27:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-08-15 13:11:50 +00:00
|
|
|
logger.Error("Received error from resource service: not able to parse error returned", "error", err)
|
2023-08-04 18:27:48 +00:00
|
|
|
}
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
}
|
2023-08-15 13:11:50 +00:00
|
|
|
|
|
|
|
type listHandler struct {
|
|
|
|
reg resource.Registration
|
|
|
|
client pbresource.ResourceServiceClient
|
|
|
|
parseToken func(req *http.Request, token *string)
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *listHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var token string
|
|
|
|
h.parseToken(r, &token)
|
|
|
|
ctx := metadata.AppendToOutgoingContext(r.Context(), HeaderConsulToken, token)
|
|
|
|
|
|
|
|
tenancyInfo, params := parseParams(r)
|
|
|
|
if params["consistent"] == "true" {
|
|
|
|
ctx = metadata.AppendToOutgoingContext(ctx, HeaderConsistencyMode, "consistent")
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp, err := h.client.List(ctx, &pbresource.ListRequest{
|
|
|
|
Type: h.reg.Type,
|
|
|
|
Tenancy: tenancyInfo,
|
|
|
|
NamePrefix: params["namePrefix"],
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
handleResponseError(err, w, h.logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output := make([]json.RawMessage, len(rsp.Resources))
|
|
|
|
for idx, res := range rsp.Resources {
|
|
|
|
b, err := jsonMarshal(res)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
h.logger.Error("Failed to unmarshal GRPC resource response", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
output[idx] = b
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.MarshalIndent(struct {
|
|
|
|
Resources []json.RawMessage `json:"resources"`
|
|
|
|
}{output}, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
h.logger.Error("Failed to correctly format the list response", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(b)
|
|
|
|
}
|