1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
Subject: [PATCH] [catalystcloud] Keystone requires authentication when using the /v3/ec3token endpoint
---
Index: heat/heat/api/aws/ec2token.py
===================================================================
--- heat.orig/heat/api/aws/ec2token.py
+++ heat/heat/api/aws/ec2token.py
@@ -17,9 +17,12 @@ from oslo_config import cfg
from oslo_config import types
from oslo_log import log as logging
from oslo_serialization import jsonutils as json
-import requests
import webob
+import keystoneauth1.exceptions as ks_exceptions
+import keystoneauth1.loading.conf as ks_loading
+import keystoneauth1.session as ks_session
+
from heat.api.aws import exception
from heat.common import endpoint_utils
from heat.common.i18n import _
@@ -58,6 +61,7 @@ opts = [
help=_('Timeout in seconds for HTTP requests.')),
]
cfg.CONF.register_opts(opts, group='ec2authtoken')
+ks_loading.register_conf_options(cfg.CONF, group='ec2authtoken')
class EC2Token(wsgi.Middleware):
@@ -67,6 +71,42 @@ class EC2Token(wsgi.Middleware):
self.conf = conf
self.application = app
self._ssl_options = None
+ auth_uris = [self._conf_get_auth_uri()]
+ auth_group = self._find_auth_group()
+ if self._conf_get('multi_cloud'):
+ # TODO(adrianjarvis) support config section per cloud
+ auth_uris = self._conf_get('allowed_auth_uris')
+
+ if not auth_uris:
+ LOG.error("No auth_uri found for ec2token middleware, ec2 token "
+ "validation unavailable.")
+ self._ks_sessions = {uri: self._create_ks_session(uri, auth_group)
+ for uri in auth_uris}
+
+ @staticmethod
+ def _find_auth_group():
+ # fall back to using keystone_authtoken settings.
+ if cfg.CONF.ec2authtoken.get("auth_type"):
+ return "ec2authtoken"
+ return "keystone_authtoken"
+
+ @staticmethod
+ def _create_ks_session(auth_uri, group="ec2authtoken"):
+ # load the keystone auth configuration from the group
+ # configuration section and create a keystone session. We assume
+ # the same plugin and credentials for each multicloud.
+ try:
+ auth_plugin = ks_loading.load_from_conf_options(
+ cfg.CONF, group, auth_url=auth_uri)
+ return ks_session.Session(auth=auth_plugin)
+ except ks_exceptions.AuthPluginException:
+ LOG.error(
+ "No service auth configuration found for %s. "
+ "ec2tokens API calls will be unauthenticated. "
+ "New versions of keystone require service auth." % auth_uri,
+ exc_info=True,
+ )
+ return None
def _conf_get(self, name):
# try config from paste-deploy first
@@ -185,10 +225,10 @@ class EC2Token(wsgi.Middleware):
raise exception.HeatMissingAuthenticationTokenError()
LOG.info("AWS credentials found, checking against keystone.")
-
- if not auth_uri:
- LOG.error("Ec2Token authorization failed, no auth_uri "
- "specified in config file")
+ session = self._ks_sessions.get(auth_uri)
+ if not auth_uri or not session:
+ LOG.error("Ec2Token authorization failed, incorrect ec2token "
+ "section specified in config file")
raise exception.HeatInternalFailureError(_('Service '
'misconfigured'))
# Make a copy of args for authentication and signature verification.
@@ -214,11 +254,11 @@ class EC2Token(wsgi.Middleware):
keystone_ec2_uri = self._conf_get_keystone_ec2_uri(auth_uri)
timeout = self._conf_get('timeout')
LOG.info('Authenticating with %s', keystone_ec2_uri)
- response = requests.post(keystone_ec2_uri, data=creds_json,
- headers=headers,
- verify=self.ssl_options['verify'],
- cert=self.ssl_options['cert'],
- timeout=timeout)
+ response = session.post(keystone_ec2_uri, data=creds_json,
+ headers=headers,
+ verify=self.ssl_options['verify'],
+ cert=self.ssl_options['cert'],
+ timeout=timeout)
result = response.json()
try:
token_id = response.headers['X-Subject-Token']
Index: heat/heat/tests/api/aws/test_api_ec2token.py
===================================================================
--- heat.orig/heat/tests/api/aws/test_api_ec2token.py
+++ heat/heat/tests/api/aws/test_api_ec2token.py
@@ -17,7 +17,10 @@ from unittest import mock
from oslo_config import cfg
from oslo_utils import importutils
-import requests
+
+import keystoneauth1.exceptions.auth_plugins as ks_exceptions
+import keystoneauth1.loading.conf
+import keystoneauth1.session
from heat.api.aws import ec2token
from heat.api.aws import exception
@@ -31,7 +34,11 @@ class Ec2TokenTest(common.HeatTestCase):
def setUp(self):
super(Ec2TokenTest, self).setUp()
- self.patchobject(requests, 'post')
+ self.session_cls_mock = self.patchobject(
+ keystoneauth1.session, 'Session')
+ self.session_mock = self.session_cls_mock.return_value
+ self.ks_load_from_conf_options = self.patchobject(
+ keystoneauth1.loading.conf, "load_from_conf_options")
def _dummy_GET_request(self, params=None, environ=None):
# Mangle the params dict into a query string
@@ -260,7 +267,7 @@ class Ec2TokenTest(common.HeatTestCase):
self.verify_cert = cert
self.verify_req_headers = req_headers
if direct_mock:
- requests.post.return_value = DummyHTTPResponse()
+ self.session_mock.post.return_value = DummyHTTPResponse()
else:
return DummyHTTPResponse()
@@ -285,7 +292,7 @@ class Ec2TokenTest(common.HeatTestCase):
self.assertEqual('tenant', dummy_req.headers['X-Tenant-Name'])
self.assertEqual('abcd1234', dummy_req.headers['X-Tenant-Id'])
- requests.post.assert_called_once_with(
+ self.session_mock.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,
@@ -315,7 +322,7 @@ class Ec2TokenTest(common.HeatTestCase):
self.assertEqual('woot', ec2.__call__(dummy_req))
self.assertEqual('aa,bb,cc', dummy_req.headers['X-Roles'])
- requests.post.assert_called_once_with(
+ self.session_mock.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,
@@ -341,7 +348,7 @@ class Ec2TokenTest(common.HeatTestCase):
self.assertRaises(exception.HeatInvalidClientTokenIdError,
ec2.__call__, dummy_req)
- requests.post.assert_called_once_with(
+ self.session_mock.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,
@@ -367,7 +374,7 @@ class Ec2TokenTest(common.HeatTestCase):
self.assertRaises(exception.HeatSignatureError,
ec2.__call__, dummy_req)
- requests.post.assert_called_once_with(
+ self.session_mock.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,
@@ -392,7 +399,7 @@ class Ec2TokenTest(common.HeatTestCase):
self.assertRaises(exception.HeatAccessDeniedError,
ec2.__call__, dummy_req)
- requests.post.assert_called_once_with(
+ self.session_mock.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,
@@ -413,7 +420,7 @@ class Ec2TokenTest(common.HeatTestCase):
params={'AWSAccessKeyId': 'foo'})
self.assertEqual('woot', ec2.__call__(dummy_req))
- requests.post.assert_called_once_with(
+ self.session_mock.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,
@@ -449,12 +456,12 @@ class Ec2TokenTest(common.HeatTestCase):
response=ok_resp,
params={'AWSAccessKeyId': 'foo'}, direct_mock=False)
- requests.post.side_effect = [m_p, m_p2]
+ self.session_mock.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.assertEqual(2, self.session_mock.post.call_count)
+ self.session_mock.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,
@@ -490,13 +497,13 @@ class Ec2TokenTest(common.HeatTestCase):
response=err_resp2,
params={'AWSAccessKeyId': 'foo'}, direct_mock=False)
- requests.post.side_effect = [m_p, m_p2]
+ self.session_mock.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.assertEqual(2, self.session_mock.post.call_count)
+ self.session_mock.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,
@@ -546,7 +553,7 @@ class Ec2TokenTest(common.HeatTestCase):
params={'AWSAccessKeyId': 'foo'})
self.assertEqual('woot', ec2.__call__(dummy_req))
- requests.post.assert_called_with(
+ self.session_mock.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,
@@ -570,7 +577,7 @@ class Ec2TokenTest(common.HeatTestCase):
params={'AWSAccessKeyId': 'foo'})
self.assertEqual('woot', ec2.__call__(dummy_req))
- requests.post.assert_called_with(
+ self.session_mock.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,
@@ -599,7 +606,7 @@ class Ec2TokenTest(common.HeatTestCase):
params={'AWSAccessKeyId': 'foo'})
self.assertEqual('woot', ec2.__call__(dummy_req))
- requests.post.assert_called_with(
+ self.session_mock.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,
@@ -614,3 +621,30 @@ class Ec2TokenTest(common.HeatTestCase):
ec2_filter = ec2token.EC2Token_filter_factory(global_conf={})
self.assertIsNone(ec2_filter(None).application)
+
+ def test_init_ks_session_fails(self):
+ self.ks_load_from_conf_options.side_effect = (
+ ks_exceptions.NoMatchingPlugin("password"))
+ 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', str(ex))
+
+ def test_init_ks_session_multicloud(self):
+ dummy_conf = {
+ 'allowed_auth_uris': [
+ 'http://123:5000/v2.0', 'http://456:5000/v2.0'],
+ 'multi_cloud': True
+ }
+ ec2token.EC2Token(app='woot', conf=dummy_conf)
+ self.assertEqual(2, self.session_cls_mock.call_count)
+ self.ks_load_from_conf_options.assert_has_calls([
+ mock.call(mock.ANY, mock.ANY, auth_url="http://123:5000/v2.0"),
+ mock.call(mock.ANY, mock.ANY, auth_url="http://456:5000/v2.0")]
+ )
|