From a0b9eae15b389f49023e3205579b9f6ce4cd0d61 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 06:56:29 +0100 Subject: [PATCH 01/10] main.py - fixed wrong "origin_ref" in CodeResponse - fixed issue - removed the now unnecessary table "auth" --- app/main.py | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/app/main.py b/app/main.py index f35d3fc..08c0310 100644 --- a/app/main.py +++ b/app/main.py @@ -201,16 +201,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(), @@ -228,10 +225,8 @@ async def auth_v1_token(request: Request): j = json.loads((await request.body()).decode('utf-8')) 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'): @@ -246,7 +241,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, } @@ -267,12 +262,9 @@ async def auth_v1_token(request: Request): async def leasing_v1_lessor(request: Request): j, token = json.loads((await request.body()).decode('utf-8')), get_token(request) - code_challenge = token['origin_ref'] + origin_ref = token['origin_ref'] scope_ref_list = j['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}') + logging.info(f'> [ create ]: {origin_ref}: create leases for scope_ref_list {scope_ref_list}') cur_time = datetime.utcnow() lease_result_list = [] @@ -311,11 +303,10 @@ async def leasing_v1_lessor(request: Request): async def leasing_v1_lessor_lease(request: Request): token = get_token(request) - 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 = { @@ -332,10 +323,8 @@ async def leasing_v1_lessor_lease(request: Request): async def leasing_v1_lease_renew(request: Request, lease_ref: str): token = get_token(request) - 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') @@ -361,12 +350,11 @@ async def leasing_v1_lease_renew(request: Request, lease_ref: str): async def leasing_v1_lessor_lease_remove(request: Request): token = get_token(request) - 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 = { From 9809bbdbd13f18437fdcfc71551f222e072f2519 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 07:16:41 +0100 Subject: [PATCH 02/10] bump version to 0.6 --- version.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 3f5e3b16c590364923114d822a0bd3003bfd7fcd Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 07:35:37 +0100 Subject: [PATCH 03/10] added api tests --- .gitlab-ci.yml | 6 +++++- test/main.py | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/main.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c24c913..70b8e02 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,9 +16,13 @@ build: - docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:${CI_BUILD_REF} test: + image: python:3.10-alpine stage: test + before_script: + - pip install pytest httpx + - cd test script: - - echo "Nothing to do ..." + - pytest main.py deploy: stage: deploy diff --git a/test/main.py b/test/main.py new file mode 100644 index 0000000..d28d1a4 --- /dev/null +++ b/test/main.py @@ -0,0 +1,23 @@ +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) + + +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' From 906af9430a95c8a29a3ffe73677a25f8f08a882b Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 07:36:33 +0100 Subject: [PATCH 04/10] .gitlab-ci.yml - fixed installing dependencies --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 70b8e02..b0291b8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -19,6 +19,7 @@ test: image: python:3.10-alpine stage: test before_script: + - pip install -r requirements.txt - pip install pytest httpx - cd test script: From d5d156e70e0cff08137c6bfcff4572bb4f7c4f16 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 07:38:53 +0100 Subject: [PATCH 05/10] .gitlab-ci.yml - create test certificates --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b0291b8..05f12ef 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -21,7 +21,8 @@ test: before_script: - pip install -r requirements.txt - pip install pytest httpx - - cd test + - openssl genrsa -out app/instance.private.pem 2048 + - openssl rsa -in app/instance.private.pem -outform PEM -pubout -out app/instance.public.pem script: - pytest main.py From 67ed6108a29db249829fa138c9725d5fd3c6f4d2 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 07:40:27 +0100 Subject: [PATCH 06/10] .gitlab-ci.yml - changed test image to bullseye --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 05f12ef..24e6cc9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,7 +16,7 @@ build: - docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:${CI_BUILD_REF} test: - image: python:3.10-alpine + image: python:3.10-slim-bullseye stage: test before_script: - pip install -r requirements.txt From 3367977652b2fb56a3163a6db284d287a2390905 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 07:41:18 +0100 Subject: [PATCH 07/10] .gitlab-ci.yml - fixed cd into test --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 24e6cc9..4dd4114 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -23,6 +23,7 @@ test: - pip install pytest httpx - openssl genrsa -out app/instance.private.pem 2048 - openssl rsa -in app/instance.private.pem -outform PEM -pubout -out app/instance.public.pem + - cd test script: - pytest main.py From 2c1c9b63b41b5e512d1461272ea3aa4bf4869bf7 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 07:41:23 +0100 Subject: [PATCH 08/10] .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) 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 From a58549a1627693322b27374481fe8970806adbce Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 07:43:02 +0100 Subject: [PATCH 09/10] .gitlab-ci.yml - fixed test cert path --- .gitlab-ci.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4dd4114..49e8c5f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -21,8 +21,9 @@ test: before_script: - pip install -r requirements.txt - pip install pytest httpx - - openssl genrsa -out app/instance.private.pem 2048 - - openssl rsa -in app/instance.private.pem -outform PEM -pubout -out app/instance.public.pem + - mkdir -p app/cert + - openssl genrsa -out app/cert/instance.private.pem 2048 + - openssl rsa -in app/cert/instance.private.pem -outform PEM -pubout -out app/cert/instance.public.pem - cd test script: - pytest main.py From 3f71c88d48145fb91853de5ded7d5ab11cd5b442 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 07:48:47 +0100 Subject: [PATCH 10/10] added some test --- test/main.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/main.py b/test/main.py index d28d1a4..aeb52e3 100644 --- a/test/main.py +++ b/test/main.py @@ -21,3 +21,51 @@ 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": "00112233-4455-6677-8899-aabbccddeeff" + } + response = client.post('/auth/v1/origin', json=payload) + assert response.status_code == 200 + + +def test_auth_v1_code(): + pass + + +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