Merge pull request #2730 from hashicorp/f-cli-https-parsing

Clean up http-addr parsing for https and unix sockets
This commit is contained in:
Kyle Havlovitz 2017-02-10 20:23:45 -05:00 committed by GitHub
commit b79f613241
4 changed files with 38 additions and 12 deletions

View File

@ -346,13 +346,22 @@ func NewClient(config *Config) (*Client, error) {
config.HttpClient = defConfig.HttpClient config.HttpClient = defConfig.HttpClient
} }
if parts := strings.SplitN(config.Address, "unix://", 2); len(parts) == 2 { parts := strings.SplitN(config.Address, "://", 2)
trans := cleanhttp.DefaultTransport() if len(parts) == 2 {
trans.Dial = func(_, _ string) (net.Conn, error) { switch parts[0] {
return net.Dial("unix", parts[1]) case "http":
} case "https":
config.HttpClient = &http.Client{ config.Scheme = "https"
Transport: trans, case "unix":
trans := cleanhttp.DefaultTransport()
trans.Dial = func(_, _ string) (net.Conn, error) {
return net.Dial("unix", parts[1])
}
config.HttpClient = &http.Client{
Transport: trans,
}
default:
return nil, fmt.Errorf("Unknown protocol scheme: %s", parts[0])
} }
config.Address = parts[1] config.Address = parts[1]
} }

View File

@ -1082,7 +1082,16 @@ func (c *Command) Run(args []string) int {
} }
// Get the new client http listener addr // Get the new client http listener addr
httpAddr, err := config.ClientListener(config.Addresses.HTTP, config.Ports.HTTP) var httpAddr net.Addr
var err error
if config.Ports.HTTP != -1 {
httpAddr, err = config.ClientListener(config.Addresses.HTTP, config.Ports.HTTP)
} else if config.Ports.HTTPS != -1 {
httpAddr, err = config.ClientListener(config.Addresses.HTTPS, config.Ports.HTTPS)
} else if len(config.WatchPlans) > 0 {
c.Ui.Error("Error: cannot use watches if both HTTP and HTTPS are disabled")
return 1
}
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to determine HTTP address: %v", err)) c.Ui.Error(fmt.Sprintf("Failed to determine HTTP address: %v", err))
} }
@ -1092,7 +1101,12 @@ func (c *Command) Run(args []string) int {
go func(wp *watch.WatchPlan) { go func(wp *watch.WatchPlan) {
wp.Handler = makeWatchHandler(logOutput, wp.Exempt["handler"]) wp.Handler = makeWatchHandler(logOutput, wp.Exempt["handler"])
wp.LogOutput = c.logOutput wp.LogOutput = c.logOutput
if err := wp.Run(httpAddr.String()); err != nil { addr := httpAddr.String()
// If it's a unix socket, prefix with unix:// so the client initializes correctly
if httpAddr.Network() == "unix" {
addr = "unix://" + addr
}
if err := wp.Run(addr); err != nil {
c.Ui.Error(fmt.Sprintf("Error running watch: %v", err)) c.Ui.Error(fmt.Sprintf("Error running watch: %v", err))
} }
}(wp) }(wp)

View File

@ -86,7 +86,8 @@ func (c *Command) httpFlagsClient(f *flag.FlagSet) *flag.FlagSet {
"The `address` and port of the Consul HTTP agent. The value can be an IP "+ "The `address` and port of the Consul HTTP agent. The value can be an IP "+
"address or DNS address, but it must also include the port. This can "+ "address or DNS address, but it must also include the port. This can "+
"also be specified via the CONSUL_HTTP_ADDR environment variable. The "+ "also be specified via the CONSUL_HTTP_ADDR environment variable. The "+
"default value is 127.0.0.1:8500.") "default value is http://127.0.0.1:8500. The scheme can also be set to "+
"HTTPS by setting the environment variable CONSUL_HTTP_SSL=true.")
f.Var(&c.token, "token", f.Var(&c.token, "token",
"ACL token to use in the request. This can also be specified via the "+ "ACL token to use in the request. This can also be specified via the "+
"CONSUL_HTTP_TOKEN environment variable. If unspecified, the query will "+ "CONSUL_HTTP_TOKEN environment variable. If unspecified, the query will "+

View File

@ -1,7 +1,9 @@
* `-http-addr=<addr>` - Address of the Consul agent with the port. This can be * `-http-addr=<addr>` - Address of the Consul agent with the port. This can be
an IP address or DNS address, but it must include the port. This can also be an IP address or DNS address, but it must include the port. This can also be
specified via the `CONSUL_HTTP_ADDR` environment variable. The default value is specified via the `CONSUL_HTTP_ADDR` environment variable. In Consul 0.8 and
127.0.0.1:8500. later, the default value is http://127.0.0.1:8500, and https can optionally
be used instead. The scheme can also be set to HTTPS by setting the
environment variable `CONSUL_HTTP_SSL=true`.
* `-token=<value>` - ACL token to use in the request. This can also be specified * `-token=<value>` - ACL token to use in the request. This can also be specified
via the `CONSUL_HTTP_TOKEN` environment variable. If unspecified, the query via the `CONSUL_HTTP_TOKEN` environment variable. If unspecified, the query