2017-10-18 00:25:16 +02:00
|
|
|
// Package disco handles Terraform's remote service discovery protocol.
|
|
|
|
//
|
|
|
|
// This protocol allows mapping from a service hostname, as produced by the
|
|
|
|
// svchost package, to a set of services supported by that host and the
|
|
|
|
// endpoint information for each supported service.
|
|
|
|
package disco
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2018-12-10 11:06:05 +01:00
|
|
|
"fmt"
|
2017-10-18 00:25:16 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
2018-12-13 23:12:36 +01:00
|
|
|
"github.com/hashicorp/terraform/httpclient"
|
2017-10-18 00:25:16 +02:00
|
|
|
"github.com/hashicorp/terraform/svchost"
|
2017-10-18 17:45:52 +02:00
|
|
|
"github.com/hashicorp/terraform/svchost/auth"
|
2017-10-18 00:25:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-12-10 11:06:05 +01:00
|
|
|
// Fixed path to the discovery manifest.
|
|
|
|
discoPath = "/.well-known/terraform.json"
|
|
|
|
|
|
|
|
// Arbitrary-but-small number to prevent runaway redirect loops.
|
|
|
|
maxRedirects = 3
|
|
|
|
|
|
|
|
// Arbitrary-but-small time limit to prevent UI "hangs" during discovery.
|
|
|
|
discoTimeout = 11 * time.Second
|
|
|
|
|
|
|
|
// 1MB - to prevent abusive services from using loads of our memory.
|
|
|
|
maxDiscoDocBytes = 1 * 1024 * 1024
|
2017-10-18 00:25:16 +02:00
|
|
|
)
|
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
// httpTransport is overridden during tests, to skip TLS verification.
|
|
|
|
var httpTransport = cleanhttp.DefaultPooledTransport()
|
2017-10-18 00:25:16 +02:00
|
|
|
|
|
|
|
// Disco is the main type in this package, which allows discovery on given
|
|
|
|
// hostnames and caches the results by hostname to avoid repeated requests
|
|
|
|
// for the same information.
|
|
|
|
type Disco struct {
|
2018-12-10 11:06:05 +01:00
|
|
|
hostCache map[svchost.Hostname]*Host
|
2017-10-18 17:45:52 +02:00
|
|
|
credsSrc auth.CredentialsSource
|
2017-10-25 16:47:36 +02:00
|
|
|
|
2018-02-28 17:40:17 +01:00
|
|
|
// Transport is a custom http.RoundTripper to use.
|
|
|
|
Transport http.RoundTripper
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
// New returns a new initialized discovery object.
|
|
|
|
func New() *Disco {
|
|
|
|
return NewWithCredentialsSource(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithCredentialsSource returns a new discovery object initialized with
|
|
|
|
// the given credentials source.
|
|
|
|
func NewWithCredentialsSource(credsSrc auth.CredentialsSource) *Disco {
|
2018-12-10 11:06:05 +01:00
|
|
|
return &Disco{
|
|
|
|
hostCache: make(map[svchost.Hostname]*Host),
|
|
|
|
credsSrc: credsSrc,
|
2019-01-28 09:37:41 +01:00
|
|
|
Transport: httpTransport,
|
2018-12-10 11:06:05 +01:00
|
|
|
}
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:45:52 +02:00
|
|
|
// SetCredentialsSource provides a credentials source that will be used to
|
|
|
|
// add credentials to outgoing discovery requests, where available.
|
|
|
|
//
|
|
|
|
// If this method is never called, no outgoing discovery requests will have
|
|
|
|
// credentials.
|
|
|
|
func (d *Disco) SetCredentialsSource(src auth.CredentialsSource) {
|
|
|
|
d.credsSrc = src
|
|
|
|
}
|
|
|
|
|
2019-08-08 01:32:23 +02:00
|
|
|
// CredentialsSource returns the credentials source associated with the receiver,
|
|
|
|
// or an empty credentials source if none is associated.
|
|
|
|
func (d *Disco) CredentialsSource() auth.CredentialsSource {
|
|
|
|
if d.credsSrc == nil {
|
|
|
|
// We'll return an empty one just to save the caller from having to
|
|
|
|
// protect against the nil case, since this interface already allows
|
|
|
|
// for the possibility of there being no credentials at all.
|
|
|
|
return auth.StaticCredentialsSource(nil)
|
|
|
|
}
|
|
|
|
return d.credsSrc
|
|
|
|
}
|
|
|
|
|
2018-07-05 21:28:29 +02:00
|
|
|
// CredentialsForHost returns a non-nil HostCredentials if the embedded source has
|
|
|
|
// credentials available for the host, and a nil HostCredentials if it does not.
|
2018-12-10 11:06:05 +01:00
|
|
|
func (d *Disco) CredentialsForHost(hostname svchost.Hostname) (auth.HostCredentials, error) {
|
2018-07-05 21:28:29 +02:00
|
|
|
if d.credsSrc == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-12-10 11:06:05 +01:00
|
|
|
return d.credsSrc.ForHost(hostname)
|
2018-07-05 21:28:29 +02:00
|
|
|
}
|
|
|
|
|
2017-10-26 00:57:03 +02:00
|
|
|
// ForceHostServices provides a pre-defined set of services for a given
|
|
|
|
// host, which prevents the receiver from attempting network-based discovery
|
|
|
|
// for the given host. Instead, the given services map will be returned
|
|
|
|
// verbatim.
|
|
|
|
//
|
|
|
|
// When providing "forced" services, any relative URLs are resolved against
|
|
|
|
// the initial discovery URL that would have been used for network-based
|
|
|
|
// discovery, yielding the same results as if the given map were published
|
|
|
|
// at the host's default discovery URL, though using absolute URLs is strongly
|
|
|
|
// recommended to make the configured behavior more explicit.
|
2018-12-10 11:06:05 +01:00
|
|
|
func (d *Disco) ForceHostServices(hostname svchost.Hostname, services map[string]interface{}) {
|
2017-10-26 00:57:03 +02:00
|
|
|
if services == nil {
|
|
|
|
services = map[string]interface{}{}
|
|
|
|
}
|
2019-01-28 09:37:41 +01:00
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
d.hostCache[hostname] = &Host{
|
2017-10-26 00:57:03 +02:00
|
|
|
discoURL: &url.URL{
|
|
|
|
Scheme: "https",
|
2018-12-10 11:06:05 +01:00
|
|
|
Host: string(hostname),
|
2017-10-26 00:57:03 +02:00
|
|
|
Path: discoPath,
|
|
|
|
},
|
2018-12-13 23:12:36 +01:00
|
|
|
hostname: hostname.ForDisplay(),
|
|
|
|
services: services,
|
2019-01-28 09:37:41 +01:00
|
|
|
transport: d.Transport,
|
2017-10-26 00:57:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:25:16 +02:00
|
|
|
// Discover runs the discovery protocol against the given hostname (which must
|
|
|
|
// already have been validated and prepared with svchost.ForComparison) and
|
|
|
|
// returns an object describing the services available at that host.
|
|
|
|
//
|
|
|
|
// If a given hostname supports no Terraform services at all, a non-nil but
|
|
|
|
// empty Host object is returned. When giving feedback to the end user about
|
2018-12-10 11:06:05 +01:00
|
|
|
// such situations, we say "host <name> does not provide a <service> service",
|
|
|
|
// regardless of whether that is due to that service specifically being absent
|
|
|
|
// or due to the host not providing Terraform services at all, since we don't
|
|
|
|
// wish to expose the detail of whole-host discovery to an end-user.
|
|
|
|
func (d *Disco) Discover(hostname svchost.Hostname) (*Host, error) {
|
|
|
|
if host, cached := d.hostCache[hostname]; cached {
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
host, err := d.discover(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.hostCache[hostname] = host
|
|
|
|
|
|
|
|
return host, nil
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiscoverServiceURL is a convenience wrapper for discovery on a given
|
|
|
|
// hostname and then looking up a particular service in the result.
|
2018-12-10 11:06:05 +01:00
|
|
|
func (d *Disco) DiscoverServiceURL(hostname svchost.Hostname, serviceID string) (*url.URL, error) {
|
|
|
|
host, err := d.Discover(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return host.ServiceURL(serviceID)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// discover implements the actual discovery process, with its result cached
|
|
|
|
// by the public-facing Discover method.
|
2018-12-10 11:06:05 +01:00
|
|
|
func (d *Disco) discover(hostname svchost.Hostname) (*Host, error) {
|
2017-10-18 00:25:16 +02:00
|
|
|
discoURL := &url.URL{
|
|
|
|
Scheme: "https",
|
2018-12-10 11:06:05 +01:00
|
|
|
Host: hostname.String(),
|
2017-10-18 00:25:16 +02:00
|
|
|
Path: discoPath,
|
|
|
|
}
|
2017-10-25 16:47:36 +02:00
|
|
|
|
2017-10-18 00:25:16 +02:00
|
|
|
client := &http.Client{
|
2019-01-28 09:37:41 +01:00
|
|
|
Transport: d.Transport,
|
2017-10-18 00:25:16 +02:00
|
|
|
Timeout: discoTimeout,
|
|
|
|
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
log.Printf("[DEBUG] Service discovery redirected to %s", req.URL)
|
|
|
|
if len(via) > maxRedirects {
|
2018-12-10 11:06:05 +01:00
|
|
|
return errors.New("too many redirects") // this error will never actually be seen
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &http.Request{
|
2018-12-13 23:12:36 +01:00
|
|
|
Header: make(http.Header),
|
2017-10-18 00:25:16 +02:00
|
|
|
Method: "GET",
|
|
|
|
URL: discoURL,
|
|
|
|
}
|
2018-12-13 23:12:36 +01:00
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("User-Agent", httpclient.UserAgentString())
|
2017-10-18 00:25:16 +02:00
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
creds, err := d.CredentialsForHost(hostname)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] Failed to get credentials for %s: %s (ignoring)", hostname, err)
|
2017-10-18 17:45:52 +02:00
|
|
|
}
|
2018-12-10 11:06:05 +01:00
|
|
|
if creds != nil {
|
|
|
|
// Update the request to include credentials.
|
|
|
|
creds.PrepareRequest(req)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
log.Printf("[DEBUG] Service discovery for %s at %s", hostname, discoURL)
|
|
|
|
|
2017-10-18 00:25:16 +02:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2018-12-10 11:06:05 +01:00
|
|
|
return nil, fmt.Errorf("Failed to request discovery document: %v", err)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
2018-06-29 16:14:54 +02:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
host := &Host{
|
|
|
|
// Use the discovery URL from resp.Request in
|
|
|
|
// case the client followed any redirects.
|
2018-12-13 23:12:36 +01:00
|
|
|
discoURL: resp.Request.URL,
|
|
|
|
hostname: hostname.ForDisplay(),
|
2019-01-28 09:37:41 +01:00
|
|
|
transport: d.Transport,
|
2018-12-10 11:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the host without any services.
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return host, nil
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("Failed to request discovery document: %s", resp.Status)
|
|
|
|
}
|
2017-10-18 00:25:16 +02:00
|
|
|
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
mediaType, _, err := mime.ParseMediaType(contentType)
|
|
|
|
if err != nil {
|
2018-12-10 11:06:05 +01:00
|
|
|
return nil, fmt.Errorf("Discovery URL has a malformed Content-Type %q", contentType)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
if mediaType != "application/json" {
|
2018-12-10 11:06:05 +01:00
|
|
|
return nil, fmt.Errorf("Discovery URL returned an unsupported Content-Type %q", mediaType)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
// This doesn't catch chunked encoding, because ContentLength is -1 in that case.
|
2017-10-18 00:25:16 +02:00
|
|
|
if resp.ContentLength > maxDiscoDocBytes {
|
|
|
|
// Size limit here is not a contractual requirement and so we may
|
|
|
|
// adjust it over time if we find a different limit is warranted.
|
2018-12-10 11:06:05 +01:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Discovery doc response is too large (got %d bytes; limit %d)",
|
|
|
|
resp.ContentLength, maxDiscoDocBytes,
|
|
|
|
)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
// If the response is using chunked encoding then we can't predict its
|
|
|
|
// size, but we'll at least prevent reading the entire thing into memory.
|
2017-10-18 00:25:16 +02:00
|
|
|
lr := io.LimitReader(resp.Body, maxDiscoDocBytes)
|
|
|
|
|
|
|
|
servicesBytes, err := ioutil.ReadAll(lr)
|
|
|
|
if err != nil {
|
2018-12-10 11:06:05 +01:00
|
|
|
return nil, fmt.Errorf("Error reading discovery document body: %v", err)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var services map[string]interface{}
|
|
|
|
err = json.Unmarshal(servicesBytes, &services)
|
|
|
|
if err != nil {
|
2018-12-10 11:06:05 +01:00
|
|
|
return nil, fmt.Errorf("Failed to decode discovery document as a JSON object: %v", err)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
2018-12-10 11:06:05 +01:00
|
|
|
host.services = services
|
2017-10-18 00:25:16 +02:00
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
return host, nil
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Forget invalidates any cached record of the given hostname. If the host
|
|
|
|
// has no cache entry then this is a no-op.
|
2018-12-10 11:06:05 +01:00
|
|
|
func (d *Disco) Forget(hostname svchost.Hostname) {
|
|
|
|
delete(d.hostCache, hostname)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ForgetAll is like Forget, but for all of the hostnames that have cache entries.
|
|
|
|
func (d *Disco) ForgetAll() {
|
2018-12-10 11:06:05 +01:00
|
|
|
d.hostCache = make(map[svchost.Hostname]*Host)
|
2017-10-18 00:25:16 +02:00
|
|
|
}
|