Skip to content

Commit

Permalink
[no ci] first pass at plumbing grpc_ca_file
Browse files Browse the repository at this point in the history
  • Loading branch information
shoenig committed Jan 9, 2023
1 parent 9db2d8a commit d472184
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 47 deletions.
4 changes: 3 additions & 1 deletion client/allocrunner/consul_grpc_sock_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func newConsulGRPCSocketHook(
if !ok {
consulGRPCPort = consulGRPCFallbackPort
}
logger.Info("CONSUL GRPC DEBUG", "consulGRPCPort", consulGRPCPort)

return &consulGRPCSocketHook{
alloc: alloc,
Expand Down Expand Up @@ -195,7 +196,6 @@ func (p *grpcSocketProxy) run(alloc *structs.Allocation) error {
return fmt.Errorf("error parsing Consul address %q: %v",
p.config.Addr, err)
}

destAddr = net.JoinHostPort(host, p.consulGRPCFallbackPort)
}

Expand Down Expand Up @@ -302,6 +302,8 @@ func proxyConn(ctx context.Context, logger hclog.Logger, destAddr string, conn n
// Close the connection when we're done with it.
defer conn.Close()

logger.Info("CONSUL GRPC DEBUG", "destAddr", destAddr)

ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand Down
72 changes: 33 additions & 39 deletions client/allocrunner/taskrunner/envoy_bootstrap_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,29 @@ const (
)

type consulTransportConfig struct {
HTTPAddr string // required
Auth string // optional, env CONSUL_HTTP_AUTH
SSL string // optional, env CONSUL_HTTP_SSL
VerifySSL string // optional, env CONSUL_HTTP_SSL_VERIFY
CAFile string // optional, arg -ca-file
CertFile string // optional, arg -client-cert
KeyFile string // optional, arg -client-key
Namespace string // optional, only consul Enterprise, env CONSUL_NAMESPACE
HTTPAddr string // required
Auth string // optional, env CONSUL_HTTP_AUTH
SSL string // optional, env CONSUL_HTTP_SSL
VerifySSL string // optional, env CONSUL_HTTP_SSL_VERIFY
GRPCCAFile string // optional, arg -grpc-ca-file
CAFile string // optional, arg -ca-file
CertFile string // optional, arg -client-cert
KeyFile string // optional, arg -client-key
Namespace string // optional, only consul Enterprise, env CONSUL_NAMESPACE
// CAPath (dir) not supported by Nomad's config object
}

func newConsulTransportConfig(consul *config.ConsulConfig) consulTransportConfig {
func newConsulTransportConfig(cc *config.ConsulConfig) consulTransportConfig {
return consulTransportConfig{
HTTPAddr: consul.Addr,
Auth: consul.Auth,
SSL: decodeTriState(consul.EnableSSL),
VerifySSL: decodeTriState(consul.VerifySSL),
CAFile: consul.CAFile,
CertFile: consul.CertFile,
KeyFile: consul.KeyFile,
Namespace: consul.Namespace,
HTTPAddr: cc.Addr,
Auth: cc.Auth,
SSL: decodeTriState(cc.EnableSSL),
VerifySSL: decodeTriState(cc.VerifySSL),
GRPCCAFile: cc.GRPCCAFile,
CAFile: cc.CAFile,
CertFile: cc.CertFile,
KeyFile: cc.KeyFile,
Namespace: cc.Namespace,
}
}

Expand Down Expand Up @@ -125,7 +127,7 @@ type envoyBootstrapHook struct {
// envoyBootstrapWaitTime is the total amount of time hook will wait for Consul
envoyBootstrapWaitTime time.Duration

// envoyBootstrapInitialGap is the initial wait gap when retyring
// envoyBootstrapInitialGap is the initial wait gap when retrying
envoyBoostrapInitialGap time.Duration

// envoyBootstrapMaxJitter is the maximum amount of jitter applied to retries
Expand Down Expand Up @@ -280,6 +282,8 @@ func (h *envoyBootstrapHook) Prestart(ctx context.Context, req *ifs.TaskPrestart
// Create command line arguments
bootstrapArgs := bootstrap.args()

h.logger.Info("CONSUL GRPC DEBUG", "bootstrap args", bootstrapArgs)

// Write args to file for debugging
argsFile, err := os.Create(bootstrapCmdPath)
if err != nil {
Expand Down Expand Up @@ -542,29 +546,19 @@ func (e envoyBootstrapArgs) args() []string {
"-bootstrap",
}

if v := e.gateway; v != "" {
arguments = append(arguments, "-gateway", v)
}

if v := e.siToken; v != "" {
arguments = append(arguments, "-token", v)
}

if v := e.consulConfig.CAFile; v != "" {
arguments = append(arguments, "-ca-file", v)
}

if v := e.consulConfig.CertFile; v != "" {
arguments = append(arguments, "-client-cert", v)
}

if v := e.consulConfig.KeyFile; v != "" {
arguments = append(arguments, "-client-key", v)
appendIfSet := func(param, value string) {
if value != "" {
arguments = append(arguments, param, value)
}
}

if v := e.namespace; v != "" {
arguments = append(arguments, "-namespace", v)
}
appendIfSet("-gateway", e.gateway)
appendIfSet("-token", e.siToken)
appendIfSet("-grpc-ca-file", e.consulConfig.GRPCCAFile)
appendIfSet("-ca-file", e.consulConfig.CAFile)
appendIfSet("-client-cert", e.consulConfig.CertFile)
appendIfSet("-client-key", e.consulConfig.KeyFile)
appendIfSet("-namespace", e.namespace)

return arguments
}
Expand Down
16 changes: 9 additions & 7 deletions client/allocrunner/taskrunner/envoy_bootstrap_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ var (
}

consulTLSConfig = consulTransportConfig{
HTTPAddr: "2.2.2.2", // arg
Auth: "user:password", // env
SSL: "true", // env
VerifySSL: "true", // env
CAFile: "/etc/tls/ca-file", // arg
CertFile: "/etc/tls/cert-file", // arg
KeyFile: "/etc/tls/key-file", // arg
HTTPAddr: "2.2.2.2", // arg
Auth: "user:password", // env
SSL: "true", // env
VerifySSL: "true", // env
GRPCCAFile: "/etc/tls/grpc-ca-file", // arg
CAFile: "/etc/tls/ca-file", // arg
CertFile: "/etc/tls/cert-file", // arg
KeyFile: "/etc/tls/key-file", // arg
}
)

Expand Down Expand Up @@ -175,6 +176,7 @@ func TestEnvoyBootstrapHook_envoyBootstrapArgs(t *testing.T) {
"-address", "127.0.0.1:19100",
"-proxy-id", "s1-sidecar-proxy",
"-bootstrap",
"-grpc-ca-file", "/etc/tls/grpc-ca-file",
"-ca-file", "/etc/tls/ca-file",
"-client-cert", "/etc/tls/cert-file",
"-client-key", "/etc/tls/key-file",
Expand Down
8 changes: 8 additions & 0 deletions nomad/structs/config/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ type ConsulConfig struct {
// Uses Consul's default and env var.
VerifySSL *bool `hcl:"verify_ssl"`

// GRPCCAFile is the path to the ca certificate used for Consul gRPC communication.
//
// Uses Consul's default and env var.
GRPCCAFile string `hcl:"grpc_ca_file"`

// CAFile is the path to the ca certificate used for Consul communication.
//
// Uses Consul's default and env var.
Expand Down Expand Up @@ -219,6 +224,9 @@ func (c *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
if b.ShareSSL != nil {
result.ShareSSL = pointer.Of(*b.ShareSSL)
}
if b.GRPCCAFile != "" {
result.GRPCCAFile = b.GRPCCAFile
}
if b.CAFile != "" {
result.CAFile = b.CAFile
}
Expand Down
3 changes: 3 additions & 0 deletions nomad/structs/config/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestConsulConfig_Merge(t *testing.T) {
Auth: "1",
EnableSSL: &no,
VerifySSL: &no,
GRPCCAFile: "1",
CAFile: "1",
CertFile: "1",
KeyFile: "1",
Expand Down Expand Up @@ -81,6 +82,7 @@ func TestConsulConfig_Merge(t *testing.T) {
Auth: "2",
EnableSSL: &yes,
VerifySSL: &yes,
GRPCCAFile: "2",
CAFile: "2",
CertFile: "2",
KeyFile: "2",
Expand Down Expand Up @@ -108,6 +110,7 @@ func TestConsulConfig_Merge(t *testing.T) {
Auth: "2",
EnableSSL: &yes,
VerifySSL: &yes,
GRPCCAFile: "2",
CAFile: "2",
CertFile: "2",
KeyFile: "2",
Expand Down
4 changes: 4 additions & 0 deletions website/content/docs/configuration/consul.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ configuring Nomad to talk to Consul via DNS such as consul.service.consul
respective services, each tagged appropriately with either `http` or `rpc`
tag. Nomad servers also advertise a `serf` tagged service.

- `grpc_ca_file` `(string: "")` - Specifies an optional path to the GRPC CA
certificate used for communication between Connect sidecar proxies and Consul
agents. Will default to the `CONSUL_GRPC_CACERT` environment variable if set.

- `ca_file` `(string: "")` - Specifies an optional path to the CA certificate
used for Consul communication. This defaults to the system bundle if
unspecified. Will default to the `CONSUL_CACERT` environment variable if set.
Expand Down

0 comments on commit d472184

Please sign in to comment.