From 3265ad52414a56edb56d278a890e239d2fe03155 Mon Sep 17 00:00:00 2001
From: Donagh McCabe <donagh.mccabe@hp.com>
Date: Mon, 17 Sep 2012 16:42:16 +0100
Subject: [PATCH] Catch authorization failures

Catch the most common authorization exceptions.

Fixes bug 1048560

Change-Id: I81c562d6093e94e827e6583dcb31db8408980476
---
 swiftclient/client.py     | 16 +++++++++++-----
 tests/test_swiftclient.py |  9 +++++++++
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/swiftclient/client.py b/swiftclient/client.py
index cac480b9..4c5e2099 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -237,11 +237,17 @@ def get_keystoneclient_2_0(auth_url, user, key, os_options):
     """
     from keystoneclient.v2_0 import client as ksclient
     from keystoneclient import exceptions
-    _ksclient = ksclient.Client(username=user,
-                                password=key,
-                                tenant_name=os_options.get('tenant_name'),
-                                tenant_id=os_options.get('tenant_id'),
-                                auth_url=auth_url)
+    try:
+        _ksclient = ksclient.Client(username=user,
+                                    password=key,
+                                    tenant_name=os_options.get('tenant_name'),
+                                    tenant_id=os_options.get('tenant_id'),
+                                    auth_url=auth_url)
+    except exceptions.Unauthorized:
+        raise ClientException('Unauthorised. Check username, password'
+                              ' and tenant name/id')
+    except exceptions.AuthorizationFailure, err:
+        raise ClientException('Authorization Failure. %s' % err)
     service_type = os_options.get('service_type') or 'object-store'
     endpoint_type = os_options.get('endpoint_type') or 'publicURL'
     try:
diff --git a/tests/test_swiftclient.py b/tests/test_swiftclient.py
index 08bf6cc8..d8d385ac 100644
--- a/tests/test_swiftclient.py
+++ b/tests/test_swiftclient.py
@@ -240,6 +240,15 @@ class TestGetAuth(MockHttpTest):
                           'http://www.tests.com', 'asdf', 'asdf',
                           os_options=os_options, auth_version='2.0')
 
+    def test_auth_v2_ks_exception(self):
+        c.get_keystoneclient_2_0 = fake_get_keystoneclient_2_0(
+                                       {},
+                                       c.ClientException)
+        self.assertRaises(c.ClientException, c.get_auth,
+                          'http://www.tests.com', 'asdf', 'asdf',
+                          os_options={},
+                          auth_version='2.0')
+
 class TestGetAccount(MockHttpTest):
 
     def test_no_content(self):