Skip to content

Commit 7d2e472

Browse files
authored
test: Safe printing of secrets (#306)
1 parent 88c4d78 commit 7d2e472

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

tests/integration/conftest.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,38 @@
2121
SERVICE_SECRET_ENV = "SERVICE_SECRET"
2222

2323

24+
class Secret:
25+
"""
26+
Class to hold sensitive data in testing. This prevents passwords
27+
and such to be printed in logs or any other reports.
28+
More info: https://github.com/pytest-dev/pytest/issues/8613
29+
30+
NOTE: be careful, assert Secret("") == "" would still print
31+
on failure
32+
"""
33+
34+
def __init__(self, value):
35+
self.value = value
36+
37+
def __repr__(self):
38+
return "Secret(********)"
39+
40+
def __str___(self):
41+
return "*******"
42+
43+
2444
def must_env(var_name: str) -> str:
2545
assert var_name in environ, f"Expected {var_name} to be provided in environment"
2646
LOGGER.info(f"{var_name}: {environ[var_name]}")
2747
return environ[var_name]
2848

2949

3050
@fixture(scope="session")
31-
def rm_settings(api_endpoint, username, password) -> Settings:
51+
def rm_settings(api_endpoint, username: str, password: Secret) -> Settings:
3252
return Settings(
3353
server=api_endpoint,
3454
user=username,
35-
password=password,
55+
password=password.value,
3656
default_region="us-east-1",
3757
)
3858

@@ -69,7 +89,7 @@ def username() -> str:
6989

7090
@fixture(scope="session")
7191
def password() -> str:
72-
return must_env(PASSWORD_ENV)
92+
return Secret(must_env(PASSWORD_ENV))
7393

7494

7595
@fixture(scope="session")
@@ -89,14 +109,14 @@ def service_id() -> str:
89109

90110
@fixture(scope="session")
91111
def service_secret() -> str:
92-
return must_env(SERVICE_SECRET_ENV)
112+
return Secret(must_env(SERVICE_SECRET_ENV))
93113

94114

95115
@fixture
96-
def service_auth(service_id, service_secret) -> ServiceAccount:
97-
return ServiceAccount(service_id, service_secret)
116+
def service_auth(service_id: str, service_secret: Secret) -> ServiceAccount:
117+
return ServiceAccount(service_id, service_secret.value)
98118

99119

100120
@fixture(scope="session")
101-
def password_auth(username, password) -> UsernamePassword:
102-
return UsernamePassword(username, password)
121+
def password_auth(username: str, password: Secret) -> UsernamePassword:
122+
return UsernamePassword(username, password.value)

0 commit comments

Comments
 (0)