Skip to content

Commit 0c758d6

Browse files
Merge pull request #103 from tapis-project/dev
Update staging release
2 parents a2fb975 + 61d2c3e commit 0c758d6

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ build: build.api build.migrations build.test
2929

3030
# ----- run tests
3131
test: build.test
32-
cd $(cwd); touch service.log; chmod a+w service.log; docker-compose run $(api)-tests;
32+
cd $(cwd); touch service.log; chmod a+w service.log; docker-compose run -e MFA_GEN_CODE=$(MFA_GEN_CODE) $(api)-tests;
3333

3434
# ----- shutdown the currently running services
3535
down:

service/models.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -724,15 +724,24 @@ def from_ldap3_entry(cls, tenant_id, entry):
724724
attrs = {}
725725
try:
726726
cn = entry['cn'][0]
727-
except Exception as e:
727+
except Exception:
728728
logger.error(f"Got exception trying to get cn from entry; entry: {entry}")
729729
raise DAOError("Unable to parse LDAP user objects.")
730-
# the cn is the uid/username
731-
attrs['uid'] = cn
730+
# the cn is supposed to be the uid/username
731+
# however, some tenants have cn configured incorrectly
732+
# we can look for uid instead
733+
if 'uid' in entry:
734+
logger.debug(f"Found uid in entry: {entry['uid']}")
735+
attrs['uid'] = entry['uid'][0]
736+
else:
737+
attrs['uid'] = cn
732738
# compute the DN from the CN
733739
tenant = tenants.get_tenant_config(tenant_id)
734740
ldap_user_dn = tenant.ldap_user_dn
735-
attrs['dn'] = f'cn={cn},{ldap_user_dn}'
741+
if "${username}" in ldap_user_dn:
742+
attrs['dn'] = ldap_user_dn.replace("${username}", attrs['uid'])
743+
else:
744+
attrs['dn'] = f'cn={cn},{ldap_user_dn}'
736745
# the remaining params are computed directly in the same way -- as the first entry in an array of bytes
737746
params = ['givenName', 'sn', 'mail', 'telephoneNumber', 'mobile', 'createTimestamp',
738747
'uidNumber', 'userPassword']

service/tests/basic_test.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import datetime
33
import pytest
44
import json
5+
import pyotp
6+
import os
57

68
from tapisservice.auth import validate_token
79
from tapisservice.config import conf as tapisconf
@@ -19,6 +21,8 @@
1921
TEST_CLIENT_REDIRECT_URI = 'http://localhost:5000/testsuite'
2022
TEST_USERNAME = 'testuser1'
2123
TEST_PASSWORD = 'testuser1'
24+
MFA_USERNAME = 'cicsvc'
25+
MFA_GEN_CODE = os.environ.get('MFA_GEN_CODE')
2226

2327
@pytest.fixture
2428
def client():
@@ -244,13 +248,19 @@ def get_jwt(client):
244248
return access_token_str
245249

246250

247-
def gen_mfa_token(username, tokencode=None):
251+
@pytest.fixture
252+
def mfa_token(tokencode=None):
248253
"""
249254
Generate a OTP mfa code using pyotp given a username and token code.
250255
If a token code is not provided, a random one will be used.
251256
"""
252-
pass
253-
257+
if tokencode is None:
258+
tokencode = MFA_GEN_CODE
259+
print(f'DEBUG:: generating MFA token with tokencode: {tokencode}')
260+
if tokencode is None:
261+
print(f'ERROR! tokencode should not be None! Env: {os.environ.items()}')
262+
totp = pyotp.TOTP(tokencode)
263+
return totp.now()
254264

255265
# =====================
256266
# Actual test functions
@@ -272,7 +282,6 @@ def test_get_jwt(client):
272282
def test_get_mfa_config(client):
273283
print('top of get mfa config')
274284
try:
275-
print(f'what the heck is going on here')
276285
tenant_config = tenant_configs_cache.get_config(TEST_TENANT_ID)
277286
print(f'after tenant config get:: {tenant_config}')
278287
mfa_config = json.loads(tenant_config.mfa_config)
@@ -295,6 +304,9 @@ def test_get_mfa_config(client):
295304
print(f'got {e} while trying to get mfa config for tenant {TEST_TENANT_ID}')
296305
raise Exception()
297306

307+
def test_get_mfa_code(client, mfa_token):
308+
print(f'got mfa tken:: {mfa_token}')
309+
assert mfa_token is not None
298310

299311
## Health Check
300312
# hello
@@ -318,7 +330,9 @@ def test_get_metadata(client):
318330

319331
## Admin
320332
# get_config
333+
# TODO
321334
# update_config
335+
# TODO
322336

323337
## Clients
324338

@@ -361,8 +375,10 @@ def test_authenticator_create_clients(client, capsys): ## TODO: this works, but
361375
check_clients_table(TEST_CLIENT_ID, TEST_CLIENT_REDIRECT_URI, 'A Test Client', "This is a client just for testing")
362376

363377
# Get client details
378+
# TODO
364379

365380
# Update client details
381+
# TODO
366382

367383
# Permanantly set a client to inactive
368384
def test_authenticator_delete_clients(client):
@@ -540,6 +556,8 @@ def test_password_grant_no_client(client, init_db):
540556
assert 'refresh_token' not in response.json['result']
541557

542558
# Create a v2 bearer token from a Tapis v3 JWT
559+
# TODO
560+
543561
# Revoke a token
544562
def test_revoke_token(client, init_db):
545563
"""
@@ -591,10 +609,15 @@ def test_revoke_token(client, init_db):
591609

592610
check_refresh_token_table(refresh_token_claims, "password", True, TEST_CLIENT_ID)
593611

612+
# Note: Device code checks are below
613+
594614
## Profiles
595615
# get_userinfo
616+
# TODO
596617
# list_profiles
618+
# TODO
597619
# get_profile
620+
# TODO
598621

599622
## grant type tests
600623

@@ -846,7 +869,19 @@ def test_exchange_device_code(client):
846869
validate_access_token(response)
847870

848871
## MFA tests
872+
# TODO
873+
def test_mfa_valid_code(mfa_token):
874+
# uses the cicsvc creds to auth.
875+
response = mfa.call_mfa(mfa_token, TEST_TENANT_ID, MFA_USERNAME)
876+
print(f'DEBUG:: mfa response: {response}')
877+
assert response is True
878+
879+
def test_mfa_invalid_code(mfa_token):
880+
response = mfa.call_mfa('123456', TEST_TENANT_ID, MFA_USERNAME)
881+
print(f'DEBUG:: mfa response: {response}')
882+
assert response is False
849883

850884
## OAuth2ProviderExtCallback tests
885+
# TODO
851886

852887

tests-requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pytest==5.1.2
1+
pytest==5.1.2
2+
pyotp==2.9.0

0 commit comments

Comments
 (0)