From 788d889b24f79345990b8bca7e9a41e6f0e03e42 Mon Sep 17 00:00:00 2001 From: Sreekumar S Date: Mon, 19 Sep 2016 11:45:09 +0530 Subject: [PATCH] Fixes KeyError while updating bgp peer This will fix the issue of KeyError being thrown when 'password' is not supplied while updating bgp peer. The dict get() method is used to return None when 'password' key is not available. Two unit tests are also added which will test the cases 1) Exception thrown if the auth type is 'none', and a password is supplied when updating 2) When the password is not supplied This was fixed in master on project openstack/neutron-dynamic-routing with change id- Ief9e88ca12b04eb08b0cc4e60f9d883f1e282ae9 Since the bug was originally raised for mitaka, the original PS mentioned above is being manually backported here. Change-Id: Ib1f477c6243e5cf92143a21bc86e9759c9de4803 Closes-Bug: #1616066 --- neutron/db/bgp_db.py | 2 +- neutron/tests/unit/db/test_bgp_db.py | 37 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/neutron/db/bgp_db.py b/neutron/db/bgp_db.py index d698e800fed..3df7be19052 100644 --- a/neutron/db/bgp_db.py +++ b/neutron/db/bgp_db.py @@ -266,7 +266,7 @@ class BgpDbMixin(common_db.CommonDbMixin): bp = bgp_peer[bgp_ext.BGP_PEER_BODY_KEY_NAME] with context.session.begin(subtransactions=True): bgp_peer_db = self._get_bgp_peer(context, bgp_peer_id) - if ((bp['password'] is not None) and + if ((bp.get('password') is not None) and (bgp_peer_db['auth_type'] == 'none')): raise bgp_ext.BgpPeerNotAuthenticated(bgp_peer_id=bgp_peer_id) bgp_peer_db.update(bp) diff --git a/neutron/tests/unit/db/test_bgp_db.py b/neutron/tests/unit/db/test_bgp_db.py index 0ac4b03a0bb..593a43b4efe 100644 --- a/neutron/tests/unit/db/test_bgp_db.py +++ b/neutron/tests/unit/db/test_bgp_db.py @@ -272,6 +272,43 @@ class BgpTests(test_plugin.Ml2PluginV2TestCase, for key in args: self.assertEqual(args[key], peer[key]) + def test_update_bgp_peer_auth_type_none(self): + args = {'tenant_id': _uuid(), + 'remote_as': '1111', + 'peer_ip': '10.10.10.10', + 'auth_type': 'md5'} + with self.bgp_peer(tenant_id=args['tenant_id'], + remote_as=args['remote_as'], + peer_ip=args['peer_ip'], + auth_type='none', + name="my-peer") as peer: + data = {'bgp_peer': {'password': "my-secret", + 'name': "my-peer1"}} + self.assertRaises(bgp.BgpPeerNotAuthenticated, + self.bgp_plugin.update_bgp_peer, + self.context, peer['id'], data) + + def test_update_bgp_peer_password_none(self): + args = {'tenant_id': _uuid(), + 'remote_as': 1111, + 'peer_ip': '10.10.10.10', + 'name': 'my-peer', + 'auth_type': 'none'} + with self.bgp_peer(tenant_id=args['tenant_id'], + remote_as=args['remote_as'], + peer_ip=args['peer_ip'], + auth_type=args['auth_type'], + name=args['name']) as peer: + data = {'bgp_peer': {'name': "my-peer1"}} + updated_peer = self.bgp_plugin.update_bgp_peer(self.context, + peer['id'], + data) + for key in args: + if key == 'name': + self.assertEqual('my-peer1', updated_peer[key]) + else: + self.assertEqual(peer[key], updated_peer[key]) + def test_bgp_peer_show_non_existent(self): self.assertRaises(bgp.BgpPeerNotFound, self.bgp_plugin.get_bgp_peer,