Merge "Cleanup rndc backend"
This commit is contained in:
commit
3cc0816ef2
@ -28,6 +28,7 @@ from designate import exceptions
|
||||
from designate import utils
|
||||
from designate.backend import base
|
||||
from designate.utils import DEFAULT_MDNS_PORT
|
||||
from designate.i18n import _LI
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
@ -42,20 +43,34 @@ class Bind9Backend(base.Backend):
|
||||
def __init__(self, target):
|
||||
super(Bind9Backend, self).__init__(target)
|
||||
|
||||
# TODO(Federico): make attributes private, run _rndc_base at init time
|
||||
self.host = self.options.get('host', '127.0.0.1')
|
||||
self.port = int(self.options.get('port', 53))
|
||||
self.view = self.options.get('view')
|
||||
self.rndc_host = self.options.get('rndc_host', '127.0.0.1')
|
||||
self.rndc_port = int(self.options.get('rndc_port', 953))
|
||||
self.rndc_config_file = self.options.get('rndc_config_file')
|
||||
self.rndc_key_file = self.options.get('rndc_key_file')
|
||||
self._host = self.options.get('host', '127.0.0.1')
|
||||
self._port = int(self.options.get('port', 53))
|
||||
self._view = self.options.get('view')
|
||||
|
||||
# Removes zone files when a zone is deleted.
|
||||
# This option will take effect on bind>=9.10.0.
|
||||
self.clean_zonefile = strutils.bool_from_string(
|
||||
self._clean_zonefile = strutils.bool_from_string(
|
||||
self.options.get('clean_zonefile', 'false'))
|
||||
|
||||
self._rndc_call_base = self._generate_rndc_base_call()
|
||||
|
||||
def _generate_rndc_base_call(self):
|
||||
"""Generate argument list to execute rndc"""
|
||||
rndc_host = self.options.get('rndc_host', '127.0.0.1')
|
||||
rndc_port = int(self.options.get('rndc_port', 953))
|
||||
rndc_bin_path = self.options.get('rndc_bin_path', 'rndc')
|
||||
rndc_config_file = self.options.get('rndc_config_file')
|
||||
rndc_key_file = self.options.get('rndc_key_file')
|
||||
rndc_call = [rndc_bin_path, '-s', rndc_host, '-p', str(rndc_port)]
|
||||
|
||||
if rndc_config_file:
|
||||
rndc_call.extend(['-c', rndc_config_file])
|
||||
|
||||
if rndc_key_file:
|
||||
rndc_call.extend(['-k', rndc_key_file])
|
||||
|
||||
return rndc_call
|
||||
|
||||
def create_zone(self, context, zone):
|
||||
"""Create a new Zone by executin rndc, then notify mDNS
|
||||
Do not raise exceptions if the zone already exists.
|
||||
@ -70,10 +85,7 @@ class Bind9Backend(base.Backend):
|
||||
# Ensure different MiniDNS instances are targeted for AXFRs
|
||||
random.shuffle(masters)
|
||||
|
||||
if self.view:
|
||||
view = 'in %s' % self.view
|
||||
else:
|
||||
view = ''
|
||||
view = 'in %s' % self._view if self._view else ''
|
||||
|
||||
rndc_op = [
|
||||
'addzone',
|
||||
@ -90,7 +102,7 @@ class Bind9Backend(base.Backend):
|
||||
raise
|
||||
|
||||
self.mdns_api.notify_zone_changed(
|
||||
context, zone, self.host, self.port, self.timeout,
|
||||
context, zone, self._host, self._port, self.timeout,
|
||||
self.retry_interval, self.max_retries, self.delay)
|
||||
|
||||
def delete_zone(self, context, zone):
|
||||
@ -99,16 +111,13 @@ class Bind9Backend(base.Backend):
|
||||
"""
|
||||
LOG.debug('Delete Zone')
|
||||
|
||||
if self.view:
|
||||
view = 'in %s' % self.view
|
||||
else:
|
||||
view = ''
|
||||
view = 'in %s' % self._view if self._view else ''
|
||||
|
||||
rndc_op = [
|
||||
'delzone',
|
||||
'%s %s' % (zone['name'].rstrip('.'), view),
|
||||
]
|
||||
if self.clean_zonefile:
|
||||
if self._clean_zonefile:
|
||||
rndc_op.insert(1, '-clean')
|
||||
|
||||
try:
|
||||
@ -118,29 +127,18 @@ class Bind9Backend(base.Backend):
|
||||
if "not found" not in six.text_type(e):
|
||||
raise
|
||||
|
||||
def _rndc_base(self):
|
||||
rndc_call = [
|
||||
'rndc',
|
||||
'-s', self.rndc_host,
|
||||
'-p', str(self.rndc_port),
|
||||
]
|
||||
|
||||
if self.rndc_config_file:
|
||||
rndc_call.extend(
|
||||
['-c', self.rndc_config_file])
|
||||
|
||||
if self.rndc_key_file:
|
||||
rndc_call.extend(
|
||||
['-k', self.rndc_key_file])
|
||||
|
||||
return rndc_call
|
||||
|
||||
def _execute_rndc(self, rndc_op):
|
||||
"""Execute rndc
|
||||
|
||||
:param rndc_op: rndc arguments
|
||||
:type rndc_op: list
|
||||
:returns: None
|
||||
:raises: exceptions.Backend
|
||||
"""
|
||||
try:
|
||||
rndc_call = self._rndc_base()
|
||||
rndc_call.extend(rndc_op)
|
||||
LOG.debug('Executing RNDC call: %s' % " ".join(rndc_call))
|
||||
rndc_call = self._rndc_call_base + rndc_op
|
||||
LOG.debug('Executing RNDC call: %r', rndc_call)
|
||||
utils.execute(*rndc_call)
|
||||
except utils.processutils.ProcessExecutionError as e:
|
||||
LOG.debug('RNDC call failure: %s' % e)
|
||||
LOG.info(_LI('RNDC call failure: %s'), e)
|
||||
raise exceptions.Backend(e)
|
||||
|
@ -12,13 +12,19 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
import mock
|
||||
|
||||
from designate import exceptions
|
||||
from designate import objects
|
||||
from designate.tests.test_backend import BackendTestCase
|
||||
from designate.backend.impl_bind9 import Bind9Backend
|
||||
|
||||
# TODO(Federico): test execute() calls
|
||||
RNDC_BIN_PATH = "/usr/sbin/rndc"
|
||||
RNDC_NOT_AVAILABLE = not os.path.isfile(RNDC_BIN_PATH)
|
||||
|
||||
|
||||
class Bind9BackendTestCase(BackendTestCase):
|
||||
@ -32,13 +38,14 @@ class Bind9BackendTestCase(BackendTestCase):
|
||||
|
||||
target = objects.PoolTarget.from_dict({
|
||||
'id': '4588652b-50e7-46b9-b688-a9bad40a873e',
|
||||
'type': 'powerdns',
|
||||
'type': 'bind9',
|
||||
'masters': [{'host': '192.0.2.1', 'port': 53},
|
||||
{'host': '192.0.2.2', 'port': 35}],
|
||||
'options': [{'key': 'host', 'value': '192.0.2.3'},
|
||||
{'key': 'port', 'value': 53},
|
||||
{'key': 'rndc_host', 'value': '192.0.2.4'},
|
||||
{'key': 'rndc_port', 'value': 953},
|
||||
{'key': 'rndc_bin_path', 'value': '/usr/sbin/rndc'},
|
||||
{'key': 'rndc_config_file', 'value': '/etc/rndc.conf'},
|
||||
{'key': 'rndc_key_file', 'value': '/etc/rndc.key'},
|
||||
{'key': 'clean_zonefile', 'value': 'true'}],
|
||||
@ -46,12 +53,106 @@ class Bind9BackendTestCase(BackendTestCase):
|
||||
|
||||
self.backend = Bind9Backend(target)
|
||||
|
||||
@mock.patch('designate.utils.execute')
|
||||
def test_create_zone(self, execute):
|
||||
context = self.get_context()
|
||||
self.backend.create_zone(context, self.zone)
|
||||
def test_backend_init(self):
|
||||
expected = ['/usr/sbin/rndc', '-s', '192.0.2.4', '-p', '953',
|
||||
'-c', '/etc/rndc.conf', '-k', '/etc/rndc.key']
|
||||
self.assertEqual(expected, self.backend._rndc_call_base)
|
||||
|
||||
def test_backend_init_using_defaults(self):
|
||||
target = objects.PoolTarget.from_dict({
|
||||
'id': '4588652b-50e7-46b9-b688-a9bad40a873e',
|
||||
'type': 'bind9',
|
||||
'masters': [{'host': '192.0.2.1', 'port': 53},
|
||||
{'host': '192.0.2.2', 'port': 35}],
|
||||
'options': []
|
||||
})
|
||||
backend = Bind9Backend(target)
|
||||
expected = ['rndc', '-s', '127.0.0.1', '-p', '953']
|
||||
self.assertEqual(expected, backend._rndc_call_base)
|
||||
|
||||
@mock.patch('designate.utils.execute')
|
||||
def test_delete_zone(self, execute):
|
||||
def test_create_zone(self, mock_exe):
|
||||
context = self.get_context()
|
||||
self.backend.create_zone(context, self.zone)
|
||||
self.assertEqual(1, mock_exe.call_count)
|
||||
args = mock_exe.call_args[0]
|
||||
self.assertEqual((
|
||||
'/usr/sbin/rndc', '-s', '192.0.2.4', '-p', '953',
|
||||
'-c', '/etc/rndc.conf', '-k', '/etc/rndc.key', 'addzone',
|
||||
), args[:10])
|
||||
|
||||
e1 = 'example.com { type slave; masters { 192.0.2.1 port 53; 192.0.2.2 port 35;}; file "slave.example.com.cca7908b-dad4-4c50-adba-fb67d4c556e8"; };' # noqa
|
||||
e2 = 'example.com { type slave; masters { 192.0.2.2 port 35; 192.0.2.1 port 53;}; file "slave.example.com.cca7908b-dad4-4c50-adba-fb67d4c556e8"; };' # noqa
|
||||
self.assertTrue(args[-1] == e1 or args[-1] == e2)
|
||||
|
||||
@mock.patch('designate.utils.execute')
|
||||
def test_delete_zone(self, mock_exe):
|
||||
context = self.get_context()
|
||||
self.backend.delete_zone(context, self.zone)
|
||||
mock_exe.assert_called_with(
|
||||
'/usr/sbin/rndc', '-s', '192.0.2.4', '-p', '953',
|
||||
'-c', '/etc/rndc.conf', '-k', '/etc/rndc.key',
|
||||
'delzone', '-clean', 'example.com '
|
||||
)
|
||||
|
||||
|
||||
class Bind9BackendFunctionalTestCase(BackendTestCase):
|
||||
|
||||
# Run the real rndc, if available
|
||||
|
||||
def setUp(self):
|
||||
super(Bind9BackendFunctionalTestCase, self).setUp()
|
||||
|
||||
self.CONF.set_override('root_helper', ' ') # disable rootwrap
|
||||
self._conf_fn = tempfile.mkstemp(prefix='rndc-', suffix='.conf')[1]
|
||||
self._key_fn = tempfile.mkstemp(prefix='rndc-', suffix='.key')[1]
|
||||
with open(self._key_fn, 'w') as f:
|
||||
f.write("""
|
||||
key "rndc-key" {
|
||||
algorithm hmac-md5;
|
||||
secret "iNeLyEHGbOrogTw+nB/KwQ==";
|
||||
};
|
||||
""")
|
||||
with open(self._conf_fn, 'w') as f:
|
||||
f.write("""
|
||||
key "rndc-key" {
|
||||
algorithm hmac-md5;
|
||||
secret "iNeLyEHGbOrogTw+nB/KwQ==";
|
||||
};
|
||||
|
||||
options {
|
||||
default-key "rndc-key";
|
||||
default-server 127.0.0.1;
|
||||
default-port 953;
|
||||
};
|
||||
""")
|
||||
self.zone = objects.Zone(id='cca7908b-dad4-4c50-adba-fb67d4c556e8',
|
||||
name='example.com.',
|
||||
email='example@example.com')
|
||||
target = objects.PoolTarget.from_dict({
|
||||
'id': '4588652b-50e7-46b9-b688-a9bad40a873e',
|
||||
'type': 'bind9',
|
||||
'masters': [{'host': '127.0.0.1', 'port': 33353}],
|
||||
'options': [{'key': 'host', 'value': '127.0.0.1'},
|
||||
{'key': 'port', 'value': 33353},
|
||||
{'key': 'rndc_host', 'value': '127.0.0.1'},
|
||||
{'key': 'rndc_port', 'value': 33953},
|
||||
{'key': 'rndc_bin_path', 'value': RNDC_BIN_PATH},
|
||||
{'key': 'rndc_config_file', 'value': self._conf_fn},
|
||||
{'key': 'rndc_key_file', 'value': self._key_fn},
|
||||
{'key': 'clean_zonefile', 'value': 'true'}],
|
||||
})
|
||||
|
||||
self.backend = Bind9Backend(target)
|
||||
|
||||
@unittest.skipIf(RNDC_NOT_AVAILABLE, "rndc binary not installed")
|
||||
def test_create_zone_call_rndc_connection_refused(self):
|
||||
# Run rndc againts a closed port. Albeit this does not perform a
|
||||
# successful rndc run, it is enough to test the argument parsing
|
||||
context = self.get_context()
|
||||
exp_msg = 'rndc: connect failed: 127.0.0.1#33953: connection refused'
|
||||
try:
|
||||
self.backend.create_zone(context, self.zone)
|
||||
unittest.fail("Did not raise an exception")
|
||||
except exceptions.Backend as e:
|
||||
self.assertTrue(exp_msg in str(e))
|
||||
|
Loading…
Reference in New Issue
Block a user