-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from Uninett/feature.template_secrets_as_dict
Make TEMPLATE_SECRET_* variables also available as a dict
- Loading branch information
Showing
4 changed files
with
52 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Functions that aid in the building of Jinja template contexts""" | ||
import os | ||
|
||
|
||
def get_environment_secrets(prefix="TEMPLATE_SECRET_"): | ||
"""Returns a dictionary of secrets stored in environment variables""" | ||
template_secrets = {env: value for env, value in os.environ.items() if env.startswith(prefix)} | ||
# Also make secrets available as a dict, so keys can be constructed dynamically in templates | ||
template_secrets["TEMPLATE_SECRET"] = { | ||
env.replace(prefix, ""): value for env, value in template_secrets.items() | ||
} | ||
|
||
return template_secrets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
import unittest | ||
|
||
from cnaas_nms.tools.jinja_helpers import get_environment_secrets | ||
|
||
|
||
class EnvironmentSecretsTest(unittest.TestCase): | ||
def setUp(self) -> None: | ||
os.environ['TEMPLATE_SECRET_FOO'] = 'bar' | ||
os.environ['TEMPLATE_SECRET_TEST_TYPE'] = 'cromulent' | ||
os.environ['NOT_A_SECRET'] = 'foobar' | ||
|
||
def tearDown(self) -> None: | ||
del os.environ['TEMPLATE_SECRET_FOO'] | ||
del os.environ['TEMPLATE_SECRET_TEST_TYPE'] | ||
del os.environ['NOT_A_SECRET'] | ||
|
||
def test_that_individual_secrets_are_present(self): | ||
secrets = get_environment_secrets() | ||
self.assertEquals(secrets['TEMPLATE_SECRET_FOO'], 'bar') | ||
self.assertEquals(secrets['TEMPLATE_SECRET_TEST_TYPE'], 'cromulent') | ||
|
||
def test_that_secret_dict_is_set_properly(self): | ||
secrets = get_environment_secrets() | ||
self.assertIn('TEMPLATE_SECRET', secrets) | ||
|
||
secret_dict = secrets.get('TEMPLATE_SECRET') | ||
self.assertEquals(secret_dict['FOO'], 'bar') | ||
self.assertEquals(secret_dict['TEST_TYPE'], 'cromulent') | ||
|
||
def test_that_non_secret_variables_arent_included(self): | ||
secrets = get_environment_secrets() | ||
self.assertNotIn('NOT_A_SECRET', secrets) |