From 97655c1fe633f6b1340ce899bb1ad65284926399 Mon Sep 17 00:00:00 2001
From: Davanum Srinivas <davanum@gmail.com>
Date: Sun, 28 Feb 2016 23:14:12 -0500
Subject: [PATCH] Fail quickly if there on bad password

Inspired by:
https://bugs.launchpad.net/oslo.messaging/+bug/1508512/comments/1

Add authentication_failure_close capability on the client to
tell the server that it can send us connection.close command
indicating ACCESS_REFUSED as the reasona. If this capability is
absent then authentication failures are reported in the legacy
fashion: by abruptly closing the network connection.

Partial-Bug: #1508512
Change-Id: Icf6108678256e6f922620837c71f8827fe8e8c52
---
 oslo_messaging/_drivers/impl_rabbit.py          |  4 ++++
 .../tests/drivers/test_impl_rabbit.py           | 17 +++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/oslo_messaging/_drivers/impl_rabbit.py b/oslo_messaging/_drivers/impl_rabbit.py
index 1776fb53d..4a0f76a58 100644
--- a/oslo_messaging/_drivers/impl_rabbit.py
+++ b/oslo_messaging/_drivers/impl_rabbit.py
@@ -522,6 +522,10 @@ class Connection(object):
             failover_strategy=self.kombu_failover_strategy,
             transport_options={
                 'confirm_publish': True,
+                'client_properties': {'capabilities': {
+                    'authentication_failure_close': True,
+                    'connection.blocked': True,
+                    'consumer_cancel_notify': True}},
                 'on_blocked': self._on_connection_blocked,
                 'on_unblocked': self._on_connection_unblocked,
             },
diff --git a/oslo_messaging/tests/drivers/test_impl_rabbit.py b/oslo_messaging/tests/drivers/test_impl_rabbit.py
index bb449606d..742f4d545 100644
--- a/oslo_messaging/tests/drivers/test_impl_rabbit.py
+++ b/oslo_messaging/tests/drivers/test_impl_rabbit.py
@@ -191,11 +191,20 @@ class TestRabbitDriverLoadSSL(test_utils.BaseTestCase):
 
         transport._driver._get_connection()
         connection_klass.assert_called_once_with(
-            'memory:///', transport_options={'confirm_publish': True,
-                                             'on_blocked': mock.ANY,
-                                             'on_unblocked': mock.ANY},
+            'memory:///', transport_options={
+                'client_properties': {
+                    'capabilities': {
+                        'connection.blocked': True,
+                        'consumer_cancel_notify': True,
+                        'authentication_failure_close': True
+                    }
+                },
+                'confirm_publish': True,
+                'on_blocked': mock.ANY,
+                'on_unblocked': mock.ANY},
             ssl=self.expected, login_method='AMQPLAIN',
-            heartbeat=60, failover_strategy='round-robin')
+            heartbeat=60, failover_strategy='round-robin'
+        )
 
 
 class TestRabbitPublisher(test_utils.BaseTestCase):