Skip to content

Commit

Permalink
feat: dynamic MongoDB config via PYEUDIW_MONGO_TEST_AUTH_INLINE
Browse files Browse the repository at this point in the history
- Replaced hardcoded MongoDB credentials with dynamic env variable.
- Added fallback to 'satosa:thatpassword' for unauthenticated setups.
- Updated config to parse username/password inline.
- Documented usage and default behavior.
  • Loading branch information
LadyCodesItBetter committed Nov 20, 2024
1 parent e02393b commit 8431fa8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
30 changes: 27 additions & 3 deletions example/satosa/integration_test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,39 @@ This integration test will verify a full authentication flow of a simulated IT-W
### Environment

An up an running Openid4VP Relying Party is a requirement of this project.
The intended Relying Party of this integration test is the example one provided in the repostiory [https://github.com/italia/Satosa-Saml2Spid](https://github.com/italia/Satosa-Saml2Spid).
The intended Relying Party of this integration test is the example one provided in the repository [https://github.com/italia/Satosa-Saml2Spid](https://github.com/italia/Satosa-Saml2Spid).
That project will provide full instruction on how to setup such an environment with Docker.

Before starting, make sure that the `pyeudiw_backend.yaml` is properly configured and included in the file `proxy_conf.yaml` that is running in your Docker environemnt.
Before starting, make sure that the `pyeudiw_backend.yaml` is properly configured and included in the file `proxy_conf.yaml` that is running in your Docker environment.
This project folder always provide up to date example of the pyeudiw plugin configuration in the file [pyeudiw_backend.yaml](./pyeudiw_backend.yaml), as well as other configuration file of the module in [static](./static/) and [template](./template/) folders.

#### MongoDB Configuration for Tests

The MongoDB connection is configured dynamically using the environment variable `PYEUDIW_MONGO_TEST_AUTH_INLINE`.

#### How It Works
- The value of `PYEUDIW_MONGO_TEST_AUTH_INLINE` should be in the format `username:password`.
- If the variable is not set, the configuration defaults to:
- **Authentication**: Defaults to `satosa:thatpassword`.
- **MongoDB URL**: `mongodb://satosa:thatpassword@localhost:27017/?timeoutMS=2000`.

#### Example Usage
1. **With Authentication**:
Set the environment variable:
```bash
export PYEUDIW_MONGO_TEST_AUTH_INLINE="satosa:thatpassword"
```

#### Custom Behavior
You can override the default credentials by setting the environment variable:

```bash
export PYEUDIW_MONGO_TEST_AUTH_INLINE="customuser:custompassword"
```

### Dependencies

Requirements eexclusive to the integration test can be installed with
Requirements exclusive to the integration test can be installed with

pip install -r requirements_test.txt

Expand Down
7 changes: 4 additions & 3 deletions example/satosa/integration_test/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from cryptojwt.jws.jws import JWS
from cryptojwt.jwk.jwk import key_from_jwk_dict
from pyeudiw.tests.federation.base import (
Expand All @@ -21,16 +22,16 @@
"class": "MongoStorage",
"init_params": {
# according to Satosa-Saml2Spid demo
"url": "mongodb://satosa:thatpassword@localhost:27017/?timeoutMS=2000",
"url": f"mongodb://{os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword')}@localhost:27017/?timeoutMS=2000",
"conf": {
"db_name": "eudiw",
"db_sessions_collection": "sessions",
"db_trust_attestations_collection": "trust_attestations",
"db_trust_anchors_collection": "trust_anchors"
},
"connection_params": {
"username": "satosa",
"password": "thatpassword"
"username": os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword').split(':')[0],
"password": os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword').split(':')[1] if ':' in os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword') else ""
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions pyeudiw/tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pathlib

from pyeudiw.tools.utils import exp_from_now, iat_now
Expand Down Expand Up @@ -157,7 +158,7 @@
"class": "MongoCache",
"init_params": {
# according to Satosa-Saml2Spid demo
"url": "mongodb://satosa:thatpassword@localhost:27017/?timeoutMS=2000",
"url": f"mongodb://{os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword')}@localhost:27017/?timeoutMS=2000",
"conf": {
"db_name": "eudiw"
},
Expand All @@ -169,7 +170,7 @@
"class": "MongoStorage",
"init_params": {
# according to Satosa-Saml2Spid demo
"url": "mongodb://satosa:thatpassword@localhost:27017/?timeoutMS=2000",
"url": f"mongodb://{os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword')}@localhost:27017/?timeoutMS=2000",
"conf": {
"db_name": "test-eudiw",
"db_sessions_collection": "sessions",
Expand Down Expand Up @@ -462,7 +463,7 @@
"class": "MongoCache",
"init_params": {
# according to Satosa-Saml2Spid demo
"url": "mongodb://satosa:thatpassword@localhost:27017/?timeoutMS=2000",
"url": f"mongodb://{os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword')}@localhost:27017/?timeoutMS=2000",
"conf": {
"db_name": "eudiw"
},
Expand All @@ -474,7 +475,7 @@
"class": "MongoStorage",
"init_params": {
# according to Satosa-Saml2Spid demo
"url": "mongodb://satosa:thatpassword@localhost:27017/?timeoutMS=2000",
"url": f"mongodb://{os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword')}@localhost:27017/?timeoutMS=2000",
"conf": {
"db_name": "test-eudiw",
"db_sessions_collection": "sessions",
Expand Down
3 changes: 2 additions & 1 deletion pyeudiw/tests/storage/test_mongo_cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import uuid

import pytest
Expand All @@ -10,7 +11,7 @@ class TestMongoCache:
def create_storage_instance(self):
self.cache = MongoCache(
{"db_name": "eudiw"},
"mongodb://satosa:thatpassword@localhost:27017/",
f"mongodb://{os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword')}@localhost:27017/?timeoutMS=2000",
{}
)

Expand Down
3 changes: 2 additions & 1 deletion pyeudiw/tests/storage/test_mongo_storage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import uuid
import time
import pytest
Expand All @@ -15,7 +16,7 @@ def create_storage_instance(self):
"db_trust_attestations_collection": "trust_attestations",
"db_trust_anchors_collection": "trust_anchors"
},
"mongodb://satosa:thatpassword@localhost:27017/",
f"mongodb://{os.getenv('PYEUDIW_MONGO_TEST_AUTH_INLINE', 'satosa:thatpassword')}@localhost:27017/?timeoutMS=2000",
{}
)

Expand Down

0 comments on commit 8431fa8

Please sign in to comment.