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
|
|
|
|
2022-04-20 00:24:21 +00:00
|
|
|
package dataplane
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
2023-09-06 22:46:25 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-01-11 18:40:09 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2023-09-06 22:46:25 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
|
|
|
2023-09-06 22:46:25 +00:00
|
|
|
"github.com/hashicorp/consul/internal/catalog"
|
|
|
|
"github.com/hashicorp/consul/internal/mesh"
|
|
|
|
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1"
|
|
|
|
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v1alpha1"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
|
2022-10-10 17:40:27 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/configentry"
|
2022-04-20 00:24:21 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
2022-07-13 15:33:48 +00:00
|
|
|
external "github.com/hashicorp/consul/agent/grpc-external"
|
2022-10-10 17:40:27 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-01-11 18:40:09 +00:00
|
|
|
"github.com/hashicorp/consul/agent/xds/accesslogs"
|
2022-04-20 00:24:21 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbdataplane"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) GetEnvoyBootstrapParams(ctx context.Context, req *pbdataplane.GetEnvoyBootstrapParamsRequest) (*pbdataplane.GetEnvoyBootstrapParamsResponse, error) {
|
2023-09-06 22:46:25 +00:00
|
|
|
proxyID := req.ProxyId
|
|
|
|
if req.GetServiceId() != "" {
|
|
|
|
proxyID = req.GetServiceId()
|
|
|
|
}
|
|
|
|
logger := s.Logger.Named("get-envoy-bootstrap-params").With("proxy_id", proxyID, "request_id", external.TraceID())
|
2022-04-20 00:24:21 +00:00
|
|
|
|
|
|
|
logger.Trace("Started processing request")
|
|
|
|
defer logger.Trace("Finished processing request")
|
|
|
|
|
2022-09-28 16:56:59 +00:00
|
|
|
options, err := external.QueryOptionsFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-20 00:24:21 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
entMeta := acl.NewEnterpriseMetaWithPartition(req.GetPartition(), req.GetNamespace())
|
2022-09-28 16:56:59 +00:00
|
|
|
authz, err := s.ACLResolver.ResolveTokenAndDefaultMeta(options.Token, &entMeta, &authzContext)
|
2022-04-20 00:24:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Unauthenticated, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-09-06 22:46:25 +00:00
|
|
|
if s.EnableV2 {
|
|
|
|
// Get the workload.
|
|
|
|
workloadId := &pbresource.ID{
|
|
|
|
Name: proxyID,
|
|
|
|
Tenancy: &pbresource.Tenancy{
|
|
|
|
Namespace: req.Namespace,
|
|
|
|
Partition: req.Partition,
|
|
|
|
},
|
|
|
|
Type: catalog.WorkloadType,
|
|
|
|
}
|
|
|
|
workloadRsp, err := s.ResourceAPIClient.Read(ctx, &pbresource.ReadRequest{
|
|
|
|
Id: workloadId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// This error should already include the gRPC status code and so we don't need to wrap it
|
|
|
|
// in status.Error.
|
2023-09-20 00:02:01 +00:00
|
|
|
logger.Error("Error looking up workload", "error", err)
|
2023-09-06 22:46:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var workload pbcatalog.Workload
|
|
|
|
err = workloadRsp.Resource.Data.UnmarshalTo(&workload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, "failed to parse workload data")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only workloads that have an associated identity can ask for proxy bootstrap parameters.
|
|
|
|
if workload.Identity == "" {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "workload %q doesn't have identity associated with it", req.ProxyId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo (ishustava): ACL enforcement ensuring there's identity:write permissions.
|
|
|
|
|
|
|
|
// Get all proxy configurations for this workload. Currently we're only looking
|
|
|
|
// for proxy configurations in the same tenancy as the workload.
|
|
|
|
// todo (ishustava): we need to support wildcard proxy configurations as well.
|
|
|
|
|
|
|
|
proxyCfgList, err := s.ResourceAPIClient.List(ctx, &pbresource.ListRequest{
|
|
|
|
Tenancy: workloadRsp.Resource.Id.GetTenancy(),
|
|
|
|
Type: mesh.ProxyConfigurationType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-20 00:02:01 +00:00
|
|
|
logger.Error("Error looking up proxyConfiguration", "error", err)
|
2023-09-06 22:46:25 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect and merge proxy configs.
|
|
|
|
// todo (ishustava): sorting and conflict resolution.
|
|
|
|
bootstrapCfg := &pbmesh.BootstrapConfig{}
|
|
|
|
dynamicCfg := &pbmesh.DynamicConfig{}
|
|
|
|
for _, cfgResource := range proxyCfgList.Resources {
|
|
|
|
var proxyCfg pbmesh.ProxyConfiguration
|
|
|
|
err = cfgResource.Data.UnmarshalTo(&proxyCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to parse proxy configuration data: %q", cfgResource.Id.Name)
|
|
|
|
}
|
|
|
|
if isWorkloadSelected(req.ProxyId, proxyCfg.Workloads) {
|
|
|
|
proto.Merge(bootstrapCfg, proxyCfg.BootstrapConfig)
|
|
|
|
proto.Merge(dynamicCfg, proxyCfg.DynamicConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
accessLogs := makeAccessLogs(dynamicCfg.GetAccessLogs(), logger)
|
|
|
|
|
|
|
|
return &pbdataplane.GetEnvoyBootstrapParamsResponse{
|
|
|
|
Identity: workload.Identity,
|
|
|
|
Partition: workloadRsp.Resource.Id.Tenancy.Partition,
|
|
|
|
Namespace: workloadRsp.Resource.Id.Tenancy.Namespace,
|
|
|
|
BootstrapConfig: bootstrapCfg,
|
|
|
|
Datacenter: s.Datacenter,
|
|
|
|
NodeName: workload.NodeName,
|
|
|
|
AccessLogs: accessLogs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The remainder of this file focuses on v1 implementation of this endpoint.
|
|
|
|
|
2022-04-20 00:24:21 +00:00
|
|
|
store := s.GetStore()
|
|
|
|
|
2023-09-06 22:46:25 +00:00
|
|
|
_, svc, err := store.ServiceNode(req.GetNodeId(), req.GetNodeName(), proxyID, &entMeta, structs.DefaultPeerKeyword)
|
2022-04-20 00:24:21 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error looking up service", "error", err)
|
|
|
|
if errors.Is(err, state.ErrNodeNotFound) {
|
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
} else if strings.Contains(err.Error(), "Node ID or name required") {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
} else {
|
|
|
|
return nil, status.Error(codes.Internal, "Failure looking up service")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if svc == nil {
|
|
|
|
return nil, status.Error(codes.NotFound, "Service not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := authz.ToAllowAuthorizer().ServiceReadAllowed(svc.ServiceName, &authzContext); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:40:27 +00:00
|
|
|
_, ns, err := configentry.MergeNodeServiceWithCentralConfig(
|
|
|
|
nil,
|
|
|
|
store,
|
|
|
|
svc.ToNodeService(),
|
|
|
|
logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error merging with central config", "error", err)
|
|
|
|
return nil, status.Errorf(codes.Unknown, "Error merging central config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bootstrapConfig, err := structpb.NewStruct(ns.Proxy.Config)
|
2022-04-20 00:24:21 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error creating the envoy boostrap params config", "error", err)
|
|
|
|
return nil, status.Error(codes.Unknown, "Error creating the envoy boostrap params config")
|
|
|
|
}
|
|
|
|
|
2023-01-11 18:40:09 +00:00
|
|
|
// Inspect access logging
|
|
|
|
// This is non-essential, and don't want to return an error unless there is a more serious issue
|
|
|
|
var accessLogs []string
|
2023-09-06 22:46:25 +00:00
|
|
|
if ns != nil {
|
|
|
|
accessLogs = makeAccessLogs(&ns.Proxy.AccessLogs, logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build out the response
|
|
|
|
var serviceName string
|
|
|
|
if svc.ServiceKind == structs.ServiceKindConnectProxy {
|
|
|
|
serviceName = svc.ServiceProxy.DestinationServiceName
|
|
|
|
} else {
|
|
|
|
serviceName = svc.ServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pbdataplane.GetEnvoyBootstrapParamsResponse{
|
|
|
|
Identity: serviceName,
|
|
|
|
Service: serviceName,
|
|
|
|
Partition: svc.EnterpriseMeta.PartitionOrDefault(),
|
|
|
|
Namespace: svc.EnterpriseMeta.NamespaceOrDefault(),
|
|
|
|
Config: bootstrapConfig,
|
|
|
|
Datacenter: s.Datacenter,
|
|
|
|
NodeName: svc.Node,
|
|
|
|
AccessLogs: accessLogs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeAccessLogs(logs structs.AccessLogs, logger hclog.Logger) []string {
|
|
|
|
var accessLogs []string
|
|
|
|
if logs.GetEnabled() {
|
|
|
|
envoyLoggers, err := accesslogs.MakeAccessLogs(logs, false)
|
2023-01-11 18:40:09 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Warn("Error creating the envoy access log config", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
accessLogs = make([]string, 0, len(envoyLoggers))
|
|
|
|
|
|
|
|
for _, msg := range envoyLoggers {
|
|
|
|
logConfig, err := protojson.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("Error marshaling the envoy access log config", "error", err)
|
|
|
|
}
|
|
|
|
accessLogs = append(accessLogs, string(logConfig))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 22:46:25 +00:00
|
|
|
return accessLogs
|
2022-04-20 00:24:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 22:46:25 +00:00
|
|
|
func isWorkloadSelected(name string, selector *pbcatalog.WorkloadSelector) bool {
|
|
|
|
for _, prefix := range selector.Prefixes {
|
|
|
|
if strings.Contains(name, prefix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, selectorName := range selector.Names {
|
|
|
|
if name == selectorName {
|
|
|
|
return true
|
|
|
|
}
|
2022-04-20 00:24:21 +00:00
|
|
|
}
|
2023-09-06 22:46:25 +00:00
|
|
|
|
|
|
|
return false
|
2022-04-20 00:24:21 +00:00
|
|
|
}
|