2018-03-29 15:25:11 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2018-05-12 19:16:39 +00:00
|
|
|
"sync"
|
2018-03-29 15:25:11 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StaticResolver is a statically defined resolver. This can be used to connect
|
|
|
|
// to an known-Connect endpoint without performing service discovery.
|
|
|
|
type StaticResolver struct {
|
|
|
|
// Addr is the network address (including port) of the instance. It must be
|
|
|
|
// the connect-enabled mTLS server and may be a proxy in front of the actual
|
|
|
|
// target service process. It is a string in any valid form for passing
|
|
|
|
// directly to `net.Dial("tcp", addr)`.
|
|
|
|
Addr string
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
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
|
2018-05-12 19:16:39 +00:00
|
|
|
|
|
|
|
// trustDomain stores the cluster's trust domain it's populated once on first
|
|
|
|
// Resolve call and blocks all resolutions.
|
|
|
|
trustDomain string
|
|
|
|
trustDomainMu sync.Mutex
|
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) {
|
2018-05-12 19:16:39 +00:00
|
|
|
// Fetch trust domain if we've not done that yet
|
|
|
|
err := cr.ensureTrustDomain()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
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
|
|
|
// TODO(banks): we need to figure out what API changes are needed for
|
|
|
|
// prepared queries to become connect-aware. How do we signal that we want
|
|
|
|
// connect-enabled endpoints vs the direct ones for the responses?
|
|
|
|
return "", nil, fmt.Errorf("not implemented")
|
|
|
|
default:
|
|
|
|
return "", nil, fmt.Errorf("unknown resolver type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 19:16:39 +00:00
|
|
|
func (cr *ConsulResolver) ensureTrustDomain() error {
|
|
|
|
cr.trustDomainMu.Lock()
|
|
|
|
defer cr.trustDomainMu.Unlock()
|
|
|
|
|
|
|
|
if cr.trustDomain != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
roots, _, err := cr.Client.Agent().ConnectCARoots(nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed fetching cluster trust domain: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if roots.TrustDomain == "" {
|
|
|
|
return fmt.Errorf("cluster trust domain empty, connect not bootstrapped")
|
|
|
|
}
|
|
|
|
|
|
|
|
cr.trustDomain = roots.TrustDomain
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
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-06 03:21:26 +00:00
|
|
|
addr, certURI := cr.resolveServiceEntry(svcs[idx])
|
|
|
|
return addr, certURI, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *ConsulResolver) resolveQuery(ctx context.Context) (string, connect.CertURI, error) {
|
|
|
|
resp, _, err := cr.Client.PreparedQuery().ExecuteConnect(cr.Name, cr.queryOptions(ctx))
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, certURI := cr.resolveServiceEntry(&svcs[idx])
|
|
|
|
return addr, certURI, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *ConsulResolver) resolveServiceEntry(entry *api.ServiceEntry) (string, connect.CertURI) {
|
|
|
|
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-06-06 03:21:26 +00:00
|
|
|
service := entry.Service.Service
|
|
|
|
if !entry.Service.Connect.Native {
|
|
|
|
service = entry.Service.ProxyDestination
|
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-05-12 19:16:39 +00:00
|
|
|
Host: cr.trustDomain,
|
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,
|
2018-04-26 13:01:20 +00:00
|
|
|
}
|
2018-03-29 15:25:11 +00:00
|
|
|
|
2018-06-06 03:21:26 +00:00
|
|
|
return fmt.Sprintf("%s:%d", addr, port), certURI
|
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,
|
|
|
|
}
|
|
|
|
return q.WithContext(ctx)
|
|
|
|
}
|