Skip to content

Commit b5a2813

Browse files
authored
Merge pull request #7 from datakind/sftp-integration
Sftp integration
2 parents 9600f61 + 6895cc1 commit b5a2813

File tree

4 files changed

+6
-55
lines changed

4 files changed

+6
-55
lines changed

src/worker/authn.py

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import jwt
22

3-
from fastapi.security import (
4-
OAuth2PasswordBearer,
5-
APIKeyHeader,
6-
)
3+
from fastapi.security import OAuth2PasswordBearer
74
from pydantic import BaseModel
85
from datetime import timedelta, datetime, timezone
96
from .config import env_vars
107
from typing import Annotated
11-
from fastapi import Depends, HTTPException, status, Security
8+
from fastapi import Depends, HTTPException, status
129
from jwt.exceptions import InvalidTokenError
1310

1411
oauth2_scheme = OAuth2PasswordBearer(
1512
tokenUrl="token",
1613
)
1714

18-
api_key_header = APIKeyHeader(name="X-API-KEY", scheme_name="api-key", auto_error=False)
19-
api_key_inst_header = APIKeyHeader(
20-
name="INST", scheme_name="api-inst", auto_error=False
21-
)
22-
# The following is for use by the frontend enduser only.
23-
api_key_enduser_header = APIKeyHeader(
24-
name="ENDUSER", scheme_name="api-enduser", auto_error=False
25-
)
26-
2715

2816
class Token(BaseModel):
2917
access_token: str
@@ -34,29 +22,6 @@ class TokenData(BaseModel):
3422
username: str | None = None
3523

3624

37-
def get_api_key(
38-
api_key_header: str = Security(api_key_header),
39-
api_key_inst_header: str = Security(api_key_inst_header),
40-
api_key_enduser_header: str = Security(api_key_enduser_header),
41-
) -> tuple:
42-
"""Retrieve the api key and enduser header key if present.
43-
44-
Args:
45-
api_key_header: The API key passed in the HTTP header.
46-
47-
Returns:
48-
A tuple with the api key and enduser header if present. Authentication happens elsewhere.
49-
Raises:
50-
HTTPException: If the API key is invalid or missing.
51-
"""
52-
if api_key_header:
53-
return (api_key_header, api_key_inst_header, api_key_enduser_header)
54-
raise HTTPException(
55-
status_code=status.HTTP_401_UNAUTHORIZED,
56-
detail="Invalid or missing API Key",
57-
)
58-
59-
6025
def check_creds(username: str, password: str) -> bool:
6126
if username == env_vars["USERNAME"] and password == env_vars["PASSWORD"]:
6227
return True

src/worker/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from dotenv import load_dotenv
55

66
# defaults to unit test values.
7-
os.environ["ENV_FILE_PATH"] = "/Users/oluwadolaposalako/sst-app-api/src/.env"
87

98
env_vars = {
109
"ENV": "LOCAL",

src/worker/main.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import logging
55
from typing import Any, Annotated
6-
from fastapi import FastAPI, Depends, HTTPException, status, Security
6+
from fastapi import FastAPI, Depends, HTTPException, status
77
from fastapi.responses import FileResponse
88

99
from pydantic import BaseModel
@@ -15,13 +15,7 @@
1515
fetch_institution_ids,
1616
)
1717
from .config import sftp_vars, env_vars, startup_env_vars
18-
from .authn import (
19-
Token,
20-
get_current_username,
21-
check_creds,
22-
create_access_token,
23-
get_api_key,
24-
)
18+
from .authn import Token, get_current_username, check_creds, create_access_token
2519
from datetime import timedelta
2620
import os
2721

@@ -150,7 +144,6 @@ async def execute_pdp_pull(
150144
req: PdpPullRequest,
151145
current_username: Annotated[str, Depends(get_current_username)],
152146
storage_control: Annotated[StorageControl, Depends(StorageControl)],
153-
api_key_enduser_tuple: str = Security(get_api_key),
154147
) -> Any:
155148
"""Performs the PDP pull of the file."""
156149
storage_control.create_bucket_if_not_exists(get_sftp_bucket_name(env_vars["ENV"]))
@@ -172,9 +165,7 @@ async def execute_pdp_pull(
172165

173166
temp_valid_pdp_ids, temp_invalid_ids = fetch_institution_ids(
174167
pdp_ids=list(signed_urls.keys()),
175-
backend_api_key=next(
176-
key for key in api_key_enduser_tuple if key is not None
177-
),
168+
backend_api_key=env_vars["BACKEND_API_KEY"],
178169
)
179170

180171
valid_pdp_ids.append(temp_valid_pdp_ids)

src/worker/main_test.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from fastapi.testclient import TestClient
77
from .main import app
8-
from .authn import get_current_username, get_api_key
8+
from .authn import get_current_username
99
from unittest import mock
1010
from .utilities import StorageControl
1111

@@ -20,14 +20,10 @@ def get_current_username_override():
2020
def storage_control_override():
2121
return MOCK_STORAGE
2222

23-
def get_api_key_override():
24-
return ("valid_api_key", "end_user")
25-
2623
app.dependency_overrides[StorageControl] = storage_control_override
2724

2825
app.dependency_overrides[get_current_username] = get_current_username_override
2926

30-
app.dependency_overrides[get_api_key] = get_api_key_override
3127
client = TestClient(app, root_path="/workers/api/v1")
3228
yield client
3329
app.dependency_overrides.clear()

0 commit comments

Comments
 (0)