From c04a30785f06218510b3d37a917ab9e02e21b977 Mon Sep 17 00:00:00 2001 From: reyesj2 <94730068+reyesj2@users.noreply.github.com> Date: Wed, 8 Jul 2026 16:15:02 -0500 Subject: [PATCH] make elastic agent state persistent to prevent re-enrollment during soup / reboots --- salt/common/tools/sbin/so-restart | 3 +++ salt/common/tools/sbin/so-stop | 2 ++ salt/elasticfleet/enabled.sls | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/salt/common/tools/sbin/so-restart b/salt/common/tools/sbin/so-restart index 14747d134..b3864d2d1 100755 --- a/salt/common/tools/sbin/so-restart +++ b/salt/common/tools/sbin/so-restart @@ -35,6 +35,9 @@ case $1 in "elastic-fleet"|"elasticfleet") docker_check_running "elastic-fleet" "--stop" docker rm "so-elastic-fleet" 2> /dev/null + # Removing the elastic fleet state directory, so that the next startup re-enrolls with a fresh policy + rm -rf /opt/so/conf/elastic-fleet/state + salt-call state.apply elasticfleet queue=True ;; *) diff --git a/salt/common/tools/sbin/so-stop b/salt/common/tools/sbin/so-stop index d036a7b63..9218b44f7 100755 --- a/salt/common/tools/sbin/so-stop +++ b/salt/common/tools/sbin/so-stop @@ -29,6 +29,8 @@ case $1 in "elasticfleet"|"elastic-fleet") docker_check_running "elastic-fleet" "--stop" docker rm "so-elastic-fleet" 2> /dev/null + # Removing the elastic fleet state directory, so that the next startup re-enrolls with a fresh policy + rm -rf /opt/so/conf/elastic-fleet/state ;; *) docker_check_running "$1" "--stop" diff --git a/salt/elasticfleet/enabled.sls b/salt/elasticfleet/enabled.sls index 60cf27deb..ea64a4cb1 100644 --- a/salt/elasticfleet/enabled.sls +++ b/salt/elasticfleet/enabled.sls @@ -11,6 +11,10 @@ {# This value is generated during node install and stored in minion pillar #} {% set SERVICETOKEN = salt['pillar.get']('elasticfleet:config:server:es_token','') %} +{# Prevent Elastic Agent from re-enrolling with a new agent.id everytime the container starts up. + - if a fresh enrollment is needed use 'so-stop elasticfleet' + #} +{% set ENROLLED = salt['file.file_exists']('/opt/so/conf/elastic-fleet/state/fleet.enc') %} include: - ca @@ -65,6 +69,7 @@ so-elastic-fleet: - /etc/pki/elasticfleet-server.crt:/etc/pki/elasticfleet-server.crt:ro - /etc/pki/elasticfleet-server.key:/etc/pki/elasticfleet-server.key:ro - /etc/pki/tls/certs/intca.crt:/etc/pki/tls/certs/intca.crt:ro + - /opt/so/conf/elastic-fleet/state:/usr/share/elastic-agent/state - /opt/so/log/elasticfleet:/usr/share/elastic-agent/logs {% if DOCKERMERGED.containers['so-elastic-fleet'].custom_bind_mounts %} {% for BIND in DOCKERMERGED.containers['so-elastic-fleet'].custom_bind_mounts %} @@ -72,6 +77,7 @@ so-elastic-fleet: {% endfor %} {% endif %} - environment: + {% if not ENROLLED %} - FLEET_SERVER_ENABLE=true - FLEET_URL=https://{{ GLOBALS.hostname }}:8220 - FLEET_SERVER_ELASTICSEARCH_HOST=https://{{ GLOBALS.manager }}:9200 @@ -81,6 +87,9 @@ so-elastic-fleet: - FLEET_SERVER_CERT_KEY=/etc/pki/elasticfleet-server.key - FLEET_CA=/etc/pki/tls/certs/intca.crt - FLEET_SERVER_ELASTICSEARCH_CA=/etc/pki/tls/certs/intca.crt + {% endif %} + - STATE_PATH=/usr/share/elastic-agent/state + - CONFIG_PATH=/usr/share/elastic-agent/state - LOGS_PATH=logs {% if DOCKERMERGED.containers['so-elastic-fleet'].extra_env %} {% for XTRAENV in DOCKERMERGED.containers['so-elastic-fleet'].extra_env %} @@ -99,6 +108,7 @@ so-elastic-fleet: - x509: etc_elasticfleet_crt - require: - file: trusttheca + - file: eastatedir - x509: etc_elasticfleet_key - x509: etc_elasticfleet_crt