def test_get_signature_header_notlast(self): req_env = {'HTTP_AUTHORIZATION': ('Authorization: foo Credential=foo/bar, ' 'Signature=xyz,' 'SignedHeaders=content-type;host;x-amz-date ')} dummy_req = self._dummy_GET_request(environ=req_env) ec2 = ec2token.EC2Token(app=None, conf={}) self.assertEqual('xyz', ec2._get_signature(dummy_req))
def test_call_auth_noaccess(self): # If there's no accesskey in params or header, but there is a # Signature, we expect HeatMissingAuthenticationTokenError params = {'Signature': 'foo'} dummy_req = self._dummy_GET_request(params) ec2 = ec2token.EC2Token(app='xyz', conf={}) self.assertRaises(exception.HeatMissingAuthenticationTokenError, ec2.__call__, dummy_req)
def test_call_auth_nosig(self): req_env = {'HTTP_AUTHORIZATION': ('Authorization: foo Credential=foo/bar, ' 'SignedHeaders=content-type;host;x-amz-date')} dummy_req = self._dummy_GET_request(environ=req_env) ec2 = ec2token.EC2Token(app='xyz', conf={}) self.assertRaises(exception.HeatIncompleteSignatureError, ec2.__call__, dummy_req)
def test_conf_get_opts(self): cfg.CONF.set_default('auth_uri', 'http://192.0.2.9/v2.0/', group='ec2authtoken') ec2 = ec2token.EC2Token(app=None, conf={}) self.assertEqual(ec2._conf_get('auth_uri'), 'http://192.0.2.9/v2.0/') self.assertEqual(ec2._conf_get_keystone_ec2_uri(), 'http://192.0.2.9/v2.0/ec2tokens')
def test_call_auth_nouser(self): req_env = {'HTTP_AUTHORIZATION': ('Authorization: foo ' 'SignedHeaders=content-type;host;x-amz-date,' 'Signature=xyz')} dummy_req = self._dummy_GET_request(environ=req_env) ec2 = ec2token.EC2Token(app='xyz', conf={}) self.assertRaises(exception.HeatMissingAuthenticationTokenError, ec2.__call__, dummy_req)
def test_call_x_auth_nouser_x_auth_user(self): req_env = {'HTTP_X_AUTH_USER': '******', 'HTTP_AUTHORIZATION': ('Authorization: foo ' 'SignedHeaders=content-type;host;x-amz-date,' 'Signature=xyz')} dummy_req = self._dummy_GET_request(environ=req_env) ec2 = ec2token.EC2Token(app='xyz', conf={}) self.assertEqual(ec2.__call__(dummy_req), 'xyz')
def test_conf_get_opts(self): cfg.CONF.set_default('auth_uri', 'http://192.0.2.9/v2.0/', group='ec2authtoken') cfg.CONF.set_default('auth_uri', 'this-should-be-ignored', group='clients_keystone') ec2 = ec2token.EC2Token(app=None, conf={}) self.assertEqual('http://192.0.2.9/v2.0/', ec2._conf_get('auth_uri')) self.assertEqual( 'http://192.0.2.9/v2.0/ec2tokens', ec2._conf_get_keystone_ec2_uri('http://192.0.2.9/v2.0/'))
def test_call_badconf_no_authuri(self): ec2 = ec2token.EC2Token(app='woot', conf={}) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1'} dummy_req = self._dummy_GET_request(params, req_env) ex = self.assertRaises(exception.HeatInternalFailureError, ec2.__call__, dummy_req) self.assertEqual('Service misconfigured', six.text_type(ex))
def test_conf_get_clients_keystone_opts(self): cfg.CONF.set_default('auth_uri', None, group='ec2authtoken') cfg.CONF.set_default('auth_uri', 'http://192.0.2.9', group='clients_keystone') with mock.patch('keystoneclient.discover.Discover') as discover: class MockDiscover(object): def url_for(self, endpoint): return 'http://192.0.2.9/v3/' discover.return_value = MockDiscover() ec2 = ec2token.EC2Token(app=None, conf={}) self.assertEqual( 'http://192.0.2.9/v3/ec2tokens', ec2._conf_get_keystone_ec2_uri('http://192.0.2.9/v3/'))
def test_call_err_multicloud_none_allowed(self): dummy_conf = { 'allowed_auth_uris': [], 'multi_cloud': True } ec2 = ec2token.EC2Token(app='woot', conf=dummy_conf) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1'} dummy_req = self._dummy_GET_request(params, req_env) self.assertRaises(exception.HeatAccessDeniedError, ec2.__call__, dummy_req)
def test_conf_get_ssl_opts(self): cfg.CONF.set_default('auth_uri', 'https://192.0.2.9/v2.0/', group='ec2authtoken') cfg.CONF.set_default('ca_file', '/home/user/cacert.pem', group='ec2authtoken') cfg.CONF.set_default('insecure', 'false', group='ec2authtoken') cfg.CONF.set_default('cert_file', '/home/user/mycert', group='ec2authtoken') cfg.CONF.set_default('key_file', '/home/user/mykey', group='ec2authtoken') ec2 = ec2token.EC2Token(app=None, conf={}) self.assertEqual('/home/user/cacert.pem', ec2.ssl_options['verify']) self.assertEqual(('/home/user/mycert', '/home/user/mykey'), ec2.ssl_options['cert'])
def test_call_ok_v2(self): dummy_conf = {'auth_uri': 'http://123:5000/v2.0'} ec2 = ec2token.EC2Token(app='woot', conf=dummy_conf) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1'} dummy_req = self._dummy_GET_request(params, req_env) ok_resp = json.dumps({'token': { 'project': {'name': 'tenant', 'id': 'abcd1234'}}}) self._stub_http_connection(response=ok_resp, params={'AWSAccessKeyId': 'foo'}) self.m.ReplayAll() self.assertEqual('woot', ec2.__call__(dummy_req)) self.m.VerifyAll()
def test_call_ok_multicloud(self): dummy_conf = { 'allowed_auth_uris': ['http://123:5000/v2.0', 'http://456:5000/v2.0'], 'multi_cloud': True } ec2 = ec2token.EC2Token(app='woot', conf=dummy_conf) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = { 'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1' } dummy_req = self._dummy_GET_request(params, req_env) ok_resp = json.dumps({ 'access': { 'metadata': {}, 'token': { 'id': 123, 'tenant': { 'name': 'tenant', 'id': 'abcd1234' } } } }) err_msg = "EC2 access key not found." err_resp = json.dumps({'error': {'message': err_msg}}) # first request fails self._stub_http_connection(req_url='http://123:5000/v2.0/ec2tokens', response=err_resp, params={'AWSAccessKeyId': 'foo'}) # second request passes self._stub_http_connection(req_url='http://456:5000/v2.0/ec2tokens', response=ok_resp, params={'AWSAccessKeyId': 'foo'}) self.m.ReplayAll() self.assertEqual('woot', ec2.__call__(dummy_req)) self.m.VerifyAll()
def test_call_ok_auth_uri_ec2authtoken(self): dummy_url = 'http://123:5000/v2.0' cfg.CONF.set_default('auth_uri', dummy_url, group='ec2authtoken') ec2 = ec2token.EC2Token(app='woot', conf={}) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1'} dummy_req = self._dummy_GET_request(params, req_env) ok_resp = json.dumps({'access': {'metadata': {}, 'token': { 'id': 123, 'tenant': {'name': 'tenant', 'id': 'abcd1234'}}}}) self._stub_http_connection(response=ok_resp, params={'AWSAccessKeyId': 'foo'}) self.m.ReplayAll() self.assertEqual(ec2.__call__(dummy_req), 'woot') self.m.VerifyAll()
def test_call_ok_auth_uri_ec2authtoken_long(self): # Prove we tolerate a url which already includes the /ec2tokens path dummy_url = 'http://123:5000/v2.0/ec2tokens' cfg.CONF.set_default('auth_uri', dummy_url, group='ec2authtoken') ec2 = ec2token.EC2Token(app='woot', conf={}) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1'} dummy_req = self._dummy_GET_request(params, req_env) ok_resp = json.dumps({'token': { 'project': {'name': 'tenant', 'id': 'abcd1234'}}}) self._stub_http_connection(response=ok_resp, params={'AWSAccessKeyId': 'foo'}) self.m.ReplayAll() self.assertEqual('woot', ec2.__call__(dummy_req)) self.m.VerifyAll()
def test_call_err_denied(self): dummy_conf = {'auth_uri': 'http://123:5000/v2.0'} ec2 = ec2token.EC2Token(app='woot', conf=dummy_conf) auth_str = ('Authorization: foo Credential=foo/bar, ' 'SignedHeaders=content-type;host;x-amz-date, ' 'Signature=xyz') req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1', 'HTTP_AUTHORIZATION': auth_str} dummy_req = self._dummy_GET_request(environ=req_env) err_resp = json.dumps({}) self._stub_http_connection(headers={'Authorization': auth_str}, response=err_resp) self.m.ReplayAll() self.assertRaises(exception.HeatAccessDeniedError, ec2.__call__, dummy_req) self.m.VerifyAll()
def test_call_ok_auth_uri_ks_authtoken(self): # Import auth_token to have keystone_authtoken settings setup. importutils.import_module('keystonemiddleware.auth_token') dummy_url = 'http://123:5000/v2.0' cfg.CONF.set_override('auth_uri', dummy_url, group='keystone_authtoken') ec2 = ec2token.EC2Token(app='woot', conf={}) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1'} dummy_req = self._dummy_GET_request(params, req_env) ok_resp = json.dumps({'token': { 'project': {'name': 'tenant', 'id': 'abcd1234'}}}) self._stub_http_connection(response=ok_resp, params={'AWSAccessKeyId': 'foo'}) self.m.ReplayAll() self.assertEqual('woot', ec2.__call__(dummy_req)) self.m.VerifyAll()
def test_call_ok_auth_uri_ec2authtoken(self): dummy_url = 'http://123:5000/v2.0' cfg.CONF.set_default('auth_uri', dummy_url, group='ec2authtoken') ec2 = ec2token.EC2Token(app='woot', conf={}) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1'} dummy_req = self._dummy_GET_request(params, req_env) ok_resp = json.dumps({'token': { 'project': {'name': 'tenant', 'id': 'abcd1234'}}}) self._stub_http_connection(response=ok_resp, params={'AWSAccessKeyId': 'foo'}) self.assertEqual('woot', ec2.__call__(dummy_req)) requests.post.assert_called_with( self.verify_req_url, data=self.verify_data, verify=self.verify_verify, cert=self.verify_cert, headers=self.verify_req_headers)
def test_call_err_multicloud(self): dummy_conf = { 'allowed_auth_uris': [ 'http://123:5000/v2.0', 'http://456:5000/v2.0'], 'multi_cloud': True } ec2 = ec2token.EC2Token(app='woot', conf=dummy_conf) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1'} dummy_req = self._dummy_GET_request(params, req_env) err_resp1 = json.dumps({}) err_msg2 = "EC2 access key not found." err_resp2 = json.dumps({'error': {'message': err_msg2}}) # first request fails with HeatAccessDeniedError m_p = self._stub_http_connection( req_url='http://123:5000/v2.0/ec2tokens', response=err_resp1, params={'AWSAccessKeyId': 'foo'}, direct_mock=False) # second request fails with HeatInvalidClientTokenIdError m_p2 = self._stub_http_connection( req_url='http://456:5000/v2.0/ec2tokens', response=err_resp2, params={'AWSAccessKeyId': 'foo'}, direct_mock=False) requests.post.side_effect = [m_p, m_p2] # raised error matches last failure self.assertRaises(exception.HeatInvalidClientTokenIdError, ec2.__call__, dummy_req) self.assertEqual(2, requests.post.call_count) requests.post.assert_called_with( self.verify_req_url, data=self.verify_data, verify=self.verify_verify, cert=self.verify_cert, headers=self.verify_req_headers)
def test_call_ok_multicloud(self): dummy_conf = { 'allowed_auth_uris': [ 'http://123:5000/v2.0', 'http://456:5000/v2.0'], 'multi_cloud': True } ec2 = ec2token.EC2Token(app='woot', conf=dummy_conf) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1'} dummy_req = self._dummy_GET_request(params, req_env) ok_resp = json.dumps({'token': { 'project': {'name': 'tenant', 'id': 'abcd1234'}}}) err_msg = "EC2 access key not found." err_resp = json.dumps({'error': {'message': err_msg}}) # first request fails m_p = self._stub_http_connection( req_url='http://123:5000/v2.0/ec2tokens', response=err_resp, params={'AWSAccessKeyId': 'foo'}, direct_mock=False) # second request passes m_p2 = self._stub_http_connection( req_url='http://456:5000/v2.0/ec2tokens', response=ok_resp, params={'AWSAccessKeyId': 'foo'}, direct_mock=False) requests.post.side_effect = [m_p, m_p2] self.assertEqual('woot', ec2.__call__(dummy_req)) self.assertEqual(2, requests.post.call_count) requests.post.assert_called_with( self.verify_req_url, data=self.verify_data, verify=self.verify_verify, cert=self.verify_cert, headers=self.verify_req_headers)
def test_call_ok_roles(self): dummy_conf = {'auth_uri': 'http://123:5000/v2.0'} ec2 = ec2token.EC2Token(app='woot', conf=dummy_conf) auth_str = ('Authorization: foo Credential=foo/bar, ' 'SignedHeaders=content-type;host;x-amz-date, ' 'Signature=xyz') req_env = { 'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1', 'HTTP_AUTHORIZATION': auth_str } dummy_req = self._dummy_GET_request(environ=req_env) ok_resp = json.dumps({ 'token': { 'id': 123, 'project': { 'name': 'tenant', 'id': 'abcd1234' }, 'roles': [{ 'name': 'aa' }, { 'name': 'bb' }, { 'name': 'cc' }] } }) self._stub_http_connection(headers={'Authorization': auth_str}, response=ok_resp) self.m.ReplayAll() self.assertEqual('woot', ec2.__call__(dummy_req)) self.assertEqual('aa,bb,cc', dummy_req.headers['X-Roles']) self.m.VerifyAll()
def test_call_err_signature(self): dummy_conf = {'auth_uri': 'http://123:5000/v2.0'} ec2 = ec2token.EC2Token(app='woot', conf=dummy_conf) auth_str = ('Authorization: foo Credential=foo/bar, ' 'SignedHeaders=content-type;host;x-amz-date, ' 'Signature=xyz') req_env = {'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1', 'HTTP_AUTHORIZATION': auth_str} dummy_req = self._dummy_GET_request(environ=req_env) err_msg = "EC2 signature not supplied." err_resp = json.dumps({'error': {'message': err_msg}}) self._stub_http_connection(headers={'Authorization': auth_str}, response=err_resp) self.assertRaises(exception.HeatSignatureError, ec2.__call__, dummy_req) requests.post.assert_called_once_with( self.verify_req_url, data=self.verify_data, verify=self.verify_verify, cert=self.verify_cert, headers=self.verify_req_headers)
def test_call_err_multicloud(self): dummy_conf = { 'allowed_auth_uris': ['http://123:5000/v2.0', 'http://456:5000/v2.0'], 'multi_cloud': True } ec2 = ec2token.EC2Token(app='woot', conf=dummy_conf) params = {'AWSAccessKeyId': 'foo', 'Signature': 'xyz'} req_env = { 'SERVER_NAME': 'heat', 'SERVER_PORT': '8000', 'PATH_INFO': '/v1' } dummy_req = self._dummy_GET_request(params, req_env) err_resp1 = json.dumps({}) err_msg2 = "EC2 access key not found." err_resp2 = json.dumps({'error': {'message': err_msg2}}) # first request fails with HeatAccessDeniedError self._stub_http_connection(req_url='http://123:5000/v2.0/ec2tokens', response=err_resp1, params={'AWSAccessKeyId': 'foo'}) # second request fails with HeatInvalidClientTokenIdError self._stub_http_connection(req_url='http://456:5000/v2.0/ec2tokens', response=err_resp2, params={'AWSAccessKeyId': 'foo'}) self.m.ReplayAll() # raised error matches last failure self.assertRaises(exception.HeatInvalidClientTokenIdError, ec2.__call__, dummy_req) self.m.VerifyAll()
def test_conf_get_ssl_default_options(self): ec2 = ec2token.EC2Token(app=None, conf={}) self.assertTrue(ec2.ssl_options['verify'], "SSL verify should be True by default") self.assertIsNone(ec2.ssl_options['cert'], "SSL client cert should be None by default")
def test_call_x_auth_user(self): req_env = {'HTTP_X_AUTH_USER': '******'} dummy_req = self._dummy_GET_request(environ=req_env) ec2 = ec2token.EC2Token(app='xyz', conf={}) self.assertEqual('xyz', ec2.__call__(dummy_req))
def test_get_access_param_new(self): params = {'X-Amz-Credential': 'foo/bar'} dummy_req = self._dummy_GET_request(params) ec2 = ec2token.EC2Token(app=None, conf={}) self.assertEqual('foo', ec2._get_access(dummy_req))
def test_get_access_param_old(self): params = {'AWSAccessKeyId': 'foo'} dummy_req = self._dummy_GET_request(params) ec2 = ec2token.EC2Token(app=None, conf={}) self.assertEqual('foo', ec2._get_access(dummy_req))
def test_conf_get_opts(self): cfg.CONF.set_default('auth_uri', 'abc', group='ec2authtoken') cfg.CONF.set_default('keystone_ec2_uri', 'xyz', group='ec2authtoken') ec2 = ec2token.EC2Token(app=None, conf={}) self.assertEqual(ec2._conf_get('auth_uri'), 'abc') self.assertEqual(ec2._conf_get('keystone_ec2_uri'), 'xyz')
def test_get_signature_param_new(self): params = {'X-Amz-Signature': 'foo'} dummy_req = self._dummy_GET_request(params) ec2 = ec2token.EC2Token(app=None, conf={}) self.assertEqual('foo', ec2._get_signature(dummy_req))
def test_conf_ssl_insecure_option(self): ec2 = ec2token.EC2Token(app=None, conf={}) cfg.CONF.set_default('insecure', 'True', group='ec2authtoken') cfg.CONF.set_default('ca_file', None, group='ec2authtoken') self.assertFalse(ec2.ssl_options['verify'])