2017-11-27 22:48:39 +01:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-02-13 22:54:27 +01:00
|
|
|
"os"
|
2017-11-27 22:48:39 +01:00
|
|
|
"path"
|
2020-02-13 22:54:27 +01:00
|
|
|
"strconv"
|
2017-11-27 22:48:39 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-02-13 22:54:27 +01:00
|
|
|
"github.com/hashicorp/go-retryablehttp"
|
2020-03-19 15:20:10 +01:00
|
|
|
svchost "github.com/hashicorp/terraform-svchost"
|
2019-10-11 11:34:26 +02:00
|
|
|
"github.com/hashicorp/terraform-svchost/disco"
|
2021-05-17 18:54:53 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/httpclient"
|
2020-10-16 23:26:05 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/logging"
|
2021-05-17 18:45:36 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/registry/regsrc"
|
|
|
|
"github.com/hashicorp/terraform/internal/registry/response"
|
2017-11-27 22:48:39 +01:00
|
|
|
"github.com/hashicorp/terraform/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-07-27 20:59:03 +02:00
|
|
|
xTerraformGet = "X-Terraform-Get"
|
|
|
|
xTerraformVersion = "X-Terraform-Version"
|
|
|
|
modulesServiceID = "modules.v1"
|
|
|
|
providersServiceID = "providers.v1"
|
2020-02-13 22:54:27 +01:00
|
|
|
|
|
|
|
// registryDiscoveryRetryEnvName is the name of the environment variable that
|
|
|
|
// can be configured to customize number of retries for module and provider
|
|
|
|
// discovery requests with the remote registry.
|
|
|
|
registryDiscoveryRetryEnvName = "TF_REGISTRY_DISCOVERY_RETRY"
|
|
|
|
defaultRetry = 1
|
2020-03-05 17:37:06 +01:00
|
|
|
|
|
|
|
// registryClientTimeoutEnvName is the name of the environment variable that
|
|
|
|
// can be configured to customize the timeout duration (seconds) for module
|
|
|
|
// and provider discovery with the remote registry.
|
|
|
|
registryClientTimeoutEnvName = "TF_REGISTRY_CLIENT_TIMEOUT"
|
|
|
|
|
|
|
|
// defaultRequestTimeout is the default timeout duration for requests to the
|
|
|
|
// remote registry.
|
|
|
|
defaultRequestTimeout = 10 * time.Second
|
2017-11-27 22:48:39 +01:00
|
|
|
)
|
|
|
|
|
2020-03-05 17:37:06 +01:00
|
|
|
var (
|
|
|
|
tfVersion = version.String()
|
2020-02-13 22:54:27 +01:00
|
|
|
|
2020-03-05 17:37:06 +01:00
|
|
|
discoveryRetry int
|
|
|
|
requestTimeout time.Duration
|
|
|
|
)
|
2017-11-27 22:48:39 +01:00
|
|
|
|
2020-02-13 22:54:27 +01:00
|
|
|
func init() {
|
|
|
|
configureDiscoveryRetry()
|
2020-03-05 17:37:06 +01:00
|
|
|
configureRequestTimeout()
|
2020-02-13 22:54:27 +01:00
|
|
|
}
|
|
|
|
|
2017-11-27 22:48:39 +01:00
|
|
|
// Client provides methods to query Terraform Registries.
|
|
|
|
type Client struct {
|
|
|
|
// this is the client to be used for all requests.
|
2020-02-13 22:54:27 +01:00
|
|
|
client *retryablehttp.Client
|
2017-11-27 22:48:39 +01:00
|
|
|
|
|
|
|
// services is a required *disco.Disco, which may have services and
|
|
|
|
// credentials pre-loaded.
|
|
|
|
services *disco.Disco
|
|
|
|
}
|
|
|
|
|
2018-06-29 16:14:54 +02:00
|
|
|
// NewClient returns a new initialized registry client.
|
2018-07-05 21:28:29 +02:00
|
|
|
func NewClient(services *disco.Disco, client *http.Client) *Client {
|
2017-11-27 22:48:39 +01:00
|
|
|
if services == nil {
|
2018-07-05 21:28:29 +02:00
|
|
|
services = disco.New()
|
2017-11-27 22:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if client == nil {
|
2018-02-28 17:40:43 +01:00
|
|
|
client = httpclient.New()
|
2017-11-27 22:48:39 +01:00
|
|
|
client.Timeout = requestTimeout
|
|
|
|
}
|
2020-02-13 22:54:27 +01:00
|
|
|
retryableClient := retryablehttp.NewClient()
|
|
|
|
retryableClient.HTTPClient = client
|
|
|
|
retryableClient.RetryMax = discoveryRetry
|
|
|
|
retryableClient.RequestLogHook = requestLogHook
|
|
|
|
retryableClient.ErrorHandler = maxRetryErrorHandler
|
2017-11-27 22:48:39 +01:00
|
|
|
|
2020-10-18 16:01:48 +02:00
|
|
|
logOutput := logging.LogOutput()
|
2020-03-03 18:22:34 +01:00
|
|
|
retryableClient.Logger = log.New(logOutput, "", log.Flags())
|
|
|
|
|
2020-02-13 22:54:27 +01:00
|
|
|
services.Transport = retryableClient.HTTPClient.Transport
|
2017-11-27 22:48:39 +01:00
|
|
|
|
2019-10-11 11:34:26 +02:00
|
|
|
services.SetUserAgent(httpclient.TerraformUserAgent(version.String()))
|
|
|
|
|
2017-11-27 22:48:39 +01:00
|
|
|
return &Client{
|
2020-02-13 22:54:27 +01:00
|
|
|
client: retryableClient,
|
2017-11-27 22:48:39 +01:00
|
|
|
services: services,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 00:04:51 +02:00
|
|
|
// Discover queries the host, and returns the url for the registry.
|
2018-12-10 11:06:05 +01:00
|
|
|
func (c *Client) Discover(host svchost.Hostname, serviceID string) (*url.URL, error) {
|
|
|
|
service, err := c.services.DiscoverServiceURL(host, serviceID)
|
|
|
|
if err != nil {
|
2019-03-27 19:20:15 +01:00
|
|
|
return nil, &ServiceUnreachableError{err}
|
2018-01-18 00:31:05 +01:00
|
|
|
}
|
2017-12-05 21:06:17 +01:00
|
|
|
if !strings.HasSuffix(service.Path, "/") {
|
|
|
|
service.Path += "/"
|
|
|
|
}
|
2018-12-10 11:06:05 +01:00
|
|
|
return service, nil
|
2017-11-27 22:48:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-31 00:44:06 +02:00
|
|
|
// ModuleVersions queries the registry for a module, and returns the available versions.
|
|
|
|
func (c *Client) ModuleVersions(module *regsrc.Module) (*response.ModuleVersions, error) {
|
2017-11-27 22:48:39 +01:00
|
|
|
host, err := module.SvcHost()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
service, err := c.Discover(host, modulesServiceID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-11-27 22:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p, err := url.Parse(path.Join(module.Module(), "versions"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
service = service.ResolveReference(p)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] fetching module versions from %q", service)
|
|
|
|
|
2020-02-13 22:54:27 +01:00
|
|
|
req, err := retryablehttp.NewRequest("GET", service.String(), nil)
|
2017-11-27 22:48:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:54:27 +01:00
|
|
|
c.addRequestCreds(host, req.Request)
|
2017-11-27 22:48:39 +01:00
|
|
|
req.Header.Set(xTerraformVersion, tfVersion)
|
|
|
|
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
|
|
|
// OK
|
|
|
|
case http.StatusNotFound:
|
2018-02-14 01:36:36 +01:00
|
|
|
return nil, &errModuleNotFound{addr: module}
|
2017-11-27 22:48:39 +01:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("error looking up module versions: %s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
var versions response.ModuleVersions
|
|
|
|
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
if err := dec.Decode(&versions); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mod := range versions.Modules {
|
|
|
|
for _, v := range mod.Versions {
|
|
|
|
log.Printf("[DEBUG] found available version %q for %s", v.Version, mod.Source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &versions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) addRequestCreds(host svchost.Hostname, req *http.Request) {
|
2018-07-05 21:28:29 +02:00
|
|
|
creds, err := c.services.CredentialsForHost(host)
|
2017-11-27 22:48:39 +01:00
|
|
|
if err != nil {
|
2018-01-17 03:05:26 +01:00
|
|
|
log.Printf("[WARN] Failed to get credentials for %s: %s (ignoring)", host, err)
|
2017-11-27 22:48:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if creds != nil {
|
|
|
|
creds.PrepareRequest(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:44:06 +02:00
|
|
|
// ModuleLocation find the download location for a specific version module.
|
2017-11-27 22:48:39 +01:00
|
|
|
// This returns a string, because the final location may contain special go-getter syntax.
|
2018-07-31 00:44:06 +02:00
|
|
|
func (c *Client) ModuleLocation(module *regsrc.Module, version string) (string, error) {
|
2017-11-27 22:48:39 +01:00
|
|
|
host, err := module.SvcHost()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:06:05 +01:00
|
|
|
service, err := c.Discover(host, modulesServiceID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2017-11-27 22:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var p *url.URL
|
|
|
|
if version == "" {
|
|
|
|
p, err = url.Parse(path.Join(module.Module(), "download"))
|
|
|
|
} else {
|
|
|
|
p, err = url.Parse(path.Join(module.Module(), version, "download"))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
download := service.ResolveReference(p)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] looking up module location from %q", download)
|
|
|
|
|
2020-02-13 22:54:27 +01:00
|
|
|
req, err := retryablehttp.NewRequest("GET", download.String(), nil)
|
2017-11-27 22:48:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:54:27 +01:00
|
|
|
c.addRequestCreds(host, req.Request)
|
2017-11-27 22:48:39 +01:00
|
|
|
req.Header.Set(xTerraformVersion, tfVersion)
|
|
|
|
|
|
|
|
resp, err := c.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// there should be no body, but save it for logging
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error reading response body from registry: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK, http.StatusNoContent:
|
|
|
|
// OK
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return "", fmt.Errorf("module %q version %q not found", module, version)
|
|
|
|
default:
|
|
|
|
// anything else is an error:
|
|
|
|
return "", fmt.Errorf("error getting download location for %q: %s resp:%s", module, resp.Status, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the download location is in the X-Terraform-Get header
|
|
|
|
location := resp.Header.Get(xTerraformGet)
|
|
|
|
if location == "" {
|
|
|
|
return "", fmt.Errorf("failed to get download URL for %q: %s resp:%s", module, resp.Status, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If location looks like it's trying to be a relative URL, treat it as
|
|
|
|
// one.
|
|
|
|
//
|
|
|
|
// We don't do this for just _any_ location, since the X-Terraform-Get
|
|
|
|
// header is a go-getter location rather than a URL, and so not all
|
|
|
|
// possible values will parse reasonably as URLs.)
|
|
|
|
//
|
|
|
|
// When used in conjunction with go-getter we normally require this header
|
|
|
|
// to be an absolute URL, but we are more liberal here because third-party
|
|
|
|
// registry implementations may not "know" their own absolute URLs if
|
|
|
|
// e.g. they are running behind a reverse proxy frontend, or such.
|
|
|
|
if strings.HasPrefix(location, "/") || strings.HasPrefix(location, "./") || strings.HasPrefix(location, "../") {
|
|
|
|
locationURL, err := url.Parse(location)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("invalid relative URL for %q: %s", module, err)
|
|
|
|
}
|
|
|
|
locationURL = download.ResolveReference(locationURL)
|
|
|
|
location = locationURL.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return location, nil
|
|
|
|
}
|
2018-07-27 20:59:03 +02:00
|
|
|
|
2020-02-14 20:43:10 +01:00
|
|
|
// configureDiscoveryRetry configures the number of retries the registry client
|
|
|
|
// will attempt for requests with retryable errors, like 502 status codes
|
2020-02-13 22:54:27 +01:00
|
|
|
func configureDiscoveryRetry() {
|
|
|
|
discoveryRetry = defaultRetry
|
|
|
|
|
|
|
|
if v := os.Getenv(registryDiscoveryRetryEnvName); v != "" {
|
|
|
|
retry, err := strconv.Atoi(v)
|
|
|
|
if err == nil && retry > 0 {
|
|
|
|
discoveryRetry = retry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func requestLogHook(logger retryablehttp.Logger, req *http.Request, i int) {
|
|
|
|
if i > 0 {
|
|
|
|
logger.Printf("[INFO] Previous request to the remote registry failed, attempting retry.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func maxRetryErrorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) {
|
|
|
|
// Close the body per library instructions
|
|
|
|
if resp != nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:20:10 +01:00
|
|
|
// Additional error detail: if we have a response, use the status code;
|
|
|
|
// if we have an error, use that; otherwise nothing. We will never have
|
|
|
|
// both response and error.
|
2020-02-13 22:54:27 +01:00
|
|
|
var errMsg string
|
2020-03-19 15:20:10 +01:00
|
|
|
if resp != nil {
|
|
|
|
errMsg = fmt.Sprintf(": %d", resp.StatusCode)
|
|
|
|
} else if err != nil {
|
|
|
|
errMsg = fmt.Sprintf(": %s", err)
|
2020-02-13 22:54:27 +01:00
|
|
|
}
|
2020-03-19 15:20:10 +01:00
|
|
|
|
|
|
|
// This function is always called with numTries=RetryMax+1. If we made any
|
|
|
|
// retry attempts, include that in the error message.
|
2020-02-13 22:54:27 +01:00
|
|
|
if numTries > 1 {
|
2020-03-19 15:20:10 +01:00
|
|
|
return resp, fmt.Errorf("the request failed after %d attempts, please try again later%s",
|
|
|
|
numTries, errMsg)
|
2020-02-13 22:54:27 +01:00
|
|
|
}
|
2020-03-19 15:20:10 +01:00
|
|
|
return resp, fmt.Errorf("the request failed, please try again later%s", errMsg)
|
2020-02-13 22:54:27 +01:00
|
|
|
}
|
2020-03-05 17:37:06 +01:00
|
|
|
|
|
|
|
// configureRequestTimeout configures the registry client request timeout from
|
|
|
|
// environment variables
|
|
|
|
func configureRequestTimeout() {
|
|
|
|
requestTimeout = defaultRequestTimeout
|
|
|
|
|
|
|
|
if v := os.Getenv(registryClientTimeoutEnvName); v != "" {
|
|
|
|
timeout, err := strconv.Atoi(v)
|
|
|
|
if err == nil && timeout > 0 {
|
|
|
|
requestTimeout = time.Duration(timeout) * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|