github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/agent/structs/connect.go (about)

     1  package structs
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/mapstructure"
     7  )
     8  
     9  // ConnectAuthorizeRequest is the structure of a request to authorize
    10  // a connection.
    11  type ConnectAuthorizeRequest struct {
    12  	// Target is the name of the service that is being requested.
    13  	Target string
    14  
    15  	// ClientCertURI is a unique identifier for the requesting client. This
    16  	// is currently the URI SAN from the TLS client certificate.
    17  	//
    18  	// ClientCertSerial is a colon-hex-encoded of the serial number for
    19  	// the requesting client cert. This is used to check against revocation
    20  	// lists.
    21  	ClientCertURI    string
    22  	ClientCertSerial string
    23  }
    24  
    25  // ProxyExecMode encodes the mode for running a managed connect proxy.
    26  type ProxyExecMode int
    27  
    28  const (
    29  	// ProxyExecModeUnspecified uses the global default proxy mode.
    30  	ProxyExecModeUnspecified ProxyExecMode = iota
    31  
    32  	// ProxyExecModeDaemon executes a proxy process as a supervised daemon.
    33  	ProxyExecModeDaemon
    34  
    35  	// ProxyExecModeScript executes a proxy config script on each change to it's
    36  	// config.
    37  	ProxyExecModeScript
    38  
    39  	// ProxyExecModeTest tracks the start/stop of the proxy in-memory
    40  	// and is only used for tests. This shouldn't be set outside of tests,
    41  	// but even if it is it has no external effect.
    42  	ProxyExecModeTest
    43  )
    44  
    45  // NewProxyExecMode returns the proper ProxyExecMode for the given string value.
    46  func NewProxyExecMode(raw string) (ProxyExecMode, error) {
    47  	switch raw {
    48  	case "":
    49  		return ProxyExecModeUnspecified, nil
    50  	case "daemon":
    51  		return ProxyExecModeDaemon, nil
    52  	case "script":
    53  		return ProxyExecModeScript, nil
    54  	default:
    55  		return 0, fmt.Errorf("invalid exec mode: %s", raw)
    56  	}
    57  }
    58  
    59  // String implements Stringer
    60  func (m ProxyExecMode) String() string {
    61  	switch m {
    62  	case ProxyExecModeUnspecified:
    63  		return "global_default"
    64  	case ProxyExecModeDaemon:
    65  		return "daemon"
    66  	case ProxyExecModeScript:
    67  		return "script"
    68  	case ProxyExecModeTest:
    69  		return "test"
    70  	default:
    71  		return "unknown"
    72  	}
    73  }
    74  
    75  // ConnectManagedProxy represents the agent-local state for a configured proxy
    76  // instance. This is never stored or sent to the servers and is only used to
    77  // store the config for the proxy that the agent needs to track. For now it's
    78  // really generic with only the fields the agent needs to act on defined while
    79  // the rest of the proxy config is passed as opaque bag of attributes to support
    80  // arbitrary config params for third-party proxy integrations. "External"
    81  // proxies by definition register themselves and manage their own config
    82  // externally so are never represented in agent state.
    83  type ConnectManagedProxy struct {
    84  	// ExecMode is one of daemon or script.
    85  	ExecMode ProxyExecMode
    86  
    87  	// Command is the command to execute. Empty defaults to self-invoking the same
    88  	// consul binary with proxy subcomand for ProxyExecModeDaemon and is an error
    89  	// for ProxyExecModeScript.
    90  	Command []string
    91  
    92  	// Config is the arbitrary configuration data provided with the registration.
    93  	Config map[string]interface{}
    94  
    95  	// Upstreams are the dependencies the proxy should setup outgoing listeners for.
    96  	Upstreams Upstreams
    97  
    98  	// ProxyService is a pointer to the local proxy's service record for
    99  	// convenience. The proxies ID and name etc. can be read from there. It may be
   100  	// nil if the agent is starting up and hasn't registered the service yet. We
   101  	// ignore it when calculating the hash value since the only thing that effects
   102  	// the proxy's config is the ID of the target service which is already
   103  	// represented below.
   104  	ProxyService *NodeService `hash:"ignore"`
   105  
   106  	// TargetServiceID is the ID of the target service on the localhost. It may
   107  	// not exist yet since bootstrapping is allowed to happen in either order.
   108  	TargetServiceID string
   109  }
   110  
   111  // ConnectManagedProxyConfig represents the parts of the proxy config the agent
   112  // needs to understand. It's bad UX to make the user specify these separately
   113  // just to make parsing simpler for us so this encapsulates the fields in
   114  // ConnectManagedProxy.Config that we care about. They are all optional anyway
   115  // and this is used to decode them with mapstructure.
   116  type ConnectManagedProxyConfig struct {
   117  	BindAddress         string `mapstructure:"bind_address"`
   118  	BindPort            int    `mapstructure:"bind_port"`
   119  	LocalServiceAddress string `mapstructure:"local_service_address"`
   120  	LocalServicePort    int    `mapstructure:"local_service_port"`
   121  }
   122  
   123  // ParseConfig attempts to read the fields we care about from the otherwise
   124  // opaque config map. They are all optional but it may fail if one is specified
   125  // but an invalid value.
   126  func (p *ConnectManagedProxy) ParseConfig() (*ConnectManagedProxyConfig, error) {
   127  	var cfg ConnectManagedProxyConfig
   128  	d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
   129  		ErrorUnused:      false,
   130  		WeaklyTypedInput: true, // allow string port etc.
   131  		Result:           &cfg,
   132  	})
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  	err = d.Decode(p.Config)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  	return &cfg, nil
   141  }