feat(consul): add basic auth to consul provider (#12679)

This commit is contained in:
Laurent Commarieu 2017-03-20 14:00:44 +01:00 committed by Paul Stack
parent 1c7b8c78ee
commit d54a8da7d4
3 changed files with 21 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package consul
import ( import (
"log" "log"
"net/http" "net/http"
"strings"
consulapi "github.com/hashicorp/consul/api" consulapi "github.com/hashicorp/consul/api"
) )
@ -11,6 +12,7 @@ type Config struct {
Datacenter string `mapstructure:"datacenter"` Datacenter string `mapstructure:"datacenter"`
Address string `mapstructure:"address"` Address string `mapstructure:"address"`
Scheme string `mapstructure:"scheme"` Scheme string `mapstructure:"scheme"`
HttpAuth string `mapstructure:"http_auth"`
Token string `mapstructure:"token"` Token string `mapstructure:"token"`
CAFile string `mapstructure:"ca_file"` CAFile string `mapstructure:"ca_file"`
CertFile string `mapstructure:"cert_file"` CertFile string `mapstructure:"cert_file"`
@ -41,6 +43,18 @@ func (c *Config) Client() (*consulapi.Client, error) {
} }
config.HttpClient.Transport.(*http.Transport).TLSClientConfig = cc config.HttpClient.Transport.(*http.Transport).TLSClientConfig = cc
if c.HttpAuth != "" {
var username, password string
if strings.Contains(c.HttpAuth, ":") {
split := strings.SplitN(c.HttpAuth, ":", 2)
username = split[0]
password = split[1]
} else {
username = c.HttpAuth
}
config.HttpAuth = &consulapi.HttpBasicAuth{username, password}
}
if c.Token != "" { if c.Token != "" {
config.Token = c.Token config.Token = c.Token
} }

View File

@ -35,6 +35,12 @@ func Provider() terraform.ResourceProvider {
}, "http"), }, "http"),
}, },
"http_auth": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("CONSUL_HTTP_AUTH", ""),
},
"ca_file": &schema.Schema{ "ca_file": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,

View File

@ -45,6 +45,7 @@ The following arguments are supported:
* `address` - (Optional) The HTTP(S) API address of the agent to use. Defaults to "127.0.0.1:8500". * `address` - (Optional) The HTTP(S) API address of the agent to use. Defaults to "127.0.0.1:8500".
* `scheme` - (Optional) The URL scheme of the agent to use ("http" or "https"). Defaults to "http". * `scheme` - (Optional) The URL scheme of the agent to use ("http" or "https"). Defaults to "http".
* `http_auth` - (Optional) HTTP Basic Authentication credentials to be used when communicating with Consul, in the format of either `user` or `user:pass`. This may also be specified using the `CONSUL_HTTP_AUTH` environment variable.
* `datacenter` - (Optional) The datacenter to use. Defaults to that of the agent. * `datacenter` - (Optional) The datacenter to use. Defaults to that of the agent.
* `token` - (Optional) The ACL token to use by default when making requests to the agent. * `token` - (Optional) The ACL token to use by default when making requests to the agent.
* `ca_file` - (Optional) A path to a PEM-encoded certificate authority used to verify the remote agent's certificate. * `ca_file` - (Optional) A path to a PEM-encoded certificate authority used to verify the remote agent's certificate.