summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOleksandr Kuchmystyi <o.kuchmystyi@vyos.io>2026-11-02 16:41:41 +0300
committerOleksandr Kuchmystyi <o.kuchmystyi@vyos.io>2026-11-11 10:29:46 +0300
commitdf579f5727d93837fe9feee70dbcb3814bdbabc6 (patch)
tree77eeb145bb87dd063b4b8e52269b7d189df377e1 /src
parentf46c01ccdc9075602546873c4d1bbdaea8df32eb (diff)
downloadvyos-1x-df579f5727d93837fe9feee70dbcb3814bdbabc6.tar.gz
vyos-1x-df579f5727d93837fe9feee70dbcb3814bdbabc6.zip
telegraf: T9329: Add PKI based TLS options for InfluxDB output
Add a reusable `tls` node for Telegraf outputs and use it for InfluxDB: `ca-certificate`, `certificate`, `server-name` and `insecure`. Referenced PKI objects are written to `/run/telegraf/certs`.
Diffstat (limited to 'src')
-rw-r--r--src/conf_mode/pki.py4
-rw-r--r--src/conf_mode/service_monitoring_telegraf.py151
-rw-r--r--src/migration-scripts/monitoring/2-to-338
3 files changed, 187 insertions, 6 deletions
diff --git a/src/conf_mode/pki.py b/src/conf_mode/pki.py
index abc819b89..f90526ad0 100644
--- a/src/conf_mode/pki.py
+++ b/src/conf_mode/pki.py
@@ -101,6 +101,10 @@ sync_search = [
{
'keys': ['certificate', 'ca_certificate'],
'path': ['service', 'stunnel'],
+ },
+ {
+ 'keys': ['certificate', 'ca_certificate'],
+ 'path': ['service', 'monitoring', 'telegraf'],
}
]
diff --git a/src/conf_mode/service_monitoring_telegraf.py b/src/conf_mode/service_monitoring_telegraf.py
index 9aff8c5a2..a16937f72 100644
--- a/src/conf_mode/service_monitoring_telegraf.py
+++ b/src/conf_mode/service_monitoring_telegraf.py
@@ -21,12 +21,22 @@ from shutil import rmtree
from vyos.config import Config
from vyos.configdict import is_node_changed
+from vyos.configverify import verify_pki_ca_certificate
+from vyos.configverify import verify_pki_certificate
from vyos.configverify import verify_vrf
from vyos.ifconfig import Section
+from vyos.pki import encode_certificate
+from vyos.pki import find_chain
+from vyos.pki import load_certificate
+from vyos.pki import wrap_certificate
+from vyos.pki import wrap_private_key
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.dict import dict_search_args
+from vyos.utils.file import makedir
+from vyos.utils.file import write_file
from vyos.utils.process import call
from vyos.utils.permission import chown
from vyos.utils.process import cmdl
@@ -35,13 +45,124 @@ from vyos import ConfigError
from vyos import airbag
airbag.enable()
-cache_dir = f'/etc/telegraf/.cache'
-config_telegraf = f'/run/telegraf/telegraf.conf'
-telegraf_env = '/run/telegraf/telegraf.env'
+cache_dir = '/etc/telegraf/.cache'
+config_dir = '/run/telegraf'
+telegraf_env = f'{config_dir}/telegraf.env'
+cert_dir = f'{config_dir}/certs'
+config_telegraf = f'{config_dir}/telegraf.conf'
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'
+# Output plugins (CLI node names, mangled) supporting the 'tls' node
+# defined in 'include/monitoring/telegraf-output-tls.xml.i'
+supported_tls_outputs = ['influxdb']
+
+
+def _cleanup_tls_certs():
+ if os.path.isdir(cert_dir):
+ rmtree(cert_dir, ignore_errors=True)
+
+
+def _verify_tls(monitoring: dict, output: str) -> None:
+ """Verify the common 'tls' node of a Telegraf output plugin"""
+
+ tls = dict_search_args(monitoring, output, 'tls')
+ if not tls:
+ return
+
+ cli_output = output.replace('_', '-')
+ if 'insecure' in tls and 'ca_certificate' in tls:
+ raise ConfigError(
+ f'Telegraf {cli_output} TLS options "ca-certificate" '
+ 'and "insecure" are mutually exclusive!'
+ )
+
+ if 'ca_certificate' in tls:
+ verify_pki_ca_certificate(monitoring, tls['ca_certificate'])
+
+ if 'certificate' in tls:
+ verify_pki_certificate(
+ monitoring, tls['certificate'], no_password_protected=True
+ )
+
+ url = dict_search_args(monitoring, output, 'url')
+ if url and not url.lower().startswith('https://'):
+ raise ConfigError(
+ f'Telegraf {cli_output} TLS options have no effect as "url" does not use HTTPS!'
+ )
+
+
+def _client_certificate_chain(monitoring: dict, cert_name: str) -> str:
+ """
+ Return the PEM encoded client certificate followed by all intermediate CA
+ certificates found in the PKI configuration.
+ """
+ pki_cert = monitoring['pki']['certificate'][cert_name]
+ leaf = load_certificate(pki_cert['certificate'])
+
+ ca_certs = set()
+ for ca_config in (dict_search_args(monitoring, 'pki', 'ca') or {}).values():
+ ca_cert = load_certificate(ca_config.get('certificate', ''))
+ if ca_cert:
+ ca_certs.add(ca_cert)
+
+ chain = find_chain(leaf, ca_certs)
+ return '\n'.join(encode_certificate(cert) for cert in chain)
+
+
+def _generate_tls(monitoring: dict, output: str):
+ """
+ Write PKI objects referenced by the 'tls' node of a Telegraf output plugin
+ to disk and store the resulting file paths in the config dict for the
+ template engine.
+ """
+
+ tls = dict_search_args(monitoring, output, 'tls')
+ if not tls:
+ return
+
+ prefix = os.path.join(cert_dir, output.replace('_', '-'))
+
+ user = group = 'telegraf'
+ owner = dict(user=user, group=group)
+ # The file's owner can read and write to the file, while everyone else can only read it
+ cert_mode = 0o644
+ # The owner can read and write the file
+ key_mode = 0o600
+
+ if 'ca_certificate' in tls:
+ ca_name = tls['ca_certificate']
+ pki_ca = monitoring['pki']['ca'][ca_name]
+ tls['ca_certificate_file'] = f'{prefix}-ca.pem'
+ write_file(
+ tls['ca_certificate_file'],
+ wrap_certificate(pki_ca['certificate']),
+ mode=cert_mode,
+ **owner,
+ )
+
+ if 'certificate' in tls:
+ cert_name = tls['certificate']
+ pki_cert = monitoring['pki']['certificate'][cert_name]
+ tls['certificate_file'] = f'{prefix}-cert.pem'
+ tls['certificate_key_file'] = f'{prefix}-cert.key'
+
+ # Include intermediate CA certificates, otherwise mutual TLS fails if
+ # the server only trusts the root CA
+ write_file(
+ tls['certificate_file'],
+ _client_certificate_chain(monitoring, cert_name),
+ mode=cert_mode,
+ **owner,
+ )
+ write_file(
+ tls['certificate_key_file'],
+ wrap_private_key(pki_cert['private']['key']),
+ mode=key_mode,
+ **owner,
+ )
+
def get_nft_filter_chains():
""" Get nft chains for table filter """
try:
@@ -75,9 +196,13 @@ def get_config(config=None):
if not conf.exists(base):
return None
- monitoring = conf.get_config_dict(base, key_mangling=('-', '_'),
- get_first_key=True,
- no_tag_node_value_mangle=True)
+ monitoring = conf.get_config_dict(
+ base,
+ key_mangling=('-', '_'),
+ get_first_key=True,
+ no_tag_node_value_mangle=True,
+ with_pki=True,
+ )
tmp = is_node_changed(conf, base + ['vrf'])
if tmp: monitoring.update({'restart_required': {}})
@@ -268,9 +393,17 @@ def verify(monitoring):
'same VRF for FRR metrics to be relayed locally!'
)
+ # Verify TLS settings of all outputs supporting them
+ for output in supported_tls_outputs:
+ _verify_tls(monitoring, output)
+
return None
def generate(monitoring):
+ # Re-create PKI files referenced by output plugins - always start from
+ # an empty directory so no stale certificates/keys are left behind
+ _cleanup_tls_certs()
+
if not monitoring:
# Delete config and systemd files
config_files = [
@@ -289,6 +422,12 @@ def generate(monitoring):
return None
+ # Telegraf must own its runtime directory (e.g. for the PID file), ensure
+ # it is created with proper ownership before the certificate directory
+ makedir(config_dir, user='telegraf', group='telegraf')
+ for output in supported_tls_outputs:
+ _generate_tls(monitoring, output)
+
# Create telegraf cache dir
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
diff --git a/src/migration-scripts/monitoring/2-to-3 b/src/migration-scripts/monitoring/2-to-3
new file mode 100644
index 000000000..cd85374d6
--- /dev/null
+++ b/src/migration-scripts/monitoring/2-to-3
@@ -0,0 +1,38 @@
+# Copyright (C) VyOS Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# T9329: The InfluxDB output used to unconditionally skip verification of the
+# remote server certificate (`insecure_skip_verify = true`). Certificate
+# verification is now enabled by default and can be configured via the new
+# 'tls' node. Preserve the previous behavior for existing HTTPS endpoints by
+# explicitly setting 'tls insecure'.
+
+from vyos.configtree import ConfigTree
+
+base = ['service', 'monitoring', 'telegraf', 'influxdb']
+
+
+def migrate(config: ConfigTree) -> None:
+ url_path = base + ['url']
+ if not config.exists(url_path):
+ # Nothing to do
+ return
+
+ # Prevent migration for already enabled TLS
+ if config.exists(base + ['tls']):
+ return
+
+ url = config.return_value(url_path)
+ if url.lower().startswith('https://'):
+ config.set(base + ['tls', 'insecure'])