Remove and block DB access in dhcpbridge

This fixes an issue of direct DB access from happening in dhcpbridge
by using an object method instead. For this to work and to prohibit
future DB access from happening, the objects indirection API is set
and DB access is blocked in a similar way to how it is done in
nova-compute and nova-network.

Change-Id: I5e3152aad13343a6a1ecd9b5b511429159c9d4c2
Closes-Bug: #1316074
This commit is contained in:
Hans Lindgren
2014-05-05 12:30:18 +02:00
parent 6df520049d
commit 9b41af32bb

View File

@@ -22,13 +22,18 @@ from __future__ import print_function
import os
import sys
import traceback
from oslo.config import cfg
from nova.conductor import rpcapi as conductor_rpcapi
from nova import config
from nova import context
from nova import db
import nova.db.api
from nova import exception
from nova.network import rpcapi as network_rpcapi
from nova.objects import base as objects_base
from nova.objects import network as network_obj
from nova.openstack.common.gettextutils import _
from nova.openstack.common import importutils
from nova.openstack.common import jsonutils
@@ -38,6 +43,7 @@ from nova import rpc
CONF = cfg.CONF
CONF.import_opt('host', 'nova.netconf')
CONF.import_opt('network_manager', 'nova.service')
CONF.import_opt('use_local', 'nova.conductor.api', group='conductor')
LOG = logging.getLogger(__name__)
@@ -65,9 +71,9 @@ def del_lease(mac, ip_address):
def init_leases(network_id):
"""Get the list of hosts for a network."""
ctxt = context.get_admin_context()
network_ref = db.network_get(ctxt, network_id)
network = network_obj.Network.get_by_id(ctxt, network_id)
network_manager = importutils.import_object(CONF.network_manager)
return network_manager.get_dhcp_leases(ctxt, network_ref)
return network_manager.get_dhcp_leases(ctxt, network)
def add_action_parsers(subparsers):
@@ -92,6 +98,20 @@ CONF.register_cli_opt(
handler=add_action_parsers))
def block_db_access():
class NoDB(object):
def __getattr__(self, attr):
return self
def __call__(self, *args, **kwargs):
stacktrace = "".join(traceback.format_stack())
LOG.error(_('No db access allowed in nova-dhcpbridge: %s'),
stacktrace)
raise exception.DBNotAllowed('nova-dhcpbridge')
nova.db.api.IMPL = NoDB()
def main():
"""Parse environment and arguments and call the appropriate action."""
config.parse_args(sys.argv,
@@ -101,6 +121,11 @@ def main():
global LOG
LOG = logging.getLogger('nova.dhcpbridge')
if not CONF.conductor.use_local:
block_db_access()
objects_base.NovaObject.indirection_api = \
conductor_rpcapi.ConductorAPI()
if CONF.action.name in ['add', 'del', 'old']:
msg = (_("Called '%(action)s' for mac '%(mac)s' with ip '%(ip)s'") %
{"action": CONF.action.name,