Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions otter/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,10 @@ def authenticate_tenant(self, tenant_id, log=None):
see :meth:`IAuthenticator.authenticate_tenant`
"""
auth = partial(self._auth_me, log=log)

d = user_for_tenant(self._admin_url,
self._identity_admin_user,
self._identity_admin_password,
tenant_id, log=log)
d = auth()
d.addCallback(lambda ignore: user_for_tenant(self._admin_url, self._identity_admin_user,
self._token,
log=log))

def impersonate(user):
iud = impersonate_user(self._admin_url,
Expand Down Expand Up @@ -371,7 +370,7 @@ def endpoints_for_token(auth_endpoint, identity_admin_token, user_token,
return d


def user_for_tenant(auth_endpoint, username, password, tenant_id, log=None):
def user_for_tenant(auth_endpoint, username, token, log=None):
"""
Use a super secret API to get the special actual username for a tenant id.

Expand All @@ -383,14 +382,14 @@ def user_for_tenant(auth_endpoint, username, password, tenant_id, log=None):
:return: Username of the magical identity:user-admin user for the tenantid.
"""
d = treq.get(
append_segments(auth_endpoint.replace('v2.0', 'v1.1'), 'mosso', str(tenant_id)),
auth=(username, password),
append_segments(auth_endpoint, 'users')+'?name='+str(username),
headers=headers(token),
allow_redirects=False,
log=log)
d.addCallback(check_success, [301])
d.addErrback(wrap_upstream_error, 'identity', 'mosso', auth_endpoint)
d.addCallback(check_success, [200, 203])
d.addErrback(wrap_upstream_error, 'identity', 'users', auth_endpoint)
d.addCallback(treq.json_content)
d.addCallback(lambda user: user['user']['id'])
d.addCallback(lambda user: user['user']['username'])
return d


Expand Down
33 changes: 16 additions & 17 deletions otter/test/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
from otter.effect_dispatcher import get_simple_dispatcher
from otter.test.utils import SameJSON, iMock, mock_log, patch
from otter.util.http import APIError, UpstreamError
from otter.util.http import APIError, UpstreamError, headers


expected_headers = {'accept': ['application/json'],
Expand Down Expand Up @@ -299,18 +299,18 @@ def test_user_for_tenant(self):
the list of users for a given tenant.
"""
response = mock.Mock(code=200)
response_body = {'user': {'id': 'ausername'}}
response_body = {'user': {'username': 'username'}}
self.treq.json_content.return_value = succeed(response_body)
self.treq.get.return_value = succeed(response)

d = user_for_tenant('http://identity/v2.0', 'username', 'password',
111111, log=self.log)
d = user_for_tenant('http://identity/v2.0', 'username', 'auth-token',
log=self.log)

self.assertEqual(self.successResultOf(d), 'ausername')
self.assertEqual(self.successResultOf(d), 'username')

self.treq.get.assert_called_once_with(
'http://identity/v1.1/mosso/111111',
auth=('username', 'password'),
'http://identity/v2.0/users?name=username',
headers=headers('auth-token'),
allow_redirects=False, log=self.log)

def test_user_for_tenant_propagates_errors(self):
Expand All @@ -321,8 +321,7 @@ def test_user_for_tenant_propagates_errors(self):
self.treq.content.return_value = succeed('error_body')
self.treq.get.return_value = succeed(response)

d = user_for_tenant('http://identity/v2.0', 'username', 'password',
111111)
d = user_for_tenant('http://identity/v2.0', 'username', 'auth-token')
failure = self.failureResultOf(d)

self.assertTrue(failure.check(UpstreamError))
Expand Down Expand Up @@ -509,15 +508,15 @@ def test_authenticate_tenant_gets_user_for_specified_tenant(self):
"""
self.successResultOf(self.ia.authenticate_tenant(111111))
self.user_for_tenant.assert_called_once_with(self.admin_url, self.user,
self.password, 111111,
'auth-token',
log=None)

self.user_for_tenant.reset_mock()

self.successResultOf(self.ia.authenticate_tenant(111111, log=self.log))

self.user_for_tenant.assert_called_once_with(self.admin_url, self.user,
self.password, 111111,
'auth-token',
log=self.log)

def test_authenticate_tenant_impersonates_first_user(self):
Expand Down Expand Up @@ -548,12 +547,12 @@ def test_authenticate_tenant_retries_impersonates_first_user(self):
succeed({'access': {'token': {'id': 'impersonation_token'}}})]
self.successResultOf(self.ia.authenticate_tenant(111111, self.log))
self.impersonate_user.assert_has_calls(
[mock.call(self.admin_url, None, 'test_user', log=self.log),
[mock.call(self.admin_url, 'auth-token', 'test_user', log=self.log),
mock.call(self.admin_url, 'auth-token', 'test_user', log=self.log)])
self.authenticate_user.assert_called_once_with(self.url, self.user,
self.authenticate_user.assert_called_with(self.url, self.user,
self.password,
log=self.log)
self.log.msg.assert_called_once_with('Getting new identity admin token')
self.log.msg.assert_called_with('Getting new identity admin token')

def test_authenticate_tenant_gets_endpoints_for_the_impersonation_token(self):
"""
Expand All @@ -575,12 +574,12 @@ def test_authenticate_tenant_retries_getting_endpoints_for_the_impersonation_tok
succeed({'endpoints': [{'name': 'anEndpoint', 'type': 'anType'}]})]
self.successResultOf(self.ia.authenticate_tenant(111111, log=self.log))
self.endpoints_for_token.assert_has_calls(
[mock.call(self.admin_url, None, 'impersonation_token', log=self.log),
[mock.call(self.admin_url, 'auth-token', 'impersonation_token', log=self.log),
mock.call(self.admin_url, 'auth-token', 'impersonation_token', log=self.log)])
self.authenticate_user.assert_called_once_with(self.url, self.user,
self.authenticate_user.assert_called_with(self.url, self.user,
self.password,
log=self.log)
self.log.msg.assert_called_once_with('Getting new identity admin token')
self.log.msg.assert_called_with('Getting new identity admin token')

def test_authenticate_tenant_returns_impersonation_token_and_endpoint_list(self):
"""
Expand Down