OpenStack Identity (Keystone) Middleware
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_auth_token_middleware.py 110KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734
  1. # Copyright 2012 OpenStack Foundation
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import datetime
  15. import os
  16. import shutil
  17. import stat
  18. import tempfile
  19. import time
  20. import uuid
  21. import fixtures
  22. from keystoneauth1 import exceptions as ksa_exceptions
  23. from keystoneauth1 import fixture
  24. from keystoneauth1 import loading
  25. from keystoneauth1 import session
  26. from keystoneclient.common import cms
  27. from keystoneclient import exceptions as ksc_exceptions
  28. import mock
  29. import oslo_cache
  30. from oslo_log import log as logging
  31. from oslo_serialization import jsonutils
  32. from oslo_utils import timeutils
  33. import pbr.version
  34. import six
  35. import testresources
  36. import testtools
  37. from testtools import matchers
  38. import webob
  39. import webob.dec
  40. from keystonemiddleware import auth_token
  41. from keystonemiddleware.auth_token import _base
  42. from keystonemiddleware.auth_token import _cache
  43. from keystonemiddleware.auth_token import _exceptions as ksm_exceptions
  44. from keystonemiddleware.auth_token import _revocations
  45. from keystonemiddleware.tests.unit.auth_token import base
  46. from keystonemiddleware.tests.unit import client_fixtures
  47. EXPECTED_V2_DEFAULT_ENV_RESPONSE = {
  48. 'HTTP_X_IDENTITY_STATUS': 'Confirmed',
  49. 'HTTP_X_TENANT_ID': 'tenant_id1',
  50. 'HTTP_X_TENANT_NAME': 'tenant_name1',
  51. 'HTTP_X_USER_ID': 'user_id1',
  52. 'HTTP_X_USER_NAME': 'user_name1',
  53. 'HTTP_X_ROLES': 'role1,role2',
  54. 'HTTP_X_IS_ADMIN_PROJECT': 'True',
  55. 'HTTP_X_USER': 'user_name1', # deprecated (diablo-compat)
  56. 'HTTP_X_TENANT': 'tenant_name1', # deprecated (diablo-compat)
  57. 'HTTP_X_ROLE': 'role1,role2', # deprecated (diablo-compat)
  58. }
  59. EXPECTED_V2_DEFAULT_SERVICE_ENV_RESPONSE = {
  60. 'HTTP_X_SERVICE_IDENTITY_STATUS': 'Confirmed',
  61. 'HTTP_X_SERVICE_PROJECT_ID': 'service_project_id1',
  62. 'HTTP_X_SERVICE_PROJECT_NAME': 'service_project_name1',
  63. 'HTTP_X_SERVICE_USER_ID': 'service_user_id1',
  64. 'HTTP_X_SERVICE_USER_NAME': 'service_user_name1',
  65. 'HTTP_X_SERVICE_ROLES': 'service,service_role2',
  66. }
  67. EXPECTED_V3_DEFAULT_ENV_ADDITIONS = {
  68. 'HTTP_X_PROJECT_DOMAIN_ID': 'domain_id1',
  69. 'HTTP_X_PROJECT_DOMAIN_NAME': 'domain_name1',
  70. 'HTTP_X_USER_DOMAIN_ID': 'domain_id1',
  71. 'HTTP_X_USER_DOMAIN_NAME': 'domain_name1',
  72. 'HTTP_X_IS_ADMIN_PROJECT': 'True'
  73. }
  74. EXPECTED_V3_DEFAULT_SERVICE_ENV_ADDITIONS = {
  75. 'HTTP_X_SERVICE_PROJECT_DOMAIN_ID': 'service_domain_id1',
  76. 'HTTP_X_SERVICE_PROJECT_DOMAIN_NAME': 'service_domain_name1',
  77. 'HTTP_X_SERVICE_USER_DOMAIN_ID': 'service_domain_id1',
  78. 'HTTP_X_SERVICE_USER_DOMAIN_NAME': 'service_domain_name1'
  79. }
  80. BASE_HOST = 'https://keystone.example.com:1234'
  81. BASE_URI = '%s/testadmin' % BASE_HOST
  82. FAKE_ADMIN_TOKEN_ID = 'admin_token2'
  83. FAKE_ADMIN_TOKEN = jsonutils.dumps(
  84. {'access': {'token': {'id': FAKE_ADMIN_TOKEN_ID,
  85. 'expires': '2022-10-03T16:58:01Z'}}})
  86. VERSION_LIST_v3 = fixture.DiscoveryList(href=BASE_URI)
  87. VERSION_LIST_v2 = fixture.DiscoveryList(v3=False, href=BASE_URI)
  88. ERROR_TOKEN = '7ae290c2a06244c4b41692eb4e9225f2'
  89. TIMEOUT_TOKEN = '4ed1c5e53beee59458adcf8261a8cae2'
  90. def cleanup_revoked_file(filename):
  91. try:
  92. os.remove(filename)
  93. except OSError:
  94. pass
  95. def strtime(at=None):
  96. at = at or timeutils.utcnow()
  97. return at.strftime(timeutils.PERFECT_TIME_FORMAT)
  98. class TimezoneFixture(fixtures.Fixture):
  99. @staticmethod
  100. def supported():
  101. # tzset is only supported on Unix.
  102. return hasattr(time, 'tzset')
  103. def __init__(self, new_tz):
  104. super(TimezoneFixture, self).__init__()
  105. self.tz = new_tz
  106. self.old_tz = os.environ.get('TZ')
  107. def setUp(self):
  108. super(TimezoneFixture, self).setUp()
  109. if not self.supported():
  110. raise NotImplementedError('timezone override is not supported.')
  111. os.environ['TZ'] = self.tz
  112. time.tzset()
  113. self.addCleanup(self.cleanup)
  114. def cleanup(self):
  115. if self.old_tz is not None:
  116. os.environ['TZ'] = self.old_tz
  117. elif 'TZ' in os.environ:
  118. del os.environ['TZ']
  119. time.tzset()
  120. class TimeFixture(fixtures.Fixture):
  121. def __init__(self, new_time, normalize=True):
  122. super(TimeFixture, self).__init__()
  123. if isinstance(new_time, six.string_types):
  124. new_time = timeutils.parse_isotime(new_time)
  125. if normalize:
  126. new_time = timeutils.normalize_time(new_time)
  127. self.new_time = new_time
  128. def setUp(self):
  129. super(TimeFixture, self).setUp()
  130. timeutils.set_time_override(self.new_time)
  131. self.addCleanup(timeutils.clear_time_override)
  132. class FakeApp(object):
  133. """This represents a WSGI app protected by the auth_token middleware."""
  134. SUCCESS = b'SUCCESS'
  135. FORBIDDEN = b'FORBIDDEN'
  136. expected_env = {}
  137. def __init__(self, expected_env=None, need_service_token=False):
  138. self.expected_env = dict(EXPECTED_V2_DEFAULT_ENV_RESPONSE)
  139. if expected_env:
  140. self.expected_env.update(expected_env)
  141. self.need_service_token = need_service_token
  142. @webob.dec.wsgify
  143. def __call__(self, req):
  144. for k, v in self.expected_env.items():
  145. assert req.environ[k] == v, '%s != %s' % (req.environ[k], v)
  146. resp = webob.Response()
  147. if (req.environ.get('HTTP_X_IDENTITY_STATUS') == 'Invalid' and
  148. req.environ['HTTP_X_SERVICE_IDENTITY_STATUS'] == 'Invalid'):
  149. # Simulate delayed auth forbidding access with arbitrary status
  150. # code to differentiate checking this code path
  151. resp.status = 419
  152. resp.body = FakeApp.FORBIDDEN
  153. elif req.environ.get('HTTP_X_SERVICE_IDENTITY_STATUS') == 'Invalid':
  154. # Simulate delayed auth forbidding access with arbitrary status
  155. # code to differentiate checking this code path
  156. resp.status = 420
  157. resp.body = FakeApp.FORBIDDEN
  158. elif req.environ['HTTP_X_IDENTITY_STATUS'] == 'Invalid':
  159. # Simulate delayed auth forbidding access
  160. resp.status = 403
  161. resp.body = FakeApp.FORBIDDEN
  162. elif (self.need_service_token is True and
  163. req.environ.get('HTTP_X_SERVICE_TOKEN') is None):
  164. # Simulate requiring composite auth
  165. # Arbitrary value to allow checking this code path
  166. resp.status = 418
  167. resp.body = FakeApp.FORBIDDEN
  168. else:
  169. resp.body = FakeApp.SUCCESS
  170. return resp
  171. class v3FakeApp(FakeApp):
  172. """This represents a v3 WSGI app protected by the auth_token middleware."""
  173. def __init__(self, expected_env=None, need_service_token=False):
  174. # with v3 additions, these are for the DEFAULT TOKEN
  175. v3_default_env_additions = dict(EXPECTED_V3_DEFAULT_ENV_ADDITIONS)
  176. if expected_env:
  177. v3_default_env_additions.update(expected_env)
  178. super(v3FakeApp, self).__init__(expected_env=v3_default_env_additions,
  179. need_service_token=need_service_token)
  180. class CompositeBase(object):
  181. """Base composite auth object with common service token environment."""
  182. def __init__(self, expected_env=None):
  183. comp_expected_env = dict(EXPECTED_V2_DEFAULT_SERVICE_ENV_RESPONSE)
  184. if expected_env:
  185. comp_expected_env.update(expected_env)
  186. super(CompositeBase, self).__init__(
  187. expected_env=comp_expected_env, need_service_token=True)
  188. class CompositeFakeApp(CompositeBase, FakeApp):
  189. """A fake v2 WSGI app protected by composite auth_token middleware."""
  190. def __init__(self, expected_env):
  191. super(CompositeFakeApp, self).__init__(expected_env=expected_env)
  192. class v3CompositeFakeApp(CompositeBase, v3FakeApp):
  193. """A fake v3 WSGI app protected by composite auth_token middleware."""
  194. def __init__(self, expected_env=None):
  195. # with v3 additions, these are for the DEFAULT SERVICE TOKEN
  196. v3_default_service_env_additions = dict(
  197. EXPECTED_V3_DEFAULT_SERVICE_ENV_ADDITIONS)
  198. if expected_env:
  199. v3_default_service_env_additions.update(expected_env)
  200. super(v3CompositeFakeApp, self).__init__(
  201. v3_default_service_env_additions)
  202. class FakeOsloCache(_cache._FakeClient):
  203. """A fake oslo_cache object.
  204. The memcache and oslo_cache interfaces are almost the same except we need
  205. to return NO_VALUE when not found.
  206. """
  207. def get(self, key):
  208. return super(FakeOsloCache, self).get(key) or oslo_cache.NO_VALUE
  209. class BaseAuthTokenMiddlewareTest(base.BaseAuthTokenTestCase):
  210. """Base test class for auth_token middleware.
  211. All the tests allow for running with auth_token
  212. configured for receiving v2 or v3 tokens, with the
  213. choice being made by passing configuration data into
  214. setUp().
  215. The base class will, by default, run all the tests
  216. expecting v2 token formats. Child classes can override
  217. this to specify, for instance, v3 format.
  218. """
  219. def setUp(self, expected_env=None, auth_version=None, fake_app=None):
  220. super(BaseAuthTokenMiddlewareTest, self).setUp()
  221. self.logger = self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
  222. # the default oslo_cache is null cache, always use an in-mem cache
  223. self.useFixture(fixtures.MockPatchObject(auth_token.AuthProtocol,
  224. '_create_oslo_cache',
  225. return_value=FakeOsloCache()))
  226. self.expected_env = expected_env or dict()
  227. self.fake_app = fake_app or FakeApp
  228. self.middleware = None
  229. signing_dir = self._setup_signing_directory()
  230. self.conf = {
  231. 'identity_uri': 'https://keystone.example.com:1234/testadmin/',
  232. 'signing_dir': signing_dir,
  233. 'auth_version': auth_version,
  234. 'www_authenticate_uri': 'https://keystone.example.com:1234',
  235. 'admin_user': uuid.uuid4().hex,
  236. }
  237. self.auth_version = auth_version
  238. self.response_status = None
  239. self.response_headers = None
  240. def call_middleware(self, **kwargs):
  241. return self.call(self.middleware, **kwargs)
  242. def _setup_signing_directory(self):
  243. directory_name = self.useFixture(fixtures.TempDir()).path
  244. # Copy the sample certificate files into the temporary directory.
  245. for filename in ['cacert.pem', 'signing_cert.pem', ]:
  246. shutil.copy2(os.path.join(client_fixtures.CERTDIR, filename),
  247. os.path.join(directory_name, filename))
  248. return directory_name
  249. def set_middleware(self, expected_env=None, conf=None):
  250. """Configure the class ready to call the auth_token middleware.
  251. Set up the various fake items needed to run the middleware.
  252. Individual tests that need to further refine these can call this
  253. function to override the class defaults.
  254. """
  255. if conf:
  256. self.conf.update(conf)
  257. if expected_env:
  258. self.expected_env.update(expected_env)
  259. self.middleware = auth_token.AuthProtocol(
  260. self.fake_app(self.expected_env), self.conf)
  261. self.middleware._revocations._list = jsonutils.dumps(
  262. {"revoked": [], "extra": "success"})
  263. def update_expected_env(self, expected_env={}):
  264. self.middleware._app.expected_env.update(expected_env)
  265. def purge_token_expected_env(self):
  266. for key in six.iterkeys(self.token_expected_env):
  267. del self.middleware._app.expected_env[key]
  268. def purge_service_token_expected_env(self):
  269. for key in six.iterkeys(self.service_token_expected_env):
  270. del self.middleware._app.expected_env[key]
  271. def assertLastPath(self, path):
  272. if path:
  273. self.assertEqual(BASE_URI + path,
  274. self.requests_mock.last_request.url)
  275. else:
  276. self.assertIsNone(self.requests_mock.last_request)
  277. class DiabloAuthTokenMiddlewareTest(BaseAuthTokenMiddlewareTest,
  278. testresources.ResourcedTestCase):
  279. resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)]
  280. """Auth Token middleware should understand Diablo keystone responses."""
  281. def setUp(self):
  282. # pre-diablo only had Tenant ID, which was also the Name
  283. expected_env = {
  284. 'HTTP_X_TENANT_ID': 'tenant_id1',
  285. 'HTTP_X_TENANT_NAME': 'tenant_id1',
  286. # now deprecated (diablo-compat)
  287. 'HTTP_X_TENANT': 'tenant_id1',
  288. }
  289. super(DiabloAuthTokenMiddlewareTest, self).setUp(
  290. expected_env=expected_env)
  291. self.requests_mock.get(BASE_URI,
  292. json=VERSION_LIST_v2,
  293. status_code=300)
  294. self.requests_mock.post("%s/v2.0/tokens" % BASE_URI,
  295. text=FAKE_ADMIN_TOKEN)
  296. self.token_id = self.examples.VALID_DIABLO_TOKEN
  297. token_response = self.examples.JSON_TOKEN_RESPONSES[self.token_id]
  298. url = "%s/v2.0/tokens/%s" % (BASE_URI, self.token_id)
  299. self.requests_mock.get(url, text=token_response)
  300. self.set_middleware()
  301. def test_valid_diablo_response(self):
  302. resp = self.call_middleware(headers={'X-Auth-Token': self.token_id})
  303. self.assertIn('keystone.token_info', resp.request.environ)
  304. class CachePoolTest(BaseAuthTokenMiddlewareTest):
  305. def test_use_cache_from_env(self):
  306. # If `swift.cache` is set in the environment and `cache` is set in the
  307. # config then the env cache is used.
  308. env = {'swift.cache': 'CACHE_TEST'}
  309. conf = {
  310. 'cache': 'swift.cache'
  311. }
  312. self.set_middleware(conf=conf)
  313. self.middleware._token_cache.initialize(env)
  314. with self.middleware._token_cache._cache_pool.reserve() as cache:
  315. self.assertEqual(cache, 'CACHE_TEST')
  316. def test_not_use_cache_from_env(self):
  317. # If `swift.cache` is set in the environment but `cache` isn't set
  318. # initialize the config then the env cache isn't used.
  319. self.set_middleware()
  320. env = {'swift.cache': 'CACHE_TEST'}
  321. self.middleware._token_cache.initialize(env)
  322. with self.middleware._token_cache._cache_pool.reserve() as cache:
  323. self.assertNotEqual(cache, 'CACHE_TEST')
  324. def test_multiple_context_managers_share_single_client(self):
  325. self.set_middleware()
  326. token_cache = self.middleware._token_cache
  327. env = {}
  328. token_cache.initialize(env)
  329. caches = []
  330. with token_cache._cache_pool.reserve() as cache:
  331. caches.append(cache)
  332. with token_cache._cache_pool.reserve() as cache:
  333. caches.append(cache)
  334. self.assertIs(caches[0], caches[1])
  335. self.assertEqual(set(caches), set(token_cache._cache_pool))
  336. def test_nested_context_managers_create_multiple_clients(self):
  337. self.set_middleware()
  338. env = {}
  339. self.middleware._token_cache.initialize(env)
  340. token_cache = self.middleware._token_cache
  341. with token_cache._cache_pool.reserve() as outer_cache:
  342. with token_cache._cache_pool.reserve() as inner_cache:
  343. self.assertNotEqual(outer_cache, inner_cache)
  344. self.assertEqual(
  345. set([inner_cache, outer_cache]),
  346. set(token_cache._cache_pool))
  347. class GeneralAuthTokenMiddlewareTest(BaseAuthTokenMiddlewareTest,
  348. testresources.ResourcedTestCase):
  349. """General Token Behavior tests.
  350. These tests are not affected by the token format
  351. (see CommonAuthTokenMiddlewareTest).
  352. """
  353. resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)]
  354. def test_fixed_cache_key_length(self):
  355. self.set_middleware()
  356. short_string = uuid.uuid4().hex
  357. long_string = 8 * uuid.uuid4().hex
  358. token_cache = self.middleware._token_cache
  359. hashed_short_string_key, context_ = token_cache._get_cache_key(
  360. short_string)
  361. hashed_long_string_key, context_ = token_cache._get_cache_key(
  362. long_string)
  363. # The hash keys should always match in length
  364. self.assertThat(hashed_short_string_key,
  365. matchers.HasLength(len(hashed_long_string_key)))
  366. def test_config_revocation_cache_timeout(self):
  367. conf = {
  368. 'revocation_cache_time': '24',
  369. 'www_authenticate_uri': 'https://keystone.example.com:1234',
  370. 'admin_user': uuid.uuid4().hex
  371. }
  372. middleware = auth_token.AuthProtocol(self.fake_app, conf)
  373. self.assertEqual(middleware._revocations._cache_timeout,
  374. datetime.timedelta(seconds=24))
  375. def test_conf_values_type_convert(self):
  376. conf = {
  377. 'revocation_cache_time': '24',
  378. 'identity_uri': 'https://keystone.example.com:1234',
  379. 'include_service_catalog': '0',
  380. 'nonexsit_option': '0',
  381. }
  382. middleware = auth_token.AuthProtocol(self.fake_app, conf)
  383. self.assertEqual(datetime.timedelta(seconds=24),
  384. middleware._revocations._cache_timeout)
  385. self.assertFalse(middleware._include_service_catalog)
  386. self.assertEqual('0', middleware._conf.get('nonexsit_option'))
  387. def test_deprecated_conf_values(self):
  388. servers = 'localhost:11211'
  389. conf = {
  390. 'memcache_servers': servers
  391. }
  392. middleware = auth_token.AuthProtocol(self.fake_app, conf)
  393. self.assertEqual([servers], middleware._conf.get('memcached_servers'))
  394. def test_conf_values_type_convert_with_wrong_key(self):
  395. conf = {
  396. 'wrong_key': '123'
  397. }
  398. log = 'The option "wrong_key" in conf is not known to auth_token'
  399. auth_token.AuthProtocol(self.fake_app, conf)
  400. self.assertThat(self.logger.output, matchers.Contains(log))
  401. def test_conf_values_type_convert_with_wrong_value(self):
  402. conf = {
  403. 'include_service_catalog': '123',
  404. }
  405. self.assertRaises(ksm_exceptions.ConfigurationError,
  406. auth_token.AuthProtocol, self.fake_app, conf)
  407. def test_auth_region_name(self):
  408. token = fixture.V3Token()
  409. auth_url = 'http://keystone-auth.example.com:5000'
  410. east_url = 'http://keystone-east.example.com:5000'
  411. west_url = 'http://keystone-west.example.com:5000'
  412. auth_versions = fixture.DiscoveryList(href=auth_url)
  413. east_versions = fixture.DiscoveryList(href=east_url)
  414. west_versions = fixture.DiscoveryList(href=west_url)
  415. s = token.add_service('identity')
  416. s.add_endpoint(interface='admin', url=east_url, region='east')
  417. s.add_endpoint(interface='admin', url=west_url, region='west')
  418. self.requests_mock.get(auth_url, json=auth_versions)
  419. self.requests_mock.get(east_url, json=east_versions)
  420. self.requests_mock.get(west_url, json=west_versions)
  421. self.requests_mock.post(
  422. '%s/v3/auth/tokens' % auth_url,
  423. headers={'X-Subject-Token': uuid.uuid4().hex},
  424. json=token)
  425. east_mock = self.requests_mock.get(
  426. '%s/v3/auth/tokens' % east_url,
  427. headers={'X-Subject-Token': uuid.uuid4().hex},
  428. json=fixture.V3Token())
  429. west_mock = self.requests_mock.get(
  430. '%s/v3/auth/tokens' % west_url,
  431. headers={'X-Subject-Token': uuid.uuid4().hex},
  432. json=fixture.V3Token())
  433. loading.register_auth_conf_options(self.cfg.conf,
  434. group=_base.AUTHTOKEN_GROUP)
  435. opts = loading.get_auth_plugin_conf_options('v3password')
  436. self.cfg.register_opts(opts, group=_base.AUTHTOKEN_GROUP)
  437. self.cfg.config(auth_url=auth_url + '/v3',
  438. auth_type='v3password',
  439. username='user',
  440. password='pass',
  441. user_domain_id=uuid.uuid4().hex,
  442. group=_base.AUTHTOKEN_GROUP)
  443. self.assertEqual(0, east_mock.call_count)
  444. self.assertEqual(0, west_mock.call_count)
  445. east_app = self.create_simple_middleware(conf=dict(region_name='east'))
  446. self.call(east_app, headers={'X-Auth-Token': uuid.uuid4().hex})
  447. self.assertEqual(1, east_mock.call_count)
  448. self.assertEqual(0, west_mock.call_count)
  449. west_app = self.create_simple_middleware(conf=dict(region_name='west'))
  450. self.call(west_app, headers={'X-Auth-Token': uuid.uuid4().hex})
  451. self.assertEqual(1, east_mock.call_count)
  452. self.assertEqual(1, west_mock.call_count)
  453. class CommonAuthTokenMiddlewareTest(object):
  454. """These tests are run once using v2 tokens and again using v3 tokens."""
  455. def test_init_does_not_call_http(self):
  456. conf = {
  457. 'revocation_cache_time': '1'
  458. }
  459. self.create_simple_middleware(conf=conf)
  460. self.assertLastPath(None)
  461. def test_auth_with_no_token_does_not_call_http(self):
  462. middleware = self.create_simple_middleware()
  463. self.call(middleware, expected_status=401)
  464. self.assertLastPath(None)
  465. def test_init_by_ipv6Addr_auth_host(self):
  466. del self.conf['identity_uri']
  467. conf = {
  468. 'auth_host': '2001:2013:1:f101::1',
  469. 'auth_port': '1234',
  470. 'auth_protocol': 'http',
  471. 'www_authenticate_uri': None,
  472. 'auth_version': 'v3.0',
  473. }
  474. middleware = self.create_simple_middleware(conf=conf)
  475. self.assertEqual('http://[2001:2013:1:f101::1]:1234',
  476. middleware._www_authenticate_uri)
  477. def assert_valid_request_200(self, token, with_catalog=True):
  478. resp = self.call_middleware(headers={'X-Auth-Token': token})
  479. if with_catalog:
  480. self.assertTrue(resp.request.headers.get('X-Service-Catalog'))
  481. else:
  482. self.assertNotIn('X-Service-Catalog', resp.request.headers)
  483. self.assertEqual(FakeApp.SUCCESS, resp.body)
  484. self.assertIn('keystone.token_info', resp.request.environ)
  485. return resp.request
  486. def test_valid_uuid_request(self):
  487. for _ in range(2): # Do it twice because first result was cached.
  488. token = self.token_dict['uuid_token_default']
  489. self.assert_valid_request_200(token)
  490. self.assert_valid_last_url(token)
  491. def test_valid_uuid_request_with_auth_fragments(self):
  492. del self.conf['identity_uri']
  493. self.conf['auth_protocol'] = 'https'
  494. self.conf['auth_host'] = 'keystone.example.com'
  495. self.conf['auth_port'] = '1234'
  496. self.conf['auth_admin_prefix'] = '/testadmin'
  497. self.set_middleware()
  498. self.assert_valid_request_200(self.token_dict['uuid_token_default'])
  499. self.assert_valid_last_url(self.token_dict['uuid_token_default'])
  500. def _test_cache_revoked(self, token, revoked_form=None):
  501. # When the token is cached and revoked, 401 is returned.
  502. self.middleware._check_revocations_for_cached = True
  503. # Token should be cached as ok after this.
  504. self.call_middleware(headers={'X-Auth-Token': token})
  505. # Put it in revocation list.
  506. self.middleware._revocations._list = self.get_revocation_list_json(
  507. token_ids=[revoked_form or token])
  508. self.call_middleware(headers={'X-Auth-Token': token},
  509. expected_status=401)
  510. def test_cached_revoked_error(self):
  511. # When the token is cached and revocation list retrieval fails,
  512. # 503 is returned
  513. token = self.token_dict['uuid_token_default']
  514. self.middleware._check_revocations_for_cached = True
  515. # Token should be cached as ok after this.
  516. resp = self.call_middleware(headers={'X-Auth-Token': token})
  517. self.assertEqual(200, resp.status_int)
  518. # Cause the revocation list to be fetched again next time so we can
  519. # test the case where that retrieval fails
  520. self.middleware._revocations._fetched_time = datetime.datetime.min
  521. with mock.patch.object(self.middleware._revocations, '_fetch',
  522. side_effect=ksm_exceptions.RevocationListError):
  523. self.call_middleware(headers={'X-Auth-Token': token},
  524. expected_status=503)
  525. def test_cached_revoked_uuid(self):
  526. # When the UUID token is cached and revoked, 401 is returned.
  527. self._test_cache_revoked(self.token_dict['uuid_token_default'])
  528. def test_valid_signed_request(self):
  529. for _ in range(2): # Do it twice because first result was cached.
  530. self.assert_valid_request_200(
  531. self.token_dict['signed_token_scoped'])
  532. # ensure that signed requests do not generate HTTP traffic
  533. self.assertLastPath(None)
  534. def test_valid_signed_compressed_request(self):
  535. self.assert_valid_request_200(
  536. self.token_dict['signed_token_scoped_pkiz'])
  537. # ensure that signed requests do not generate HTTP traffic
  538. self.assertLastPath(None)
  539. def test_revoked_token_receives_401(self):
  540. self.middleware._revocations._list = (
  541. self.get_revocation_list_json())
  542. token = self.token_dict['revoked_token']
  543. self.call_middleware(headers={'X-Auth-Token': token},
  544. expected_status=401)
  545. def test_revoked_token_receives_401_sha256(self):
  546. self.conf['hash_algorithms'] = ','.join(['sha256', 'md5'])
  547. self.set_middleware()
  548. self.middleware._revocations._list = (
  549. self.get_revocation_list_json(mode='sha256'))
  550. token = self.token_dict['revoked_token']
  551. self.call_middleware(headers={'X-Auth-Token': token},
  552. expected_status=401)
  553. def test_cached_revoked_pki(self):
  554. # When the PKI token is cached and revoked, 401 is returned.
  555. token = self.token_dict['signed_token_scoped']
  556. revoked_form = cms.cms_hash_token(token)
  557. self._test_cache_revoked(token, revoked_form)
  558. def test_cached_revoked_pkiz(self):
  559. # When the PKIZ token is cached and revoked, 401 is returned.
  560. token = self.token_dict['signed_token_scoped_pkiz']
  561. revoked_form = cms.cms_hash_token(token)
  562. self._test_cache_revoked(token, revoked_form)
  563. def test_revoked_token_receives_401_md5_secondary(self):
  564. # When hash_algorithms has 'md5' as the secondary hash and the
  565. # revocation list contains the md5 hash for a token, that token is
  566. # considered revoked so returns 401.
  567. self.conf['hash_algorithms'] = ','.join(['sha256', 'md5'])
  568. self.set_middleware()
  569. self.middleware._revocations._list = (
  570. self.get_revocation_list_json())
  571. token = self.token_dict['revoked_token']
  572. self.call_middleware(headers={'X-Auth-Token': token},
  573. expected_status=401)
  574. def _test_revoked_hashed_token(self, token_name):
  575. # If hash_algorithms is set as ['sha256', 'md5'],
  576. # and check_revocations_for_cached is True,
  577. # and a token is in the cache because it was successfully validated
  578. # using the md5 hash, then
  579. # if the token is in the revocation list by md5 hash, it'll be
  580. # rejected and auth_token returns 401.
  581. self.conf['hash_algorithms'] = ','.join(['sha256', 'md5'])
  582. self.conf['check_revocations_for_cached'] = 'true'
  583. self.set_middleware()
  584. token = self.token_dict[token_name]
  585. # Put the token in the revocation list.
  586. token_hashed = cms.cms_hash_token(token)
  587. self.middleware._revocations._list = self.get_revocation_list_json(
  588. token_ids=[token_hashed])
  589. # First, request is using the hashed token, is valid so goes in
  590. # cache using the given hash.
  591. self.call_middleware(headers={'X-Auth-Token': token_hashed})
  592. # This time use the PKI(Z) token
  593. # Should find the token in the cache and revocation list.
  594. self.call_middleware(headers={'X-Auth-Token': token},
  595. expected_status=401)
  596. def test_revoked_hashed_pki_token(self):
  597. self._test_revoked_hashed_token('signed_token_scoped')
  598. def test_revoked_hashed_pkiz_token(self):
  599. self._test_revoked_hashed_token('signed_token_scoped_pkiz')
  600. def test_revoked_pki_token_by_audit_id(self):
  601. # When the audit ID is in the revocation list, the token is invalid.
  602. self.set_middleware()
  603. token = self.token_dict['signed_token_scoped']
  604. # Put the token audit ID in the revocation list,
  605. # the entry will have a false token ID so the token ID doesn't match.
  606. fake_token_id = uuid.uuid4().hex
  607. # The audit_id value is in examples/pki/cms/auth_*_token_scoped.json.
  608. audit_id = 'SLIXlXQUQZWUi9VJrqdXqA'
  609. revocation_list_data = {
  610. 'revoked': [
  611. {
  612. 'id': fake_token_id,
  613. 'audit_id': audit_id
  614. },
  615. ]
  616. }
  617. self.middleware._revocations._list = jsonutils.dumps(
  618. revocation_list_data)
  619. self.call_middleware(headers={'X-Auth-Token': token},
  620. expected_status=401)
  621. def get_revocation_list_json(self, token_ids=None, mode=None):
  622. if token_ids is None:
  623. key = 'revoked_token_hash' + (('_' + mode) if mode else '')
  624. token_ids = [self.token_dict[key]]
  625. revocation_list = {'revoked': [{'id': x, 'expires': timeutils.utcnow()}
  626. for x in token_ids]}
  627. return jsonutils.dumps(revocation_list)
  628. def test_is_signed_token_revoked_returns_false(self):
  629. # explicitly setting an empty revocation list here to document intent
  630. self.middleware._revocations._list = jsonutils.dumps(
  631. {"revoked": [], "extra": "success"})
  632. result = self.middleware._revocations._any_revoked(
  633. [self.token_dict['revoked_token_hash']])
  634. self.assertFalse(result)
  635. def test_is_signed_token_revoked_returns_true(self):
  636. self.middleware._revocations._list = (
  637. self.get_revocation_list_json())
  638. result = self.middleware._revocations._any_revoked(
  639. [self.token_dict['revoked_token_hash']])
  640. self.assertTrue(result)
  641. def test_is_signed_token_revoked_returns_true_sha256(self):
  642. self.conf['hash_algorithms'] = ','.join(['sha256', 'md5'])
  643. self.set_middleware()
  644. self.middleware._revocations._list = (
  645. self.get_revocation_list_json(mode='sha256'))
  646. result = self.middleware._revocations._any_revoked(
  647. [self.token_dict['revoked_token_hash_sha256']])
  648. self.assertTrue(result)
  649. def test_validate_offline_raises_exception_for_revoked_token(self):
  650. self.middleware._revocations._list = (
  651. self.get_revocation_list_json())
  652. self.assertRaises(ksm_exceptions.InvalidToken,
  653. self.middleware._validate_offline,
  654. self.token_dict['revoked_token'],
  655. [self.token_dict['revoked_token_hash']])
  656. def test_validate_offline_raises_exception_for_revoked_token_s256(self):
  657. self.conf['hash_algorithms'] = ','.join(['sha256', 'md5'])
  658. self.set_middleware()
  659. self.middleware._revocations._list = (
  660. self.get_revocation_list_json(mode='sha256'))
  661. self.assertRaises(ksm_exceptions.InvalidToken,
  662. self.middleware._validate_offline,
  663. self.token_dict['revoked_token'],
  664. [self.token_dict['revoked_token_hash_sha256'],
  665. self.token_dict['revoked_token_hash']])
  666. def test_validate_offline_raises_exception_for_revoked_pkiz_token(self):
  667. self.middleware._revocations._list = (
  668. self.examples.REVOKED_TOKEN_PKIZ_LIST_JSON)
  669. self.assertRaises(ksm_exceptions.InvalidToken,
  670. self.middleware._validate_offline,
  671. self.token_dict['revoked_token_pkiz'],
  672. [self.token_dict['revoked_token_pkiz_hash']])
  673. def test_validate_offline_succeeds_for_unrevoked_token(self):
  674. self.middleware._revocations._list = (
  675. self.get_revocation_list_json())
  676. token = self.middleware._validate_offline(
  677. self.token_dict['signed_token_scoped'],
  678. [self.token_dict['signed_token_scoped_hash']])
  679. self.assertIsInstance(token, dict)
  680. def test_verify_signed_compressed_token_succeeds_for_unrevoked_token(self):
  681. self.middleware._revocations._list = (
  682. self.get_revocation_list_json())
  683. token = self.middleware._validate_offline(
  684. self.token_dict['signed_token_scoped_pkiz'],
  685. [self.token_dict['signed_token_scoped_hash']])
  686. self.assertIsInstance(token, dict)
  687. def test_validate_offline_token_succeeds_for_unrevoked_token_sha256(self):
  688. self.conf['hash_algorithms'] = ','.join(['sha256', 'md5'])
  689. self.set_middleware()
  690. self.middleware._revocations._list = (
  691. self.get_revocation_list_json(mode='sha256'))
  692. token = self.middleware._validate_offline(
  693. self.token_dict['signed_token_scoped'],
  694. [self.token_dict['signed_token_scoped_hash_sha256'],
  695. self.token_dict['signed_token_scoped_hash']])
  696. self.assertIsInstance(token, dict)
  697. def test_get_token_revocation_list_fetched_time_returns_min(self):
  698. self.middleware._revocations._fetched_time = None
  699. # Get rid of the revoked file
  700. revoked_path = self.middleware._signing_directory.calc_path(
  701. _revocations.Revocations._FILE_NAME)
  702. os.remove(revoked_path)
  703. self.assertEqual(self.middleware._revocations._fetched_time,
  704. datetime.datetime.min)
  705. # FIXME(blk-u): move the unit tests into unit/test_auth_token.py
  706. def test_get_token_revocation_list_fetched_time_returns_mtime(self):
  707. self.middleware._revocations._fetched_time = None
  708. revoked_path = self.middleware._signing_directory.calc_path(
  709. _revocations.Revocations._FILE_NAME)
  710. mtime = os.path.getmtime(revoked_path)
  711. fetched_time = datetime.datetime.utcfromtimestamp(mtime)
  712. self.assertEqual(fetched_time,
  713. self.middleware._revocations._fetched_time)
  714. @testtools.skipUnless(TimezoneFixture.supported(),
  715. 'TimezoneFixture not supported')
  716. def test_get_token_revocation_list_fetched_time_returns_utc(self):
  717. with TimezoneFixture('UTC-1'):
  718. self.middleware._revocations._list = jsonutils.dumps(
  719. self.examples.REVOCATION_LIST)
  720. self.middleware._revocations._fetched_time = None
  721. fetched_time = self.middleware._revocations._fetched_time
  722. self.assertTrue(timeutils.is_soon(fetched_time, 1))
  723. def test_get_token_revocation_list_fetched_time_returns_value(self):
  724. expected = self.middleware._revocations._fetched_time
  725. self.assertEqual(self.middleware._revocations._fetched_time,
  726. expected)
  727. def test_get_revocation_list_returns_fetched_list(self):
  728. # auth_token uses v2 to fetch this, so don't allow the v3
  729. # tests to override the fake http connection
  730. self.middleware._revocations._fetched_time = None
  731. # Get rid of the revoked file
  732. revoked_path = self.middleware._signing_directory.calc_path(
  733. _revocations.Revocations._FILE_NAME)
  734. os.remove(revoked_path)
  735. self.assertEqual(self.middleware._revocations._list,
  736. self.examples.REVOCATION_LIST)
  737. def test_get_revocation_list_returns_current_list_from_memory(self):
  738. self.assertEqual(self.middleware._revocations._list,
  739. self.middleware._revocations._list_prop)
  740. def test_get_revocation_list_returns_current_list_from_disk(self):
  741. in_memory_list = self.middleware._revocations._list
  742. self.middleware._revocations._list_prop = None
  743. self.assertEqual(self.middleware._revocations._list,
  744. in_memory_list)
  745. def test_invalid_revocation_list_raises_error(self):
  746. self.requests_mock.get(self.revocation_url, json={})
  747. self.assertRaises(ksm_exceptions.RevocationListError,
  748. self.middleware._revocations._fetch)
  749. def test_fetch_revocation_list(self):
  750. # auth_token uses v2 to fetch this, so don't allow the v3
  751. # tests to override the fake http connection
  752. fetched = jsonutils.loads(self.middleware._revocations._fetch())
  753. self.assertEqual(fetched, self.examples.REVOCATION_LIST)
  754. def test_request_invalid_uuid_token(self):
  755. # remember because we are testing the middleware we stub the connection
  756. # to the keystone server, but this is not what gets returned
  757. invalid_uri = "%s/v2.0/tokens/invalid-token" % BASE_URI
  758. self.requests_mock.get(invalid_uri, status_code=404)
  759. resp = self.call_middleware(headers={'X-Auth-Token': 'invalid-token'},
  760. expected_status=401)
  761. self.assertEqual('Keystone uri="https://keystone.example.com:1234"',
  762. resp.headers['WWW-Authenticate'])
  763. def test_request_invalid_signed_token(self):
  764. token = self.examples.INVALID_SIGNED_TOKEN
  765. resp = self.call_middleware(headers={'X-Auth-Token': token},
  766. expected_status=401)
  767. self.assertEqual('Keystone uri="https://keystone.example.com:1234"',
  768. resp.headers['WWW-Authenticate'])
  769. def test_request_invalid_signed_pkiz_token(self):
  770. token = self.examples.INVALID_SIGNED_PKIZ_TOKEN
  771. resp = self.call_middleware(headers={'X-Auth-Token': token},
  772. expected_status=401)
  773. self.assertEqual('Keystone uri="https://keystone.example.com:1234"',
  774. resp.headers['WWW-Authenticate'])
  775. def test_request_no_token(self):
  776. resp = self.call_middleware(expected_status=401)
  777. self.assertEqual('Keystone uri="https://keystone.example.com:1234"',
  778. resp.headers['WWW-Authenticate'])
  779. def test_request_no_token_http(self):
  780. resp = self.call_middleware(method='HEAD', expected_status=401)
  781. self.assertEqual('Keystone uri="https://keystone.example.com:1234"',
  782. resp.headers['WWW-Authenticate'])
  783. def test_request_blank_token(self):
  784. resp = self.call_middleware(headers={'X-Auth-Token': ''},
  785. expected_status=401)
  786. self.assertEqual('Keystone uri="https://keystone.example.com:1234"',
  787. resp.headers['WWW-Authenticate'])
  788. def _get_cached_token(self, token, mode='md5'):
  789. token_id = cms.cms_hash_token(token, mode=mode)
  790. return self.middleware._token_cache.get(token_id)
  791. def test_memcache(self):
  792. token = self.token_dict['signed_token_scoped']
  793. self.call_middleware(headers={'X-Auth-Token': token})
  794. self.assertIsNotNone(self._get_cached_token(token))
  795. def test_expired(self):
  796. token = self.token_dict['signed_token_scoped_expired']
  797. self.call_middleware(headers={'X-Auth-Token': token},
  798. expected_status=401)
  799. def test_memcache_set_invalid_uuid(self):
  800. invalid_uri = "%s/v2.0/tokens/invalid-token" % BASE_URI
  801. self.requests_mock.get(invalid_uri, status_code=404)
  802. token = 'invalid-token'
  803. self.call_middleware(headers={'X-Auth-Token': token},
  804. expected_status=401)
  805. self.assertEqual(auth_token._CACHE_INVALID_INDICATOR,
  806. self._get_cached_token(token))
  807. def test_memcache_hit_invalid_token(self):
  808. token = 'invalid-token'
  809. invalid_uri = '%s/v2.0/tokens/invalid-token' % BASE_URI
  810. self.requests_mock.get(invalid_uri, status_code=404)
  811. # Call once to cache token's invalid state; verify it cached as such
  812. self.call_middleware(headers={'X-Auth-Token': token},
  813. expected_status=401)
  814. self.assertEqual(auth_token._CACHE_INVALID_INDICATOR,
  815. self._get_cached_token(token))
  816. # Call again for a cache hit; verify it detected as cached and invalid
  817. self.call_middleware(headers={'X-Auth-Token': token},
  818. expected_status=401)
  819. self.assertIn('Cached token is marked unauthorized',
  820. self.logger.output)
  821. def test_memcache_set_expired(self, extra_conf={}, extra_environ={}):
  822. token_cache_time = 10
  823. conf = {
  824. 'token_cache_time': '%s' % token_cache_time,
  825. }
  826. conf.update(extra_conf)
  827. self.set_middleware(conf=conf)
  828. token = self.token_dict['signed_token_scoped']
  829. self.call_middleware(headers={'X-Auth-Token': token})
  830. req = webob.Request.blank('/')
  831. req.headers['X-Auth-Token'] = token
  832. req.environ.update(extra_environ)
  833. now = datetime.datetime.utcnow()
  834. self.useFixture(TimeFixture(now))
  835. req.get_response(self.middleware)
  836. self.assertIsNotNone(self._get_cached_token(token))
  837. timeutils.advance_time_seconds(token_cache_time)
  838. self.assertIsNone(self._get_cached_token(token))
  839. def test_swift_memcache_set_expired(self):
  840. extra_conf = {'cache': 'swift.cache'}
  841. extra_environ = {'swift.cache': _cache._FakeClient()}
  842. self.test_memcache_set_expired(extra_conf, extra_environ)
  843. def test_http_error_not_cached_token(self):
  844. """Test to don't cache token as invalid on network errors.
  845. We use UUID tokens since they are the easiest one to reach
  846. get_http_connection.
  847. """
  848. self.set_middleware(conf={'http_request_max_retries': '0'})
  849. self.call_middleware(headers={'X-Auth-Token': ERROR_TOKEN},
  850. expected_status=503)
  851. self.assertIsNone(self._get_cached_token(ERROR_TOKEN))
  852. self.assert_valid_last_url(ERROR_TOKEN)
  853. def test_http_request_max_retries(self):
  854. times_retry = 10
  855. body_string = 'The Keystone service is temporarily unavailable.'
  856. conf = {'http_request_max_retries': '%s' % times_retry}
  857. self.set_middleware(conf=conf)
  858. with mock.patch('time.sleep') as mock_obj:
  859. self.call_middleware(headers={'X-Auth-Token': ERROR_TOKEN},
  860. expected_status=503,
  861. expected_body_string=body_string)
  862. self.assertEqual(mock_obj.call_count, times_retry)
  863. def test_request_timeout(self):
  864. self.call_middleware(headers={'X-Auth-Token': TIMEOUT_TOKEN},
  865. expected_status=503)
  866. self.assertIsNone(self._get_cached_token(TIMEOUT_TOKEN))
  867. self.assert_valid_last_url(TIMEOUT_TOKEN)
  868. def test_nocatalog(self):
  869. conf = {
  870. 'include_service_catalog': 'False'
  871. }
  872. self.set_middleware(conf=conf)
  873. self.assert_valid_request_200(self.token_dict['uuid_token_default'],
  874. with_catalog=False)
  875. def assert_kerberos_bind(self, token, bind_level,
  876. use_kerberos=True, success=True):
  877. conf = {
  878. 'enforce_token_bind': bind_level,
  879. 'auth_version': self.auth_version,
  880. }
  881. self.set_middleware(conf=conf)
  882. req = webob.Request.blank('/')
  883. req.headers['X-Auth-Token'] = token
  884. if use_kerberos:
  885. if use_kerberos is True:
  886. req.environ['REMOTE_USER'] = self.examples.KERBEROS_BIND
  887. else:
  888. req.environ['REMOTE_USER'] = use_kerberos
  889. req.environ['AUTH_TYPE'] = 'Negotiate'
  890. resp = req.get_response(self.middleware)
  891. if success:
  892. self.assertEqual(200, resp.status_int)
  893. self.assertEqual(FakeApp.SUCCESS, resp.body)
  894. self.assertIn('keystone.token_info', req.environ)
  895. self.assert_valid_last_url(token)
  896. else:
  897. self.assertEqual(401, resp.status_int)
  898. msg = 'Keystone uri="https://keystone.example.com:1234"'
  899. self.assertEqual(msg, resp.headers['WWW-Authenticate'])
  900. def test_uuid_bind_token_disabled_with_kerb_user(self):
  901. for use_kerberos in [True, False]:
  902. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  903. bind_level='disabled',
  904. use_kerberos=use_kerberos,
  905. success=True)
  906. def test_uuid_bind_token_disabled_with_incorrect_ticket(self):
  907. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  908. bind_level='kerberos',
  909. use_kerberos='ronald@MCDONALDS.COM',
  910. success=False)
  911. def test_uuid_bind_token_permissive_with_kerb_user(self):
  912. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  913. bind_level='permissive',
  914. use_kerberos=True,
  915. success=True)
  916. def test_uuid_bind_token_permissive_without_kerb_user(self):
  917. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  918. bind_level='permissive',
  919. use_kerberos=False,
  920. success=False)
  921. def test_uuid_bind_token_permissive_with_unknown_bind(self):
  922. token = self.token_dict['uuid_token_unknown_bind']
  923. for use_kerberos in [True, False]:
  924. self.assert_kerberos_bind(token,
  925. bind_level='permissive',
  926. use_kerberos=use_kerberos,
  927. success=True)
  928. def test_uuid_bind_token_permissive_with_incorrect_ticket(self):
  929. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  930. bind_level='kerberos',
  931. use_kerberos='ronald@MCDONALDS.COM',
  932. success=False)
  933. def test_uuid_bind_token_strict_with_kerb_user(self):
  934. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  935. bind_level='strict',
  936. use_kerberos=True,
  937. success=True)
  938. def test_uuid_bind_token_strict_with_kerbout_user(self):
  939. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  940. bind_level='strict',
  941. use_kerberos=False,
  942. success=False)
  943. def test_uuid_bind_token_strict_with_unknown_bind(self):
  944. token = self.token_dict['uuid_token_unknown_bind']
  945. for use_kerberos in [True, False]:
  946. self.assert_kerberos_bind(token,
  947. bind_level='strict',
  948. use_kerberos=use_kerberos,
  949. success=False)
  950. def test_uuid_bind_token_required_with_kerb_user(self):
  951. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  952. bind_level='required',
  953. use_kerberos=True,
  954. success=True)
  955. def test_uuid_bind_token_required_without_kerb_user(self):
  956. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  957. bind_level='required',
  958. use_kerberos=False,
  959. success=False)
  960. def test_uuid_bind_token_required_with_unknown_bind(self):
  961. token = self.token_dict['uuid_token_unknown_bind']
  962. for use_kerberos in [True, False]:
  963. self.assert_kerberos_bind(token,
  964. bind_level='required',
  965. use_kerberos=use_kerberos,
  966. success=False)
  967. def test_uuid_bind_token_required_without_bind(self):
  968. for use_kerberos in [True, False]:
  969. self.assert_kerberos_bind(self.token_dict['uuid_token_default'],
  970. bind_level='required',
  971. use_kerberos=use_kerberos,
  972. success=False)
  973. def test_uuid_bind_token_named_kerberos_with_kerb_user(self):
  974. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  975. bind_level='kerberos',
  976. use_kerberos=True,
  977. success=True)
  978. def test_uuid_bind_token_named_kerberos_without_kerb_user(self):
  979. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  980. bind_level='kerberos',
  981. use_kerberos=False,
  982. success=False)
  983. def test_uuid_bind_token_named_kerberos_with_unknown_bind(self):
  984. token = self.token_dict['uuid_token_unknown_bind']
  985. for use_kerberos in [True, False]:
  986. self.assert_kerberos_bind(token,
  987. bind_level='kerberos',
  988. use_kerberos=use_kerberos,
  989. success=False)
  990. def test_uuid_bind_token_named_kerberos_without_bind(self):
  991. for use_kerberos in [True, False]:
  992. self.assert_kerberos_bind(self.token_dict['uuid_token_default'],
  993. bind_level='kerberos',
  994. use_kerberos=use_kerberos,
  995. success=False)
  996. def test_uuid_bind_token_named_kerberos_with_incorrect_ticket(self):
  997. self.assert_kerberos_bind(self.token_dict['uuid_token_bind'],
  998. bind_level='kerberos',
  999. use_kerberos='ronald@MCDONALDS.COM',
  1000. success=False)
  1001. def test_uuid_bind_token_with_unknown_named_FOO(self):
  1002. token = self.token_dict['uuid_token_bind']
  1003. for use_kerberos in [True, False]:
  1004. self.assert_kerberos_bind(token,
  1005. bind_level='FOO',
  1006. use_kerberos=use_kerberos,
  1007. success=False)
  1008. def test_caching_token_on_verify(self):
  1009. # When the token is cached it isn't cached again when it's verified.
  1010. # The token cache has to be initialized with our cache instance.
  1011. self.middleware._token_cache._env_cache_name = 'cache'
  1012. cache = _cache._FakeClient()
  1013. self.middleware._token_cache.initialize(env={'cache': cache})
  1014. # Mock cache.set since then the test can verify call_count.
  1015. orig_cache_set = cache.set
  1016. cache.set = mock.Mock(side_effect=orig_cache_set)
  1017. token = self.token_dict['signed_token_scoped']
  1018. self.call_middleware(headers={'X-Auth-Token': token})
  1019. self.assertThat(1, matchers.Equals(cache.set.call_count))
  1020. self.call_middleware(headers={'X-Auth-Token': token})
  1021. # Assert that the token wasn't cached again.
  1022. self.assertThat(1, matchers.Equals(cache.set.call_count))
  1023. def test_auth_plugin(self):
  1024. for service_url in (self.examples.UNVERSIONED_SERVICE_URL,
  1025. self.examples.SERVICE_URL):
  1026. self.requests_mock.get(service_url,
  1027. json=VERSION_LIST_v3,
  1028. status_code=300)
  1029. token = self.token_dict['uuid_token_default']
  1030. resp = self.call_middleware(headers={'X-Auth-Token': token})
  1031. self.assertEqual(FakeApp.SUCCESS, resp.body)
  1032. token_auth = resp.request.environ['keystone.token_auth']
  1033. endpoint_filter = {'service_type': self.examples.SERVICE_TYPE,
  1034. 'version': 3}
  1035. url = token_auth.get_endpoint(session.Session(), **endpoint_filter)
  1036. self.assertEqual('%s/v3' % BASE_URI, url)
  1037. self.assertTrue(token_auth.has_user_token)
  1038. self.assertFalse(token_auth.has_service_token)
  1039. self.assertIsNone(token_auth.service)
  1040. def test_doesnt_auto_set_content_type(self):
  1041. # webob will set content_type = 'text/html' by default if nothing is
  1042. # provided. We don't want our middleware messing with the content type
  1043. # of the underlying applications.
  1044. text = uuid.uuid4().hex
  1045. def _middleware(environ, start_response):
  1046. start_response(200, [])
  1047. return text
  1048. def _start_response(status_code, headerlist, exc_info=None):
  1049. self.assertIn('200', status_code) # will be '200 OK'
  1050. self.assertEqual([], headerlist)
  1051. m = auth_token.AuthProtocol(_middleware, self.conf)
  1052. env = {'REQUEST_METHOD': 'GET',
  1053. 'HTTP_X_AUTH_TOKEN': self.token_dict['uuid_token_default']}
  1054. r = m(env, _start_response)
  1055. self.assertEqual(text, r)
  1056. def test_auth_plugin_service_token(self):
  1057. url = 'http://test.url'
  1058. text = uuid.uuid4().hex
  1059. self.requests_mock.get(url, text=text)
  1060. token = self.token_dict['uuid_token_default']
  1061. resp = self.call_middleware(headers={'X-Auth-Token': token})
  1062. self.assertEqual(200, resp.status_int)
  1063. self.assertEqual(FakeApp.SUCCESS, resp.body)
  1064. s = session.Session(auth=resp.request.environ['keystone.token_auth'])
  1065. resp = s.get(url)
  1066. self.assertEqual(text, resp.text)
  1067. self.assertEqual(200, resp.status_code)
  1068. headers = self.requests_mock.last_request.headers
  1069. self.assertEqual(FAKE_ADMIN_TOKEN_ID, headers['X-Service-Token'])
  1070. def test_service_token_with_valid_service_role_not_required(self):
  1071. self.conf['service_token_roles'] = ['service']
  1072. self.conf['service_token_roles_required'] = False
  1073. self.set_middleware(conf=self.conf)
  1074. user_token = self.token_dict['uuid_token_default']
  1075. service_token = self.token_dict['uuid_service_token_default']
  1076. resp = self.call_middleware(headers={'X-Auth-Token': user_token,
  1077. 'X-Service-Token': service_token})
  1078. self.assertEqual('Confirmed',
  1079. resp.request.headers['X-Service-Identity-Status'])
  1080. def test_service_token_with_invalid_service_role_not_required(self):
  1081. self.conf['service_token_roles'] = [uuid.uuid4().hex]
  1082. self.conf['service_token_roles_required'] = False
  1083. self.set_middleware(conf=self.conf)
  1084. user_token = self.token_dict['uuid_token_default']
  1085. service_token = self.token_dict['uuid_service_token_default']
  1086. resp = self.call_middleware(headers={'X-Auth-Token': user_token,
  1087. 'X-Service-Token': service_token})
  1088. self.assertEqual('Confirmed',
  1089. resp.request.headers['X-Service-Identity-Status'])
  1090. def test_service_token_with_valid_service_role_required(self):
  1091. self.conf['service_token_roles'] = ['service']
  1092. self.conf['service_token_roles_required'] = True
  1093. self.set_middleware(conf=self.conf)
  1094. user_token = self.token_dict['uuid_token_default']
  1095. service_token = self.token_dict['uuid_service_token_default']
  1096. resp = self.call_middleware(headers={'X-Auth-Token': user_token,
  1097. 'X-Service-Token': service_token})
  1098. self.assertEqual('Confirmed',
  1099. resp.request.headers['X-Service-Identity-Status'])
  1100. def test_service_token_with_invalid_service_role_required(self):
  1101. self.conf['service_token_roles'] = [uuid.uuid4().hex]
  1102. self.conf['service_token_roles_required'] = True
  1103. self.set_middleware(conf=self.conf)
  1104. user_token = self.token_dict['uuid_token_default']
  1105. service_token = self.token_dict['uuid_service_token_default']
  1106. resp = self.call_middleware(headers={'X-Auth-Token': user_token,
  1107. 'X-Service-Token': service_token},
  1108. expected_status=401)
  1109. self.assertEqual('Invalid',
  1110. resp.request.headers['X-Service-Identity-Status'])
  1111. class V2CertDownloadMiddlewareTest(BaseAuthTokenMiddlewareTest,
  1112. testresources.ResourcedTestCase):
  1113. resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)]
  1114. def __init__(self, *args, **kwargs):
  1115. super(V2CertDownloadMiddlewareTest, self).__init__(*args, **kwargs)
  1116. self.auth_version = 'v2.0'
  1117. self.fake_app = None
  1118. self.ca_path = '/v2.0/certificates/ca'
  1119. self.signing_path = '/v2.0/certificates/signing'
  1120. def setUp(self):
  1121. super(V2CertDownloadMiddlewareTest, self).setUp(
  1122. auth_version=self.auth_version,
  1123. fake_app=self.fake_app)
  1124. self.logger = self.useFixture(fixtures.FakeLogger())
  1125. self.base_dir = tempfile.mkdtemp()
  1126. self.addCleanup(shutil.rmtree, self.base_dir)
  1127. self.cert_dir = os.path.join(self.base_dir, 'certs')
  1128. os.makedirs(self.cert_dir, stat.S_IRWXU)
  1129. conf = {
  1130. 'signing_dir': self.cert_dir,
  1131. 'auth_version': self.auth_version,
  1132. }
  1133. self.requests_mock.get(BASE_URI,
  1134. json=VERSION_LIST_v3,
  1135. status_code=300)
  1136. self.set_middleware(conf=conf)
  1137. # Usually we supply a signed_dir with pre-installed certificates,
  1138. # so invocation of /usr/bin/openssl succeeds. This time we give it
  1139. # an empty directory, so it fails.
  1140. def test_request_no_token_dummy(self):
  1141. cms._ensure_subprocess()
  1142. self.requests_mock.get('%s%s' % (BASE_URI, self.ca_path),
  1143. status_code=404)
  1144. self.requests_mock.get('%s%s' % (BASE_URI, self.signing_path),
  1145. status_code=404)
  1146. token = self.middleware._validate_offline(
  1147. self.examples.SIGNED_TOKEN_SCOPED,
  1148. [self.examples.SIGNED_TOKEN_SCOPED_HASH])
  1149. self.assertIsNone(token)
  1150. self.assertIn('Fetch certificate config failed', self.logger.output)
  1151. self.assertIn('fallback to online validation', self.logger.output)
  1152. def test_fetch_signing_cert(self):
  1153. data = 'FAKE CERT'
  1154. url = "%s%s" % (BASE_URI, self.signing_path)
  1155. self.requests_mock.get(url, text=data)
  1156. self.middleware._fetch_signing_cert()
  1157. signing_cert_path = self.middleware._signing_directory.calc_path(
  1158. self.middleware._SIGNING_CERT_FILE_NAME)
  1159. with open(signing_cert_path, 'r') as f:
  1160. self.assertEqual(f.read(), data)
  1161. self.assertEqual(url, self.requests_mock.last_request.url)
  1162. def test_fetch_signing_ca(self):
  1163. data = 'FAKE CA'
  1164. url = "%s%s" % (BASE_URI, self.ca_path)
  1165. self.requests_mock.get(url, text=data)
  1166. self.middleware._fetch_ca_cert()
  1167. ca_file_path = self.middleware._signing_directory.calc_path(
  1168. self.middleware._SIGNING_CA_FILE_NAME)
  1169. with open(ca_file_path, 'r') as f:
  1170. self.assertEqual(f.read(), data)
  1171. self.assertEqual(url, self.requests_mock.last_request.url)
  1172. def test_prefix_trailing_slash(self):
  1173. del self.conf['identity_uri']
  1174. self.conf['auth_protocol'] = 'https'
  1175. self.conf['auth_host'] = 'keystone.example.com'
  1176. self.conf['auth_port'] = '1234'
  1177. self.conf['auth_admin_prefix'] = '/newadmin/'
  1178. base_url = '%s/newadmin' % BASE_HOST
  1179. ca_url = "%s%s" % (base_url, self.ca_path)
  1180. signing_url = "%s%s" % (base_url, self.signing_path)
  1181. self.requests_mock.get(base_url,
  1182. json=VERSION_LIST_v3,
  1183. status_code=300)
  1184. self.requests_mock.get(ca_url, text='FAKECA')
  1185. self.requests_mock.get(signing_url, text='FAKECERT')
  1186. self.set_middleware(conf=self.conf)
  1187. self.middleware._fetch_ca_cert()
  1188. self.assertEqual(ca_url, self.requests_mock.last_request.url)
  1189. self.middleware._fetch_signing_cert()
  1190. self.assertEqual(signing_url, self.requests_mock.last_request.url)
  1191. def test_without_prefix(self):
  1192. del self.conf['identity_uri']
  1193. self.conf['auth_protocol'] = 'https'
  1194. self.conf['auth_host'] = 'keystone.example.com'
  1195. self.conf['auth_port'] = '1234'
  1196. self.conf['auth_admin_prefix'] = ''
  1197. ca_url = "%s%s" % (BASE_HOST, self.ca_path)
  1198. signing_url = "%s%s" % (BASE_HOST, self.signing_path)
  1199. self.requests_mock.get(BASE_HOST,
  1200. json=VERSION_LIST_v3,
  1201. status_code=300)
  1202. self.requests_mock.get(ca_url, text='FAKECA')
  1203. self.requests_mock.get(signing_url, text='FAKECERT')
  1204. self.set_middleware(conf=self.conf)
  1205. self.middleware._fetch_ca_cert()
  1206. self.assertEqual(ca_url, self.requests_mock.last_request.url)
  1207. self.middleware._fetch_signing_cert()
  1208. self.assertEqual(signing_url, self.requests_mock.last_request.url)
  1209. class V3CertDownloadMiddlewareTest(V2CertDownloadMiddlewareTest):
  1210. def __init__(self, *args, **kwargs):
  1211. super(V3CertDownloadMiddlewareTest, self).__init__(*args, **kwargs)
  1212. self.auth_version = 'v3.0'
  1213. self.fake_app = v3FakeApp
  1214. self.ca_path = '/v3/OS-SIMPLE-CERT/ca'
  1215. self.signing_path = '/v3/OS-SIMPLE-CERT/certificates'
  1216. def network_error_response(request, context):
  1217. raise ksa_exceptions.ConnectFailure("Network connection refused.")
  1218. def request_timeout_response(request, context):
  1219. raise ksa_exceptions.RequestTimeout(
  1220. "Request to https://host/token/path timed out")
  1221. class v2AuthTokenMiddlewareTest(BaseAuthTokenMiddlewareTest,
  1222. CommonAuthTokenMiddlewareTest,
  1223. testresources.ResourcedTestCase):
  1224. """v2 token specific tests.
  1225. There are some differences between how the auth-token middleware handles
  1226. v2 and v3 tokens over and above the token formats, namely:
  1227. - A v3 keystone server will auto scope a token to a user's default project
  1228. if no scope is specified. A v2 server assumes that the auth-token
  1229. middleware will do that.
  1230. - A v2 keystone server may issue a token without a catalog, even with a
  1231. tenant
  1232. The tests below were originally part of the generic AuthTokenMiddlewareTest
  1233. class, but now, since they really are v2 specific, they are included here.
  1234. """
  1235. resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)]
  1236. def setUp(self):
  1237. super(v2AuthTokenMiddlewareTest, self).setUp()
  1238. self.token_dict = {
  1239. 'uuid_token_default': self.examples.UUID_TOKEN_DEFAULT,
  1240. 'uuid_token_unscoped': self.examples.UUID_TOKEN_UNSCOPED,
  1241. 'uuid_token_bind': self.examples.UUID_TOKEN_BIND,
  1242. 'uuid_token_unknown_bind': self.examples.UUID_TOKEN_UNKNOWN_BIND,
  1243. 'signed_token_scoped': self.examples.SIGNED_TOKEN_SCOPED,
  1244. 'signed_token_scoped_pkiz': self.examples.SIGNED_TOKEN_SCOPED_PKIZ,
  1245. 'signed_token_scoped_hash': self.examples.SIGNED_TOKEN_SCOPED_HASH,
  1246. 'signed_token_scoped_hash_sha256':
  1247. self.examples.SIGNED_TOKEN_SCOPED_HASH_SHA256,
  1248. 'signed_token_scoped_expired':
  1249. self.examples.SIGNED_TOKEN_SCOPED_EXPIRED,
  1250. 'revoked_token': self.examples.REVOKED_TOKEN,
  1251. 'revoked_token_pkiz': self.examples.REVOKED_TOKEN_PKIZ,
  1252. 'revoked_token_pkiz_hash':
  1253. self.examples.REVOKED_TOKEN_PKIZ_HASH,
  1254. 'revoked_token_hash': self.examples.REVOKED_TOKEN_HASH,
  1255. 'revoked_token_hash_sha256':
  1256. self.examples.REVOKED_TOKEN_HASH_SHA256,
  1257. 'uuid_service_token_default':
  1258. self.examples.UUID_SERVICE_TOKEN_DEFAULT,
  1259. }
  1260. self.requests_mock.get(BASE_URI,
  1261. json=VERSION_LIST_v2,
  1262. status_code=300)
  1263. self.requests_mock.post('%s/v2.0/tokens' % BASE_URI,
  1264. text=FAKE_ADMIN_TOKEN)
  1265. self.revocation_url = '%s/v2.0/tokens/revoked' % BASE_URI
  1266. self.requests_mock.get(self.revocation_url,
  1267. text=self.examples.SIGNED_REVOCATION_LIST)
  1268. for token in (self.examples.UUID_TOKEN_DEFAULT,
  1269. self.examples.UUID_TOKEN_UNSCOPED,
  1270. self.examples.UUID_TOKEN_BIND,
  1271. self.examples.UUID_TOKEN_UNKNOWN_BIND,
  1272. self.examples.UUID_TOKEN_NO_SERVICE_CATALOG,
  1273. self.examples.UUID_SERVICE_TOKEN_DEFAULT,
  1274. self.examples.SIGNED_TOKEN_SCOPED_KEY,
  1275. self.examples.SIGNED_TOKEN_SCOPED_PKIZ_KEY,):
  1276. url = "%s/v2.0/tokens/%s" % (BASE_URI, token)
  1277. text = self.examples.JSON_TOKEN_RESPONSES[token]
  1278. self.requests_mock.get(url, text=text)
  1279. url = '%s/v2.0/tokens/%s' % (BASE_URI, ERROR_TOKEN)
  1280. self.requests_mock.get(url, text=network_error_response)
  1281. url = '%s/v2.0/tokens/%s' % (BASE_URI, TIMEOUT_TOKEN)
  1282. self.requests_mock.get(url, text=request_timeout_response)
  1283. self.set_middleware()
  1284. def assert_unscoped_default_tenant_auto_scopes(self, token):
  1285. """Unscoped v2 requests with a default tenant should ``auto-scope``.
  1286. The implied scope is the user's tenant ID.
  1287. """
  1288. resp = self.call_middleware(headers={'X-Auth-Token': token})
  1289. self.assertEqual(FakeApp.SUCCESS, resp.body)
  1290. self.assertIn('keystone.token_info', resp.request.environ)
  1291. def assert_valid_last_url(self, token_id):
  1292. self.assertLastPath("/v2.0/tokens/%s" % token_id)
  1293. def test_default_tenant_uuid_token(self):
  1294. self.assert_unscoped_default_tenant_auto_scopes(
  1295. self.examples.UUID_TOKEN_DEFAULT)
  1296. def test_default_tenant_signed_token(self):
  1297. self.assert_unscoped_default_tenant_auto_scopes(
  1298. self.examples.SIGNED_TOKEN_SCOPED)
  1299. def assert_unscoped_token_receives_401(self, token):
  1300. """Unscoped requests with no default tenant ID should be rejected."""
  1301. resp = self.call_middleware(headers={'X-Auth-Token': token},
  1302. expected_status=401)
  1303. self.assertEqual('Keystone uri="https://keystone.example.com:1234"',
  1304. resp.headers['WWW-Authenticate'])
  1305. def test_unscoped_uuid_token_receives_401(self):
  1306. self.assert_unscoped_token_receives_401(
  1307. self.examples.UUID_TOKEN_UNSCOPED)
  1308. def test_unscoped_pki_token_receives_401(self):
  1309. self.assert_unscoped_token_receives_401(
  1310. self.examples.SIGNED_TOKEN_UNSCOPED)
  1311. def test_request_prevent_service_catalog_injection(self):
  1312. token = self.examples.UUID_TOKEN_NO_SERVICE_CATALOG
  1313. resp = self.call_middleware(headers={'X-Service-Catalog': '[]',
  1314. 'X-Auth-Token': token})
  1315. self.assertFalse(resp.request.headers.get('X-Service-Catalog'))
  1316. self.assertEqual(FakeApp.SUCCESS, resp.body)
  1317. def test_user_plugin_token_properties(self):
  1318. token = self.examples.UUID_TOKEN_DEFAULT
  1319. token_data = self.examples.TOKEN_RESPONSES[token]
  1320. service = self.examples.UUID_SERVICE_TOKEN_DEFAULT
  1321. resp = self.call_middleware(headers={'X-Service-Catalog': '[]',
  1322. 'X-Auth-Token': token,
  1323. 'X-Service-Token': service})
  1324. self.assertEqual(FakeApp.SUCCESS, resp.body)
  1325. token_auth = resp.request.environ['keystone.token_auth']
  1326. self.assertTrue(token_auth.has_user_token)
  1327. self.assertTrue(token_auth.has_service_token)
  1328. self.assertEqual(token_data.user_id, token_auth.user.user_id)
  1329. self.assertEqual(token_data.tenant_id, token_auth.user.project_id)
  1330. self.assertThat(token_auth.user.role_names, matchers.HasLength(2))
  1331. self.assertIn('role1', token_auth.user.role_names)
  1332. self.assertIn('role2', token_auth.user.role_names)
  1333. self.assertIsNone(token_auth.user.trust_id)
  1334. self.assertIsNone(token_auth.user.user_domain_id)
  1335. self.assertIsNone(token_auth.user.project_domain_id)
  1336. self.assertThat(token_auth.service.role_names, matchers.HasLength(2))
  1337. self.assertIn('service', token_auth.service.role_names)
  1338. self.assertIn('service_role2', token_auth.service.role_names)
  1339. self.assertIsNone(token_auth.service.trust_id)
  1340. class CrossVersionAuthTokenMiddlewareTest(BaseAuthTokenMiddlewareTest,
  1341. testresources.ResourcedTestCase):
  1342. resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)]
  1343. def test_valid_uuid_request_forced_to_2_0(self):
  1344. """Test forcing auth_token to use lower api version.
  1345. By installing the v3 http hander, auth_token will be get
  1346. a version list that looks like a v3 server - from which it
  1347. would normally chose v3.0 as the auth version. However, here
  1348. we specify v2.0 in the configuration - which should force
  1349. auth_token to use that version instead.
  1350. """
  1351. conf = {
  1352. 'auth_version': 'v2.0'
  1353. }
  1354. self.requests_mock.get(BASE_URI,
  1355. json=VERSION_LIST_v3,
  1356. status_code=300)
  1357. self.requests_mock.post('%s/v2.0/tokens' % BASE_URI,
  1358. text=FAKE_ADMIN_TOKEN)
  1359. token = self.examples.UUID_TOKEN_DEFAULT
  1360. url = "%s/v2.0/tokens/%s" % (BASE_URI, token)
  1361. text = self.examples.JSON_TOKEN_RESPONSES[token]
  1362. self.requests_mock.get(url, text=text)
  1363. self.set_middleware(conf=conf)
  1364. # This tests will only work is auth_token has chosen to use the
  1365. # lower, v2, api version
  1366. self.call_middleware(headers={'X-Auth-Token': token})
  1367. self.assertEqual(url, self.requests_mock.last_request.url)
  1368. class v3AuthTokenMiddlewareTest(BaseAuthTokenMiddlewareTest,
  1369. CommonAuthTokenMiddlewareTest,
  1370. testresources.ResourcedTestCase):
  1371. """Test auth_token middleware with v3 tokens.
  1372. Re-execute the AuthTokenMiddlewareTest class tests, but with the
  1373. auth_token middleware configured to expect v3 tokens back from
  1374. a keystone server.
  1375. This is done by configuring the AuthTokenMiddlewareTest class via
  1376. its Setup(), passing in v3 style data that will then be used by
  1377. the tests themselves. This approach has been used to ensure we
  1378. really are running the same tests for both v2 and v3 tokens.
  1379. There a few additional specific test for v3 only:
  1380. - We allow an unscoped token to be validated (as unscoped), where
  1381. as for v2 tokens, the auth_token middleware is expected to try and
  1382. auto-scope it (and fail if there is no default tenant)
  1383. - Domain scoped tokens
  1384. Since we don't specify an auth version for auth_token to use, by
  1385. definition we are thefore implicitely testing that it will use
  1386. the highest available auth version, i.e. v3.0
  1387. """
  1388. resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)]
  1389. def setUp(self):
  1390. super(v3AuthTokenMiddlewareTest, self).setUp(
  1391. auth_version='v3.0',
  1392. fake_app=v3FakeApp)
  1393. self.token_dict = {
  1394. 'uuid_token_default': self.examples.v3_UUID_TOKEN_DEFAULT,
  1395. 'uuid_token_unscoped': self.examples.v3_UUID_TOKEN_UNSCOPED,
  1396. 'uuid_token_bind': self.examples.v3_UUID_TOKEN_BIND,
  1397. 'uuid_token_unknown_bind':
  1398. self.examples.v3_UUID_TOKEN_UNKNOWN_BIND,
  1399. 'signed_token_scoped': self.examples.SIGNED_v3_TOKEN_SCOPED,
  1400. 'signed_token_scoped_pkiz':
  1401. self.examples.SIGNED_v3_TOKEN_SCOPED_PKIZ,
  1402. 'signed_token_scoped_hash':
  1403. self.examples.SIGNED_v3_TOKEN_SCOPED_HASH,
  1404. 'signed_token_scoped_hash_sha256':
  1405. self.examples.SIGNED_v3_TOKEN_SCOPED_HASH_SHA256,
  1406. 'signed_token_scoped_expired':
  1407. self.examples.SIGNED_TOKEN_SCOPED_EXPIRED,
  1408. 'revoked_token': self.examples.REVOKED_v3_TOKEN,
  1409. 'revoked_token_pkiz': self.examples.REVOKED_v3_TOKEN_PKIZ,
  1410. 'revoked_token_hash': self.examples.REVOKED_v3_TOKEN_HASH,
  1411. 'revoked_token_hash_sha256':
  1412. self.examples.REVOKED_v3_TOKEN_HASH_SHA256,
  1413. 'revoked_token_pkiz_hash':
  1414. self.examples.REVOKED_v3_PKIZ_TOKEN_HASH,
  1415. 'uuid_service_token_default':
  1416. self.examples.v3_UUID_SERVICE_TOKEN_DEFAULT,
  1417. }
  1418. self.requests_mock.get(BASE_URI,
  1419. json=VERSION_LIST_v3,
  1420. status_code=300)
  1421. # TODO(jamielennox): auth_token middleware uses a v2 admin token
  1422. # regardless of the auth_version that is set.
  1423. self.requests_mock.post('%s/v2.0/tokens' % BASE_URI,
  1424. text=FAKE_ADMIN_TOKEN)
  1425. self.revocation_url = '%s/v3/auth/tokens/OS-PKI/revoked' % BASE_URI
  1426. self.requests_mock.get(self.revocation_url,
  1427. text=self.examples.SIGNED_REVOCATION_LIST)
  1428. self.requests_mock.get('%s/v3/auth/tokens' % BASE_URI,
  1429. text=self.token_response,
  1430. headers={'X-Subject-Token': uuid.uuid4().hex})
  1431. self.set_middleware()
  1432. def token_response(self, request, context):
  1433. auth_id = request.headers.get('X-Auth-Token')
  1434. token_id = request.headers.get('X-Subject-Token')
  1435. self.assertEqual(auth_id, FAKE_ADMIN_TOKEN_ID)
  1436. if token_id == ERROR_TOKEN:
  1437. msg = "Network connection refused."
  1438. raise ksa_exceptions.ConnectFailure(msg)
  1439. elif token_id == TIMEOUT_TOKEN:
  1440. request_timeout_response(request, context)
  1441. try:
  1442. response = self.examples.JSON_TOKEN_RESPONSES[token_id]
  1443. except KeyError:
  1444. response = ""
  1445. context.status_code = 404
  1446. return response
  1447. def assert_valid_last_url(self, token_id):
  1448. self.assertLastPath('/v3/auth/tokens')
  1449. def test_valid_unscoped_uuid_request(self):
  1450. # Remove items that won't be in an unscoped token
  1451. delta_expected_env = {
  1452. 'HTTP_X_PROJECT_ID': None,
  1453. 'HTTP_X_PROJECT_NAME': None,
  1454. 'HTTP_X_PROJECT_DOMAIN_ID': None,
  1455. 'HTTP_X_PROJECT_DOMAIN_NAME': None,
  1456. 'HTTP_X_TENANT_ID': None,
  1457. 'HTTP_X_TENANT_NAME': None,
  1458. 'HTTP_X_ROLES': '',
  1459. 'HTTP_X_TENANT': None,
  1460. 'HTTP_X_ROLE': '',
  1461. }
  1462. self.set_middleware(expected_env=delta_expected_env)
  1463. self.assert_valid_request_200(self.examples.v3_UUID_TOKEN_UNSCOPED,
  1464. with_catalog=False)
  1465. self.assertLastPath('/v3/auth/tokens')
  1466. def test_valid_system_scoped_token_request(self):
  1467. delta_expected_env = {
  1468. 'HTTP_OPENSTACK_SYSTEM_SCOPE': 'all',
  1469. 'HTTP_X_PROJECT_ID': None,
  1470. 'HTTP_X_PROJECT_NAME': None,
  1471. 'HTTP_X_PROJECT_DOMAIN_ID': None,
  1472. 'HTTP_X_PROJECT_DOMAIN_NAME': None,
  1473. 'HTTP_X_TENANT_ID': None,
  1474. 'HTTP_X_TENANT_NAME': None,
  1475. 'HTTP_X_TENANT': None
  1476. }
  1477. self.set_middleware(expected_env=delta_expected_env)
  1478. self.assert_valid_request_200(self.examples.v3_SYSTEM_SCOPED_TOKEN)
  1479. self.assertLastPath('/v3/auth/tokens')
  1480. def test_domain_scoped_uuid_request(self):
  1481. # Modify items compared to default token for a domain scope
  1482. delta_expected_env = {
  1483. 'HTTP_X_DOMAIN_ID': 'domain_id1',
  1484. 'HTTP_X_DOMAIN_NAME': 'domain_name1',
  1485. 'HTTP_X_PROJECT_ID': None,
  1486. 'HTTP_X_PROJECT_NAME': None,
  1487. 'HTTP_X_PROJECT_DOMAIN_ID': None,
  1488. 'HTTP_X_PROJECT_DOMAIN_NAME': None,
  1489. 'HTTP_X_TENANT_ID': None,
  1490. 'HTTP_X_TENANT_NAME': None,
  1491. 'HTTP_X_TENANT': None
  1492. }
  1493. self.set_middleware(expected_env=delta_expected_env)
  1494. self.assert_valid_request_200(
  1495. self.examples.v3_UUID_TOKEN_DOMAIN_SCOPED)
  1496. self.assertLastPath('/v3/auth/tokens')
  1497. def test_gives_v2_catalog(self):
  1498. self.set_middleware()
  1499. req = self.assert_valid_request_200(
  1500. self.examples.SIGNED_v3_TOKEN_SCOPED)
  1501. catalog = jsonutils.loads(req.headers['X-Service-Catalog'])
  1502. for service in catalog:
  1503. for endpoint in service['endpoints']:
  1504. # no point checking everything, just that it's in v2 format
  1505. self.assertIn('adminURL', endpoint)
  1506. self.assertIn('publicURL', endpoint)
  1507. self.assertIn('internalURL', endpoint)
  1508. def test_fallback_to_online_validation_with_signing_error(self):
  1509. self.requests_mock.get('%s/v3/OS-SIMPLE-CERT/certificates' % BASE_URI,
  1510. status_code=404)
  1511. self.assert_valid_request_200(self.token_dict['signed_token_scoped'])
  1512. self.assert_valid_request_200(
  1513. self.token_dict['signed_token_scoped_pkiz'])
  1514. def test_fallback_to_online_validation_with_ca_error(self):
  1515. self.requests_mock.get('%s/v3/OS-SIMPLE-CERT/ca' % BASE_URI,
  1516. status_code=404)
  1517. self.assert_valid_request_200(self.token_dict['signed_token_scoped'])
  1518. self.assert_valid_request_200(
  1519. self.token_dict['signed_token_scoped_pkiz'])
  1520. def test_fallback_to_online_validation_with_revocation_list_error(self):
  1521. self.requests_mock.get(self.revocation_url, status_code=404)
  1522. self.assert_valid_request_200(self.token_dict['signed_token_scoped'])
  1523. self.assert_valid_request_200(
  1524. self.token_dict['signed_token_scoped_pkiz'])
  1525. def test_user_plugin_token_properties(self):
  1526. token = self.examples.v3_UUID_TOKEN_DEFAULT
  1527. token_data = self.examples.TOKEN_RESPONSES[token]
  1528. service = self.examples.v3_UUID_SERVICE_TOKEN_DEFAULT
  1529. service_data = self.examples.TOKEN_RESPONSES[service]
  1530. resp = self.call_middleware(headers={'X-Service-Catalog': '[]',
  1531. 'X-Auth-Token': token,
  1532. 'X-Service-Token': service})
  1533. self.assertEqual(FakeApp.SUCCESS, resp.body)
  1534. token_auth = resp.request.environ['keystone.token_auth']
  1535. self.assertTrue(token_auth.has_user_token)
  1536. self.assertTrue(token_auth.has_service_token)
  1537. self.assertEqual(token_data.user_id, token_auth.user.user_id)
  1538. self.assertEqual(token_data.project_id, token_auth.user.project_id)
  1539. self.assertEqual(token_data.user_domain_id,
  1540. token_auth.user.user_domain_id)
  1541. self.assertEqual(token_data.project_domain_id,
  1542. token_auth.user.project_domain_id)
  1543. self.assertThat(token_auth.user.role_names, matchers.HasLength(2))
  1544. self.assertIn('role1', token_auth.user.role_names)
  1545. self.assertIn('role2', token_auth.user.role_names)
  1546. self.assertIsNone(token_auth.user.trust_id)
  1547. self.assertEqual(service_data.user_id, token_auth.service.user_id)
  1548. self.assertEqual(service_data.project_id,
  1549. token_auth.service.project_id)
  1550. self.assertEqual(service_data.user_domain_id,
  1551. token_auth.service.user_domain_id)
  1552. self.assertEqual(service_data.project_domain_id,
  1553. token_auth.service.project_domain_id)
  1554. self.assertThat(token_auth.service.role_names, matchers.HasLength(2))
  1555. self.assertIn('service', token_auth.service.role_names)
  1556. self.assertIn('service_role2', token_auth.service.role_names)
  1557. self.assertIsNone(token_auth.service.trust_id)
  1558. def test_expire_stored_in_cache(self):
  1559. # tests the upgrade path from storing a tuple vs just the data in the
  1560. # cache. Can be removed in the future.
  1561. token = 'mytoken'
  1562. data = 'this_data'
  1563. self.set_middleware()
  1564. self.middleware._token_cache.initialize({})
  1565. now = datetime.datetime.utcnow()
  1566. delta = datetime.timedelta(hours=1)
  1567. expires = strtime(at=(now + delta))
  1568. self.middleware._token_cache.set(token, (data, expires))
  1569. new_data = self.middleware.fetch_token(token)
  1570. self.assertEqual(data, new_data)
  1571. def test_not_is_admin_project(self):
  1572. token = self.examples.v3_NOT_IS_ADMIN_PROJECT
  1573. self.set_middleware(expected_env={'HTTP_X_IS_ADMIN_PROJECT': 'False'})
  1574. req = self.assert_valid_request_200(token)
  1575. self.assertIs(False,
  1576. req.environ['keystone.token_auth'].user.is_admin_project)
  1577. def test_service_token_with_valid_service_role_not_required(self):
  1578. s = super(v3AuthTokenMiddlewareTest, self)
  1579. s.test_service_token_with_valid_service_role_not_required()
  1580. e = self.requests_mock.request_history[3].qs.get('allow_expired')
  1581. self.assertEqual(['1'], e)
  1582. def test_service_token_with_invalid_service_role_not_required(self):
  1583. s = super(v3AuthTokenMiddlewareTest, self)
  1584. s.test_service_token_with_invalid_service_role_not_required()
  1585. e = self.requests_mock.request_history[3].qs.get('allow_expired')
  1586. self.assertIsNone(e)
  1587. def test_service_token_with_valid_service_role_required(self):
  1588. s = super(v3AuthTokenMiddlewareTest, self)
  1589. s.test_service_token_with_valid_service_role_required()
  1590. e = self.requests_mock.request_history[3].qs.get('allow_expired')
  1591. self.assertEqual(['1'], e)
  1592. def test_service_token_with_invalid_service_role_required(self):
  1593. s = super(v3AuthTokenMiddlewareTest, self)
  1594. s.test_service_token_with_invalid_service_role_required()
  1595. e = self.requests_mock.request_history[3].qs.get('allow_expired')
  1596. self.assertIsNone(e)
  1597. class DelayedAuthTests(BaseAuthTokenMiddlewareTest):
  1598. def test_header_in_401(self):
  1599. body = uuid.uuid4().hex
  1600. www_authenticate_uri = 'http://local.test'
  1601. conf = {'delay_auth_decision': 'True',
  1602. 'auth_version': 'v3.0',
  1603. 'www_authenticate_uri': www_authenticate_uri}
  1604. middleware = self.create_simple_middleware(status='401 Unauthorized',
  1605. body=body,
  1606. conf=conf)
  1607. resp = self.call(middleware, expected_status=401)
  1608. self.assertEqual(six.b(body), resp.body)
  1609. self.assertEqual('Keystone uri="%s"' % www_authenticate_uri,
  1610. resp.headers['WWW-Authenticate'])
  1611. def test_delayed_auth_values(self):
  1612. conf = {'www_authenticate_uri': 'http://local.test'}
  1613. status = '401 Unauthorized'
  1614. middleware = self.create_simple_middleware(status=status, conf=conf)
  1615. self.assertFalse(middleware._delay_auth_decision)
  1616. for v in ('True', '1', 'on', 'yes'):
  1617. conf = {'delay_auth_decision': v,
  1618. 'www_authenticate_uri': 'http://local.test'}
  1619. middleware = self.create_simple_middleware(status=status,
  1620. conf=conf)
  1621. self.assertTrue(middleware._delay_auth_decision)
  1622. for v in ('False', '0', 'no'):
  1623. conf = {'delay_auth_decision': v,
  1624. 'www_authenticate_uri': 'http://local.test'}
  1625. middleware = self.create_simple_middleware(status=status,
  1626. conf=conf)
  1627. self.assertFalse(middleware._delay_auth_decision)
  1628. def test_auth_plugin_with_no_tokens(self):
  1629. body = uuid.uuid4().hex
  1630. www_authenticate_uri = 'http://local.test'
  1631. conf = {
  1632. 'delay_auth_decision': True,
  1633. 'www_authenticate_uri': www_authenticate_uri
  1634. }
  1635. middleware = self.create_simple_middleware(body=body, conf=conf)
  1636. resp = self.call(middleware)
  1637. self.assertEqual(six.b(body), resp.body)
  1638. token_auth = resp.request.environ['keystone.token_auth']
  1639. self.assertFalse(token_auth.has_user_token)
  1640. self.assertIsNone(token_auth.user)
  1641. self.assertFalse(token_auth.has_service_token)
  1642. self.assertIsNone(token_auth.service)
  1643. class CommonCompositeAuthTests(object):
  1644. """Test Composite authentication.
  1645. Test the behaviour of adding a service-token.
  1646. """
  1647. def test_composite_auth_ok(self):
  1648. token = self.token_dict['uuid_token_default']
  1649. service_token = self.token_dict['uuid_service_token_default']
  1650. fake_logger = fixtures.FakeLogger(level=logging.DEBUG)
  1651. self.middleware.logger = self.useFixture(fake_logger)
  1652. resp = self.call_middleware(headers={'X-Auth-Token': token,
  1653. 'X-Service-Token': service_token})
  1654. self.assertEqual(FakeApp.SUCCESS, resp.body)
  1655. expected_env = dict(EXPECTED_V2_DEFAULT_ENV_RESPONSE)
  1656. expected_env.update(EXPECTED_V2_DEFAULT_SERVICE_ENV_RESPONSE)
  1657. # role list may get reordered, check for string pieces individually
  1658. self.assertIn('Received request from user: ', fake_logger.output)
  1659. self.assertIn('user_id %(HTTP_X_USER_ID)s, '
  1660. 'project_id %(HTTP_X_TENANT_ID)s, '
  1661. 'roles ' % expected_env, fake_logger.output)
  1662. self.assertIn('service: user_id %(HTTP_X_SERVICE_USER_ID)s, '
  1663. 'project_id %(HTTP_X_SERVICE_PROJECT_ID)s, '
  1664. 'roles ' % expected_env, fake_logger.output)
  1665. roles = ','.join([expected_env['HTTP_X_SERVICE_ROLES'],
  1666. expected_env['HTTP_X_ROLES']])
  1667. for r in roles.split(','):
  1668. self.assertIn(r, fake_logger.output)
  1669. def test_composite_auth_invalid_service_token(self):
  1670. token = self.token_dict['uuid_token_default']
  1671. service_token = 'invalid-service-token'
  1672. resp = self.call_middleware(headers={'X-Auth-Token': token,
  1673. 'X-Service-Token': service_token},
  1674. expected_status=401)
  1675. expected_body = b'The request you have made requires authentication.'
  1676. self.assertThat(resp.body, matchers.Contains(expected_body))
  1677. def test_composite_auth_no_service_token(self):
  1678. self.purge_service_token_expected_env()
  1679. req = webob.Request.blank('/')
  1680. req.headers['X-Auth-Token'] = self.token_dict['uuid_token_default']
  1681. # Ensure injection of service headers is not possible
  1682. for key, value in self.service_token_expected_env.items():
  1683. header_key = key[len('HTTP_'):].replace('_', '-')
  1684. req.headers[header_key] = value
  1685. # Check arbitrary headers not removed
  1686. req.headers['X-Foo'] = 'Bar'
  1687. resp = req.get_response(self.middleware)
  1688. for key in six.iterkeys(self.service_token_expected_env):
  1689. header_key = key[len('HTTP_'):].replace('_', '-')
  1690. self.assertFalse(req.headers.get(header_key))
  1691. self.assertEqual('Bar', req.headers.get('X-Foo'))
  1692. self.assertEqual(418, resp.status_int)
  1693. self.assertEqual(FakeApp.FORBIDDEN, resp.body)
  1694. def test_composite_auth_invalid_user_token(self):
  1695. token = 'invalid-token'
  1696. service_token = self.token_dict['uuid_service_token_default']
  1697. resp = self.call_middleware(headers={'X-Auth-Token': token,
  1698. 'X-Service-Token': service_token},
  1699. expected_status=401)
  1700. expected_body = b'The request you have made requires authentication.'
  1701. self.assertThat(resp.body, matchers.Contains(expected_body))
  1702. def test_composite_auth_no_user_token(self):
  1703. service_token = self.token_dict['uuid_service_token_default']
  1704. resp = self.call_middleware(headers={'X-Service-Token': service_token},
  1705. expected_status=401)
  1706. expected_body = b'The request you have made requires authentication.'
  1707. self.assertThat(resp.body, matchers.Contains(expected_body))
  1708. def test_composite_auth_delay_ok(self):
  1709. self.middleware._delay_auth_decision = True
  1710. token = self.token_dict['uuid_token_default']
  1711. service_token = self.token_dict['uuid_service_token_default']
  1712. resp = self.call_middleware(headers={'X-Auth-Token': token,
  1713. 'X-Service-Token': service_token})
  1714. self.assertEqual(FakeApp.SUCCESS, resp.body)
  1715. def test_composite_auth_delay_invalid_service_token(self):
  1716. self.middleware._delay_auth_decision = True
  1717. self.purge_service_token_expected_env()
  1718. expected_env = {
  1719. 'HTTP_X_SERVICE_IDENTITY_STATUS': 'Invalid',
  1720. }
  1721. self.update_expected_env(expected_env)
  1722. token = self.token_dict['uuid_token_default']
  1723. service_token = 'invalid-service-token'
  1724. resp = self.call_middleware(headers={'X-Auth-Token': token,
  1725. 'X-Service-Token': service_token},
  1726. expected_status=420)
  1727. self.assertEqual(FakeApp.FORBIDDEN, resp.body)
  1728. def test_composite_auth_delay_invalid_service_and_user_tokens(self):
  1729. self.middleware._delay_auth_decision = True
  1730. self.purge_service_token_expected_env()
  1731. self.purge_token_expected_env()
  1732. expected_env = {
  1733. 'HTTP_X_IDENTITY_STATUS': 'Invalid',
  1734. 'HTTP_X_SERVICE_IDENTITY_STATUS': 'Invalid',
  1735. }
  1736. self.update_expected_env(expected_env)
  1737. token = 'invalid-token'
  1738. service_token = 'invalid-service-token'
  1739. resp = self.call_middleware(headers={'X-Auth-Token': token,
  1740. 'X-Service-Token': service_token},
  1741. expected_status=419)
  1742. self.assertEqual(FakeApp.FORBIDDEN, resp.body)
  1743. def test_composite_auth_delay_no_service_token(self):
  1744. self.middleware._delay_auth_decision = True
  1745. self.purge_service_token_expected_env()
  1746. req = webob.Request.blank('/')
  1747. req.headers['X-Auth-Token'] = self.token_dict['uuid_token_default']
  1748. # Ensure injection of service headers is not possible
  1749. for key, value in self.service_token_expected_env.items():
  1750. header_key = key[len('HTTP_'):].replace('_', '-')
  1751. req.headers[header_key] = value
  1752. # Check arbitrary headers not removed
  1753. req.headers['X-Foo'] = 'Bar'
  1754. resp = req.get_response(self.middleware)
  1755. for key in six.iterkeys(self.service_token_expected_env):
  1756. header_key = key[len('HTTP_'):].replace('_', '-')
  1757. self.assertFalse(req.headers.get(header_key))
  1758. self.assertEqual('Bar', req.headers.get('X-Foo'))
  1759. self.assertEqual(418, resp.status_int)
  1760. self.assertEqual(FakeApp.FORBIDDEN, resp.body)
  1761. def test_composite_auth_delay_invalid_user_token(self):
  1762. self.middleware._delay_auth_decision = True
  1763. self.purge_token_expected_env()
  1764. expected_env = {
  1765. 'HTTP_X_IDENTITY_STATUS': 'Invalid',
  1766. }
  1767. self.update_expected_env(expected_env)
  1768. token = 'invalid-token'
  1769. service_token = self.token_dict['uuid_service_token_default']
  1770. resp = self.call_middleware(headers={'X-Auth-Token': token,
  1771. 'X-Service-Token': service_token},
  1772. expected_status=403)
  1773. self.assertEqual(FakeApp.FORBIDDEN, resp.body)
  1774. def test_composite_auth_delay_no_user_token(self):
  1775. self.middleware._delay_auth_decision = True
  1776. self.purge_token_expected_env()
  1777. expected_env = {
  1778. 'HTTP_X_IDENTITY_STATUS': 'Invalid',
  1779. }
  1780. self.update_expected_env(expected_env)
  1781. service_token = self.token_dict['uuid_service_token_default']
  1782. resp = self.call_middleware(headers={'X-Service-Token': service_token},
  1783. expected_status=403)
  1784. self.assertEqual(FakeApp.FORBIDDEN, resp.body)
  1785. def assert_kerberos_composite_bind(self, user_token, service_token,
  1786. bind_level):
  1787. conf = {
  1788. 'enforce_token_bind': bind_level,
  1789. 'auth_version': self.auth_version,
  1790. }
  1791. self.set_middleware(conf=conf)
  1792. req = webob.Request.blank('/')
  1793. req.headers['X-Auth-Token'] = user_token
  1794. req.headers['X-Service-Token'] = service_token
  1795. req.environ['REMOTE_USER'] = self.examples.SERVICE_KERBEROS_BIND
  1796. req.environ['AUTH_TYPE'] = 'Negotiate'
  1797. resp = req.get_response(self.middleware)
  1798. self.assertEqual(200, resp.status_int)
  1799. self.assertEqual(FakeApp.SUCCESS, resp.body)
  1800. self.assertIn('keystone.token_info', req.environ)
  1801. def test_composite_auth_with_bind(self):
  1802. token = self.token_dict['uuid_token_bind']
  1803. service_token = self.token_dict['uuid_service_token_bind']
  1804. self.assert_kerberos_composite_bind(token,
  1805. service_token,
  1806. bind_level='required')
  1807. class v2CompositeAuthTests(BaseAuthTokenMiddlewareTest,
  1808. CommonCompositeAuthTests,
  1809. testresources.ResourcedTestCase):
  1810. """Test auth_token middleware with v2 token based composite auth.
  1811. Execute the Composite auth class tests, but with the
  1812. auth_token middleware configured to expect v2 tokens back from
  1813. a keystone server.
  1814. """
  1815. resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)]
  1816. def setUp(self):
  1817. super(v2CompositeAuthTests, self).setUp(
  1818. expected_env=EXPECTED_V2_DEFAULT_SERVICE_ENV_RESPONSE,
  1819. fake_app=CompositeFakeApp)
  1820. uuid_token_default = self.examples.UUID_TOKEN_DEFAULT
  1821. uuid_service_token_default = self.examples.UUID_SERVICE_TOKEN_DEFAULT
  1822. uuid_token_bind = self.examples.UUID_TOKEN_BIND
  1823. uuid_service_token_bind = self.examples.UUID_SERVICE_TOKEN_BIND
  1824. self.token_dict = {
  1825. 'uuid_token_default': uuid_token_default,
  1826. 'uuid_service_token_default': uuid_service_token_default,
  1827. 'uuid_token_bind': uuid_token_bind,
  1828. 'uuid_service_token_bind': uuid_service_token_bind,
  1829. }
  1830. self.requests_mock.get(BASE_URI,
  1831. json=VERSION_LIST_v2,
  1832. status_code=300)
  1833. self.requests_mock.post('%s/v2.0/tokens' % BASE_URI,
  1834. text=FAKE_ADMIN_TOKEN)
  1835. self.requests_mock.get('%s/v2.0/tokens/revoked' % BASE_URI,
  1836. text=self.examples.SIGNED_REVOCATION_LIST,
  1837. status_code=200)
  1838. for token in (self.examples.UUID_TOKEN_DEFAULT,
  1839. self.examples.UUID_SERVICE_TOKEN_DEFAULT,
  1840. self.examples.UUID_TOKEN_BIND,
  1841. self.examples.UUID_SERVICE_TOKEN_BIND):
  1842. text = self.examples.JSON_TOKEN_RESPONSES[token]
  1843. self.requests_mock.get('%s/v2.0/tokens/%s' % (BASE_URI, token),
  1844. text=text)
  1845. for invalid_uri in ("%s/v2.0/tokens/invalid-token" % BASE_URI,
  1846. "%s/v2.0/tokens/invalid-service-token" % BASE_URI):
  1847. self.requests_mock.get(invalid_uri, text='', status_code=404)
  1848. self.token_expected_env = dict(EXPECTED_V2_DEFAULT_ENV_RESPONSE)
  1849. self.service_token_expected_env = dict(
  1850. EXPECTED_V2_DEFAULT_SERVICE_ENV_RESPONSE)
  1851. self.set_middleware()
  1852. class v3CompositeAuthTests(BaseAuthTokenMiddlewareTest,
  1853. CommonCompositeAuthTests,
  1854. testresources.ResourcedTestCase):
  1855. """Test auth_token middleware with v3 token based composite auth.
  1856. Execute the Composite auth class tests, but with the
  1857. auth_token middleware configured to expect v3 tokens back from
  1858. a keystone server.
  1859. """
  1860. resources = [('examples', client_fixtures.EXAMPLES_RESOURCE)]
  1861. def setUp(self):
  1862. super(v3CompositeAuthTests, self).setUp(
  1863. auth_version='v3.0',
  1864. fake_app=v3CompositeFakeApp)
  1865. uuid_token_default = self.examples.v3_UUID_TOKEN_DEFAULT
  1866. uuid_serv_token_default = self.examples.v3_UUID_SERVICE_TOKEN_DEFAULT
  1867. uuid_token_bind = self.examples.v3_UUID_TOKEN_BIND
  1868. uuid_service_token_bind = self.examples.v3_UUID_SERVICE_TOKEN_BIND
  1869. self.token_dict = {
  1870. 'uuid_token_default': uuid_token_default,
  1871. 'uuid_service_token_default': uuid_serv_token_default,
  1872. 'uuid_token_bind': uuid_token_bind,
  1873. 'uuid_service_token_bind': uuid_service_token_bind,
  1874. }
  1875. self.requests_mock.get(BASE_URI, json=VERSION_LIST_v3, status_code=300)
  1876. # TODO(jamielennox): auth_token middleware uses a v2 admin token
  1877. # regardless of the auth_version that is set.
  1878. self.requests_mock.post('%s/v2.0/tokens' % BASE_URI,
  1879. text=FAKE_ADMIN_TOKEN)
  1880. self.requests_mock.get('%s/v3/auth/tokens/OS-PKI/revoked' % BASE_URI,
  1881. text=self.examples.SIGNED_REVOCATION_LIST)
  1882. self.requests_mock.get('%s/v3/auth/tokens' % BASE_URI,
  1883. text=self.token_response,
  1884. headers={'X-Subject-Token': uuid.uuid4().hex})
  1885. self.token_expected_env = dict(EXPECTED_V2_DEFAULT_ENV_RESPONSE)
  1886. self.token_expected_env.update(EXPECTED_V3_DEFAULT_ENV_ADDITIONS)
  1887. self.service_token_expected_env = dict(
  1888. EXPECTED_V2_DEFAULT_SERVICE_ENV_RESPONSE)
  1889. self.service_token_expected_env.update(
  1890. EXPECTED_V3_DEFAULT_SERVICE_ENV_ADDITIONS)
  1891. self.set_middleware()
  1892. def token_response(self, request, context):
  1893. auth_id = request.headers.get('X-Auth-Token')
  1894. token_id = request.headers.get('X-Subject-Token')
  1895. self.assertEqual(auth_id, FAKE_ADMIN_TOKEN_ID)
  1896. status = 200
  1897. response = ""
  1898. if token_id == ERROR_TOKEN:
  1899. msg = "Network connection refused."
  1900. raise ksc_exceptions.ConnectionRefused(msg)
  1901. elif token_id == TIMEOUT_TOKEN:
  1902. request_timeout_response(request, context)
  1903. try:
  1904. response = self.examples.JSON_TOKEN_RESPONSES[token_id]
  1905. except KeyError:
  1906. status = 404
  1907. context.status_code = status
  1908. return response
  1909. class OtherTests(BaseAuthTokenMiddlewareTest):
  1910. def setUp(self):
  1911. super(OtherTests, self).setUp()
  1912. self.logger = self.useFixture(fixtures.FakeLogger())
  1913. def test_unknown_server_versions(self):
  1914. versions = fixture.DiscoveryList(v2=False, v3_id='v4', href=BASE_URI)
  1915. self.set_middleware()
  1916. self.requests_mock.get(BASE_URI, json=versions, status_code=300)
  1917. self.call_middleware(headers={'X-Auth-Token': uuid.uuid4().hex},
  1918. expected_status=503)
  1919. self.assertIn('versions [v3.0, v2.0]', self.logger.output)
  1920. def _assert_auth_version(self, conf_version, identity_server_version):
  1921. self.set_middleware(conf={'auth_version': conf_version})
  1922. identity_server = self.middleware._create_identity_server()
  1923. self.assertEqual(identity_server_version,
  1924. identity_server.auth_version)
  1925. def test_micro_version(self):
  1926. self._assert_auth_version('v2', (2, 0))
  1927. self._assert_auth_version('v2.0', (2, 0))
  1928. self._assert_auth_version('v3', (3, 0))
  1929. self._assert_auth_version('v3.0', (3, 0))
  1930. self._assert_auth_version('v3.1', (3, 0))
  1931. self._assert_auth_version('v3.2', (3, 0))
  1932. self._assert_auth_version('v3.9', (3, 0))
  1933. self._assert_auth_version('v3.3.1', (3, 0))
  1934. self._assert_auth_version('v3.3.5', (3, 0))
  1935. def test_default_auth_version(self):
  1936. # VERSION_LIST_v3 contains both v2 and v3 version elements
  1937. self.requests_mock.get(BASE_URI, json=VERSION_LIST_v3, status_code=300)
  1938. self._assert_auth_version(None, (3, 0))
  1939. # VERSION_LIST_v2 contains only v2 version elements
  1940. self.requests_mock.get(BASE_URI, json=VERSION_LIST_v2, status_code=300)
  1941. self._assert_auth_version(None, (2, 0))
  1942. def test_unsupported_auth_version(self):
  1943. # If the requested version isn't supported we will use v2
  1944. self._assert_auth_version('v1', (2, 0))
  1945. self._assert_auth_version('v10', (2, 0))
  1946. class AuthProtocolLoadingTests(BaseAuthTokenMiddlewareTest):
  1947. AUTH_URL = 'http://auth.url/prefix'
  1948. DISC_URL = 'http://disc.url/prefix'
  1949. KEYSTONE_BASE_URL = 'http://keystone.url/prefix'
  1950. CRUD_URL = 'http://crud.url/prefix'
  1951. # NOTE(jamielennox): use the /v2.0 prefix here because this is what's most
  1952. # likely to be in the service catalog and we should be able to ignore it.
  1953. KEYSTONE_URL = KEYSTONE_BASE_URL + '/v2.0'
  1954. def setUp(self):
  1955. super(AuthProtocolLoadingTests, self).setUp()
  1956. self.project_id = uuid.uuid4().hex
  1957. # first touch is to discover the available versions at the auth_url
  1958. self.requests_mock.get(self.AUTH_URL,
  1959. json=fixture.DiscoveryList(href=self.DISC_URL),
  1960. status_code=300)
  1961. # then we do discovery on the URL from the service catalog. In practice
  1962. # this is mostly the same URL as before but test the full range.
  1963. self.requests_mock.get(self.KEYSTONE_BASE_URL + '/',
  1964. json=fixture.DiscoveryList(href=self.CRUD_URL),
  1965. status_code=300)
  1966. def good_request(self, app):
  1967. # admin_token is the token that the service will get back from auth
  1968. admin_token_id = uuid.uuid4().hex
  1969. admin_token = fixture.V3Token(project_id=self.project_id)
  1970. s = admin_token.add_service('identity', name='keystone')
  1971. s.add_standard_endpoints(admin=self.KEYSTONE_URL)
  1972. self.requests_mock.post('%s/v3/auth/tokens' % self.AUTH_URL,
  1973. json=admin_token,
  1974. headers={'X-Subject-Token': admin_token_id})
  1975. # user_token is the data from the user's inputted token
  1976. user_token_id = uuid.uuid4().hex
  1977. user_token = fixture.V3Token()
  1978. user_token.set_project_scope()
  1979. request_headers = {'X-Subject-Token': user_token_id,
  1980. 'X-Auth-Token': admin_token_id}
  1981. self.requests_mock.get('%s/v3/auth/tokens' % self.KEYSTONE_BASE_URL,
  1982. request_headers=request_headers,
  1983. json=user_token,
  1984. headers={'X-Subject-Token': uuid.uuid4().hex})
  1985. resp = self.call(app, headers={'X-Auth-Token': user_token_id})
  1986. return resp
  1987. def test_loading_password_plugin(self):
  1988. # the password options aren't set on config until loading time, but we
  1989. # need them set so we can override the values for testing, so force it
  1990. opts = loading.get_auth_plugin_conf_options('password')
  1991. self.cfg.register_opts(opts, group=_base.AUTHTOKEN_GROUP)
  1992. project_id = uuid.uuid4().hex
  1993. # Register the authentication options
  1994. loading.register_auth_conf_options(self.cfg.conf,
  1995. group=_base.AUTHTOKEN_GROUP)
  1996. # configure the authentication options
  1997. self.cfg.config(auth_type='password',
  1998. username='testuser',
  1999. password='testpass',
  2000. auth_url=self.AUTH_URL,
  2001. project_id=project_id,
  2002. user_domain_id='userdomainid',
  2003. group=_base.AUTHTOKEN_GROUP)
  2004. body = uuid.uuid4().hex
  2005. app = self.create_simple_middleware(body=body)
  2006. resp = self.good_request(app)
  2007. self.assertEqual(six.b(body), resp.body)
  2008. @staticmethod
  2009. def get_plugin(app):
  2010. return app._identity_server._adapter.auth
  2011. def test_invalid_plugin_fails_to_initialize(self):
  2012. loading.register_auth_conf_options(self.cfg.conf,
  2013. group=_base.AUTHTOKEN_GROUP)
  2014. self.cfg.config(auth_type=uuid.uuid4().hex,
  2015. group=_base.AUTHTOKEN_GROUP)
  2016. self.assertRaises(
  2017. ksa_exceptions.NoMatchingPlugin,
  2018. self.create_simple_middleware)
  2019. def test_plugin_loading_mixed_opts(self):
  2020. # some options via override and some via conf
  2021. opts = loading.get_auth_plugin_conf_options('password')
  2022. self.cfg.register_opts(opts, group=_base.AUTHTOKEN_GROUP)
  2023. username = 'testuser'
  2024. password = 'testpass'
  2025. # Register the authentication options
  2026. loading.register_auth_conf_options(self.cfg.conf,
  2027. group=_base.AUTHTOKEN_GROUP)
  2028. # configure the authentication options
  2029. self.cfg.config(auth_type='password',
  2030. auth_url='http://keystone.test:5000',
  2031. password=password,
  2032. project_id=self.project_id,
  2033. user_domain_id='userdomainid',
  2034. group=_base.AUTHTOKEN_GROUP)
  2035. conf = {'username': username, 'auth_url': self.AUTH_URL}
  2036. body = uuid.uuid4().hex
  2037. app = self.create_simple_middleware(body=body, conf=conf)
  2038. resp = self.good_request(app)
  2039. self.assertEqual(six.b(body), resp.body)
  2040. plugin = self.get_plugin(app)
  2041. self.assertEqual(self.AUTH_URL, plugin.auth_url)
  2042. self.assertEqual(username, plugin._username)
  2043. self.assertEqual(password, plugin._password)
  2044. self.assertEqual(self.project_id, plugin._project_id)
  2045. def test_plugin_loading_with_auth_section(self):
  2046. # some options via override and some via conf
  2047. section = 'testsection'
  2048. username = 'testuser'
  2049. password = 'testpass'
  2050. loading.register_auth_conf_options(self.cfg.conf, group=section)
  2051. opts = loading.get_auth_plugin_conf_options('password')
  2052. self.cfg.register_opts(opts, group=section)
  2053. # Register the authentication options
  2054. loading.register_auth_conf_options(self.cfg.conf,
  2055. group=_base.AUTHTOKEN_GROUP)
  2056. # configure the authentication options
  2057. self.cfg.config(auth_section=section, group=_base.AUTHTOKEN_GROUP)
  2058. self.cfg.config(auth_type='password',
  2059. auth_url=self.AUTH_URL,
  2060. password=password,
  2061. project_id=self.project_id,
  2062. user_domain_id='userdomainid',
  2063. group=section)
  2064. conf = {'username': username}
  2065. body = uuid.uuid4().hex
  2066. app = self.create_simple_middleware(body=body, conf=conf)
  2067. resp = self.good_request(app)
  2068. self.assertEqual(six.b(body), resp.body)
  2069. plugin = self.get_plugin(app)
  2070. self.assertEqual(self.AUTH_URL, plugin.auth_url)
  2071. self.assertEqual(username, plugin._username)
  2072. self.assertEqual(password, plugin._password)
  2073. self.assertEqual(self.project_id, plugin._project_id)
  2074. class TestAuthPluginUserAgentGeneration(BaseAuthTokenMiddlewareTest):
  2075. def setUp(self):
  2076. super(TestAuthPluginUserAgentGeneration, self).setUp()
  2077. self.auth_url = uuid.uuid4().hex
  2078. self.project_id = uuid.uuid4().hex
  2079. self.username = uuid.uuid4().hex
  2080. self.password = uuid.uuid4().hex
  2081. self.section = uuid.uuid4().hex
  2082. self.user_domain_id = uuid.uuid4().hex
  2083. loading.register_auth_conf_options(self.cfg.conf, group=self.section)
  2084. opts = loading.get_auth_plugin_conf_options('password')
  2085. self.cfg.register_opts(opts, group=self.section)
  2086. # Register the authentication options
  2087. loading.register_auth_conf_options(self.cfg.conf,
  2088. group=_base.AUTHTOKEN_GROUP)
  2089. # configure the authentication options
  2090. self.cfg.config(auth_section=self.section, group=_base.AUTHTOKEN_GROUP)
  2091. self.cfg.config(auth_type='password',
  2092. password=self.password,
  2093. project_id=self.project_id,
  2094. user_domain_id=self.user_domain_id,
  2095. group=self.section)
  2096. def test_no_project_configured(self):
  2097. ksm_version = uuid.uuid4().hex
  2098. conf = {'username': self.username, 'auth_url': self.auth_url}
  2099. app = self._create_app(conf, '', ksm_version)
  2100. self._assert_user_agent(app, '', ksm_version)
  2101. def test_project_in_configuration(self):
  2102. project = uuid.uuid4().hex
  2103. project_version = uuid.uuid4().hex
  2104. ksm_version = uuid.uuid4().hex
  2105. conf = {'username': self.username,
  2106. 'auth_url': self.auth_url,
  2107. 'project': project}
  2108. app = self._create_app(conf, project_version, ksm_version)
  2109. project_with_version = '{0}/{1} '.format(project, project_version)
  2110. self._assert_user_agent(app, project_with_version, ksm_version)
  2111. def test_project_not_installed_results_in_unknown_version(self):
  2112. project = uuid.uuid4().hex
  2113. conf = {'username': self.username,
  2114. 'auth_url': self.auth_url,
  2115. 'project': project}
  2116. v = pbr.version.VersionInfo('keystonemiddleware').version_string()
  2117. app = self.create_simple_middleware(conf=conf, use_global_conf=True)
  2118. project_with_version = '{0}/{1} '.format(project, 'unknown')
  2119. self._assert_user_agent(app, project_with_version, v)
  2120. def test_project_in_oslo_configuration(self):
  2121. project = uuid.uuid4().hex
  2122. project_version = uuid.uuid4().hex
  2123. ksm_version = uuid.uuid4().hex
  2124. conf = {'username': self.username, 'auth_url': self.auth_url}
  2125. with mock.patch.object(self.cfg.conf, 'project',
  2126. new=project, create=True):
  2127. app = self._create_app(conf, project_version, ksm_version)
  2128. project = '{0}/{1} '.format(project, project_version)
  2129. self._assert_user_agent(app, project, ksm_version)
  2130. def _create_app(self, conf, project_version, ksm_version):
  2131. fake_pkg_resources = mock.Mock()
  2132. fake_pkg_resources.get_distribution().version = project_version
  2133. fake_version_info = mock.Mock()
  2134. fake_version_info.version_string.return_value = ksm_version
  2135. fake_pbr_version = mock.Mock()
  2136. fake_pbr_version.VersionInfo.return_value = fake_version_info
  2137. body = uuid.uuid4().hex
  2138. at_pbr = 'keystonemiddleware._common.config.pbr.version'
  2139. with mock.patch('keystonemiddleware._common.config.pkg_resources',
  2140. new=fake_pkg_resources):
  2141. with mock.patch(at_pbr, new=fake_pbr_version):
  2142. return self.create_simple_middleware(body=body, conf=conf)
  2143. def _assert_user_agent(self, app, project, ksm_version):
  2144. sess = app._identity_server._adapter.session
  2145. expected_ua = ('{0}keystonemiddleware.auth_token/{1}'
  2146. .format(project, ksm_version))
  2147. self.assertThat(sess.user_agent, matchers.StartsWith(expected_ua))
  2148. def load_tests(loader, tests, pattern):
  2149. return testresources.OptimisingTestSuite(tests)