Client and behavior changes

* Add a parameter for sha_type to the
  client and behavior. This will allow
  using sha1 or sha2 when generating
  signatures.

Change-Id: I53ad5fc19d3e1705e7591e864527de2f0e47d38a
This commit is contained in:
josh7810 2017-06-05 10:26:14 -05:00
parent 1a8e4893b6
commit 6c42ae2142
2 changed files with 18 additions and 6 deletions

View File

@ -20,7 +20,7 @@ import json
import uuid
from copy import deepcopy
from hashlib import md5, sha1
from hashlib import md5, sha1, sha256
from random import choice
from StringIO import StringIO
from time import sleep, time
@ -779,7 +779,8 @@ class ObjectStorageAPI_Behaviors(BaseBehavior):
redirect='http://example.com/formpost',
max_file_size=104857600, max_file_count=10,
expires=None, key='', signature="",
x_delete_at=None, x_delete_after=None):
x_delete_at=None, x_delete_after=None,
sha_type=None):
"""
Creates a multipart/form-data body (RFC-2388) that can be used for
POSTs to Swift.
@ -826,6 +827,9 @@ class ObjectStorageAPI_Behaviors(BaseBehavior):
@param x_delete_after: The amount of time, in seconds, after which
the object will be deleted from the container.
@type x_delete_after: int
@param sha_type: The sha algorithm to be used when generating a
signature. Currently should only be 'sha1' or 'sha2'.
@type sha_type: string
@return: Data to be POSTed in the following format:
{
@ -846,7 +850,11 @@ class ObjectStorageAPI_Behaviors(BaseBehavior):
url = ''.join([base_url, path])
hmac_body = '{0}\n{1}\n{2}\n{3}\n{4}'.format(
path, redirect, max_file_size, max_file_count, expires)
if not signature:
if not signature and sha_type == 'sha2':
signature = hmac.new(key, hmac_body, sha256).hexdigest()
elif not signature and sha_type == 'sha1':
signature = hmac.new(key, hmac_body, sha1).hexdigest()
elif not signature and not sha_type:
signature = hmac.new(key, hmac_body, sha1).hexdigest()
form = []

View File

@ -19,7 +19,7 @@ import tarfile
import urllib
from cStringIO import StringIO
from datetime import datetime
from hashlib import sha1
from hashlib import sha1, sha256
from os.path import expanduser
from time import time, mktime
from urlparse import urlparse
@ -591,7 +591,8 @@ class ObjectStorageAPIClient(HTTPClient):
return response
def create_temp_url(self, method, container, obj, seconds, key):
def create_temp_url(self, method, container, obj, seconds, key,
sha_type=None):
method = method.upper()
base_url = '{0}/{1}/{2}'.format(self.storage_url, container, obj)
account_hash = self.storage_url.split('/v1/')[1]
@ -599,7 +600,10 @@ class ObjectStorageAPIClient(HTTPClient):
seconds = int(seconds)
expires = int(time() + seconds)
hmac_body = '{0}\n{1}\n{2}'.format(method, expires, object_path)
sig = hmac.new(key, hmac_body, sha1).hexdigest()
if sha_type == 'sha2':
sig = hmac.new(key, hmac_body, sha256).hexdigest()
else:
sig = hmac.new(key, hmac_body, sha1).hexdigest()
return {'target_url': base_url, 'signature': sig, 'expires': expires}