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 May 26, 2023
1 parent 1231475 commit b99b9c5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
22 changes: 21 additions & 1 deletion scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,16 @@ class SyslogCfg:
return interval, burst
return interval, burst


class DnsCfg:

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

def dns_update(self, *args, **kwargs):
run_cmd(['systemctl', 'restart', 'resolv-config'], True, False)


class HostConfigDaemon:
def __init__(self):
self.state_db_conn = DBConnector(STATE_DB, 0)
Expand Down Expand Up @@ -1700,6 +1710,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 @@ -1716,6 +1729,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 @@ -1726,6 +1740,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 @@ -1826,7 +1841,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 @@ -1876,6 +1895,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
32 changes: 32 additions & 0 deletions tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,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 @@ -639,6 +640,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 @@ -666,6 +668,18 @@ def test_mgmtiface_event(self):
]
mocked_check_output.assert_has_calls(expected)

def test_dns_events(self):
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
MockConfigDb.event_queue = [('DNS_NAMESERVER', '1.1.1.1')]
daemon = hostcfgd.HostConfigDaemon()
daemon.register_callbacks()
with mock.patch('hostcfgd.run_cmd') as mocked_run_cmd:
try:
daemon.start()
except TimeoutError:
pass
mocked_run_cmd.assert_has_calls([call(['systemctl', 'restart', 'resolv-config'], True, False)])

class TestSyslogHandler:
@mock.patch('hostcfgd.run_cmd')
@mock.patch('hostcfgd.SyslogCfg.parse_syslog_conf', mock.MagicMock(return_value=('100', '200')))
Expand Down Expand Up @@ -721,3 +735,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, False)])

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()
8 changes: 6 additions & 2 deletions tests/hostcfgd/test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,8 @@
}
},
"MGMT_INTERFACE": {},
"MGMT_VRF_CONFIG": {}
"MGMT_VRF_CONFIG": {},
"DNS_NAMESERVER": {}
}


Expand Down Expand Up @@ -1257,5 +1258,8 @@
"vrf_global": {
'mgmtVrfEnabled': 'true'
}
}
},
"DNS_NAMESERVER": {
"1.1.1.1": {}
},
}

0 comments on commit b99b9c5

Please sign in to comment.