Skip to content

Commit 9ceadd2

Browse files
testing logging of sensitive values
1 parent 1ca3ad9 commit 9ceadd2

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

tests/unit/test_utils.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,91 @@ def test_resolve_value_from_config():
12791279
mock_info_logger.reset_mock()
12801280

12811281

1282+
class TestLogSagemakerConfig(TestCase):
1283+
1284+
def test_sensitive_info_masking(self):
1285+
logger = logging.getLogger('sagemaker.config')
1286+
logger.setLevel(logging.DEBUG)
1287+
1288+
stream_handler = logging.StreamHandler()
1289+
logger.addHandler(stream_handler)
1290+
1291+
# source value is None
1292+
with self.assertLogs(logger, level='DEBUG') as log:
1293+
_log_sagemaker_config_single_substitution(
1294+
None,
1295+
{"apiKey": "topsecretkey"},
1296+
"config/path"
1297+
)
1298+
1299+
self.assertIn("config value that will be used = {'apiKey': '***'}", log.output[0])
1300+
1301+
# source value is None and config_value == source_value
1302+
with self.assertLogs(logger, level='DEBUG') as log:
1303+
_log_sagemaker_config_single_substitution(
1304+
{"secretword": "topsecretword"},
1305+
{"secretword": "topsecretword"},
1306+
"config/path"
1307+
)
1308+
1309+
self.assertIn("Skipped value", log.output[0])
1310+
self.assertIn("source value that will be used = {'secretword': '***'}", log.output[0])
1311+
self.assertIn("config value = {'secretword': '***'}", log.output[0])
1312+
1313+
# source value is not None and config_value != source_value
1314+
with self.assertLogs(logger, level='DEBUG') as log:
1315+
_log_sagemaker_config_single_substitution(
1316+
{"password": "supersecretpassword"},
1317+
{"apiKey": "topsecretkey"},
1318+
"config/path"
1319+
)
1320+
1321+
self.assertIn("Skipped value", log.output[0])
1322+
self.assertIn("source value that will be used = {'password': '***'}", log.output[0])
1323+
self.assertIn("config value = {'apiKey': '***'}", log.output[0])
1324+
1325+
def test_non_sensitive_info_masking(self):
1326+
logger = logging.getLogger('sagemaker.config')
1327+
logger.setLevel(logging.DEBUG)
1328+
1329+
stream_handler = logging.StreamHandler()
1330+
logger.addHandler(stream_handler)
1331+
1332+
# source value is None
1333+
with self.assertLogs(logger, level='DEBUG') as log:
1334+
_log_sagemaker_config_single_substitution(
1335+
None,
1336+
{"username": "randomvalue"},
1337+
"config/path"
1338+
)
1339+
1340+
self.assertIn("config value that will be used = {'username': 'randomvalue'}", log.output[0])
1341+
1342+
# source value is not None and config_value == source_value
1343+
with self.assertLogs(logger, level='DEBUG') as log:
1344+
_log_sagemaker_config_single_substitution(
1345+
{"nonsensitivevalue": "randomvalue"},
1346+
{"nonsensitivevalue": "randomvalue"},
1347+
"config/path"
1348+
)
1349+
1350+
self.assertIn("Skipped value", log.output[0])
1351+
self.assertIn("source value that will be used = {'nonsensitivevalue': 'randomvalue'}", log.output[0])
1352+
self.assertIn("config value = {'nonsensitivevalue': 'randomvalue'}", log.output[0])
1353+
1354+
# source value is not None and config_value != source_value
1355+
with self.assertLogs(logger, level='DEBUG') as log:
1356+
_log_sagemaker_config_single_substitution(
1357+
{"username": "nonsensitiveinfo"},
1358+
{"configvalue": "nonsensitivevalue"},
1359+
"config/path/non_sensitive"
1360+
)
1361+
1362+
self.assertIn("Skipped value", log.output[0])
1363+
self.assertIn("source value that will be used = {'username': 'nonsensitiveinfo'}", log.output[0])
1364+
self.assertIn("config value = {'configvalue': 'nonsensitivevalue'}", log.output[0])
1365+
1366+
12821367
def test_get_sagemaker_config_value():
12831368
mock_config_logger = Mock()
12841369

0 commit comments

Comments
 (0)