2018-03-21 22:35:00 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-05-19 18:20:20 +00:00
|
|
|
"net"
|
2018-03-21 22:35:00 +00:00
|
|
|
"net/http"
|
2018-04-03 18:10:59 +00:00
|
|
|
_ "net/http/pprof" // Expose pprof if configured
|
2018-05-06 15:54:36 +00:00
|
|
|
"os"
|
2018-05-19 18:04:47 +00:00
|
|
|
"sort"
|
2018-05-19 18:20:20 +00:00
|
|
|
"strconv"
|
2018-09-27 14:00:51 +00:00
|
|
|
"strings"
|
2018-03-21 22:35:00 +00:00
|
|
|
|
2018-05-19 07:11:51 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2018-03-21 22:35:00 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2018-04-03 18:10:59 +00:00
|
|
|
proxyImpl "github.com/hashicorp/consul/connect/proxy"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2018-03-21 22:35:00 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/logging"
|
2018-03-21 22:35:00 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(ui cli.Ui, shutdownCh <-chan struct{}) *cmd {
|
2018-05-19 22:47:25 +00:00
|
|
|
ui = &cli.PrefixedUi{
|
|
|
|
OutputPrefix: "==> ",
|
|
|
|
InfoPrefix: " ",
|
|
|
|
ErrorPrefix: "==> ",
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:35:00 +00:00
|
|
|
c := &cmd{UI: ui, shutdownCh: shutdownCh}
|
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
|
|
|
help string
|
|
|
|
|
|
|
|
shutdownCh <-chan struct{}
|
|
|
|
|
2020-03-30 17:50:58 +00:00
|
|
|
logger hclog.Logger
|
2018-03-21 22:35:00 +00:00
|
|
|
|
|
|
|
// flags
|
2018-05-19 18:20:20 +00:00
|
|
|
logLevel string
|
2020-01-28 23:50:41 +00:00
|
|
|
logJSON bool
|
2018-05-19 18:20:20 +00:00
|
|
|
cfgFile string
|
|
|
|
proxyID string
|
2018-09-27 14:00:51 +00:00
|
|
|
sidecarFor string
|
2018-05-19 18:20:20 +00:00
|
|
|
pprofAddr string
|
|
|
|
service string
|
|
|
|
serviceAddr string
|
|
|
|
upstreams map[string]proxyImpl.UpstreamConfig
|
|
|
|
listen string
|
2018-05-19 22:47:25 +00:00
|
|
|
register bool
|
2018-05-20 17:04:29 +00:00
|
|
|
registerId string
|
2018-05-19 18:04:47 +00:00
|
|
|
|
|
|
|
// test flags
|
|
|
|
testNoStart bool // don't start the proxy, just exit 0
|
2018-03-21 22:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
|
|
|
|
c.flags.StringVar(&c.proxyID, "proxy-id", "",
|
|
|
|
"The proxy's ID on the local agent.")
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
c.flags.StringVar(&c.sidecarFor, "sidecar-for", "",
|
|
|
|
"The ID of a service instance on the local agent that this proxy should "+
|
|
|
|
"become a sidecar for. It requires that the proxy service is registered "+
|
|
|
|
"with the agent as a connect-proxy with Proxy.DestinationServiceID set "+
|
|
|
|
"to this value. If more than one such proxy is registered it will fail.")
|
|
|
|
|
2018-03-21 22:35:00 +00:00
|
|
|
c.flags.StringVar(&c.logLevel, "log-level", "INFO",
|
|
|
|
"Specifies the log level.")
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
c.flags.BoolVar(&c.logJSON, "log-json", false,
|
|
|
|
"Output logs in JSON format.")
|
|
|
|
|
2018-03-21 22:35:00 +00:00
|
|
|
c.flags.StringVar(&c.pprofAddr, "pprof-addr", "",
|
|
|
|
"Enable debugging via pprof. Providing a host:port (or just ':port') "+
|
|
|
|
"enables profiling HTTP endpoints on that address.")
|
|
|
|
|
2018-05-19 07:43:38 +00:00
|
|
|
c.flags.StringVar(&c.service, "service", "",
|
|
|
|
"Name of the service this proxy is representing.")
|
|
|
|
|
|
|
|
c.flags.Var((*FlagUpstreams)(&c.upstreams), "upstream",
|
|
|
|
"Upstream service to support connecting to. The format should be "+
|
|
|
|
"'name:addr', such as 'db:8181'. This will make 'db' available "+
|
2018-05-19 18:20:20 +00:00
|
|
|
"on port 8181. This can be repeated multiple times.")
|
|
|
|
|
|
|
|
c.flags.StringVar(&c.serviceAddr, "service-addr", "",
|
|
|
|
"Address of the local service to proxy. Only useful if -listen "+
|
|
|
|
"and -service are both set.")
|
|
|
|
|
|
|
|
c.flags.StringVar(&c.listen, "listen", "",
|
|
|
|
"Address to listen for inbound connections to the proxied service. "+
|
|
|
|
"Must be specified with -service and -service-addr.")
|
2018-05-19 07:43:38 +00:00
|
|
|
|
2018-05-19 22:47:25 +00:00
|
|
|
c.flags.BoolVar(&c.register, "register", false,
|
|
|
|
"Self-register with the local Consul agent. Only useful with "+
|
|
|
|
"-listen.")
|
|
|
|
|
2018-05-20 17:04:29 +00:00
|
|
|
c.flags.StringVar(&c.registerId, "register-id", "",
|
|
|
|
"ID suffix for the service. Use this to disambiguate with other proxies.")
|
|
|
|
|
2018-03-21 22:35:00 +00:00
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
|
|
|
c.help = flags.Usage(help, c.flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2018-05-19 18:04:47 +00:00
|
|
|
if len(c.flags.Args()) > 0 {
|
|
|
|
c.UI.Error(fmt.Sprintf("Should have no non-flag arguments."))
|
|
|
|
return 1
|
|
|
|
}
|
2018-03-21 22:35:00 +00:00
|
|
|
|
2018-05-06 15:54:36 +00:00
|
|
|
// Load the proxy ID and token from env vars if they're set
|
|
|
|
if c.proxyID == "" {
|
2019-08-09 19:19:30 +00:00
|
|
|
c.proxyID = os.Getenv("CONNECT_PROXY_ID")
|
2018-05-06 15:54:36 +00:00
|
|
|
}
|
2018-09-27 14:00:51 +00:00
|
|
|
if c.sidecarFor == "" {
|
2019-08-09 19:19:30 +00:00
|
|
|
c.sidecarFor = os.Getenv("CONNECT_SIDECAR_FOR")
|
2018-05-07 03:27:31 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 22:35:00 +00:00
|
|
|
// Setup the log outputs
|
2020-08-19 16:09:35 +00:00
|
|
|
logConfig := logging.Config{
|
2018-03-21 22:35:00 +00:00
|
|
|
LogLevel: c.logLevel,
|
2020-01-28 23:50:41 +00:00
|
|
|
Name: logging.Proxy,
|
|
|
|
LogJSON: c.logJSON,
|
2018-03-21 22:35:00 +00:00
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
|
|
|
|
logGate := logging.GatedWriter{Writer: &cli.UiWriter{Ui: c.UI}}
|
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
logger, err := logging.Setup(logConfig, &logGate)
|
2020-06-10 20:47:35 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
2018-03-21 22:35:00 +00:00
|
|
|
return 1
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger = logger
|
2018-03-21 22:35:00 +00:00
|
|
|
|
|
|
|
// Enable Pprof if needed
|
|
|
|
if c.pprofAddr != "" {
|
|
|
|
go func() {
|
|
|
|
c.UI.Output(fmt.Sprintf("Starting pprof HTTP endpoints on "+
|
|
|
|
"http://%s/debug/pprof", c.pprofAddr))
|
|
|
|
log.Fatal(http.ListenAndServe(c.pprofAddr, nil))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Consul client
|
|
|
|
client, err := c.http.APIClient()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-05-19 22:47:25 +00:00
|
|
|
// Output this first since the config watcher below will output
|
|
|
|
// other information.
|
|
|
|
c.UI.Output("Consul Connect proxy starting...")
|
|
|
|
|
2018-05-19 07:11:51 +00:00
|
|
|
// Get the proper configuration watcher
|
|
|
|
cfgWatcher, err := c.configWatcher(client)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error preparing configuration: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2018-03-21 22:35:00 +00:00
|
|
|
|
2018-05-19 07:11:51 +00:00
|
|
|
p, err := proxyImpl.New(client, cfgWatcher, c.logger)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed initializing proxy: %s", err))
|
|
|
|
return 1
|
2018-03-21 22:35:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// Hook the shutdownCh up to close the proxy
|
2018-03-21 22:35:00 +00:00
|
|
|
go func() {
|
2018-04-03 18:10:59 +00:00
|
|
|
<-c.shutdownCh
|
|
|
|
p.Close()
|
2018-03-21 22:35:00 +00:00
|
|
|
}()
|
|
|
|
|
2018-05-20 17:04:29 +00:00
|
|
|
// Register the service if we requested it
|
|
|
|
if c.register {
|
|
|
|
monitor, err := c.registerMonitor(client)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed initializing registration: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
go monitor.Run()
|
|
|
|
defer monitor.Close()
|
|
|
|
}
|
|
|
|
|
2018-05-19 22:47:25 +00:00
|
|
|
c.UI.Info("")
|
2018-03-21 22:35:00 +00:00
|
|
|
c.UI.Output("Log data will now stream in as it occurs:\n")
|
|
|
|
logGate.Flush()
|
|
|
|
|
2018-05-19 18:04:47 +00:00
|
|
|
// Run the proxy unless our tests require we don't
|
|
|
|
if !c.testNoStart {
|
|
|
|
if err := p.Serve(); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed running proxy: %s", err))
|
|
|
|
}
|
2018-03-21 22:35:00 +00:00
|
|
|
}
|
2018-04-03 18:10:59 +00:00
|
|
|
|
2018-03-21 22:35:00 +00:00
|
|
|
c.UI.Output("Consul Connect proxy shutdown")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
func (c *cmd) lookupProxyIDForSidecar(client *api.Client) (string, error) {
|
2018-10-03 19:37:53 +00:00
|
|
|
return LookupProxyIDForSidecar(client, c.sidecarFor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupProxyIDForSidecar finds candidate local proxy registrations that are a
|
2018-10-09 16:57:26 +00:00
|
|
|
// sidecar for the given service. It will return an ID if and only if there is
|
|
|
|
// exactly one registered connect proxy with `Proxy.DestinationServiceID` set to
|
2018-10-03 19:37:53 +00:00
|
|
|
// the specified service ID.
|
|
|
|
//
|
|
|
|
// This is exported to share it with the connect envoy command.
|
|
|
|
func LookupProxyIDForSidecar(client *api.Client, sidecarFor string) (string, error) {
|
2018-09-27 14:00:51 +00:00
|
|
|
svcs, err := client.Agent().Services()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed looking up sidecar proxy info for %s: %s",
|
2018-10-03 19:37:53 +00:00
|
|
|
sidecarFor, err)
|
2018-09-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var proxyIDs []string
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if svc.Kind == api.ServiceKindConnectProxy && svc.Proxy != nil &&
|
2018-10-03 19:37:53 +00:00
|
|
|
strings.ToLower(svc.Proxy.DestinationServiceID) == sidecarFor {
|
2018-09-27 14:00:51 +00:00
|
|
|
proxyIDs = append(proxyIDs, svc.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(proxyIDs) == 0 {
|
2018-10-09 16:57:26 +00:00
|
|
|
return "", fmt.Errorf("No sidecar proxy registered for %s", sidecarFor)
|
2018-09-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
if len(proxyIDs) > 1 {
|
2018-10-09 16:57:26 +00:00
|
|
|
return "", fmt.Errorf("More than one sidecar proxy registered for %s.\n"+
|
2018-09-27 14:00:51 +00:00
|
|
|
" Start proxy with -proxy-id and one of the following IDs: %s",
|
2018-10-03 19:37:53 +00:00
|
|
|
sidecarFor, strings.Join(proxyIDs, ", "))
|
2018-09-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
return proxyIDs[0], nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 21:02:52 +00:00
|
|
|
// LookupGatewayProxy finds the gateway service registered with the local
|
|
|
|
// agent. If exactly one gateway exists it will be returned, otherwise an error
|
|
|
|
// is returned.
|
2020-03-26 16:20:56 +00:00
|
|
|
func LookupGatewayProxy(client *api.Client, kind api.ServiceKind) (*api.AgentService, error) {
|
|
|
|
svcs, err := client.Agent().ServicesWithFilter(fmt.Sprintf("Kind == `%s`", kind))
|
2019-06-18 00:52:01 +00:00
|
|
|
if err != nil {
|
2020-03-26 16:20:56 +00:00
|
|
|
return nil, fmt.Errorf("Failed looking up %s instances: %v", kind, err)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch len(svcs) {
|
|
|
|
case 0:
|
2020-03-26 16:20:56 +00:00
|
|
|
return nil, fmt.Errorf("No %s services registered with this agent", kind)
|
2019-06-18 00:52:01 +00:00
|
|
|
case 1:
|
|
|
|
for _, svc := range svcs {
|
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("This should be unreachable")
|
|
|
|
default:
|
2020-03-26 16:20:56 +00:00
|
|
|
return nil, fmt.Errorf("Cannot lookup the %s's proxy ID because multiple are registered with the agent", kind)
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-19 07:11:51 +00:00
|
|
|
func (c *cmd) configWatcher(client *api.Client) (proxyImpl.ConfigWatcher, error) {
|
|
|
|
// Use the configured proxy ID
|
2018-05-19 07:43:38 +00:00
|
|
|
if c.proxyID != "" {
|
2018-05-19 22:47:25 +00:00
|
|
|
c.UI.Info("Configuration mode: Agent API")
|
|
|
|
c.UI.Info(fmt.Sprintf(" Proxy ID: %s", c.proxyID))
|
2018-05-19 07:43:38 +00:00
|
|
|
return proxyImpl.NewAgentConfigWatcher(client, c.proxyID, c.logger)
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
if c.sidecarFor != "" {
|
|
|
|
// Running as a sidecar, we need to find the proxy-id for the requested
|
|
|
|
// service
|
|
|
|
var err error
|
|
|
|
c.proxyID, err = c.lookupProxyIDForSidecar(client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UI.Info("Configuration mode: Agent API")
|
|
|
|
c.UI.Info(fmt.Sprintf(" Sidecar for ID: %s", c.sidecarFor))
|
|
|
|
c.UI.Info(fmt.Sprintf(" Proxy ID: %s", c.proxyID))
|
|
|
|
return proxyImpl.NewAgentConfigWatcher(client, c.proxyID, c.logger)
|
|
|
|
}
|
|
|
|
|
2018-05-19 07:43:38 +00:00
|
|
|
// Otherwise, we're representing a manually specified service.
|
|
|
|
if c.service == "" {
|
2018-05-19 07:11:51 +00:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"-service or -proxy-id must be specified so that proxy can " +
|
|
|
|
"configure itself.")
|
|
|
|
}
|
|
|
|
|
2018-05-19 22:47:25 +00:00
|
|
|
c.UI.Info("Configuration mode: Flags")
|
|
|
|
c.UI.Info(fmt.Sprintf(" Service: %s", c.service))
|
|
|
|
|
2018-05-19 18:04:47 +00:00
|
|
|
// Convert our upstreams to a slice of configurations. We do this
|
|
|
|
// deterministically by alphabetizing the upstream keys. We do this so
|
|
|
|
// that tests can compare the upstream values.
|
|
|
|
upstreamKeys := make([]string, 0, len(c.upstreams))
|
|
|
|
for k := range c.upstreams {
|
|
|
|
upstreamKeys = append(upstreamKeys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(upstreamKeys)
|
2018-05-19 07:43:38 +00:00
|
|
|
upstreams := make([]proxyImpl.UpstreamConfig, 0, len(c.upstreams))
|
2018-05-19 18:04:47 +00:00
|
|
|
for _, k := range upstreamKeys {
|
2018-05-19 22:47:25 +00:00
|
|
|
config := c.upstreams[k]
|
|
|
|
|
2021-05-04 19:54:34 +00:00
|
|
|
addr := config.LocalBindSocketPath
|
|
|
|
if addr == "" {
|
|
|
|
addr = fmt.Sprintf(
|
|
|
|
"%s:%d",
|
|
|
|
config.LocalBindAddress, config.LocalBindPort)
|
|
|
|
}
|
|
|
|
|
2018-05-19 22:47:25 +00:00
|
|
|
c.UI.Info(fmt.Sprintf(
|
2021-05-04 19:54:34 +00:00
|
|
|
" Upstream: %s => %s",
|
|
|
|
k, addr))
|
2018-05-19 22:47:25 +00:00
|
|
|
upstreams = append(upstreams, config)
|
2018-05-19 07:43:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-19 18:20:20 +00:00
|
|
|
// Parse out our listener if we have one
|
|
|
|
var listener proxyImpl.PublicListenerConfig
|
|
|
|
if c.listen != "" {
|
2018-05-20 17:04:29 +00:00
|
|
|
host, port, err := c.listenParts()
|
2018-05-19 18:20:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-19 18:22:00 +00:00
|
|
|
if c.serviceAddr == "" {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"-service-addr must be specified with -listen so the proxy " +
|
|
|
|
"knows the backend service address.")
|
|
|
|
}
|
|
|
|
|
2018-05-20 17:04:29 +00:00
|
|
|
c.UI.Info(fmt.Sprintf(" Public listener: %s:%d => %s", host, port, c.serviceAddr))
|
2018-05-19 18:20:20 +00:00
|
|
|
listener.BindAddress = host
|
2018-05-20 17:04:29 +00:00
|
|
|
listener.BindPort = port
|
2018-05-19 18:20:20 +00:00
|
|
|
listener.LocalServiceAddress = c.serviceAddr
|
2018-05-19 22:47:25 +00:00
|
|
|
} else {
|
|
|
|
c.UI.Info(fmt.Sprintf(" Public listener: Disabled"))
|
2018-05-19 18:20:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-19 07:43:38 +00:00
|
|
|
return proxyImpl.NewStaticConfigWatcher(&proxyImpl.Config{
|
|
|
|
ProxiedServiceName: c.service,
|
2018-05-19 18:20:20 +00:00
|
|
|
PublicListener: listener,
|
2018-05-19 07:43:38 +00:00
|
|
|
Upstreams: upstreams,
|
|
|
|
}), nil
|
2018-05-19 07:11:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 17:04:29 +00:00
|
|
|
// registerMonitor returns the registration monitor ready to be started.
|
|
|
|
func (c *cmd) registerMonitor(client *api.Client) (*RegisterMonitor, error) {
|
|
|
|
if c.service == "" || c.listen == "" {
|
|
|
|
return nil, fmt.Errorf("-register may only be specified with -service and -listen")
|
|
|
|
}
|
|
|
|
|
|
|
|
host, port, err := c.listenParts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
m := NewRegisterMonitor(c.logger)
|
2018-05-20 17:04:29 +00:00
|
|
|
m.Logger = c.logger
|
|
|
|
m.Client = client
|
|
|
|
m.Service = c.service
|
|
|
|
m.IDSuffix = c.registerId
|
|
|
|
m.LocalAddress = host
|
|
|
|
m.LocalPort = port
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// listenParts returns the host and port parts of the -listen flag. The
|
|
|
|
// -listen flag must be non-empty prior to calling this.
|
|
|
|
func (c *cmd) listenParts() (string, int, error) {
|
|
|
|
host, portRaw, err := net.SplitHostPort(c.listen)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
port, err := strconv.ParseInt(portRaw, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return host, int(port), nil
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:35:00 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
|
|
|
}
|
|
|
|
|
|
|
|
const synopsis = "Runs a Consul Connect proxy"
|
|
|
|
const help = `
|
2018-05-19 18:34:00 +00:00
|
|
|
Usage: consul connect proxy [options]
|
2018-03-21 22:35:00 +00:00
|
|
|
|
|
|
|
Starts a Consul Connect proxy and runs until an interrupt is received.
|
2018-05-19 18:34:00 +00:00
|
|
|
The proxy can be used to accept inbound connections for a service,
|
|
|
|
wrap outbound connections to upstream services, or both. This enables
|
|
|
|
a non-Connect-aware application to use Connect.
|
|
|
|
|
|
|
|
The proxy requires service:write permissions for the service it represents.
|
2019-12-04 20:01:03 +00:00
|
|
|
The token may be passed via the CLI or the CONSUL_HTTP_TOKEN environment
|
2018-05-22 17:33:14 +00:00
|
|
|
variable.
|
2018-05-19 18:34:00 +00:00
|
|
|
|
|
|
|
Consul can automatically start and manage this proxy by specifying the
|
|
|
|
"proxy" configuration within your service definition.
|
|
|
|
|
|
|
|
The example below shows how to start a local proxy for establishing outbound
|
|
|
|
connections to "db" representing the frontend service. Once running, any
|
|
|
|
process that creates a TCP connection to the specified port (8181) will
|
|
|
|
establish a mutual TLS connection to "db" identified as "frontend".
|
|
|
|
|
|
|
|
$ consul connect proxy -service frontend -upstream db:8181
|
|
|
|
|
|
|
|
The next example starts a local proxy that also accepts inbound connections
|
|
|
|
on port 8443, authorizes the connection, then proxies it to port 8080:
|
|
|
|
|
|
|
|
$ consul connect proxy \
|
|
|
|
-service frontend \
|
|
|
|
-service-addr 127.0.0.1:8080 \
|
|
|
|
-listen ':8443'
|
|
|
|
|
|
|
|
A proxy can accept both inbound connections as well as proxy to upstream
|
|
|
|
services by specifying both the "-listen" and "-upstream" flags.
|
|
|
|
|
2018-03-21 22:35:00 +00:00
|
|
|
`
|