From e84a7b3e8d60925cee4118c3f679e2a2c3e2d160 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 22 Dec 2022 07:57:56 +0100 Subject: [PATCH 1/7] README.md - added installation method with debian and git on bare metal --- README.md | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7005045..d3a7cd3 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,9 @@ Generate client token, (see [installation](#installation)). There are some more internal api endpoints for handling authentication and lease process. -# Setup (Docker) +# Setup + +## Docker **Run this on the Docker-Host** @@ -77,6 +79,97 @@ volumes: dls-db: ``` +## Debian + +Tested on `Debian 11 (bullseye)`, Ubuntu may also work. + +**Install requirements** + +```shell +apt-get update && apt-get install git python3-venv python3-pip +``` + +**Install FastAPI-DLS** + +```shell +WORKING_DIR=/opt/fastapi-dls +mkdir -p $WORKING_DIR +cd $WORKING_DIR +git clone https://git.collinwebdesigns.de/oscar.krause/fastapi-dls . +python3 -m venv venv +source venv/bin/activate +pip install -r requirements.txt +deactivate +``` + +**Create keypair and webserver certificate** + +```shell +WORKING_DIR=/opt/fastapi-dls/app/cert +mkdir $WORKING_DIR +cd $WORKING_DIR +# create instance private and public key for singing JWT's +openssl genrsa -out $WORKING_DIR/instance.private.pem 2048 +openssl rsa -in $WORKING_DIR/instance.private.pem -outform PEM -pubout -out $WORKING_DIR/instance.public.pem +# create ssl certificate for integrated webserver (uvicorn) - because clients rely on ssl +openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout $WORKING_DIR/webserver.key -out $WORKING_DIR/webserver.crt +``` + +**Test Service** + +```shell +cd /opt/fastapi-dls/app +/opt/fastapi-dls/venv/bin/uvicorn main:app \ + --host 127.0.0.1 --port 443 \ + --app-dir /opt/fastapi-dls/app \ + --ssl-keyfile /opt/fastapi-dls/app/cert/webserver.key \ + --ssl-certfile /opt/fastapi-dls/app/cert/webserver.crt \ + --proxy-headers +``` + +**Create config file** + +```shell +cat < /etc/fastapi-dls.env +DLS_URL=127.0.0.1 +DLS_PORT=443 +LEASE_EXPIRE_DAYS=90 +DATABASE=sqlite:////opt/fastapi-dls/app/db.sqlite +EOF +``` + +**Create service** + +```shell +cat </etc/systemd/system/fastapi-dls.service +[Unit] +Description=Service for fastapi-dls +After=network.target + +[Service] +User=www-data +Group=www-data +WorkingDirectory=/opt/fastapi-dls/app +ExecStart=/opt/fastapi-dls/venv/bin/uvicorn \ + --host $DLS_URL --port $DLS_PORT \ + --app-dir /opt/fastapi-dls/app \ + --ssl-keyfile /opt/fastapi-dls/app/cert/webserver.key \ + --ssl-certfile /opt/fastapi-dls/app/cert/webserver.crt \ + --proxy-headers +EnvironmentFile=/etc/fastapi-dls.env +Restart=always +KillSignal=SIGQUIT +Type=notify +StandardError=syslog +NotifyAccess=all + +[Install] +WantedBy=multi-user.target +EOF +``` + +Now you have to run `systemctl daemon-reload`. After that you can start service with `systemctl start fastapi-dls.service`. + # Configuration | Variable | Default | Usage | From 7946c63f8c8293caa7a75d53c748c77ddb93f8a7 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 22 Dec 2022 10:14:15 +0100 Subject: [PATCH 2/7] README.md - added notice that no internet connection is required --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 1d40908..997e232 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ Minimal Delegated License Service (DLS). +This service can be used without internet connection. +Only the clients need a connection to this service on configured port. + ## Endpoints ### `GET /` From a6f7b9b595bddda88a81f7931ffa4bb68ae20b01 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 22 Dec 2022 10:14:21 +0100 Subject: [PATCH 3/7] code styling --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 997e232..49770b7 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,8 @@ WantedBy=multi-user.target EOF ``` -Now you have to run `systemctl daemon-reload`. After that you can start service with `systemctl start fastapi-dls.service`. +Now you have to run `systemctl daemon-reload`. After that you can start service +with `systemctl start fastapi-dls.service`. # Configuration From 184e858feae4155756f190b6dbdbcf470ab54286 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 22 Dec 2022 10:31:40 +0100 Subject: [PATCH 4/7] .gitlab-ci.yml - added docker image versions for gitlab registry --- .gitlab-ci.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index effbf3c..c24c913 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,7 +6,7 @@ build: interruptible: true stage: build rules: - - if: $CI_COMMIT_BRANCH + - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH tags: [ docker ] before_script: - echo "COMMIT=${CI_COMMIT_SHA}" >> version.env # COMMIT=`git rev-parse HEAD` @@ -29,6 +29,13 @@ deploy: - source version.env - echo "Building docker image for commit ${COMMIT} with version ${VERSION}" script: + - echo "GitLab-Registry" + - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY + - docker build . --tag ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:${VERSION} + - docker build . --tag ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:latest + - docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:${VERSION} + - docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}/${CI_BUILD_REF_NAME}:latest + - echo "Docker-Hub" - docker login -u $PUBLIC_REGISTRY_USER -p $PUBLIC_REGISTRY_TOKEN - docker build . --tag $PUBLIC_REGISTRY_USER/${CI_PROJECT_NAME}:${VERSION} - docker build . --tag $PUBLIC_REGISTRY_USER/${CI_PROJECT_NAME}:latest From d6d4cbc74a4e48af96ef82c073d31f50f011ab87 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 22 Dec 2022 10:31:52 +0100 Subject: [PATCH 5/7] README.md - added docker image sources --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 49770b7..e729b89 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,11 @@ There are some more internal api endpoints for handling authentication and lease ## Docker +Docker-Images are available here: + +- [Docker-Hub](https://hub.docker.com/repository/docker/collinwebdesigns/fastapi-dls): `collinwebdesigns/fastapi-dls:latest` +- GitLab-Registry: `registry.git.collinwebdesigns.de/oscar.krause/fastapi-dls/main:latest` + **Run this on the Docker-Host** ```shell From a0b9eae15b389f49023e3205579b9f6ce4cd0d61 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Fri, 23 Dec 2022 06:56:29 +0100 Subject: [PATCH 6/7] 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 7/7] 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