2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-02-09 17:58:55 +00:00
|
|
|
package discoverychain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hash/crc32"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2023-02-22 03:48:26 +00:00
|
|
|
"strings"
|
2023-02-09 17:58:55 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/configentry"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GatewayChainSynthesizer is used to synthesize a discovery chain for a
|
|
|
|
// gateway from its configuration and multiple other discovery chains.
|
|
|
|
type GatewayChainSynthesizer struct {
|
|
|
|
datacenter string
|
2023-02-13 19:18:05 +00:00
|
|
|
trustDomain string
|
|
|
|
suffix string
|
2023-02-09 17:58:55 +00:00
|
|
|
gateway *structs.APIGatewayConfigEntry
|
2023-02-17 18:18:11 +00:00
|
|
|
hostname string
|
2023-02-09 17:58:55 +00:00
|
|
|
matchesByHostname map[string][]hostnameMatch
|
|
|
|
tcpRoutes []structs.TCPRouteConfigEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
type hostnameMatch struct {
|
|
|
|
match structs.HTTPMatch
|
|
|
|
filters structs.HTTPFilters
|
|
|
|
services []structs.HTTPService
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGatewayChainSynthesizer creates a new GatewayChainSynthesizer for the
|
|
|
|
// given gateway and datacenter.
|
2023-02-13 19:18:05 +00:00
|
|
|
func NewGatewayChainSynthesizer(datacenter, trustDomain, suffix string, gateway *structs.APIGatewayConfigEntry) *GatewayChainSynthesizer {
|
2023-02-09 17:58:55 +00:00
|
|
|
return &GatewayChainSynthesizer{
|
|
|
|
datacenter: datacenter,
|
2023-02-13 19:18:05 +00:00
|
|
|
trustDomain: trustDomain,
|
|
|
|
suffix: suffix,
|
2023-02-09 17:58:55 +00:00
|
|
|
gateway: gateway,
|
|
|
|
matchesByHostname: map[string][]hostnameMatch{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTCPRoute adds a TCPRoute to use in synthesizing a discovery chain
|
|
|
|
func (l *GatewayChainSynthesizer) AddTCPRoute(route structs.TCPRouteConfigEntry) {
|
|
|
|
l.tcpRoutes = append(l.tcpRoutes, route)
|
|
|
|
}
|
|
|
|
|
2023-02-17 18:18:11 +00:00
|
|
|
// SetHostname sets the base hostname for a listener that this is being synthesized for
|
|
|
|
func (l *GatewayChainSynthesizer) SetHostname(hostname string) {
|
|
|
|
l.hostname = hostname
|
|
|
|
}
|
|
|
|
|
2023-02-09 17:58:55 +00:00
|
|
|
// AddHTTPRoute takes a new route and flattens its rule matches out per hostname.
|
|
|
|
// This is required since a single route can specify multiple hostnames, and a
|
|
|
|
// single hostname can be specified in multiple routes. Routing for a given
|
|
|
|
// hostname must behave based on the aggregate of all rules that apply to it.
|
|
|
|
func (l *GatewayChainSynthesizer) AddHTTPRoute(route structs.HTTPRouteConfigEntry) {
|
2023-02-17 18:18:11 +00:00
|
|
|
hostnames := route.FilteredHostnames(l.hostname)
|
2023-02-13 19:18:05 +00:00
|
|
|
for _, host := range hostnames {
|
2023-02-09 17:58:55 +00:00
|
|
|
matches, ok := l.matchesByHostname[host]
|
|
|
|
if !ok {
|
|
|
|
matches = []hostnameMatch{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range route.Rules {
|
|
|
|
// If a rule has no matches defined, add default match
|
|
|
|
if rule.Matches == nil {
|
|
|
|
rule.Matches = []structs.HTTPMatch{}
|
|
|
|
}
|
|
|
|
if len(rule.Matches) == 0 {
|
|
|
|
rule.Matches = []structs.HTTPMatch{{
|
|
|
|
Path: structs.HTTPPathMatch{
|
|
|
|
Match: structs.HTTPPathMatchPrefix,
|
|
|
|
Value: "/",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all matches for this rule to the list for this hostname
|
|
|
|
for _, match := range rule.Matches {
|
|
|
|
matches = append(matches, hostnameMatch{
|
|
|
|
match: match,
|
|
|
|
filters: rule.Filters,
|
|
|
|
services: rule.Services,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.matchesByHostname[host] = matches
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synthesize assembles a synthetic discovery chain from multiple other discovery chains
|
|
|
|
// that have StartNodes that are referenced by routers or splitters in the entries for the
|
|
|
|
// given CompileRequest.
|
|
|
|
//
|
|
|
|
// This is currently used to help API gateways masquarade as ingress gateways
|
|
|
|
// by providing a set of virtual config entries that change the routing behavior
|
|
|
|
// to upstreams referenced in the given HTTPRoutes or TCPRoutes.
|
2023-02-13 19:18:05 +00:00
|
|
|
func (l *GatewayChainSynthesizer) Synthesize(chains ...*structs.CompiledDiscoveryChain) ([]structs.IngressService, []*structs.CompiledDiscoveryChain, error) {
|
2023-02-09 17:58:55 +00:00
|
|
|
if len(chains) == 0 {
|
|
|
|
return nil, nil, fmt.Errorf("must provide at least one compiled discovery chain")
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:18:05 +00:00
|
|
|
services, set := l.synthesizeEntries()
|
2023-02-09 17:58:55 +00:00
|
|
|
|
2023-02-13 19:18:05 +00:00
|
|
|
if len(set) == 0 {
|
2023-02-09 17:58:55 +00:00
|
|
|
// we can't actually compile a discovery chain, i.e. we're using a TCPRoute-based listener, instead, just return the ingresses
|
2023-02-13 19:18:05 +00:00
|
|
|
// and the pre-compiled discovery chains
|
|
|
|
return services, chains, nil
|
2023-02-09 17:58:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 19:18:05 +00:00
|
|
|
compiledChains := make([]*structs.CompiledDiscoveryChain, 0, len(set))
|
|
|
|
for i, service := range services {
|
|
|
|
entries := set[i]
|
|
|
|
|
|
|
|
compiled, err := Compile(CompileRequest{
|
|
|
|
ServiceName: service.Name,
|
|
|
|
EvaluateInNamespace: service.NamespaceOrDefault(),
|
|
|
|
EvaluateInPartition: service.PartitionOrDefault(),
|
|
|
|
EvaluateInDatacenter: l.datacenter,
|
|
|
|
EvaluateInTrustDomain: l.trustDomain,
|
|
|
|
Entries: entries,
|
|
|
|
})
|
2023-02-09 17:58:55 +00:00
|
|
|
|
2023-02-13 19:18:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2023-02-09 17:58:55 +00:00
|
|
|
}
|
2023-02-22 03:48:26 +00:00
|
|
|
|
2023-03-17 17:31:04 +00:00
|
|
|
node := compiled.Nodes[compiled.StartNode]
|
|
|
|
if node.IsRouter() {
|
|
|
|
resolverPrefix := structs.DiscoveryGraphNodeTypeResolver + ":" + node.Name
|
|
|
|
|
|
|
|
// clean out the clusters that will get added for the router
|
|
|
|
for name := range compiled.Nodes {
|
|
|
|
if strings.HasPrefix(name, resolverPrefix) {
|
|
|
|
delete(compiled.Nodes, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean out the route rules that'll get added for the router
|
|
|
|
filtered := []*structs.DiscoveryRoute{}
|
|
|
|
for _, route := range node.Routes {
|
|
|
|
if strings.HasPrefix(route.NextNode, resolverPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filtered = append(filtered, route)
|
|
|
|
}
|
|
|
|
node.Routes = filtered
|
|
|
|
}
|
|
|
|
compiled.Nodes[compiled.StartNode] = node
|
|
|
|
|
2023-02-22 03:48:26 +00:00
|
|
|
// fix up the nodes for the terminal targets to either be a splitter or resolver if there is no splitter present
|
|
|
|
for name, node := range compiled.Nodes {
|
|
|
|
switch node.Type {
|
|
|
|
// we should only have these two types
|
|
|
|
case structs.DiscoveryGraphNodeTypeRouter:
|
|
|
|
for i, route := range node.Routes {
|
|
|
|
node.Routes[i].NextNode = targetForResolverNode(route.NextNode, chains)
|
|
|
|
}
|
|
|
|
case structs.DiscoveryGraphNodeTypeSplitter:
|
|
|
|
for i, split := range node.Splits {
|
|
|
|
node.Splits[i].NextNode = targetForResolverNode(split.NextNode, chains)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
compiled.Nodes[name] = node
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:18:05 +00:00
|
|
|
for _, c := range chains {
|
|
|
|
for id, target := range c.Targets {
|
|
|
|
compiled.Targets[id] = target
|
|
|
|
}
|
|
|
|
for id, node := range c.Nodes {
|
|
|
|
compiled.Nodes[id] = node
|
|
|
|
}
|
|
|
|
compiled.EnvoyExtensions = append(compiled.EnvoyExtensions, c.EnvoyExtensions...)
|
2023-02-09 17:58:55 +00:00
|
|
|
}
|
2023-02-13 19:18:05 +00:00
|
|
|
compiledChains = append(compiledChains, compiled)
|
2023-02-09 17:58:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 19:18:05 +00:00
|
|
|
return services, compiledChains, nil
|
2023-02-09 17:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// consolidateHTTPRoutes combines all rules into the shortest possible list of routes
|
|
|
|
// with one route per hostname containing all rules for that hostname.
|
|
|
|
func (l *GatewayChainSynthesizer) consolidateHTTPRoutes() []structs.HTTPRouteConfigEntry {
|
|
|
|
var routes []structs.HTTPRouteConfigEntry
|
|
|
|
|
|
|
|
for hostname, rules := range l.matchesByHostname {
|
|
|
|
// Create route for this hostname
|
|
|
|
route := structs.HTTPRouteConfigEntry{
|
|
|
|
Kind: structs.HTTPRoute,
|
2023-02-13 19:18:05 +00:00
|
|
|
Name: fmt.Sprintf("%s-%s-%s", l.gateway.Name, l.suffix, hostsKey(hostname)),
|
2023-02-09 17:58:55 +00:00
|
|
|
Hostnames: []string{hostname},
|
|
|
|
Rules: make([]structs.HTTPRouteRule, 0, len(rules)),
|
|
|
|
Meta: l.gateway.Meta,
|
|
|
|
EnterpriseMeta: l.gateway.EnterpriseMeta,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort rules for this hostname in order of precedence
|
|
|
|
sort.SliceStable(rules, func(i, j int) bool {
|
|
|
|
return compareHTTPRules(rules[i].match, rules[j].match)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Add all rules for this hostname
|
|
|
|
for _, rule := range rules {
|
|
|
|
route.Rules = append(route.Rules, structs.HTTPRouteRule{
|
|
|
|
Matches: []structs.HTTPMatch{rule.match},
|
|
|
|
Filters: rule.filters,
|
|
|
|
Services: rule.services,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
routes = append(routes, route)
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2023-02-22 03:48:26 +00:00
|
|
|
func targetForResolverNode(nodeName string, chains []*structs.CompiledDiscoveryChain) string {
|
|
|
|
resolverPrefix := structs.DiscoveryGraphNodeTypeResolver + ":"
|
|
|
|
splitterPrefix := structs.DiscoveryGraphNodeTypeSplitter + ":"
|
|
|
|
|
|
|
|
if !strings.HasPrefix(nodeName, resolverPrefix) {
|
|
|
|
return nodeName
|
|
|
|
}
|
|
|
|
|
|
|
|
splitterName := splitterPrefix + strings.TrimPrefix(nodeName, resolverPrefix)
|
|
|
|
|
|
|
|
for _, c := range chains {
|
|
|
|
for name, node := range c.Nodes {
|
|
|
|
if node.IsSplitter() && strings.HasPrefix(splitterName, name) {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeName
|
|
|
|
}
|
|
|
|
|
2023-02-09 17:58:55 +00:00
|
|
|
func hostsKey(hosts ...string) string {
|
|
|
|
sort.Strings(hosts)
|
|
|
|
hostsHash := crc32.NewIEEE()
|
|
|
|
for _, h := range hosts {
|
|
|
|
if _, err := hostsHash.Write([]byte(h)); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strconv.FormatUint(uint64(hostsHash.Sum32()), 16)
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:18:05 +00:00
|
|
|
func (l *GatewayChainSynthesizer) synthesizeEntries() ([]structs.IngressService, []*configentry.DiscoveryChainSet) {
|
2023-02-09 17:58:55 +00:00
|
|
|
services := []structs.IngressService{}
|
2023-02-13 19:18:05 +00:00
|
|
|
entries := []*configentry.DiscoveryChainSet{}
|
2023-02-09 17:58:55 +00:00
|
|
|
|
|
|
|
for _, route := range l.consolidateHTTPRoutes() {
|
2023-02-13 19:18:05 +00:00
|
|
|
entrySet := configentry.NewDiscoveryChainSet()
|
2023-02-09 17:58:55 +00:00
|
|
|
ingress, router, splitters, defaults := synthesizeHTTPRouteDiscoveryChain(route)
|
2023-02-13 19:18:05 +00:00
|
|
|
entrySet.AddRouters(router)
|
|
|
|
entrySet.AddSplitters(splitters...)
|
|
|
|
entrySet.AddServices(defaults...)
|
2023-02-09 17:58:55 +00:00
|
|
|
services = append(services, ingress)
|
2023-02-13 19:18:05 +00:00
|
|
|
entries = append(entries, entrySet)
|
2023-02-09 17:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range l.tcpRoutes {
|
|
|
|
services = append(services, synthesizeTCPRouteDiscoveryChain(route)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, entries
|
|
|
|
}
|