def test_headers_amz_example(self): """ Using example from: http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html """ hdr_text = [ 'host:iam.amazonaws.com', 'Content-type:application/x-www-form-urlencoded; charset=utf-8', 'My-header1: a b c ', 'x-amz-date:20120228T030031Z', 'My-Header2: "a b c"'] headers = dict([item.split(':') for item in hdr_text]) req = requests.Request('GET', 'http://iam.amazonaws.com', headers=headers) req = req.prepare() include = list(req.headers) result = AWS4Auth.get_canonical_headers(req, include=include) cano_headers, signed_headers = result expected = [ 'content-type:application/x-www-form-urlencoded; charset=utf-8', 'host:iam.amazonaws.com', 'my-header1:a b c', 'my-header2:"a b c"', 'x-amz-date:20120228T030031Z'] expected = '\n'.join(expected) + '\n' self.assertEqual(cano_headers, expected) expected = 'content-type;host;my-header1;my-header2;x-amz-date' self.assertEqual(signed_headers, expected)
def test_amz1(self): """ Using example data selected from: http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html """ req_text = [ 'POST https://iam.amazonaws.com/ HTTP/1.1', 'Host: iam.amazonaws.com', 'Content-Length: 54', 'Content-Type: application/x-www-form-urlencoded', 'X-Amz-Date: 20110909T233600Z', '', 'Action=ListUsers&Version=2010-05-08' ] req = request_from_text('\n'.join(req_text)) AWS4Auth.encode_body(req) hsh = hashlib.sha256(req.body) req.headers['x-amz-content-sha256'] = hsh.hexdigest() include_hdrs = ['host', 'content-type', 'x-amz-date'] result = AWS4Auth.get_canonical_headers(req, include=include_hdrs) cano_headers, signed_headers = result expected = [ 'POST', '/', '', 'content-type:application/x-www-form-urlencoded', 'host:iam.amazonaws.com', 'x-amz-date:20110909T233600Z', '', 'content-type;host;x-amz-date', 'b6359072c78d70ebee1e81adcbab4f01bf2c23245fa365ef83fe8f1f95' '5085e2' ] expected = '\n'.join(expected) cano_req = AWS4Auth.get_canonical_request(req, cano_headers, signed_headers) self.assertEqual(cano_req, expected)
def test_headers_amz_example(self): """ Using example from: http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html """ hdr_text = [ 'host:iam.amazonaws.com', 'Content-type:application/x-www-form-urlencoded; charset=utf-8', 'My-header1: a b c ', 'x-amz-date:20120228T030031Z', 'My-Header2: "a b c"' ] headers = dict([item.split(':') for item in hdr_text]) req = requests.Request('GET', 'http://iam.amazonaws.com', headers=headers) req = req.prepare() include = list(req.headers) result = AWS4Auth.get_canonical_headers(req, include=include) cano_headers, signed_headers = result expected = [ 'content-type:application/x-www-form-urlencoded; charset=utf-8', 'host:iam.amazonaws.com', 'my-header1:a b c', 'my-header2:"a b c"', 'x-amz-date:20120228T030031Z' ] expected = '\n'.join(expected) + '\n' self.assertEqual(cano_headers, expected) expected = 'content-type;host;my-header1;my-header2;x-amz-date' self.assertEqual(signed_headers, expected)
def _test_amz_test_suite_item(self, group_name, group): req = request_from_text(group['.req']) if 'content-length' in req.headers: del req.headers['content-length'] include_hdrs = list(req.headers) AWS4Auth.encode_body(req) hsh = hashlib.sha256(req.body or b'') req.headers['x-amz-content-sha256'] = hsh.hexdigest() result = AWS4Auth.get_canonical_headers(req, include_hdrs) cano_headers, signed_headers = result cano_req = AWS4Auth.get_canonical_request(req, cano_headers, signed_headers) msg = 'Group: ' + group_name self.assertEqual(cano_req, group['.creq'], msg=msg)
def test_netloc_port(self): """ Test that change in d190dcb doesn't regress - strip port from netloc before generating signature when Host header is not already present in request. """ req = requests.Request('GET', 'http://amazonaws.com:8443') preq = req.prepare() self.assertNotIn('host', preq.headers) result = AWS4Auth.get_canonical_headers(preq, include=['host']) cano_hdrs, signed_hdrs = result expected = 'host:amazonaws.com\n' self.assertEqual(cano_hdrs, expected)
def test_duplicate_headers(self): """ Tests case of duplicate headers with different cased names. Uses a mock Request object with regular dict to hold headers, since Requests PreparedRequest dict is case-insensitive. """ req = SimpleNamespace() req.headers = {'ZOO': 'zoobar', 'FOO': 'zoobar', 'zoo': 'foobar', 'Content-Type': 'text/plain', 'host': 'dummy'} include = [x for x in req.headers if x != 'Content-Type'] result = AWS4Auth.get_canonical_headers(req, include=include) cano_headers, signed_headers = result cano_expected = 'foo:zoobar\nhost:dummy\nzoo:foobar,zoobar\n' signed_expected = 'foo;host;zoo' self.assertEqual(cano_headers, cano_expected) self.assertEqual(signed_headers, signed_expected)
def test_duplicate_headers(self): """ Tests case of duplicate headers with different cased names. Uses a mock Request object with regular dict to hold headers, since Requests PreparedRequest dict is case-insensitive. """ req = SimpleNamespace() req.headers = { 'ZOO': 'zoobar', 'FOO': 'zoobar', 'zoo': 'foobar', 'Content-Type': 'text/plain', 'host': 'dummy' } include = [x for x in req.headers if x != 'Content-Type'] result = AWS4Auth.get_canonical_headers(req, include=include) cano_headers, signed_headers = result cano_expected = 'foo:zoobar\nhost:dummy\nzoo:foobar,zoobar\n' signed_expected = 'foo;host;zoo' self.assertEqual(cano_headers, cano_expected) self.assertEqual(signed_headers, signed_expected)
def test_amz1(self): """ Using example data selected from: http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html """ req_text = [ 'POST https://iam.amazonaws.com/ HTTP/1.1', 'Host: iam.amazonaws.com', 'Content-Length: 54', 'Content-Type: application/x-www-form-urlencoded', 'X-Amz-Date: 20110909T233600Z', '', 'Action=ListUsers&Version=2010-05-08'] req = request_from_text('\n'.join(req_text)) AWS4Auth.encode_body(req) hsh = hashlib.sha256(req.body) req.headers['x-amz-content-sha256'] = hsh.hexdigest() include_hdrs = ['host', 'content-type', 'x-amz-date'] result = AWS4Auth.get_canonical_headers(req, include=include_hdrs) cano_headers, signed_headers = result expected = [ 'POST', '/', '', 'content-type:application/x-www-form-urlencoded', 'host:iam.amazonaws.com', 'x-amz-date:20110909T233600Z', '', 'content-type;host;x-amz-date', 'b6359072c78d70ebee1e81adcbab4f01bf2c23245fa365ef83fe8f1f95' '5085e2'] expected = '\n'.join(expected) auth = AWS4Auth('dummy', 'dummy', 'dummy', 'host') cano_req = auth.get_canonical_request(req, cano_headers, signed_headers) self.assertEqual(cano_req, expected)