diff --git a/.gitignore b/.gitignore index 937eb28..3421248 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ venv/ .idea/ app/*.sqlite* app/cert/*.* +.pytest_cache diff --git a/app/main.py b/app/main.py index f35d3fc..ea26f64 100644 --- a/app/main.py +++ b/app/main.py @@ -53,6 +53,7 @@ LEASE_EXPIRE_DELTA = relativedelta(days=int(getenv('LEASE_EXPIRE_DAYS', 90))) DLS_URL = str(getenv('DLS_URL', 'localhost')) DLS_PORT = int(getenv('DLS_PORT', '443')) SITE_KEY_XID = getenv('SITE_KEY_XID', '00000000-0000-0000-0000-000000000000') +INSTANCE_REF = '00000000-0000-0000-0000-000000000000' INSTANCE_KEY_RSA = load_key(join(dirname(__file__), 'cert/instance.private.pem')) INSTANCE_KEY_PUB = load_key(join(dirname(__file__), 'cert/instance.public.pem')) @@ -109,15 +110,6 @@ async def client_token(): cur_time = datetime.utcnow() exp_time = cur_time + relativedelta(years=12) - service_instance_public_key_configuration = { - "service_instance_public_key_me": { - "mod": hex(INSTANCE_KEY_PUB.public_key().n)[2:], - "exp": INSTANCE_KEY_PUB.public_key().e, - }, - "service_instance_public_key_pem": INSTANCE_KEY_PUB.export_key().decode('utf-8'), - "key_retention_mode": "LATEST_ONLY" - } - payload = { "jti": str(uuid4()), "iss": "NLS Service Instance", @@ -129,7 +121,7 @@ async def client_token(): "scope_ref_list": [str(uuid4())], "fulfillment_class_ref_list": [], "service_instance_configuration": { - "nls_service_instance_ref": "00000000-0000-0000-0000-000000000000", + "nls_service_instance_ref": INSTANCE_REF, "svc_port_set_list": [ { "idx": 0, @@ -139,7 +131,14 @@ async def client_token(): ], "node_url_list": [{"idx": 0, "url": DLS_URL, "url_qr": DLS_URL, "svc_port_set_idx": 0}] }, - "service_instance_public_key_configuration": service_instance_public_key_configuration, + "service_instance_public_key_configuration": { + "service_instance_public_key_me": { + "mod": hex(INSTANCE_KEY_PUB.public_key().n)[2:], + "exp": int(INSTANCE_KEY_PUB.public_key().e), + }, + "service_instance_public_key_pem": INSTANCE_KEY_PUB.export_key().decode('utf-8'), + "key_retention_mode": "LATEST_ONLY" + }, } content = jws.sign(payload, key=jwt_encode_key, headers=None, algorithm=ALGORITHMS.RS256) @@ -155,7 +154,7 @@ async def client_token(): # {"candidate_origin_ref":"00112233-4455-6677-8899-aabbccddeeff","environment":{"fingerprint":{"mac_address_list":["ff:ff:ff:ff:ff:ff"]},"hostname":"my-hostname","ip_address_list":["192.168.178.123","fe80::","fe80::1%enp6s18"],"guest_driver_version":"510.85.02","os_platform":"Debian GNU/Linux 11 (bullseye) 11","os_version":"11 (bullseye)"},"registration_pending":false,"update_pending":false} @app.post('/auth/v1/origin') async def auth_v1_origin(request: Request): - j = json.loads((await request.body()).decode('utf-8')) + j, cur_time = json.loads((await request.body()).decode('utf-8')), datetime.utcnow() origin_ref = j['candidate_origin_ref'] logging.info(f'> [ origin ]: {origin_ref}: {j}') @@ -169,7 +168,6 @@ async def auth_v1_origin(request: Request): db['origin'].upsert(data, ['origin_ref']) - cur_time = datetime.utcnow() response = { "origin_ref": origin_ref, "environment": j['environment'], @@ -188,12 +186,11 @@ async def auth_v1_origin(request: Request): # {"code_challenge":"...","origin_ref":"00112233-4455-6677-8899-aabbccddeeff"} @app.post('/auth/v1/code') async def auth_v1_code(request: Request): - j = json.loads((await request.body()).decode('utf-8')) + j, cur_time = json.loads((await request.body()).decode('utf-8')), datetime.utcnow() origin_ref = j['origin_ref'] logging.info(f'> [ code ]: {origin_ref}: {j}') - cur_time = datetime.utcnow() delta = relativedelta(minutes=15) expires = cur_time + delta @@ -201,16 +198,13 @@ async def auth_v1_code(request: Request): 'iat': timegm(cur_time.timetuple()), 'exp': timegm(expires.timetuple()), 'challenge': j['code_challenge'], - 'origin_ref': j['code_challenge'], + 'origin_ref': j['origin_ref'], 'key_ref': SITE_KEY_XID, 'kid': SITE_KEY_XID } auth_code = jws.sign(payload, key=jwt_encode_key, headers={'kid': payload.get('kid')}, algorithm=ALGORITHMS.RS256) - db['auth'].delete(origin_ref=origin_ref, expires={'<=': cur_time - delta}) - db['auth'].insert(dict(origin_ref=origin_ref, code_challenge=j['code_challenge'], expires=expires)) - response = { "auth_code": auth_code, "sync_timestamp": cur_time.isoformat(), @@ -225,19 +219,16 @@ async def auth_v1_code(request: Request): # {"auth_code":"...","code_verifier":"..."} @app.post('/auth/v1/token') async def auth_v1_token(request: Request): - j = json.loads((await request.body()).decode('utf-8')) + j, cur_time = json.loads((await request.body()).decode('utf-8')), datetime.utcnow() payload = jwt.decode(token=j['auth_code'], key=jwt_decode_key) - code_challenge = payload['origin_ref'] - - origin_ref = db['auth'].find_one(code_challenge=code_challenge)['origin_ref'] - logging.info(f'> [ auth ]: {origin_ref} ({code_challenge}): {j}') + origin_ref = payload['origin_ref'] + logging.info(f'> [ auth ]: {origin_ref}: {j}') # validate the code challenge if payload['challenge'] != b64enc(sha256(j['code_verifier'].encode('utf-8')).digest()).rstrip(b'=').decode('utf-8'): raise HTTPException(status_code=401, detail='expected challenge did not match verifier') - cur_time = datetime.utcnow() access_expires_on = cur_time + TOKEN_EXPIRE_DELTA new_payload = { @@ -246,7 +237,7 @@ async def auth_v1_token(request: Request): 'iss': 'https://cls.nvidia.org', 'aud': 'https://cls.nvidia.org', 'exp': timegm(access_expires_on.timetuple()), - 'origin_ref': payload['origin_ref'], + 'origin_ref': origin_ref, 'key_ref': SITE_KEY_XID, 'kid': SITE_KEY_XID, } @@ -265,16 +256,12 @@ async def auth_v1_token(request: Request): # {'fulfillment_context': {'fulfillment_class_ref_list': []}, 'lease_proposal_list': [{'license_type_qualifiers': {'count': 1}, 'product': {'name': 'NVIDIA RTX Virtual Workstation'}}], 'proposal_evaluation_mode': 'ALL_OF', 'scope_ref_list': ['00112233-4455-6677-8899-aabbccddeeff']} @app.post('/leasing/v1/lessor') async def leasing_v1_lessor(request: Request): - j, token = json.loads((await request.body()).decode('utf-8')), get_token(request) + j, token, cur_time = json.loads((await request.body()).decode('utf-8')), get_token(request), datetime.utcnow() - code_challenge = token['origin_ref'] + origin_ref = token['origin_ref'] scope_ref_list = j['scope_ref_list'] + logging.info(f'> [ create ]: {origin_ref}: create leases for scope_ref_list {scope_ref_list}') - origin_ref = db['auth'].find_one(code_challenge=code_challenge)['origin_ref'] - - logging.info(f'> [ create ]: {origin_ref} ({code_challenge}): create leases for scope_ref_list {scope_ref_list}') - - cur_time = datetime.utcnow() lease_result_list = [] for scope_ref in scope_ref_list: expires = cur_time + LEASE_EXPIRE_DELTA @@ -309,15 +296,13 @@ async def leasing_v1_lessor(request: Request): # venv/lib/python3.9/site-packages/nls_dal_service_instance_dls/schema/service_instance/V1_0_21__product_mapping.sql @app.get('/leasing/v1/lessor/leases') async def leasing_v1_lessor_lease(request: Request): - token = get_token(request) + token, cur_time = get_token(request), datetime.utcnow() - code_challenge = token['origin_ref'] + origin_ref = token['origin_ref'] - origin_ref = db['auth'].find_one(code_challenge=code_challenge)['origin_ref'] active_lease_list = list(map(lambda x: x['lease_ref'], db['lease'].find(origin_ref=origin_ref))) - logging.info(f'> [ leases ]: {origin_ref} ({code_challenge}): found {len(active_lease_list)} active leases') + logging.info(f'> [ leases ]: {origin_ref}: found {len(active_lease_list)} active leases') - cur_time = datetime.utcnow() response = { "active_lease_list": active_lease_list, "sync_timestamp": cur_time.isoformat(), @@ -330,18 +315,19 @@ async def leasing_v1_lessor_lease(request: Request): # venv/lib/python3.9/site-packages/nls_core_lease/lease_single.py @app.put('/leasing/v1/lease/{lease_ref}') async def leasing_v1_lease_renew(request: Request, lease_ref: str): - token = get_token(request) + token, cur_time = get_token(request), datetime.utcnow() - code_challenge = token['origin_ref'] - - origin_ref = db['auth'].find_one(code_challenge=code_challenge)['origin_ref'] - logging.info(f'> [ renew ]: {origin_ref} ({code_challenge}): renew {lease_ref}') + origin_ref = token['origin_ref'] + logging.info(f'> [ renew ]: {origin_ref}: renew {lease_ref}') if db['lease'].count(origin_ref=origin_ref, lease_ref=lease_ref) == 0: raise HTTPException(status_code=404, detail='requested lease not available') - cur_time = datetime.utcnow() expires = cur_time + LEASE_EXPIRE_DELTA + + data = dict(origin_ref=origin_ref, lease_ref=lease_ref, lease_expires=expires, lease_last_update=cur_time) + db['lease'].update(data, ['origin_ref', 'lease_ref']) + response = { "lease_ref": lease_ref, "expires": expires.isoformat(), @@ -351,30 +337,26 @@ async def leasing_v1_lease_renew(request: Request, lease_ref: str): "sync_timestamp": cur_time.isoformat(), } - data = dict(origin_ref=origin_ref, lease_ref=lease_ref, lease_expires=expires, lease_last_update=cur_time) - db['lease'].update(data, ['origin_ref', 'lease_ref']) - return JSONResponse(response) @app.delete('/leasing/v1/lessor/leases') async def leasing_v1_lessor_lease_remove(request: Request): - token = get_token(request) + token, cur_time = get_token(request), datetime.utcnow() - code_challenge = token['origin_ref'] + origin_ref = token['origin_ref'] - origin_ref = db['auth'].find_one(code_challenge=code_challenge)['origin_ref'] released_lease_list = list(map(lambda x: x['lease_ref'], db['lease'].find(origin_ref=origin_ref))) deletions = db['lease'].delete(origin_ref=origin_ref) - logging.info(f'> [ remove ]: {origin_ref} ({code_challenge}): removed {deletions} leases') + logging.info(f'> [ remove ]: {origin_ref}: removed {deletions} leases') - cur_time = datetime.utcnow() response = { "released_lease_list": released_lease_list, "release_failure_list": None, "sync_timestamp": cur_time.isoformat(), "prompts": None } + return JSONResponse(response) diff --git a/test/main.py b/test/main.py new file mode 100644 index 0000000..0711de5 --- /dev/null +++ b/test/main.py @@ -0,0 +1,87 @@ +from uuid import uuid4 + +from jose import jwt +from starlette.testclient import TestClient +import importlib.util +import sys + +MODULE, PATH = 'main.app', '../app/main.py' + +spec = importlib.util.spec_from_file_location(MODULE, PATH) +main = importlib.util.module_from_spec(spec) +sys.modules[MODULE] = main +spec.loader.exec_module(main) + +client = TestClient(main.app) + +ORIGIN_REF = str(uuid4()) + + +def test_index(): + response = client.get('/') + assert response.status_code == 200 + + +def test_status(): + response = client.get('/status') + assert response.status_code == 200 + assert response.json()['status'] == 'up' + + +def test_client_token(): + response = client.get('/client-token') + assert response.status_code == 200 + + +def test_auth_v1_origin(): + payload = { + "registration_pending": False, + "environment": { + "guest_driver_version": "guest_driver_version", + "hostname": "myhost", + "ip_address_list": ["192.168.1.123"], + "os_version": "os_version", + "os_platform": "os_platform", + "fingerprint": {"mac_address_list": ["ff:ff:ff:ff:ff:ff"]}, + "host_driver_version": "host_driver_version" + }, + "update_pending": False, + "candidate_origin_ref": ORIGIN_REF, + } + + response = client.post('/auth/v1/origin', json=payload) + assert response.status_code == 200 + assert response.json()['origin_ref'] == ORIGIN_REF + + +def test_auth_v1_code(): + payload = { + "code_challenge": "0wmaiAMAlTIDyz4Fgt2/j0tXnGv72TYbbLs4ISRCZlY", + "origin_ref": ORIGIN_REF, + } + + response = client.post('/auth/v1/code', json=payload) + assert response.status_code == 200 + + payload = jwt.get_unverified_claims(token=response.json()['auth_code']) + assert payload['origin_ref'] == ORIGIN_REF + + +def test_auth_v1_token(): + pass + + +def test_leasing_v1_lessor(): + pass + + +def test_leasing_v1_lessor_lease(): + pass + + +def test_leasing_v1_lease_renew(): + pass + + +def test_leasing_v1_lessor_lease_remove(): + pass diff --git a/version.env b/version.env index 3407a86..f7fc9a2 100644 --- a/version.env +++ b/version.env @@ -1 +1 @@ -VERSION=0.5 +VERSION=0.6