2018-03-29 15:25:11 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2018-07-13 21:38:35 +00:00
|
|
|
"strings"
|
2018-03-29 15:25:11 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/api"
|
2019-06-04 14:02:38 +00:00
|
|
|
"github.com/hashicorp/consul/ipaddr"
|
2018-03-29 15:25:11 +00:00
|
|
|
)
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// Resolver is the interface implemented by a service discovery mechanism to get
|
|
|
|
// the address and identity of an instance to connect to via Connect as a
|
|
|
|
// client.
|
2018-03-29 15:25:11 +00:00
|
|
|
type Resolver interface {
|
|
|
|
// Resolve returns a single service instance to connect to. Implementations
|
|
|
|
// may attempt to ensure the instance returned is currently available. It is
|
|
|
|
// expected that a client will re-dial on a connection failure so making an
|
|
|
|
// effort to return a different service instance each time where available
|
|
|
|
// increases reliability. The context passed can be used to impose timeouts
|
|
|
|
// which may or may not be respected by implementations that make network
|
|
|
|
// calls to resolve the service. The addr returned is a string in any valid
|
2018-04-03 18:10:59 +00:00
|
|
|
// form for passing directly to `net.Dial("tcp", addr)`. The certURI
|
|
|
|
// represents the identity of the service instance. It will be matched against
|
|
|
|
// the TLS certificate URI SAN presented by the server and the connection
|
|
|
|
// rejected if they don't match.
|
2018-03-29 15:25:11 +00:00
|
|
|
Resolve(ctx context.Context) (addr string, certURI connect.CertURI, err error)
|
|
|
|
}
|
|
|
|
|
2018-06-07 11:26:11 +00:00
|
|
|
// StaticResolver is a statically defined resolver. This can be used to Dial a
|
|
|
|
// known Connect endpoint without performing service discovery.
|
2018-03-29 15:25:11 +00:00
|
|
|
type StaticResolver struct {
|
|
|
|
// Addr is the network address (including port) of the instance. It must be
|
2018-06-07 11:26:11 +00:00
|
|
|
// the connect-enabled mTLS listener and may be a proxy in front of the actual
|
2018-03-29 15:25:11 +00:00
|
|
|
// target service process. It is a string in any valid form for passing
|
2018-06-07 11:26:11 +00:00
|
|
|
// directly to net.Dial("tcp", addr).
|
2018-03-29 15:25:11 +00:00
|
|
|
Addr string
|
|
|
|
|
2018-06-07 11:26:11 +00:00
|
|
|
// CertURL is the identity we expect the server to present in it's TLS
|
2018-04-03 18:10:59 +00:00
|
|
|
// certificate. It must be an exact URI string match or the connection will be
|
|
|
|
// rejected.
|
2018-03-29 15:25:11 +00:00
|
|
|
CertURI connect.CertURI
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve implements Resolver by returning the static values.
|
|
|
|
func (sr *StaticResolver) Resolve(ctx context.Context) (string, connect.CertURI, error) {
|
|
|
|
return sr.Addr, sr.CertURI, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ConsulResolverTypeService indicates resolving healthy service nodes.
|
|
|
|
ConsulResolverTypeService int = iota
|
|
|
|
|
|
|
|
// ConsulResolverTypePreparedQuery indicates resolving via prepared query.
|
|
|
|
ConsulResolverTypePreparedQuery
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConsulResolver queries Consul for a service instance.
|
|
|
|
type ConsulResolver struct {
|
|
|
|
// Client is the Consul API client to use. Must be non-nil or Resolve will
|
|
|
|
// panic.
|
|
|
|
Client *api.Client
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// Namespace of the query target.
|
2018-03-29 15:25:11 +00:00
|
|
|
Namespace string
|
|
|
|
|
2021-08-20 16:57:45 +00:00
|
|
|
// Partition of the query target.
|
|
|
|
Partition string
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// Name of the query target.
|
2018-03-29 15:25:11 +00:00
|
|
|
Name string
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// Type of the query target. Should be one of the defined ConsulResolverType*
|
|
|
|
// constants. Currently defaults to ConsulResolverTypeService.
|
2018-03-29 15:25:11 +00:00
|
|
|
Type int
|
|
|
|
|
|
|
|
// Datacenter to resolve in, empty indicates agent's local DC.
|
|
|
|
Datacenter string
|
2022-12-14 20:41:07 +00:00
|
|
|
|
|
|
|
// Specifies the expression used to filter the queries results prior to returning the data.
|
|
|
|
Filter string
|
2018-03-29 15:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve performs service discovery against the local Consul agent and returns
|
|
|
|
// the address and expected identity of a suitable service instance.
|
|
|
|
func (cr *ConsulResolver) Resolve(ctx context.Context) (string, connect.CertURI, error) {
|
|
|
|
switch cr.Type {
|
|
|
|
case ConsulResolverTypeService:
|
|
|
|
return cr.resolveService(ctx)
|
|
|
|
case ConsulResolverTypePreparedQuery:
|
2018-06-06 03:21:26 +00:00
|
|
|
return cr.resolveQuery(ctx)
|
2018-03-29 15:25:11 +00:00
|
|
|
default:
|
|
|
|
return "", nil, fmt.Errorf("unknown resolver type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *ConsulResolver) resolveService(ctx context.Context) (string, connect.CertURI, error) {
|
|
|
|
health := cr.Client.Health()
|
|
|
|
|
|
|
|
svcs, _, err := health.Connect(cr.Name, "", true, cr.queryOptions(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(svcs) < 1 {
|
|
|
|
return "", nil, fmt.Errorf("no healthy instances found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Services are not shuffled by HTTP API, pick one at (pseudo) random.
|
|
|
|
idx := 0
|
|
|
|
if len(svcs) > 1 {
|
|
|
|
idx = rand.Intn(len(svcs))
|
|
|
|
}
|
|
|
|
|
2018-06-08 21:45:34 +00:00
|
|
|
return cr.resolveServiceEntry(svcs[idx])
|
2018-06-06 03:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *ConsulResolver) resolveQuery(ctx context.Context) (string, connect.CertURI, error) {
|
2018-06-06 18:50:23 +00:00
|
|
|
resp, _, err := cr.Client.PreparedQuery().Execute(cr.Name, cr.queryOptions(ctx))
|
2018-06-06 03:21:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
svcs := resp.Nodes
|
|
|
|
if len(svcs) < 1 {
|
|
|
|
return "", nil, fmt.Errorf("no healthy instances found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Services are not shuffled by HTTP API, pick one at (pseudo) random.
|
|
|
|
idx := 0
|
|
|
|
if len(svcs) > 1 {
|
|
|
|
idx = rand.Intn(len(svcs))
|
|
|
|
}
|
|
|
|
|
2018-06-08 21:45:34 +00:00
|
|
|
return cr.resolveServiceEntry(&svcs[idx])
|
2018-06-06 03:21:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 21:45:34 +00:00
|
|
|
func (cr *ConsulResolver) resolveServiceEntry(entry *api.ServiceEntry) (string, connect.CertURI, error) {
|
2018-06-06 03:21:26 +00:00
|
|
|
addr := entry.Service.Address
|
2018-03-29 15:25:11 +00:00
|
|
|
if addr == "" {
|
2018-06-06 03:21:26 +00:00
|
|
|
addr = entry.Node.Address
|
2018-03-29 15:25:11 +00:00
|
|
|
}
|
2018-06-06 03:21:26 +00:00
|
|
|
port := entry.Service.Port
|
2018-03-29 15:25:11 +00:00
|
|
|
|
2018-09-12 16:07:47 +00:00
|
|
|
service := entry.Service.Proxy.DestinationServiceName
|
2018-06-08 21:41:16 +00:00
|
|
|
if entry.Service.Connect != nil && entry.Service.Connect.Native {
|
|
|
|
service = entry.Service.Service
|
2018-06-06 03:13:18 +00:00
|
|
|
}
|
2018-06-08 21:45:34 +00:00
|
|
|
if service == "" {
|
|
|
|
// Shouldn't happen but to protect against bugs in agent API returning bad
|
|
|
|
// service response...
|
|
|
|
return "", nil, fmt.Errorf("not a valid connect service")
|
|
|
|
}
|
2018-06-06 03:13:18 +00:00
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
// Generate the expected CertURI
|
2018-04-26 13:01:20 +00:00
|
|
|
certURI := &connect.SpiffeIDService{
|
2018-11-12 20:20:12 +00:00
|
|
|
// No host since we don't validate trust domain here (we rely on x509 to
|
|
|
|
// prove trust).
|
2018-04-26 13:01:20 +00:00
|
|
|
Namespace: "default",
|
2018-06-06 03:21:26 +00:00
|
|
|
Datacenter: entry.Node.Datacenter,
|
2018-06-06 03:13:18 +00:00
|
|
|
Service: service,
|
2021-06-25 21:47:47 +00:00
|
|
|
// NOTE: this only handles the default implicit partition currently.
|
2018-04-26 13:01:20 +00:00
|
|
|
}
|
2018-03-29 15:25:11 +00:00
|
|
|
|
2019-06-04 14:02:38 +00:00
|
|
|
return ipaddr.FormatAddressPort(addr, port), certURI, nil
|
2018-03-29 15:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *ConsulResolver) queryOptions(ctx context.Context) *api.QueryOptions {
|
|
|
|
q := &api.QueryOptions{
|
|
|
|
// We may make this configurable one day but we may also implement our own
|
|
|
|
// caching which is even more stale so...
|
|
|
|
AllowStale: true,
|
|
|
|
Datacenter: cr.Datacenter,
|
2018-06-06 18:50:23 +00:00
|
|
|
|
|
|
|
// For prepared queries
|
|
|
|
Connect: true,
|
2022-12-14 20:41:07 +00:00
|
|
|
Filter: cr.Filter,
|
2018-03-29 15:25:11 +00:00
|
|
|
}
|
|
|
|
return q.WithContext(ctx)
|
|
|
|
}
|
2018-07-13 21:38:35 +00:00
|
|
|
|
|
|
|
// ConsulResolverFromAddrFunc returns a function for constructing ConsulResolver
|
|
|
|
// from a consul DNS formatted hostname (e.g. foo.service.consul or
|
|
|
|
// foo.query.consul).
|
|
|
|
//
|
|
|
|
// Note, the returned ConsulResolver resolves the query via regular agent HTTP
|
|
|
|
// discovery API. DNS is not needed or used for discovery, only the hostname
|
|
|
|
// format re-used for consistency.
|
|
|
|
func ConsulResolverFromAddrFunc(client *api.Client) func(addr string) (Resolver, error) {
|
|
|
|
// Capture client dependency
|
|
|
|
return func(addr string) (Resolver, error) {
|
|
|
|
// Http clients might provide hostname and port
|
|
|
|
host := strings.ToLower(stripPort(addr))
|
|
|
|
|
|
|
|
// For now we force use of `.consul` TLD regardless of the configured domain
|
|
|
|
// on the cluster. That's because we don't know that domain here and it
|
|
|
|
// would be really complicated to discover it inline here. We do however
|
2019-03-06 17:13:28 +00:00
|
|
|
// need to be able to distinguish a hostname with the optional datacenter
|
2018-07-13 21:38:35 +00:00
|
|
|
// segment which we can't do unambiguously if we allow arbitrary trailing
|
|
|
|
// domains.
|
|
|
|
domain := ".consul"
|
|
|
|
if !strings.HasSuffix(host, domain) {
|
|
|
|
return nil, fmt.Errorf("invalid Consul DNS domain: note Connect SDK " +
|
|
|
|
"currently requires use of .consul domain even if cluster is " +
|
|
|
|
"configured with a different domain.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the domain suffix
|
|
|
|
host = host[0 : len(host)-len(domain)]
|
|
|
|
|
|
|
|
parts := strings.Split(host, ".")
|
|
|
|
numParts := len(parts)
|
|
|
|
|
|
|
|
r := &ConsulResolver{
|
|
|
|
Client: client,
|
|
|
|
Namespace: "default",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that 3 segments may be a valid DNS name like
|
|
|
|
// <tag>.<service>.service.consul but not one we support, it might also be
|
|
|
|
// <service>.service.<datacenter>.consul which we do want to support so we
|
|
|
|
// have to figure out if the last segment is supported keyword and if not
|
|
|
|
// check if the supported keyword is further up...
|
|
|
|
|
|
|
|
// To simplify logic for now, we must match one of the following (not domain
|
|
|
|
// is stripped):
|
|
|
|
// <name>.[service|query]
|
|
|
|
// <name>.[service|query].<dc>
|
|
|
|
if numParts < 2 || numParts > 3 || !supportedTypeLabel(parts[1]) {
|
|
|
|
return nil, fmt.Errorf("unsupported Consul DNS domain: must be either " +
|
|
|
|
"<name>.service[.<datacenter>].consul or " +
|
|
|
|
"<name>.query[.<datacenter>].consul")
|
|
|
|
}
|
|
|
|
|
|
|
|
if numParts == 3 {
|
|
|
|
// Must be datacenter case
|
|
|
|
r.Datacenter = parts[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
// By know we must have a supported query type which means at least 2
|
|
|
|
// elements with first 2 being name, and type respectively.
|
|
|
|
r.Name = parts[0]
|
|
|
|
switch parts[1] {
|
|
|
|
case "service":
|
|
|
|
r.Type = ConsulResolverTypeService
|
|
|
|
case "query":
|
|
|
|
r.Type = ConsulResolverTypePreparedQuery
|
|
|
|
default:
|
|
|
|
// This should never happen (tm) unless the supportedTypeLabel
|
|
|
|
// implementation is changed and this switch isn't.
|
|
|
|
return nil, fmt.Errorf("invalid discovery type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func supportedTypeLabel(label string) bool {
|
|
|
|
return label == "service" || label == "query"
|
|
|
|
}
|
|
|
|
|
|
|
|
// stripPort copied from net/url/url.go
|
|
|
|
func stripPort(hostport string) string {
|
|
|
|
colon := strings.IndexByte(hostport, ':')
|
|
|
|
if colon == -1 {
|
|
|
|
return hostport
|
|
|
|
}
|
|
|
|
if i := strings.IndexByte(hostport, ']'); i != -1 {
|
|
|
|
return strings.TrimPrefix(hostport[:i], "[")
|
|
|
|
}
|
|
|
|
return hostport[:colon]
|
|
|
|
}
|