-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- encoding: utf-8 -*- | ||
import mock | ||
|
||
from opensearchsdk.utils import prepare_url | ||
from opensearchsdk.tests import base | ||
|
||
|
||
COMMON_PARAMS = {'Version': 'v2', | ||
'AccessKeyId': 'testid', | ||
'SignatureMethod': 'HMAC-SHA1', | ||
'SignatureVersion': '1.0', | ||
'SignatureNonce': '14053016951271226', | ||
'Timestamp': '2014-07-14T01:34:55Z' | ||
} | ||
BODY = {'query': "config=format:json,start:0,hit:20&&query:'的'", | ||
'index_name': 'ut_3885312', | ||
'format': 'json', | ||
'fetch_fields': 'title;gmt_modified'} | ||
METHOD = 'GET' | ||
KEY_ID = 'testid' | ||
KEY = 'testsecret' | ||
SIGNATURE = "fxGidmIYSsx2AMa8onxuavOijuE=" | ||
STEP_1 = 'AccessKeyId=testid&SignatureMethod=HMAC-SHA1&SignatureNonce=1405' \ | ||
'3016951271226&SignatureVersion=1.0&Timestamp=2014-07-14T01%3A34%' \ | ||
'3A55Z&Version=v2&fetch_fields=title%3Bgmt_modified&format=json&i' \ | ||
'ndex_name=ut_3885312&query=config%3Dformat%3Ajson%2Cstart%3A0%2C' \ | ||
'hit%3A20%26%26query%3A%27%E7%9A%84%27' | ||
STEP_2 = "GET&%2F&AccessKeyId%3Dtestid%26SignatureMethod%3DHMAC-SHA1%26Signatu" \ | ||
"reNonce%3D14053016951271226%26SignatureVersion%3D1.0%26Timestamp%3D2" \ | ||
"014-07-14T01%253A34%253A55Z%26Version%3Dv2%26fetch_fields%3Dtitle%25" \ | ||
"3Bgmt_modified%26format%3Djson%26index_name%3Dut_3885312%26query%3Dc" \ | ||
"onfig%253Dformat%253Ajson%252Cstart%253A0%252Chit%253A20%2526%2526qu" \ | ||
"ery%253A%2527%25E7%259A%2584%2527" | ||
SAFE = {' ': '%20', '~': '~'} | ||
|
||
|
||
class TokenTest(base.TestCase): | ||
|
||
def test_get_common_params(self): | ||
common_params = prepare_url.get_common_params(KEY_ID) | ||
self.assertEqual(6, len(common_params)) | ||
self.assertEqual(COMMON_PARAMS['Version'], common_params['Version']) | ||
self.assertEqual( | ||
COMMON_PARAMS['AccessKeyId'], common_params['AccessKeyId']) | ||
self.assertEqual( | ||
COMMON_PARAMS['SignatureMethod'], common_params['SignatureMethod']) | ||
self.assertEqual( | ||
COMMON_PARAMS['SignatureVersion'], | ||
common_params['SignatureVersion']) | ||
self.assertEqual(20, len(common_params['Timestamp'])) | ||
|
||
def test_url_quote(self): | ||
for k, v in SAFE.items(): | ||
quoted = prepare_url.url_quote(k) | ||
self.assertEqual(v, quoted) | ||
|
||
def test_get_quote_body(self): | ||
body = BODY | ||
body.update(COMMON_PARAMS) | ||
quoted_body = prepare_url.get_quote_body(body) | ||
self.assertEqual(STEP_1, quoted_body) | ||
|
||
def test_get_str_to_sign(self): | ||
str_to_sign = prepare_url.get_str_to_sign(STEP_1, METHOD) | ||
self.assertEqual(STEP_2, str_to_sign) | ||
|
||
def test_sign_str(self): | ||
signature = prepare_url.sign_str(KEY, STEP_2) | ||
self.assertEqual(SIGNATURE, signature) | ||
|
||
@mock.patch('opensearchsdk.utils.prepare_url.get_common_params', | ||
mock.Mock(return_value=COMMON_PARAMS)) | ||
def test_get_signature(self): | ||
signature = prepare_url.get_signature(METHOD, BODY, KEY, KEY_ID) | ||
self.assertEqual(SIGNATURE, signature) |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding:utf-8 -*- | ||
import base64 | ||
import hashlib | ||
import hmac | ||
import sys | ||
import time | ||
import urllib | ||
import uuid | ||
|
||
|
||
def url_quote(encode_str): | ||
if sys.stdin.encoding is None: | ||
res = urllib.quote(encode_str.decode('cp936').encode('utf8'), '~') | ||
else: | ||
res = urllib.quote( | ||
encode_str.decode(sys.stdin.encoding).encode('utf8'), '~') | ||
res = res.replace('+', '%20') | ||
res = res.replace('*', '%2A') | ||
return res | ||
|
||
|
||
def get_common_params(key_id): | ||
params = dict(Version="v2", | ||
SignatureMethod="HMAC-SHA1", | ||
SignatureVersion="1.0", | ||
Timestamp=time.strftime( | ||
"%Y-%m-%dT%H:%M:%SZ", time.gmtime()), | ||
SignatureNonce=str(uuid.uuid4()), | ||
AccessKeyId=key_id | ||
) | ||
return params | ||
|
||
|
||
def get_quote_body(body): | ||
items = body.items() | ||
items.sort() | ||
encode_str = '' | ||
for k, v in items: | ||
encode_str += url_quote(k) + '=' + url_quote(v) + '&' | ||
encode_str = encode_str[:-1] | ||
return encode_str | ||
|
||
|
||
def get_str_to_sign(encode_str, method): | ||
encode_str = url_quote(encode_str) | ||
to_sign = method + '&%2F&' + encode_str | ||
return to_sign | ||
|
||
|
||
def sign_str(key, str_to_sign): | ||
key += '&' | ||
sign = hmac.new(key, str_to_sign, hashlib.sha1).digest() | ||
signature = base64.encodestring(sign).strip() | ||
return signature | ||
|
||
|
||
def get_signature(method, body, key, key_id): | ||
common_params = get_common_params(key_id) | ||
body.update(common_params) | ||
quote_body = get_quote_body(body) | ||
str_to_sign = get_str_to_sign(quote_body, method) | ||
signature = sign_str(key, str_to_sign) | ||
return signature |