Merge pull request #37390 from rallytime/fix-34263

Don't insert __pub* keys into dnsmasq config file with set_config function
This commit is contained in:
Mike Place 2016-11-02 19:31:53 +13:00 committed by GitHub
commit 34b6c6459a
2 changed files with 49 additions and 8 deletions

View file

@ -2,14 +2,15 @@
'''
Module for managing dnsmasq
'''
# Import Python libs
from __future__ import absolute_import
import logging
import os
# Import salt libs
import salt.utils
# Import python libs
import os
import logging
from salt.exceptions import CommandExecutionError
log = logging.getLogger(__name__)
@ -98,20 +99,28 @@ def set_config(config_file='/etc/dnsmasq.conf', follow=True, **kwargs):
if filename.endswith('#') and filename.endswith('#'):
continue
includes.append('{0}/{1}'.format(dnsopts['conf-dir'], filename))
ret_kwargs = {}
for key in kwargs:
# Filter out __pub keys as they should not be added to the config file
# See Issue #34263 for more information
if key.startswith('__'):
continue
ret_kwargs[key] = kwargs[key]
if key in dnsopts:
if isinstance(dnsopts[key], str):
for config in includes:
__salt__['file.sed'](path=config,
before='^{0}=.*'.format(key),
after='{0}={1}'.format(key, kwargs[key]))
before='^{0}=.*'.format(key),
after='{0}={1}'.format(key, kwargs[key]))
else:
__salt__['file.append'](config_file,
'{0}={1}'.format(key, kwargs[key]))
'{0}={1}'.format(key, kwargs[key]))
else:
__salt__['file.append'](config_file,
'{0}={1}'.format(key, kwargs[key]))
return kwargs
return ret_kwargs
def get_config(config_file='/etc/dnsmasq.conf'):
@ -148,6 +157,12 @@ def _parse_dnamasq(filename):
Generic function for parsing dnsmasq files including includes.
'''
fileopts = {}
if not os.path.isfile(filename):
raise CommandExecutionError(
'Error: No such file \'{0}\''.format(filename)
)
with salt.utils.fopen(filename, 'r') as fp_:
for line in fp_:
if not line.strip():

View file

@ -16,6 +16,7 @@ from salttesting.mock import (
)
# Import Salt Libs
from salt.exceptions import CommandExecutionError
from salt.modules import dnsmasq
# Import python libs
@ -58,6 +59,23 @@ class DnsmasqTestCase(TestCase):
with patch.object(os, 'listdir', mock):
self.assertDictEqual(dnsmasq.set_config(), {})
@patch('salt.modules.dnsmasq.get_config', MagicMock(return_value={'conf-dir': 'A'}))
def test_set_config_filter_pub_kwargs(self):
'''
Test that the kwargs returned from running the set_config function
do not contain the __pub that may have been passed through in **kwargs.
'''
mock_domain = 'local'
mock_address = '/some-test-address.local/8.8.4.4'
with patch.dict(dnsmasq.__salt__, {'file.append': MagicMock()}):
ret = dnsmasq.set_config(follow=False,
domain=mock_domain,
address=mock_address,
__pub_pid=8184,
__pub_jid=20161101194639387946,
__pub_tgt='salt-call')
self.assertEqual(ret, {'domain': mock_domain, 'address': mock_address})
def test_get_config(self):
'''
test to dumps all options from the config file.
@ -68,6 +86,14 @@ class DnsmasqTestCase(TestCase):
with patch.object(os, 'listdir', mock):
self.assertDictEqual(dnsmasq.get_config(), {'conf-dir': 'A'})
def test_parse_dnsmasq_no_file(self):
'''
Tests that a CommandExecutionError is when a filename that doesn't exist is
passed in.
'''
self.assertRaises(CommandExecutionError, dnsmasq._parse_dnamasq, 'filename')
@patch('os.path.isfile', MagicMock(return_value=True))
def test_parse_dnamasq(self):
'''
test for generic function for parsing dnsmasq files including includes.