Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
FROM python:3.12.10-alpine3.21@sha256:9c51ecce261773a684c8345b2d4673700055c513b4d54bc0719337d3e4ee552e as dev

# Ensure git is available to install common repo from github
RUN apk update
RUN apk add git

WORKDIR /app

COPY pyproject.toml requirements.txt ./
Expand Down Expand Up @@ -29,6 +33,10 @@ CMD ["pytest", "--config-file", "test/pytest.ini", "-v"]

FROM python:3.12.10-alpine3.21@sha256:9c51ecce261773a684c8345b2d4673700055c513b4d54bc0719337d3e4ee552e as prod

# Ensure git is available to install common repo from github
RUN apk update
RUN apk add git

WORKDIR /app

COPY requirements.txt ./
Expand Down
Empty file.
72 changes: 0 additions & 72 deletions object_storage_api/auth/jwt_middleware.py

This file was deleted.

33 changes: 3 additions & 30 deletions object_storage_api/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"""

from pathlib import Path
from typing import List, Optional
from typing import List

from pydantic import BaseModel, ConfigDict, Field, SecretStr, ValidationInfo, field_validator
from ims_common.config import AuthenticationConfig
from pydantic import BaseModel, ConfigDict, SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict


Expand All @@ -26,34 +27,6 @@ class APIConfig(BaseModel):
# TODO: Some of this file is identical to the one in inventory-management-system-api - Use common repo?


class AuthenticationConfig(BaseModel):
"""
Configuration model for the JWT access token authentication/authorization.
"""

enabled: bool
public_key_path: Optional[str] = Field(default=None, validate_default=True)
jwt_algorithm: Optional[str] = Field(default=None, validate_default=True)

@field_validator("public_key_path", "jwt_algorithm")
@classmethod
def validate_optional_fields(cls, field_value: str, info: ValidationInfo) -> Optional[str]:
"""
Validator for the `public_key_path` and `jwt_algorithm` fields to make them mandatory if the value of the
`enabled` is `True`

It checks if the `enabled` field has been set to `True` and raises a `TypeError` if this is the case.

:param field_value: The value of the field.
:param info: Validation info from pydantic.
:raises ValueError: If no value is provided for the field when `enabled` is set to `True`.
:return: The value of the field.
"""
if ("enabled" in info.data and info.data["enabled"] is True) and field_value is None:
raise ValueError("Field required")
return field_value


class DatabaseConfig(BaseModel):
"""
Configuration model for the database.
Expand Down
18 changes: 0 additions & 18 deletions object_storage_api/core/consts.py

This file was deleted.

28 changes: 1 addition & 27 deletions object_storage_api/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,7 @@
from typing import Optional

from fastapi import status


class BaseAPIException(Exception):
"""Base exception for API errors."""

# Status code to return if this exception is raised
status_code: int

# Generic detail of the exception (That may be returned in a response)
response_detail: str

detail: str

def __init__(self, detail: str, response_detail: Optional[str] = None):
"""
Initialise the exception.

:param detail: Specific detail of the exception (just like Exception would take - this will only be logged
and not returned in a response).
:param response_detail: Generic detail of the exception that will be returned in a response.
"""
super().__init__(detail)

self.detail = detail

if response_detail is not None:
self.response_detail = response_detail
from ims_common.exceptions import BaseAPIException


class DatabaseError(BaseAPIException):
Expand Down
4 changes: 2 additions & 2 deletions object_storage_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ async def custom_general_exception_handler(_: Request, exc: Exception) -> JSONRe
# Add authentication middleware & dependency if enabled
if config.authentication.enabled is True:
# pylint:disable=import-outside-toplevel
from object_storage_api.auth.jwt_middleware import JWTMiddleware, security
from ims_common.jwt_middleware import create_jwt_middleware, security

app.add_middleware(JWTMiddleware)
app.add_middleware(create_jwt_middleware(config.authentication))

# This router dependency is still needed for the swagger docs show the authorise button, even though the actual
# auth is done in the middleware
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ dependencies = [
"PyJWT",
"pymongo",
"boto3",
"Pillow"
"Pillow",
"ims-common@git+https://github.com/joelvdavies/common-repo-test#egg=main"
]

[project.urls]
Expand Down