Skip to content

Commit 9428146

Browse files
authored
Merge pull request #3694 from c-po/T6489-snmpd
snmp: T6489: use new Python wrapper to interact with config filesystem
2 parents c6190f3 + 7e0e810 commit 9428146

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

data/configd-include.json

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"service_router-advert.py",
8080
"service_salt-minion.py",
8181
"service_sla.py",
82+
"service_snmp.py",
8283
"service_ssh.py",
8384
"service_tftp-server.py",
8485
"service_webproxy.py",

src/conf_mode/service_snmp.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
#
3-
# Copyright (C) 2018-2023 VyOS maintainers and contributors
3+
# Copyright (C) 2018-2024 VyOS maintainers and contributors
44
#
55
# This program is free software; you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License version 2 or later as
@@ -26,10 +26,12 @@
2626
from vyos.snmpv3_hashgen import plaintext_to_sha1
2727
from vyos.snmpv3_hashgen import random
2828
from vyos.template import render
29-
from vyos.utils.process import call
30-
from vyos.utils.permission import chmod_755
29+
from vyos.utils.configfs import delete_cli_node
30+
from vyos.utils.configfs import add_cli_node
3131
from vyos.utils.dict import dict_search
3232
from vyos.utils.network import is_addr_assigned
33+
from vyos.utils.process import call
34+
from vyos.utils.permission import chmod_755
3335
from vyos.version import get_version_data
3436
from vyos import ConfigError
3537
from vyos import airbag
@@ -192,12 +194,8 @@ def generate(snmp):
192194
return None
193195

194196
if 'v3' in snmp:
195-
# net-snmp is now regenerating the configuration file in the background
196-
# thus we need to re-open and re-read the file as the content changed.
197-
# After that we can no read the encrypted password from the config and
198-
# replace the CLI plaintext password with its encrypted version.
199-
os.environ['vyos_libexec_dir'] = '/usr/libexec/vyos'
200-
197+
# SNMPv3 uses a hashed password. If CLI defines a plaintext password,
198+
# we will hash it in the background and replace the CLI node!
201199
if 'user' in snmp['v3']:
202200
for user, user_config in snmp['v3']['user'].items():
203201
if dict_search('auth.type', user_config) == 'sha':
@@ -212,8 +210,9 @@ def generate(snmp):
212210
snmp['v3']['user'][user]['auth']['encrypted_password'] = tmp
213211
del snmp['v3']['user'][user]['auth']['plaintext_password']
214212

215-
call(f'/opt/vyatta/sbin/my_set service snmp v3 user "{user}" auth encrypted-password "{tmp}" > /dev/null')
216-
call(f'/opt/vyatta/sbin/my_delete service snmp v3 user "{user}" auth plaintext-password > /dev/null')
213+
cli_base = ['service', 'snmp', 'v3', 'user', user, 'auth']
214+
delete_cli_node(cli_base + ['plaintext-password'])
215+
add_cli_node(cli_base + ['encrypted-password'], value=tmp)
217216

218217
if dict_search('privacy.plaintext_password', user_config) is not None:
219218
tmp = hash(dict_search('privacy.plaintext_password', user_config),
@@ -222,8 +221,9 @@ def generate(snmp):
222221
snmp['v3']['user'][user]['privacy']['encrypted_password'] = tmp
223222
del snmp['v3']['user'][user]['privacy']['plaintext_password']
224223

225-
call(f'/opt/vyatta/sbin/my_set service snmp v3 user "{user}" privacy encrypted-password "{tmp}" > /dev/null')
226-
call(f'/opt/vyatta/sbin/my_delete service snmp v3 user "{user}" privacy plaintext-password > /dev/null')
224+
cli_base = ['service', 'snmp', 'v3', 'user', user, 'privacy']
225+
delete_cli_node(cli_base + ['plaintext-password'])
226+
add_cli_node(cli_base + ['encrypted-password'], value=tmp)
227227

228228
# Write client config file
229229
render(config_file_client, 'snmp/etc.snmp.conf.j2', snmp)
@@ -246,7 +246,7 @@ def apply(snmp):
246246
return None
247247

248248
# start SNMP daemon
249-
call(f'systemctl restart {systemd_service}')
249+
call(f'systemctl reload-or-restart {systemd_service}')
250250

251251
# Enable AgentX in FRR
252252
# This should be done for each daemon individually because common command

0 commit comments

Comments
 (0)