Skip to content

Commit

Permalink
changed path in maintenance core to value in APIConfig class #88
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuarnaccia5 committed Jun 24, 2024
1 parent 1948249 commit 8cc1673
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ Listed below are the environment variables supported by the application.
| `AUTHENTICATION__JWT_ALGORITHM` | The algorithm to use to decode the JWT access and refresh tokens. | Yes | |
| `AUTHENTICATION__ACCESS_TOKEN_VALIDITY_MINUTES` | Minutes after which the JWT access token expires. | Yes | |
| `AUTHENTICATION__REFRESH_TOKEN_VALIDITY_DAYS` | Days after which the JWT refresh token expires. | Yes | |
| `AUTHENTICATION__ACTIVE_USERNAMES_PATH` | The path to the `txt` file containing the active usernames and defining who can use this service. | Yes | |
| `AUTHENTICATION__ACTIVE_USERNAMES_PATH` | The path to the `txt` file containing the active usernames and defining who can use this service. | Yes |
| `MAINTENANCE__MAINTENANCE_PATH` | The path to the `json` file containing the maintenance state. | Yes | |
| `MAINTENANCE__SCHEDULED_MAINTENANCE_PATH` | The path to the `json` file containing the scheduled maintenance state. | Yes |
| `LDAP_SERVER__URL` | The URL to the LDAP server to connect to. | Yes | |
| `LDAP_SERVER__REALM` | The realm for the LDAP server. | Yes | |

Expand Down
2 changes: 2 additions & 0 deletions ldap_jwt_auth/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ AUTHENTICATION__JWT_ALGORITHM=RS256
AUTHENTICATION__ACCESS_TOKEN_VALIDITY_MINUTES=5
AUTHENTICATION__REFRESH_TOKEN_VALIDITY_DAYS=7
AUTHENTICATION__ACTIVE_USERNAMES_PATH=./active_usernames.txt
MAINTENANCE__MAINTENANCE_PATH=./maintenance/maintenance.json
MAINTENANCE__SCHEDULED_MAINTENANCE_PATH=./maintenance/scheduled_maintenance.json
LDAP_SERVER__URL=ldaps://ldap.example.com:636
LDAP_SERVER__REALM=LDAP.EXAMPLE.COM
LDAP_SERVER__CERTIFICATE_VALIDATION=true
Expand Down
7 changes: 7 additions & 0 deletions ldap_jwt_auth/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class APIConfig(BaseModel):
allowed_cors_origins: List[str]
allowed_cors_methods: List[str]

class MaintenanceConfig(BaseModel):
"""
Configuration model for maintenance
"""
maintenance_path: str
scheduled_maintenance_path: str

class AuthenticationConfig(BaseModel):
"""
Expand Down Expand Up @@ -82,6 +88,7 @@ class Config(BaseSettings):
api: APIConfig
authentication: AuthenticationConfig
ldap_server: LDAPServerConfig
maintenance: MaintenanceConfig

model_config = SettingsConfigDict(
env_file=Path(__file__).parent.parent / ".env",
Expand Down
5 changes: 3 additions & 2 deletions ldap_jwt_auth/core/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json

from pydantic import ValidationError
from ldap_jwt_auth.core.config import config
from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MissingMaintenanceFileError
from ldap_jwt_auth.core.schemas import MaintenanceState, ScheduledMaintenanceState

Expand All @@ -22,7 +23,7 @@ def get_maintenance_state(self) -> MaintenanceState:
:raises MissingMaintenanceFileError: If the maintenance state file can not be found or read
"""
try:
with open("maintenance/maintenance.json", "r", encoding='utf-8') as file:
with open(config.maintenance.maintenance_path, "r", encoding='utf-8') as file:
data = json.load(file)
return MaintenanceState(**data)
except IOError as exc:
Expand All @@ -39,7 +40,7 @@ def get_scheduled_maintenance_state(self) -> ScheduledMaintenanceState:
:raises MissingMaintenanceFileError: If the scheduled maintenance state file can not be found or read
"""
try:
with open("maintenance/scheduled_maintenance.json", "r", encoding='utf-8') as file:
with open(config.maintenance.scheduled_maintenance_path, "r", encoding='utf-8') as file:
data = json.load(file)
return ScheduledMaintenanceState(**data)
except IOError as exc:
Expand Down

0 comments on commit 8cc1673

Please sign in to comment.