Skip to content

Commit

Permalink
feat(consul): add basic auth to consul provider (#12679)
Browse files Browse the repository at this point in the history
  • Loading branch information
commarla authored and mbfrahry committed Mar 28, 2017
1 parent 702d117 commit a1e14df
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions builtin/providers/consul/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consul
import (
"log"
"net/http"
"strings"

consulapi "github.com/hashicorp/consul/api"
)
Expand All @@ -11,6 +12,7 @@ type Config struct {
Datacenter string `mapstructure:"datacenter"`
Address string `mapstructure:"address"`
Scheme string `mapstructure:"scheme"`
HttpAuth string `mapstructure:"http_auth"`
Token string `mapstructure:"token"`
CAFile string `mapstructure:"ca_file"`
CertFile string `mapstructure:"cert_file"`
Expand Down Expand Up @@ -41,6 +43,18 @@ func (c *Config) Client() (*consulapi.Client, error) {
}
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 != "" {
config.Token = c.Token
}
Expand Down
6 changes: 6 additions & 0 deletions builtin/providers/consul/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func Provider() terraform.ResourceProvider {
}, "http"),
},

"http_auth": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("CONSUL_HTTP_AUTH", ""),
},

"ca_file": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down
1 change: 1 addition & 0 deletions website/source/docs/providers/consul/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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".
* `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.
* `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.
Expand Down

0 comments on commit a1e14df

Please sign in to comment.