Skip to content

Commit 80dffbb

Browse files
Add list variables to config_helper
1 parent 77d0fe3 commit 80dffbb

6 files changed

Lines changed: 41 additions & 14 deletions

File tree

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9.16

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
2-
## v1.1.0
2+
## v1.1.2
3+
- Update config_helper to accept list environment variables
4+
5+
## v1.1.0/v1.1.1
36
- Add retries for empty responses in oauth2 client. This was added to address a known quirk in the Sierra API where this response is returned:
47
```
58
> GET / HTTP/1.1
@@ -8,6 +11,7 @@
811
> Accept: */*
912
>
1013
```
14+
- Due to an accidental deployment, v1.1.0 and v1.1.1 were both released but are identical
1115

1216
## v1.0.4 - 6/28/23
1317
- Enforce Kinesis stream 1000 records/second write limit

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ kinesis_client = KinesisClient(...)
3535
# Do not use any version below 1.0.0
3636
# All available optional dependencies can be found in pyproject.toml.
3737
# See the "Managing dependencies" section below for more details.
38-
nypl-py-utils[kinesis-client,config-helper]==1.0.4
38+
nypl-py-utils[kinesis-client,config-helper]==1.1.2
3939
```
4040

4141
## Developing locally
4242
In order to use the local version of the package instead of the global version, use a virtual environment. To set up a virtual environment and install all the necessary dependencies, run:
4343

4444
```
45-
python3 -m venv testenv
46-
source testenv/bin/activate
45+
python3 -m venv .venv
46+
source .venv/bin/activate
4747
pip install --upgrade pip
4848
pip install .
4949
pip install '.[development]'
50-
deactivate && source testenv/bin/activate
50+
deactivate && source .venv/bin/activate
5151
```
5252

5353
## Managing dependencies
@@ -61,7 +61,7 @@ The optional dependency sets also give the developer the option to manually list
6161
#### Using PostgreSQLClient in an AWS Lambda
6262
Because `psycopg` requires a statically linked version of the `libpq` library, the `PostgreSQLClient` cannot be installed as-is in an AWS Lambda function. Instead, it must be packaged as follows:
6363
```bash
64-
pip install --target ./package nypl-py-utils[postgresql-client]==1.0.1
64+
pip install --target ./package nypl-py-utils[postgresql-client]==1.1.2
6565

6666
pip install \
6767
--platform manylinux2014_x86_64 \

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "nypl_py_utils"
7-
version = "1.0.4"
7+
version = "1.1.2"
88
authors = [
99
{ name="Aaron Friedman", email="aaronfriedman@nypl.org" },
1010
]

src/nypl_py_utils/functions/config_helper.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import yaml
34

@@ -11,7 +12,8 @@ def load_env_file(run_type, file_string):
1112
"""
1213
This method loads a YAML config file containing environment variables,
1314
decrypts whichever are encrypted, and puts them all into os.environ as
14-
strings.
15+
strings. For a YAML variable containing a list of values, the list is
16+
exported into os.environ as a json string and should be loaded as such.
1517
1618
It requires the YAML file to be split into a 'PLAINTEXT_VARIABLES' section
1719
and an 'ENCRYPTED_VARIABLES' section.
@@ -43,11 +45,18 @@ def load_env_file(run_type, file_string):
4345

4446
if env_dict:
4547
for key, value in env_dict.get('PLAINTEXT_VARIABLES', {}).items():
46-
os.environ[key] = str(value)
48+
if type(value) is list:
49+
os.environ[key] = json.dumps(value)
50+
else:
51+
os.environ[key] = str(value)
4752

4853
kms_client = KmsClient()
4954
for key, value in env_dict.get('ENCRYPTED_VARIABLES', {}).items():
50-
os.environ[key] = kms_client.decrypt(value)
55+
if type(value) is list:
56+
decrypted_list = [kms_client.decrypt(v) for v in value]
57+
os.environ[key] = json.dumps(decrypted_list)
58+
else:
59+
os.environ[key] = kms_client.decrypt(value)
5160
kms_client.close()
5261

5362

tests/test_config_helper.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44
from nypl_py_utils.functions.config_helper import (
55
load_env_file, ConfigHelperError)
66

7-
_TEST_VARIABLE_NAMES = ['TEST_STRING', 'TEST_INT', 'TEST_ENCRYPTED_VARIABLE_1',
8-
'TEST_ENCRYPTED_VARIABLE_2']
7+
_TEST_VARIABLE_NAMES = [
8+
'TEST_STRING', 'TEST_INT', 'TEST_LIST', 'TEST_ENCRYPTED_VARIABLE_1',
9+
'TEST_ENCRYPTED_VARIABLE_2', 'TEST_ENCRYPTED_LIST']
10+
911
_TEST_CONFIG_CONTENTS = \
1012
'''---
1113
PLAINTEXT_VARIABLES:
1214
TEST_STRING: string-variable
1315
TEST_INT: 1
16+
TEST_LIST:
17+
- string-var
18+
- 2
1419
ENCRYPTED_VARIABLES:
1520
TEST_ENCRYPTED_VARIABLE_1: test-encryption-1
1621
TEST_ENCRYPTED_VARIABLE_2: test-encryption-2
22+
TEST_ENCRYPTED_LIST:
23+
- test-encryption-3
24+
- test-encryption-4
1725
...'''
1826

1927

@@ -22,7 +30,8 @@ class TestConfigHelper:
2230
def test_load_env_file(self, mocker):
2331
mock_kms_client = mocker.MagicMock()
2432
mock_kms_client.decrypt.side_effect = [
25-
'test-decryption-1', 'test-decryption-2']
33+
'test-decryption-1', 'test-decryption-2', 'test-decryption-3',
34+
'test-decryption-4']
2635
mocker.patch('nypl_py_utils.functions.config_helper.KmsClient',
2736
return_value=mock_kms_client)
2837
mock_file_open = mocker.patch(
@@ -34,13 +43,17 @@ def test_load_env_file(self, mocker):
3443

3544
mock_file_open.assert_called_once_with('test-path/test-env.yaml', 'r')
3645
mock_kms_client.decrypt.assert_has_calls([
37-
mocker.call('test-encryption-1'), mocker.call('test-encryption-2')]
46+
mocker.call('test-encryption-1'), mocker.call('test-encryption-2'),
47+
mocker.call('test-encryption-3'), mocker.call('test-encryption-4')]
3848
)
3949
mock_kms_client.close.assert_called_once()
4050
assert os.environ['TEST_STRING'] == 'string-variable'
4151
assert os.environ['TEST_INT'] == '1'
52+
assert os.environ['TEST_LIST'] == '["string-var", 2]'
4253
assert os.environ['TEST_ENCRYPTED_VARIABLE_1'] == 'test-decryption-1'
4354
assert os.environ['TEST_ENCRYPTED_VARIABLE_2'] == 'test-decryption-2'
55+
assert os.environ['TEST_ENCRYPTED_LIST'] == \
56+
'["test-decryption-3", "test-decryption-4"]'
4457

4558
for key in _TEST_VARIABLE_NAMES:
4659
if key in os.environ:

0 commit comments

Comments
 (0)