forked from mage-ai/mage-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[xy] Mask env var values with stars in terminal output (mage-ai#2201)
* [xy] Hide environment variables. * [xy] Add helper file to filter out env var values. * [xy] Add HIDE_ENV_VAR_VALUES variable. * [xy] Add unit test.
- Loading branch information
1 parent
28f9cfd
commit 2bc653d
Showing
4 changed files
with
52 additions
and
0 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
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,14 @@ | ||
import os | ||
|
||
MIN_SECRET_ENV_VAR_LENGTH = 5 | ||
|
||
|
||
def filter_out_env_var_values(value: str): | ||
env_var_values = dict(os.environ).values() | ||
env_var_values = [v for v in env_var_values if v and len(v) >= MIN_SECRET_ENV_VAR_LENGTH] | ||
env_var_values.sort(key=len, reverse=True) | ||
value_clean = value | ||
for env_var_value in env_var_values: | ||
replace_value = '*' * len(env_var_value) | ||
value_clean = value_clean.replace(env_var_value, replace_value) | ||
return value_clean |
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,23 @@ | ||
from mage_ai.shared.security import filter_out_env_var_values | ||
from mage_ai.tests.base_test import TestCase | ||
from unittest.mock import patch | ||
import os | ||
|
||
|
||
MOCK_ENV_VARS = { | ||
'VAR1': '123', | ||
'VAR2': '45678', | ||
'VAR3': 'abcdefg', | ||
'VAR4': '', | ||
} | ||
|
||
|
||
class SecurityTests(TestCase): | ||
@patch.dict(os.environ, MOCK_ENV_VARS) | ||
def test_filter_out_env_var_values(self): | ||
value1 = filter_out_env_var_values('12345678abcdefghij') | ||
value2 = filter_out_env_var_values('testdata') | ||
value3 = filter_out_env_var_values('test45645678') | ||
self.assertEqual(value1, '123************hij') | ||
self.assertEqual(value2, 'testdata') | ||
self.assertEqual(value3, 'test456*****') |