|
| 1 | +package taskrunner |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + hclog "github.com/hashicorp/go-hclog" |
| 11 | + ifs "github.com/hashicorp/nomad/client/allocrunner/interfaces" |
| 12 | + "github.com/hashicorp/nomad/nomad/structs" |
| 13 | + "github.com/hashicorp/nomad/nomad/structs/config" |
| 14 | + "github.com/pkg/errors" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + connectNativeHookName = "connect_native" |
| 19 | +) |
| 20 | + |
| 21 | +type connectNativeHookConfig struct { |
| 22 | + consulShareTLS bool |
| 23 | + consul consulTransportConfig |
| 24 | + alloc *structs.Allocation |
| 25 | + logger hclog.Logger |
| 26 | +} |
| 27 | + |
| 28 | +func newConnectNativeHookConfig(alloc *structs.Allocation, consul *config.ConsulConfig, logger hclog.Logger) *connectNativeHookConfig { |
| 29 | + return &connectNativeHookConfig{ |
| 30 | + alloc: alloc, |
| 31 | + logger: logger, |
| 32 | + consulShareTLS: consul.ShareSSL == nil || *consul.ShareSSL, // default enabled |
| 33 | + consul: newConsulTransportConfig(consul), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// connectNativeHook manages additional automagic configuration for a connect |
| 38 | +// native task. |
| 39 | +// |
| 40 | +// If nomad client is configured to talk to Consul using TLS (or other special |
| 41 | +// auth), the native task will inherit that configuration EXCEPT for the consul |
| 42 | +// token. |
| 43 | +// |
| 44 | +// If consul is configured with ACLs enabled, a Service Identity token will be |
| 45 | +// generated on behalf of the native service and supplied to the task. |
| 46 | +type connectNativeHook struct { |
| 47 | + // alloc is the allocation with the connect native task being run |
| 48 | + alloc *structs.Allocation |
| 49 | + |
| 50 | + // consulShareTLS is used to toggle whether the TLS configuration of the |
| 51 | + // Nomad Client may be shared with Connect Native applications. |
| 52 | + consulShareTLS bool |
| 53 | + |
| 54 | + // consulConfig is used to enable the connect native enabled task to |
| 55 | + // communicate with consul directly, as is necessary for the task to request |
| 56 | + // its connect mTLS certificates. |
| 57 | + consulConfig consulTransportConfig |
| 58 | + |
| 59 | + // logger is used to log things |
| 60 | + logger hclog.Logger |
| 61 | +} |
| 62 | + |
| 63 | +func newConnectNativeHook(c *connectNativeHookConfig) *connectNativeHook { |
| 64 | + return &connectNativeHook{ |
| 65 | + alloc: c.alloc, |
| 66 | + consulShareTLS: c.consulShareTLS, |
| 67 | + consulConfig: c.consul, |
| 68 | + logger: c.logger.Named(connectNativeHookName), |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func (connectNativeHook) Name() string { |
| 73 | + return connectNativeHookName |
| 74 | +} |
| 75 | + |
| 76 | +func (h *connectNativeHook) Prestart( |
| 77 | + ctx context.Context, |
| 78 | + request *ifs.TaskPrestartRequest, |
| 79 | + response *ifs.TaskPrestartResponse) error { |
| 80 | + |
| 81 | + if !request.Task.Kind.IsConnectNative() { |
| 82 | + response.Done = true |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + h.logger.Debug("share consul TLS configuration for connect native", "enabled", h.consulShareTLS, "task", request.Task.Name) |
| 87 | + if h.consulShareTLS { |
| 88 | + // copy TLS certificates |
| 89 | + if err := h.copyCertificates(h.consulConfig, request.TaskDir.SecretsDir); err != nil { |
| 90 | + h.logger.Error("failed to copy Consul TLS certificates", "error", err) |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + // set environment variables for communicating with Consul agent, but |
| 95 | + // only if those environment variables are not already set |
| 96 | + response.Env = h.tlsEnv(request.TaskEnv.EnvMap) |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + if err := h.maybeSetSITokenEnv(request.TaskDir.SecretsDir, request.Task.Name, response.Env); err != nil { |
| 101 | + h.logger.Error("failed to load Consul Service Identity Token", "error", err, "task", request.Task.Name) |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + // tls/acl setup for native task done |
| 106 | + response.Done = true |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +const ( |
| 111 | + secretCAFilename = "consul_ca_file" |
| 112 | + secretCertfileFilename = "consul_cert_file" |
| 113 | + secretKeyfileFilename = "consul_key_file" |
| 114 | +) |
| 115 | + |
| 116 | +func (h *connectNativeHook) copyCertificates(consulConfig consulTransportConfig, dir string) error { |
| 117 | + if err := h.copyCertificate(consulConfig.CAFile, dir, secretCAFilename); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + if err := h.copyCertificate(consulConfig.CertFile, dir, secretCertfileFilename); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + if err := h.copyCertificate(consulConfig.KeyFile, dir, secretKeyfileFilename); err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func (connectNativeHook) copyCertificate(source, dir, name string) error { |
| 130 | + if source == "" { |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + original, err := os.Open(source) |
| 135 | + if err != nil { |
| 136 | + return errors.Wrap(err, "failed to open consul TLS certificate") |
| 137 | + } |
| 138 | + defer original.Close() |
| 139 | + |
| 140 | + destination := filepath.Join(dir, name) |
| 141 | + fd, err := os.Create(destination) |
| 142 | + if err != nil { |
| 143 | + return errors.Wrapf(err, "failed to create secrets/%s", name) |
| 144 | + } |
| 145 | + defer fd.Close() |
| 146 | + |
| 147 | + if _, err := io.Copy(fd, original); err != nil { |
| 148 | + return errors.Wrapf(err, "failed to copy certificate secrets/%s", name) |
| 149 | + } |
| 150 | + |
| 151 | + if err := fd.Sync(); err != nil { |
| 152 | + return errors.Wrapf(err, "failed to write secrets/%s", name) |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +// tlsEnv creates a set of additional of environment variables to be used when launching |
| 159 | +// the connect native task. This will enable the task to communicate with Consul |
| 160 | +// if Consul has transport security turned on. |
| 161 | +// |
| 162 | +// We do NOT set CONSUL_HTTP_TOKEN from the nomad agent's consul config, as that |
| 163 | +// is a separate security concern addressed by the service identity hook. |
| 164 | +func (h *connectNativeHook) tlsEnv(env map[string]string) map[string]string { |
| 165 | + m := make(map[string]string) |
| 166 | + |
| 167 | + if _, exists := env["CONSUL_CACERT"]; !exists && h.consulConfig.CAFile != "" { |
| 168 | + m["CONSUL_CACERT"] = filepath.Join("/secrets", secretCAFilename) |
| 169 | + } |
| 170 | + |
| 171 | + if _, exists := env["CONSUL_CLIENT_CERT"]; !exists && h.consulConfig.CertFile != "" { |
| 172 | + m["CONSUL_CLIENT_CERT"] = filepath.Join("/secrets", secretCertfileFilename) |
| 173 | + } |
| 174 | + |
| 175 | + if _, exists := env["CONSUL_CLIENT_KEY"]; !exists && h.consulConfig.KeyFile != "" { |
| 176 | + m["CONSUL_CLIENT_KEY"] = filepath.Join("/secrets", secretKeyfileFilename) |
| 177 | + } |
| 178 | + |
| 179 | + if _, exists := env["CONSUL_HTTP_SSL"]; !exists { |
| 180 | + if v := h.consulConfig.SSL; v != "" { |
| 181 | + m["CONSUL_HTTP_SSL"] = v |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if _, exists := env["CONSUL_HTTP_SSL_VERIFY"]; !exists { |
| 186 | + if v := h.consulConfig.VerifySSL; v != "" { |
| 187 | + m["CONSUL_HTTP_SSL_VERIFY"] = v |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return m |
| 192 | +} |
| 193 | + |
| 194 | +// maybeSetSITokenEnv will set the CONSUL_HTTP_TOKEN environment variable in |
| 195 | +// the given env map, if the token is found to exist in the task's secrets |
| 196 | +// directory. |
| 197 | +// |
| 198 | +// Following the pattern of the envoy_bootstrap_hook, the Consul Service Identity |
| 199 | +// ACL Token is generated prior to this hook, if Consul ACLs are enabled. This is |
| 200 | +// done in the sids_hook, which places the token at secrets/si_token in the task |
| 201 | +// workspace. The content of that file is the SI token specific to this task |
| 202 | +// instance. |
| 203 | +func (h *connectNativeHook) maybeSetSITokenEnv(dir, task string, env map[string]string) error { |
| 204 | + token, err := ioutil.ReadFile(filepath.Join(dir, sidsTokenFile)) |
| 205 | + if err != nil { |
| 206 | + if !os.IsNotExist(err) { |
| 207 | + return errors.Wrapf(err, "failed to load SI token for native task %s", task) |
| 208 | + } |
| 209 | + h.logger.Trace("no SI token to load for native task", "task", task) |
| 210 | + return nil // token file DNE; acls not enabled |
| 211 | + } |
| 212 | + h.logger.Trace("recovered pre-existing SI token for native task", "task", task) |
| 213 | + env["CONSUL_HTTP_TOKEN"] = string(token) |
| 214 | + return nil |
| 215 | +} |
0 commit comments