libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject [3/4] libcloud git commit: Throw InvalidCredsError on invalid credentials, fix time retrieval from the RunAbove API.
Date Sat, 08 Aug 2015 19:15:14 GMT
Throw InvalidCredsError on invalid credentials, fix time retrieval from the
RunAbove API.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/9e9c6c1c
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/9e9c6c1c
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/9e9c6c1c

Branch: refs/heads/trunk
Commit: 9e9c6c1cbfc637d6897e91c632cb5cfc6b4823ec
Parents: 483b713
Author: Tomaz Muraus <tomaz@tomaz.me>
Authored: Sat Aug 8 20:47:25 2015 +0200
Committer: Tomaz Muraus <tomaz@tomaz.me>
Committed: Sat Aug 8 20:53:17 2015 +0200

----------------------------------------------------------------------
 libcloud/common/runabove.py | 41 +++++++++++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/9e9c6c1c/libcloud/common/runabove.py
----------------------------------------------------------------------
diff --git a/libcloud/common/runabove.py b/libcloud/common/runabove.py
index af839f3..4d45241 100644
--- a/libcloud/common/runabove.py
+++ b/libcloud/common/runabove.py
@@ -22,9 +22,17 @@ try:
 except ImportError:
     import json
 
+from libcloud.utils.py3 import httplib
+from libcloud.utils.connection import get_response_object
+from libcloud.common.types import InvalidCredsError
 from libcloud.common.base import ConnectionUserAndKey, JsonResponse
 from libcloud.httplib_ssl import LibcloudHTTPSConnection
 
+__all__ = [
+    'RunAboveResponse',
+    'RunAboveConnection'
+]
+
 API_HOST = 'api.runabove.com'
 API_ROOT = '/1.0'
 LOCATIONS = {
@@ -43,6 +51,17 @@ class RunAboveException(Exception):
     pass
 
 
+class RunAboveResponse(JsonResponse):
+    def parse_error(self):
+        response = super(RunAboveResponse, self).parse_body()
+
+        if response.get('errorCode', None) == 'INVALID_SIGNATURE':
+            raise InvalidCredsError('Signature validation failed, probably '
+                                    'using invalid credentials')
+
+        return self.body
+
+
 class RunAboveConnection(ConnectionUserAndKey):
     """
     A connection to the RunAbove API
@@ -52,7 +71,7 @@ class RunAboveConnection(ConnectionUserAndKey):
     """
     host = API_HOST
     request_path = API_ROOT
-    responseCls = JsonResponse
+    responseCls = RunAboveResponse
     timestamp = None
     ua = []
     LOCATIONS = LOCATIONS
@@ -83,17 +102,25 @@ class RunAboveConnection(ConnectionUserAndKey):
         }
         httpcon = LibcloudHTTPSConnection(self.host)
         httpcon.request(method='POST', url=action, body=data, headers=headers)
-        response = httpcon.getresponse().read()
-        json_response = json.loads(response)
+        response = httpcon.getresponse()
+
+        if response.status == httplib.UNAUTHORIZED:
+            raise InvalidCredsError()
+
+        body = response.read()
+        json_response = json.loads(body)
         httpcon.close()
         return json_response
 
     def get_timestamp(self):
         if not self._timedelta:
-            action = API_ROOT + '/auth/time'
-            response = self.connection.request('GET', action, headers={})
-            timestamp = int(response)
-            self._time_delta = timestamp - int(time.time())
+            url = 'https://%s/%s/auth/time' % (API_HOST, API_ROOT)
+            response = get_response_object(url=url, method='GET', headers={})
+            if not response or not response.body:
+                raise Exception('Failed to get current time from RunAbove API')
+
+            timestamp = int(response.body)
+            self._timedelta = timestamp - int(time.time())
         return int(time.time()) + self._timedelta
 
     def make_signature(self, method, action, data, timestamp):


Mime
View raw message