2017-10-19 22:32:19 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-10-25 23:32:43 +02:00
|
|
|
"io/ioutil"
|
2017-10-19 22:32:19 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-10-27 14:58:16 +02:00
|
|
|
"path"
|
|
|
|
"strings"
|
2017-10-19 22:32:19 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/registry/regsrc"
|
|
|
|
"github.com/hashicorp/terraform/registry/response"
|
|
|
|
"github.com/hashicorp/terraform/svchost"
|
|
|
|
"github.com/hashicorp/terraform/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultRegistry = "registry.terraform.io"
|
|
|
|
registryServiceID = "registry.v1"
|
|
|
|
xTerraformGet = "X-Terraform-Get"
|
|
|
|
xTerraformVersion = "X-Terraform-Version"
|
|
|
|
requestTimeout = 10 * time.Second
|
|
|
|
serviceID = "modules.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-10-25 23:32:43 +02:00
|
|
|
httpClient *http.Client
|
|
|
|
tfVersion = version.String()
|
2017-10-19 22:32:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-10-25 23:32:43 +02:00
|
|
|
httpClient = cleanhttp.DefaultPooledClient()
|
|
|
|
httpClient.Timeout = requestTimeout
|
2017-10-19 22:32:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type errModuleNotFound string
|
|
|
|
|
|
|
|
func (e errModuleNotFound) Error() string {
|
|
|
|
return `module "` + string(e) + `" not found`
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:43:36 +01:00
|
|
|
func (s *Storage) discoverRegURL(host svchost.Hostname) *url.URL {
|
|
|
|
regURL := s.Services.DiscoverServiceURL(host, serviceID)
|
2017-10-25 17:49:41 +02:00
|
|
|
if regURL == nil {
|
2017-10-30 17:59:52 +01:00
|
|
|
return nil
|
2017-10-19 22:32:19 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 14:58:16 +02:00
|
|
|
if !strings.HasSuffix(regURL.Path, "/") {
|
|
|
|
regURL.Path += "/"
|
2017-10-25 17:49:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 14:58:16 +02:00
|
|
|
return regURL
|
2017-10-25 23:32:43 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s *Storage) addRequestCreds(host svchost.Hostname, req *http.Request) {
|
|
|
|
if s.Creds == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
creds, err := s.Creds.ForHost(host)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARNING] Failed to get credentials for %s: %s (ignoring)", host, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if creds != nil {
|
|
|
|
creds.PrepareRequest(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 23:32:43 +02:00
|
|
|
// Lookup module versions in the registry.
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s *Storage) lookupModuleVersions(module *regsrc.Module) (*response.ModuleVersions, error) {
|
2017-11-20 23:43:36 +01:00
|
|
|
host, err := module.SvcHost()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-27 21:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-11-20 23:43:36 +01:00
|
|
|
service := s.discoverRegURL(host)
|
2017-10-30 17:59:52 +01:00
|
|
|
if service == nil {
|
2017-11-20 23:43:36 +01:00
|
|
|
return nil, fmt.Errorf("host %s does not provide Terraform modules", host)
|
2017-10-30 17:59:52 +01:00
|
|
|
}
|
2017-10-25 23:32:43 +02:00
|
|
|
|
2017-10-27 14:58:16 +02:00
|
|
|
p, err := url.Parse(path.Join(module.Module(), "versions"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
service = service.ResolveReference(p)
|
2017-10-19 22:32:19 +02:00
|
|
|
|
2017-10-27 14:58:16 +02:00
|
|
|
log.Printf("[DEBUG] fetching module versions from %q", service)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", service.String(), nil)
|
2017-10-19 22:32:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:43:36 +01:00
|
|
|
s.addRequestCreds(host, req)
|
2017-10-19 22:32:19 +02:00
|
|
|
req.Header.Set(xTerraformVersion, tfVersion)
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
resp, err := httpClient.Do(req)
|
2017-10-19 22:32:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
|
|
|
// OK
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return nil, errModuleNotFound(module.String())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-02 23:53:05 +01:00
|
|
|
for _, mod := range versions.Modules {
|
|
|
|
for _, v := range mod.Versions {
|
|
|
|
log.Printf("[DEBUG] found available version %q for %s", v.Version, mod.Source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 22:32:19 +02:00
|
|
|
return &versions, nil
|
|
|
|
}
|
2017-10-25 23:32:43 +02:00
|
|
|
|
|
|
|
// lookup the location of a specific module version in the registry
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s *Storage) lookupModuleLocation(module *regsrc.Module, version string) (string, error) {
|
2017-11-20 23:43:36 +01:00
|
|
|
host, err := module.SvcHost()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2017-10-27 21:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-11-20 23:43:36 +01:00
|
|
|
service := s.discoverRegURL(host)
|
2017-10-30 17:59:52 +01:00
|
|
|
if service == nil {
|
2017-11-20 23:43:36 +01:00
|
|
|
return "", fmt.Errorf("host %s does not provide Terraform modules", host.ForDisplay())
|
2017-10-30 17:59:52 +01:00
|
|
|
}
|
2017-10-25 23:32:43 +02:00
|
|
|
|
2017-10-27 14:58:16 +02:00
|
|
|
var p *url.URL
|
2017-10-25 23:32:43 +02:00
|
|
|
if version == "" {
|
2017-10-27 14:58:16 +02:00
|
|
|
p, err = url.Parse(path.Join(module.Module(), "download"))
|
2017-10-25 23:32:43 +02:00
|
|
|
} else {
|
2017-10-27 14:58:16 +02:00
|
|
|
p, err = url.Parse(path.Join(module.Module(), version, "download"))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2017-10-25 23:32:43 +02:00
|
|
|
}
|
2017-10-27 14:58:16 +02:00
|
|
|
download := service.ResolveReference(p)
|
2017-10-25 23:32:43 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] looking up module location from %q", download)
|
|
|
|
|
2017-10-27 14:58:16 +02:00
|
|
|
req, err := http.NewRequest("GET", download.String(), nil)
|
2017-10-25 23:32:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:43:36 +01:00
|
|
|
s.addRequestCreds(host, req)
|
2017-10-25 23:32:43 +02:00
|
|
|
req.Header.Set(xTerraformVersion, tfVersion)
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
resp, err := httpClient.Do(req)
|
2017-10-25 23:32:43 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-10-30 16:59:01 +01:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2017-10-25 23:32:43 +02:00
|
|
|
return location, nil
|
|
|
|
}
|