Skip to content

Commit

Permalink
[hostcfgd][dns] Subscribe to DNS_NAMESERVER table to react to static …
Browse files Browse the repository at this point in the history
…DNS configuration changes.
  • Loading branch information
oleksandrivantsiv committed Mar 16, 2023
1 parent 6677852 commit 843bbf9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
24 changes: 23 additions & 1 deletion scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,18 @@ class SyslogCfg:
return interval, burst


class DnsCfg:

def load(self, *args, **kwargs):
self.dns_update()

def dns_update(self, *args, **kwargs):
try:
run_cmd('systemctl restart resolv-config', True, True)
except subprocess.CalledProcessError:
syslog.syslog(syslog.LOG_ERR, 'Failed to restart resolv-config service')


class HostConfigDaemon:
def __init__(self):
# Just a sanity check to verify if the CONFIG_DB has been initialized
Expand Down Expand Up @@ -1617,6 +1629,9 @@ class HostConfigDaemon:
# Initialize SyslogCfg
self.syslogcfg = SyslogCfg()

# Initialize DnsCfg
self.dnscfg = DnsCfg()

def load(self, init_data):
features = init_data['FEATURE']
aaa = init_data['AAA']
Expand All @@ -1633,6 +1648,7 @@ class HostConfigDaemon:
mgmt_ifc = init_data.get(swsscommon.CFG_MGMT_INTERFACE_TABLE_NAME, {})
mgmt_vrf = init_data.get(swsscommon.CFG_MGMT_VRF_CONFIG_TABLE_NAME, {})
syslog = init_data.get('SYSLOG_CONFIG', {})
dns = init_data.get('DNS_NAMESERVER', {})

self.feature_handler.sync_state_field(features)
self.aaacfg.load(aaa, tacacs_global, tacacs_server, radius_global, radius_server)
Expand All @@ -1643,6 +1659,7 @@ class HostConfigDaemon:
self.devmetacfg.load(dev_meta)
self.mgmtifacecfg.load(mgmt_ifc, mgmt_vrf)
self.syslogcfg.load(syslog)
self.dnscfg.load(dns)

# Update AAA with the hostname
self.aaacfg.hostname_update(self.devmetacfg.hostname)
Expand Down Expand Up @@ -1743,7 +1760,11 @@ class HostConfigDaemon:
self.devmetacfg.hostname_update(data)

def syslog_handler(self, key, op, data):
self.syslogcfg.syslog_update(data)
self.syslogcfg.syslog_update(key, data)

def dns_nameserver_handler(self, key, op, data):
syslog.syslog(syslog.LOG_INFO, 'DNS nameserver handler...')
self.dnscfg.dns_update(key, data)

def wait_till_system_init_done(self):
# No need to print the output in the log file so using the "--quiet"
Expand Down Expand Up @@ -1793,6 +1814,7 @@ class HostConfigDaemon:
make_callback(self.mgmt_vrf_handler))

self.config_db.subscribe('SYSLOG_CONFIG', make_callback(self.syslog_handler))
self.config_db.subscribe('DNS_NAMESERVER', make_callback(self.dns_nameserver_handler))

syslog.syslog(syslog.LOG_INFO,
"Waiting for systemctl to finish initialization")
Expand Down
20 changes: 20 additions & 0 deletions tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ def test_devicemeta_event(self):
daemon.aaacfg = mock.MagicMock()
daemon.iptables = mock.MagicMock()
daemon.passwcfg = mock.MagicMock()
daemon.dnscfg = mock.MagicMock()
daemon.load(HOSTCFG_DAEMON_INIT_CFG_DB)
daemon.register_callbacks()
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
Expand Down Expand Up @@ -467,6 +468,7 @@ def test_mgmtiface_event(self):
daemon.aaacfg = mock.MagicMock()
daemon.iptables = mock.MagicMock()
daemon.passwcfg = mock.MagicMock()
daemon.dnscfg = mock.MagicMock()
daemon.load(HOSTCFG_DAEMON_INIT_CFG_DB)
with mock.patch('hostcfgd.check_output_pipe') as mocked_check_output:
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
Expand Down Expand Up @@ -549,3 +551,21 @@ def test_parse_syslog_conf(self):
interval, burst = syslog_cfg.parse_syslog_conf()
assert interval == '0'
assert burst == '0'

class TestDnsHandler:

@mock.patch('hostcfgd.run_cmd')
def test_dns_update(self, mock_run_cmd):
dns_cfg = hostcfgd.DnsCfg()
key = "1.1.1.1"
dns_cfg.dns_update(key, {})

mock_run_cmd.assert_has_calls([call('systemctl restart resolv-config', True, True)])

def test_load(self):
dns_cfg = hostcfgd.DnsCfg()
dns_cfg.dns_update = mock.MagicMock()

data = {}
dns_cfg.load(data)
dns_cfg.dns_update.assert_called()

0 comments on commit 843bbf9

Please sign in to comment.