Skip to content

Commit bdd885c

Browse files
Add mTLS env vars for container mode (#4261)
* Add mTLS env vars for container mode
1 parent b425e4f commit bdd885c

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: enhancement
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Add mTLS env var settings for containers
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
description: |
20+
Add env var bindings so fleet-server/elastic-agents started in container mode
21+
can specify mTLS variables.
22+
23+
# Affected component; a word indicating the component this changeset affects.
24+
component:
25+
26+
# PR URL; optional; the PR number that added the changeset.
27+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
28+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
29+
# Please provide it if you are adding a fragment for a different PR.
30+
#pr: https://github.com/owner/repo/1234
31+
32+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
33+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
34+
#issue: https://github.com/owner/repo/1234

internal/pkg/agent/cmd/container.go

+20
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ The following actions are possible and grouped based on the actions.
7272
FLEET_ENROLLMENT_TOKEN - token to use for enrollment. This is not needed in case FLEET_SERVER_ENABLED and FLEET_ENROLL is set. Then the token is fetched from Kibana.
7373
FLEET_CA - path to certificate authority to use with communicate with Fleet Server [$KIBANA_CA]
7474
FLEET_INSECURE - communicate with Fleet with either insecure HTTP or unverified HTTPS
75+
ELASTIC_AGENT_CERT - path to certificate to use for connecting to fleet-server.
76+
ELASTIC_AGENT_CERT_KEY - path to private key use for connecting to fleet-server.
7577
7678
7779
The following vars are need in the scenario that Elastic Agent should automatically fetch its own token.
@@ -100,6 +102,9 @@ The following actions are possible and grouped based on the actions.
100102
FLEET_SERVER_CERT - path to certificate to use for HTTPS endpoint
101103
FLEET_SERVER_CERT_KEY - path to private key for certificate to use for HTTPS endpoint
102104
FLEET_SERVER_CERT_KEY_PASSPHRASE - path to private key passphrase file for certificate to use for HTTPS endpoint
105+
FLEET_SERVER_ES_CERT - path to certificate to use for connecting to Elasticsearch
106+
FLEET_SERVER_ES_CERT_KEY - path to private key for certificate to use for connecting to Elasticsearch
107+
FLEET_SERVER_CLIENT_AUTH - fleet-server mTLS client authentication for connecting elastic-agents. Must be one of [none, optional, required]. A default of none is used.
103108
FLEET_SERVER_INSECURE_HTTP - expose Fleet Server over HTTP (not recommended; insecure)
104109
FLEET_SERVER_INIT_TIMEOUT - Sets the initial timeout when starting up the fleet server under agent. Default: 30s.
105110
@@ -432,6 +437,12 @@ func buildEnrollArgs(cfg setupConfig, token string, policyID string) ([]string,
432437
if cfg.FleetServer.Elasticsearch.CATrustedFingerprint != "" {
433438
args = append(args, "--fleet-server-es-ca-trusted-fingerprint", cfg.FleetServer.Elasticsearch.CATrustedFingerprint)
434439
}
440+
if cfg.FleetServer.Elasticsearch.Cert != "" {
441+
args = append(args, "--fleet-server-es-cert", cfg.FleetServer.Elasticsearch.Cert)
442+
}
443+
if cfg.FleetServer.Elasticsearch.CertKey != "" {
444+
args = append(args, "--fleet-server-es-cert-key", cfg.FleetServer.Elasticsearch.CertKey)
445+
}
435446
if cfg.FleetServer.Host != "" {
436447
args = append(args, "--fleet-server-host", cfg.FleetServer.Host)
437448
}
@@ -447,6 +458,9 @@ func buildEnrollArgs(cfg setupConfig, token string, policyID string) ([]string,
447458
if cfg.FleetServer.PassphrasePath != "" {
448459
args = append(args, "--fleet-server-cert-key-passphrase", cfg.FleetServer.PassphrasePath)
449460
}
461+
if cfg.FleetServer.ClientAuth != "" {
462+
args = append(args, "--fleet-server-client-auth", cfg.FleetServer.ClientAuth)
463+
}
450464

451465
for k, v := range cfg.FleetServer.Headers {
452466
args = append(args, "--header", k+"="+v)
@@ -487,6 +501,12 @@ func buildEnrollArgs(cfg setupConfig, token string, policyID string) ([]string,
487501
args = append(args, "--daemon-timeout")
488502
args = append(args, cfg.Fleet.DaemonTimeout.String())
489503
}
504+
if cfg.Fleet.Cert != "" {
505+
args = append(args, "--elastic-agent-cert", cfg.Fleet.Cert)
506+
}
507+
if cfg.Fleet.CertKey != "" {
508+
args = append(args, "--elastic-agent-cert-key", cfg.Fleet.CertKey)
509+
}
490510
return args, nil
491511
}
492512

internal/pkg/agent/cmd/container_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ func TestBuildEnrollArgs(t *testing.T) {
152152
expect: []string{"--fleet-server-service-token-path", "/path/to/token"},
153153
err: nil,
154154
},
155+
"mTLS flags": {
156+
cfg: setupConfig{
157+
Fleet: fleetConfig{
158+
Cert: "/path/to/agent.crt",
159+
CertKey: "/path/to/agent.key",
160+
},
161+
FleetServer: fleetServerConfig{
162+
Enable: true,
163+
ClientAuth: "optional",
164+
Elasticsearch: elasticsearchConfig{
165+
Cert: "/path/to/es.crt",
166+
CertKey: "/path/to/es.key",
167+
},
168+
},
169+
},
170+
expect: []string{"--fleet-server-es-cert", "/path/to/es.crt", "--fleet-server-es-cert-key", "/path/to/es.key", "--fleet-server-client-auth", "optional", "--elastic-agent-cert", "/path/to/agent.crt", "--elastic-agent-cert-key", "/path/to/agent.key"},
171+
err: nil,
172+
},
155173
}
156174

157175
for name, tc := range cases {

internal/pkg/agent/cmd/setup_config.go

+10
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ type fleetConfig struct {
2424
TokenPolicyName string `config:"token_policy_name"`
2525
URL string `config:"url"`
2626
DaemonTimeout time.Duration `config:"daemon_timeout"`
27+
Cert string `config:"cert"`
28+
CertKey string `config:"cert_key"`
2729
}
2830

2931
type fleetServerConfig struct {
3032
Cert string `config:"cert"`
3133
CertKey string `config:"cert_key"`
3234
PassphrasePath string `config:"key_passphrase_path"`
35+
ClientAuth string `config:"client_authentication"`
3336
Elasticsearch elasticsearchConfig `config:"elasticsearch"`
3437
Enable bool `config:"enable"`
3538
Host string `config:"host"`
@@ -47,6 +50,8 @@ type elasticsearchConfig struct {
4750
ServiceToken string `config:"service_token"`
4851
ServiceTokenPath string `config:"service_token_path"`
4952
Insecure bool `config:"insecure"`
53+
Cert string `config:"cert"`
54+
CertKey string `config:"cert_key"`
5055
}
5156

5257
type kibanaConfig struct {
@@ -87,18 +92,23 @@ func defaultAccessConfig() (setupConfig, error) {
8792
TokenPolicyName: envWithDefault("", "FLEET_TOKEN_POLICY_NAME"),
8893
URL: envWithDefault("", "FLEET_URL"),
8994
DaemonTimeout: envTimeout("FLEET_DAEMON_TIMEOUT"),
95+
Cert: envWithDefault("", "ELASTIC_AGENT_CERT"),
96+
CertKey: envWithDefault("", "ELASTIC_AGENT_CERT_KEY"),
9097
},
9198
FleetServer: fleetServerConfig{
9299
Cert: envWithDefault("", "FLEET_SERVER_CERT"),
93100
CertKey: envWithDefault("", "FLEET_SERVER_CERT_KEY"),
94101
PassphrasePath: envWithDefault("", "FLEET_SERVER_CERT_KEY_PASSPHRASE"),
102+
ClientAuth: envWithDefault("none", "FLEET_SERVER_CLIENT_AUTH"),
95103
Elasticsearch: elasticsearchConfig{
96104
Host: envWithDefault("http://elasticsearch:9200", "FLEET_SERVER_ELASTICSEARCH_HOST", "ELASTICSEARCH_HOST"),
97105
ServiceToken: envWithDefault("", "FLEET_SERVER_SERVICE_TOKEN"),
98106
ServiceTokenPath: envWithDefault("", "FLEET_SERVER_SERVICE_TOKEN_PATH"),
99107
CA: envWithDefault("", "FLEET_SERVER_ELASTICSEARCH_CA", "ELASTICSEARCH_CA"),
100108
CATrustedFingerprint: envWithDefault("", "FLEET_SERVER_ELASTICSEARCH_CA_TRUSTED_FINGERPRINT"),
101109
Insecure: envBool("FLEET_SERVER_ELASTICSEARCH_INSECURE"),
110+
Cert: envWithDefault("", "FLEET_SERVER_ES_CERT"),
111+
CertKey: envWithDefault("", "FLEET_SERVER_ES_CERT_KEY"),
102112
},
103113
Enable: envBool("FLEET_SERVER_ENABLE"),
104114
Host: envWithDefault("", "FLEET_SERVER_HOST"),

0 commit comments

Comments
 (0)