From dd21c0efeaf91261c3688b42248a2dea263cae2d Mon Sep 17 00:00:00 2001 From: Nataliia Solomko Date: Fri, 6 Nov 2026 12:33:47 +0200 Subject: telegraf: T9569: Pass InfluxDB token via a root-only environment file The InfluxDB token was set with Environment= in the systemd drop-in, so any local user could read it with "systemctl show". The token is now written to a root-only environment file loaded by systemd, and no longer shows up in "systemctl show". Telegraf is restarted when the token changes, as a reload would keep the old value. --- src/conf_mode/service_monitoring_telegraf.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/conf_mode/service_monitoring_telegraf.py b/src/conf_mode/service_monitoring_telegraf.py index 8b7e6615f..2caf9f1c4 100644 --- a/src/conf_mode/service_monitoring_telegraf.py +++ b/src/conf_mode/service_monitoring_telegraf.py @@ -25,6 +25,8 @@ from vyos.configverify import verify_vrf from vyos.ifconfig import Section from vyos.template import render from vyos.utils.convert import human_to_seconds +from vyos.utils.dict import dict_search +from vyos.utils.file import write_file_atomic from vyos.utils.process import call from vyos.utils.permission import chown from vyos.utils.process import cmdl @@ -35,6 +37,7 @@ airbag.enable() cache_dir = f'/etc/telegraf/.cache' config_telegraf = f'/run/telegraf/telegraf.conf' +telegraf_env = '/run/telegraf/telegraf.env' custom_scripts_dir = '/etc/telegraf/custom_scripts' syslog_telegraf = '/etc/rsyslog.d/50-telegraf.conf' systemd_override = '/run/systemd/system/telegraf.service.d/10-override.conf' @@ -79,11 +82,17 @@ def get_config(config=None): tmp = is_node_changed(conf, base + ['vrf']) if tmp: monitoring.update({'restart_required': {}}) + # A reload keeps the old environment, a new token needs a restart + tmp = is_node_changed(conf, base + ['influxdb', 'authentication', 'token']) + if tmp: + monitoring.update({'restart_required': {}}) + # We have gathered the dict representation of the CLI, but there are default # options which we need to update into the dictionary retrieved. monitoring = conf.merge_defaults(monitoring, recursive=True) monitoring['custom_scripts_dir'] = custom_scripts_dir + monitoring['telegraf_env_file'] = telegraf_env monitoring['hostname'] = get_hostname() monitoring['interfaces_ethernet'] = Section.interfaces('ethernet', vlan=False) if conf.exists('firewall'): @@ -265,7 +274,12 @@ def verify(monitoring): def generate(monitoring): if not monitoring: # Delete config and systemd files - config_files = [config_telegraf, systemd_override, syslog_telegraf] + config_files = [ + config_telegraf, + telegraf_env, + systemd_override, + syslog_telegraf, + ] for file in config_files: if os.path.isfile(file): os.unlink(file) @@ -291,6 +305,15 @@ def generate(monitoring): render(systemd_override, 'telegraf/override.conf.j2', monitoring) render(syslog_telegraf, 'telegraf/syslog_telegraf.j2', monitoring) + # Pass the InfluxDB token in an EnvironmentFile instead of Environment=, + # which any user can read via "systemctl show". Written atomically so the + # file is never readable by others. + token = dict_search('influxdb.authentication.token', monitoring) + if token: + write_file_atomic(telegraf_env, f'INFLUX_TOKEN={token}\n') + elif os.path.isfile(telegraf_env): + os.unlink(telegraf_env) + return None def apply(monitoring): -- cgit v1.2.3