Skip to content

Commit 7503e14

Browse files
committed
updating file management auth. api failsafe if env's are not present
1 parent 23e2698 commit 7503e14

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

services/api-v3/api/routes/object.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,46 @@
66
import starlette.requests
77
from api.database import get_async_session, get_engine
88
from api.routes.security import has_access
9-
from api.settings import settings
109
from fastapi import (
1110
APIRouter,
1211
Depends,
13-
File,
1412
HTTPException,
15-
Request,
16-
Response,
17-
UploadFile,
1813
)
1914
from minio import Minio
2015
from sqlalchemy import text
16+
from pydantic import Field
17+
from pydantic_settings import BaseSettings, SettingsConfigDict
18+
from functools import lru_cache
2119
from starlette.datastructures import UploadFile as StarletteUploadFile
2220

21+
22+
class FileSettings(BaseSettings):
23+
model_config = SettingsConfigDict(
24+
env_file=".env",
25+
env_file_encoding="utf-8",
26+
extra="ignore",
27+
)
28+
29+
s3_access_key: str = Field(alias="S3_ACCESS_KEY")
30+
s3_secret_key: str = Field(alias="S3_SECRET_KEY")
31+
s3_bucket: str = Field(alias="BUCKET")
32+
s3_endpoint: str = Field(alias="S3_HOST")
33+
s3_secure: bool = Field(default=True, alias="S3_SECURE")
34+
35+
36+
@lru_cache
37+
def get_file_settings() -> FileSettings:
38+
try:
39+
return FileSettings()
40+
except Exception as e:
41+
print(e)
42+
raise HTTPException(
43+
status_code=503,
44+
detail=f"File management is not configured.",
45+
)
46+
47+
48+
2349
router = APIRouter(
2450
prefix="/object",
2551
tags=["file"],
@@ -51,6 +77,7 @@ def sha256_of_uploadfile(
5177

5278

5379
def get_s3_client():
80+
settings = get_file_settings()
5481
return Minio(
5582
endpoint=settings.s3_endpoint,
5683
access_key=settings.s3_access_key,
@@ -66,6 +93,7 @@ def get_storage_host_bucket() -> tuple[str, str]:
6693
Also ensures host has no port (hostname only) if storage_host ever includes one.
6794
"""
6895
import urllib.parse
96+
settings = get_file_settings()
6997

7098
raw_host = settings.s3_endpoint
7199
parsed = urllib.parse.urlparse(
@@ -214,7 +242,6 @@ async def get_object(id: int):
214242
async def create_object(
215243
request: starlette.requests.Request,
216244
user_has_access: bool = Depends(has_access),
217-
object: UploadFile | None = File(default=None),
218245
):
219246
"""
220247
Upload to s3 and register in storage.objects.

services/api-v3/api/routes/security.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ async def redirect_callback(code: str, state: Optional[str] = None):
358358
)
359359

360360
# validate jwt https://dev.macrostrat.org/dev/me
361+
#TODO remove the groups and sub and add the user_name
361362
access_token = create_access_token(
362363
data={
363364
"sub": user.sub,
@@ -397,7 +398,7 @@ async def redirect_callback(code: str, state: Optional[str] = None):
397398
samesite=samesite,
398399
secure=secure,
399400
)
400-
401+
#TODO remove the token type
401402
refresh_jwt = jwt.encode(
402403
{
403404
"user_id": user.id,
@@ -463,6 +464,7 @@ async def refresh_token(
463464
else "web_user"
464465
)
465466
# setting new access cookie
467+
#TODO can remove groups, sub. add user_name.
466468
access_token = create_access_token(
467469
data={"sub": user.sub, "role": role, "user_id": user.id, "groups": list(ids)}
468470
)

services/api-v3/api/settings.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,24 @@
11
from pydantic import Field
22
from pydantic_settings import BaseSettings, SettingsConfigDict
33

4-
54
class Settings(BaseSettings):
6-
"""
7-
Centralized application settings for API v3.
8-
Values are loaded from environment variables and .env.
9-
"""
10-
115
model_config = SettingsConfigDict(
126
env_file=".env",
137
env_file_encoding="utf-8",
148
extra="ignore",
159
)
1610

17-
# Environment
1811
environment: str = Field(default="development", alias="ENVIRONMENT")
19-
20-
# Database
2112
database_uri: str = Field(alias="uri")
2213

23-
# OAuth
2414
redirect_uri: str = Field(alias="REDIRECT_URI_ENV")
2515
oauth_authorization_url: str = Field(alias="OAUTH_AUTHORIZATION_URL")
2616
oauth_token_url: str = Field(alias="OAUTH_TOKEN_URL")
2717
oauth_userinfo_url: str = Field(alias="OAUTH_USERINFO_URL")
2818
oauth_client_id: str = Field(alias="OAUTH_CLIENT_ID")
2919
oauth_client_secret: str = Field(alias="OAUTH_CLIENT_SECRET")
3020

31-
# JWT
3221
jwt_secret_key: str = Field(alias="SECRET_KEY")
33-
jwt_algorithm: str = Field(
34-
default="HS256",
35-
alias="JWT_ENCRYPTION_ALGORITHM",
36-
)
37-
38-
# S3 / MinIO
39-
s3_access_key: str = Field(alias="ACCESS_KEY")
40-
s3_secret_key: str = Field(alias="SECRET_KEY")
41-
s3_bucket: str = Field(alias="BUCKET")
42-
s3_endpoint: str = Field(alias="S3_HOST")
43-
s3_secure: bool = Field(default=True, alias="S3_SECURE")
44-
22+
jwt_algorithm: str = Field(default="HS256", alias="JWT_ENCRYPTION_ALGORITHM")
4523

4624
settings = Settings()

0 commit comments

Comments
 (0)