UCL-ARC / dirac-swift-api

Repository for the REST API side of the DiRAC-SWIFT project
BSD 3-Clause "New" or "Revised" License
2 stars 1 forks source link

Initialising `Settings` requires argument `jwt_secret_key` #42

Closed kyleaoman closed 10 months ago

kyleaoman commented 11 months ago

I'm following along the instructions to get started and have run into a bug (?). Initialising the Settings object defined in src/api/config.py requires a jwt_secret_key argument, otherwise pydantic raises a ValidationError. However, we there's an attempted initialisation with no arguments in the code (https://github.com/UCL-ARC/dirac-swift-api/blob/55feb15aa0ea3fedfbfd45f55dbe659a203042fb/src/api/routers/auth.py#L29C22-L29C22)

Upshot is that when I try to run src/api/main.py I get a ValidationError:

dc-oman1@dataweb> python src/api/main.py
Traceback (most recent call last):
  File "/cosma/home/durham/dc-oman1/code/dirac-swift-api/src/api/main.py", line 9, in <module>
    from api.routers import auth, file_processing
  File "/cosma/home/durham/dc-oman1/.virtualenv/remote-api/lib/python3.10/site-packages/api/routers/auth.py", line 104, in <module>
    settings: Settings = get_settings(),
  File "/cosma/home/durham/dc-oman1/.virtualenv/remote-api/lib/python3.10/site-packages/api/routers/auth.py", line 29, in get_settings
    return Settings()
  File "/cosma/home/durham/dc-oman1/.virtualenv/remote-api/lib/python3.10/site-packages/pydantic_settings/main.py", line 71, in __init__
    super().__init__(
  File "/cosma/home/durham/dc-oman1/.virtualenv/remote-api/lib/python3.10/site-packages/pydantic/main.py", line 164, in __init__
    __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
jwt_secret_key
  Field required [type=missing, input_value={}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.4/v/missing

The test suite passes and presumably the Settings() call comes up there, so perhaps there's some setup step that isn't documented and that I'm missing?

kyleaoman commented 11 months ago

Looks like I need to set an environment variable JWT_SECRET_KEY (or perhaps create a .env file). For now export JWT_SECRET_KEY="" has at least led to successfully starting up an api server.

I don't understand yet whether the api server needs itself to send requests to the authentication server - if not then perhaps it should be possible to start up the server without providing a jwt_secret_key.

kyleaoman commented 11 months ago

From what I can tell this should not be required on the server side, I'll try and come up with a sensible patch.

harryjmoss commented 10 months ago

Have updated the docs and .env.example to reflect this - should be fixed in #44

The secret key is needed to create (and then decode) the JSON web tokens to check user auth status, so will be needed.