diff --git a/oda_api/secret.py b/oda_api/secret.py new file mode 100644 index 00000000..374d736e --- /dev/null +++ b/oda_api/secret.py @@ -0,0 +1,12 @@ +from typing import Optional +import os + +def get_secret(secret_name: str) -> Optional[str]: + # Get secret by name + # For now only default renku file secret storage is supported whuch stores secrets as plain text + + secrets_dir = os.getenv('ODA_SECRET_STORAGE', '/secrets') # check for default secret location in renku platform + secrets_file = os.path.join(secrets_dir, secret_name) + if os.path.isfile(secrets_file): + with open(secrets_file, 'r') as f: + return f.read() diff --git a/tests/test_secret.py b/tests/test_secret.py new file mode 100644 index 00000000..4c65fa05 --- /dev/null +++ b/tests/test_secret.py @@ -0,0 +1,16 @@ +import os +import pytest +from oda_api.secret import get_secret + +@pytest.fixture +def secrets_path(tmp_path): + os.environ['ODA_SECRET_STORAGE'] = str(tmp_path) + yield tmp_path + del os.environ['ODA_SECRET_STORAGE'] + +def test_renku_secret(secrets_path): + secret = 'secret' + secret_name = 's' + with open(secrets_path / secret_name, 'w') as f: + f.write(secret) + assert get_secret(secret_name) == secret