Skip to content

Commit

Permalink
update to flask 3 and connexion 3
Browse files Browse the repository at this point in the history
  • Loading branch information
daisieh committed Nov 28, 2024
1 parent 852e898 commit 4d5a822
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 52 deletions.
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ mkdir -p $DAEMON_PATH/to_ingest
mkdir -p $DAEMON_PATH/results
bash /ingest_app/daemon.sh &

gunicorn server:app
gunicorn -k uvicorn.workers.UvicornWorker server:app
98 changes: 49 additions & 49 deletions ingest_operations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import connexion
from flask import request, Flask
from flask import Flask
import os
import re
import traceback
Expand Down Expand Up @@ -40,15 +40,15 @@ def generateResponse(result, response_code):

def get_headers():
headers = {}
if "Authorization" not in request.headers:
if "Authorization" not in connexion.request.headers:
return generateResponse("Bearer token required", ERROR_CODES["UNAUTHORIZED"])
try:
# New auth model
# refresh_token = request.headers["Authorization"].split("Bearer ")[1]
# refresh_token = connexion.request.headers["Authorization"].split("Bearer ")[1]
# token = auth.get_bearer_from_refresh(refresh_token)
if not request.headers["Authorization"].startswith("Bearer "):
if not connexion.request.headers["Authorization"].startswith("Bearer "):
return generateResponse("Invalid bearer token", ERROR_CODES["UNAUTHORIZED"])
token = request.headers["Authorization"].split("Bearer ")[1]
token = connexion.request.headers["Authorization"].split("Bearer ")[1]
headers["Authorization"] = "Bearer %s" % token
except Exception as e:
if "Invalid bearer token" in str(e):
Expand Down Expand Up @@ -83,22 +83,22 @@ def get_service_info():
# S3 credentials
####

def add_s3_credential():
data = connexion.request.json
token = request.headers['Authorization'].split("Bearer ")[1]
async def add_s3_credential():
data = await connexion.request.json()
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
return auth.store_s3_credential(data["endpoint"], data["bucket"], data["access_key"], data["secret_key"], token)


@app.route('/s3-credential/endpoint/<path:endpoint_id>/bucket/<path:bucket_id>')
def get_s3_credential(endpoint_id, bucket_id):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
endpoint_cleaned = re.sub(r"\W", "_", endpoint_id)
return auth.get_s3_credential(endpoint_cleaned, bucket_id, token)


@app.route('/s3-credential/endpoint/<path:endpoint_id>/bucket/<path:bucket_id>')
def delete_s3_credential(endpoint_id, bucket_id):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
endpoint_cleaned = re.sub(r"\W", "_", endpoint_id)
return auth.remove_s3_credential(endpoint_cleaned, bucket_id, token)

Expand All @@ -110,18 +110,18 @@ def delete_s3_credential(endpoint_id, bucket_id):
@app.route('/site-role/<path:role_type>')
def list_role(role_type):
try:
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
result, status_code = auth.get_role_type_in_opa(role_type, token)
return result, status_code
except Exception as e:
return {"error": str(e)}, 500


@app.route('/site-role/<path:role_type>')
def update_role(role_type):
role_members = connexion.request.json
async def update_role(role_type):
role_members = await connexion.request.json()
try:
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
result, status_code = auth.set_role_type_in_opa(role_type, role_members, token)
return result, status_code
except Exception as e:
Expand All @@ -131,7 +131,7 @@ def update_role(role_type):
@app.route('/site-role/<path:role_type>/email/<path:email>')
def is_user_in_role(role_type, email):
try:
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
result, status_code = auth.get_role_type_in_opa(role_type, token)
if status_code == 200:
return (email in result[role_type]), 200
Expand All @@ -143,7 +143,7 @@ def is_user_in_role(role_type, email):
@app.route('/site-role/<path:role_type>/email/<path:email>')
def add_user_to_role(role_type, email):
try:
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
result, status_code = auth.get_role_type_in_opa(role_type, token)
if status_code == 200:
if email not in result[role_type]:
Expand All @@ -157,7 +157,7 @@ def add_user_to_role(role_type, email):
@app.route('/site-role/<path:role_type>/email/<path:email>')
def remove_user_from_role(role_type, email):
try:
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
result, status_code = auth.get_role_type_in_opa(role_type, token)
if status_code == 200:
if email in result[role_type]:
Expand All @@ -173,11 +173,11 @@ def remove_user_from_role(role_type, email):
# Data ingest
####

def add_genomic_linkages():
dataset = connexion.request.json
do_not_index = bool(connexion.request.args.get("do_not_index", False))
async def add_genomic_linkages():
dataset = await connexion.request.json()
do_not_index = bool(connexion.request.query_params.get("do_not_index", False))
headers = get_headers()
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
response, status_code = htsget_ingest.check_genomic_data(dataset, token)
if status_code == 200:
ingest_uuid = add_to_queue({"htsget": response, "do_not_index": do_not_index})
Expand All @@ -186,11 +186,11 @@ def add_genomic_linkages():
return response, status_code


def add_clinical_donors():
dataset = connexion.request.json
batch_size = int(connexion.request.args.get("batch_size", 1000))
async def add_clinical_donors():
dataset = await connexion.request.json()
batch_size = int(connexion.request.query_params.get("batch_size", 1000))
headers = get_headers()
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
response, status_code = katsu_ingest.prep_check_clinical_data(dataset, token, batch_size)
if status_code == 200:
ingest_uuid = add_to_queue({"katsu": response})
Expand Down Expand Up @@ -227,15 +227,15 @@ def get_ingest_status(queue_id):
####

def list_program_authorizations():
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]

response, status_code = auth.list_programs_in_opa(token)
return response, status_code


def add_program_authorization():
program = connexion.request.json
token = request.headers['Authorization'].split("Bearer ")[1]
async def add_program_authorization():
program = await connexion.request.json()
token = connexion.request.headers['Authorization'].split("Bearer ")[1]

response, status_code = auth.add_program_to_opa(program, token)
check_default_site_admin(response)
Expand All @@ -244,15 +244,15 @@ def add_program_authorization():

@app.route('/program/<path:program_id>')
def get_program_authorization(program_id):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]

response, status_code = auth.get_program_in_opa(program_id, token)
return response, status_code


@app.route('/program/<path:program_id>')
def remove_program(program_id):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
response = {"errors": {}}
check_default_site_admin(response)

Expand Down Expand Up @@ -283,7 +283,7 @@ def remove_program(program_id):

@app.route('/program/<path:program_id>/email/<path:email>')
def add_user_access(program_id, email):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
try:
result, status_code = add_user_to_dataset(email, program_id, token)
return result, status_code
Expand All @@ -293,7 +293,7 @@ def add_user_access(program_id, email):

@app.route('/program/<path:program_id>/email/<path:email>')
def remove_user_access(program_id, email):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
try:
result, status_code = remove_user_from_dataset(email, program_id, token)
return result, status_code
Expand All @@ -305,22 +305,22 @@ def remove_user_access(program_id, email):
####

def add_pending_user():
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]

response, status_code = auth.add_pending_user_to_opa(token)
return response, status_code


def list_pending_users():
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]

response, status_code = auth.list_pending_users_in_opa(token)
return {"results": response}, status_code


@app.route('/user/pending/<path:user_id>')
def approve_pending_user(user_id):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
user_name = urllib.parse.unquote_plus(user_id)

response, status_code = auth.approve_pending_user_in_opa(user_name, token)
Expand All @@ -329,16 +329,16 @@ def approve_pending_user(user_id):

@app.route('/user/pending/<path:user_id>')
def reject_pending_user(user_id):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
user_name = urllib.parse.unquote_plus(user_id)

response, status_code = auth.reject_pending_user_in_opa(user_name, token)
return response, status_code


def approve_pending_users():
users = connexion.request.json
token = request.headers['Authorization'].split("Bearer ")[1]
async def approve_pending_users():
users = await connexion.request.json()
token = connexion.request.headers['Authorization'].split("Bearer ")[1]

rejected = []
for user_id in users:
Expand All @@ -353,7 +353,7 @@ def approve_pending_users():


def clear_pending_users():
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]

response, status_code = auth.clear_pending_users_in_opa(token)
return response, status_code
Expand All @@ -364,7 +364,7 @@ def clear_pending_users():

@app.route('/user/<path:user_id>/authorize')
def list_programs_for_user(user_id):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
user_name = urllib.parse.unquote_plus(user_id)
response, status_code = auth.get_user_in_opa(user_name, token)
if status_code != 200:
Expand All @@ -374,9 +374,9 @@ def list_programs_for_user(user_id):


@app.route('/user/<path:user_id>/authorize')
def authorize_program_for_user(user_id):
program_dict = connexion.request.json
token = request.headers['Authorization'].split("Bearer ")[1]
async def authorize_program_for_user(user_id):
program_dict = await connexion.request.json()
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
user_name = urllib.parse.unquote_plus(user_id)
response, status_code = auth.get_user_in_opa(user_name, token)
if status_code != 200:
Expand All @@ -395,7 +395,7 @@ def authorize_program_for_user(user_id):

@app.route('/user/<path:user_id>/authorize/<path:program_id>')
def get_program_for_user(user_id, program_id):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
user_name = urllib.parse.unquote_plus(user_id)

response, status_code = auth.get_user_in_opa(user_name, token)
Expand All @@ -409,7 +409,7 @@ def get_program_for_user(user_id, program_id):

@app.route('/user/<path:user_id>/authorize/<path:program_id>')
def remove_program_for_user(user_id, program_id):
token = request.headers['Authorization'].split("Bearer ")[1]
token = connexion.request.headers['Authorization'].split("Bearer ")[1]
user_name = urllib.parse.unquote_plus(user_id)

response, status_code = auth.get_user_in_opa(user_name, token)
Expand All @@ -425,9 +425,9 @@ def remove_program_for_user(user_id, program_id):
@app.route('/get-token')
def get_token():
# Attempt to grab the token via session_id
if not hasattr(request, 'cookies'):
if not hasattr(connexion.request, 'cookies'):
return {'error': 'Unable to use the get-token endpoint without cookies'}, 200
token = request.cookies['session_id']
token = connexion.request.cookies['session_id']

return {"token": token}, 200

Expand Down
7 changes: 5 additions & 2 deletions requirements-container.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
-r requirements.txt
Flask==2.2.5
Flask==3.1.0
Flask-Cors==5.0.0
connexion[swagger-ui]==2.14.1
connexion==3.1.0
connexion[swagger-ui]
connexion[flask]
gunicorn>=23.0.0
uvicorn[standard]==0.30.6
werkzeug>=2.3.8 # not directly required, pinned by Snyk to avoid a vulnerability

0 comments on commit 4d5a822

Please sign in to comment.