Last active
August 29, 2015 13:59
-
-
Save gdchamal/10847910 to your computer and use it in GitHub Desktop.
libcloud s3 using authentication version 4 protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/libcloud/storage/drivers/s3.py b/libcloud/storage/drivers/s3.py | |
index 9577f98..de15e72 100644 | |
--- a/libcloud/storage/drivers/s3.py | |
+++ b/libcloud/storage/drivers/s3.py | |
@@ -13,14 +13,16 @@ | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
-import time | |
import copy | |
import base64 | |
import hmac | |
import sys | |
- | |
from hashlib import sha1 | |
+from datetime import datetime | |
+from time import mktime | |
+from wsgiref.handlers import format_date_time | |
+ | |
try: | |
from lxml.etree import Element, SubElement | |
except ImportError: | |
@@ -100,16 +102,14 @@ class BaseS3Connection(ConnectionUserAndKey): | |
responseCls = S3Response | |
rawResponseCls = S3RawResponse | |
- def add_default_params(self, params): | |
- expires = str(int(time.time()) + EXPIRATION_SECONDS) | |
- params['AWSAccessKeyId'] = self.user_id | |
- params['Expires'] = expires | |
- return params | |
- | |
def pre_connect_hook(self, params, headers): | |
- params['Signature'] = self._get_aws_auth_param( | |
+ stamp = mktime(datetime.now().timetuple()) | |
+ date = format_date_time(stamp) | |
+ headers['date'] = date | |
+ signature = self._get_aws_auth_param( | |
method=self.method, headers=headers, params=params, | |
- expires=params['Expires'], secret_key=self.key, path=self.action) | |
+ expires=date, secret_key=self.key, path=self.action) | |
+ headers['Authorization'] = "AWS %s:%s" % (self.user_id, signature) | |
return params, headers | |
def _get_aws_auth_param(self, method, headers, params, expires, | |
@@ -170,6 +170,7 @@ class BaseS3Connection(ConnectionUserAndKey): | |
values_to_sign.append(value) | |
string_to_sign = '\n'.join(values_to_sign) | |
+ | |
b64_hmac = base64.b64encode( | |
hmac.new(b(secret_key), b(string_to_sign), digestmod=sha1).digest() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment