forked from All-Hands-AI/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_logging.py
120 lines (95 loc) Β· 3.92 KB
/
test_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import logging
from io import StringIO
from unittest.mock import patch
import pytest
from openhands.core.config import AppConfig, LLMConfig
from openhands.core.logger import openhands_logger as openhands_logger
@pytest.fixture
def test_handler():
stream = StringIO()
handler = logging.StreamHandler(stream)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
openhands_logger.addHandler(handler)
yield openhands_logger, stream
openhands_logger.removeHandler(handler)
def test_openai_api_key_masking(test_handler):
logger, stream = test_handler
api_key = 'sk-1234567890abcdef'
message = f"OpenAI API key: api_key='{api_key}'and there's some stuff here"
logger.info(message)
log_output = stream.getvalue()
assert api_key not in log_output
def test_azure_api_key_masking(test_handler):
logger, stream = test_handler
api_key = '1a2b3c4d5e6f7g8h9i0j'
message = f"Azure API key: api_key='{api_key}' and chatty chat with ' and \" and '"
logger.info(message)
log_output = stream.getvalue()
assert api_key not in log_output
def test_google_vertex_api_key_masking(test_handler):
logger, stream = test_handler
api_key = 'AIzaSyA1B2C3D4E5F6G7H8I9J0'
message = f"Google Vertex API key: api_key='{api_key}' or not"
logger.info(message)
log_output = stream.getvalue()
assert api_key not in log_output
def test_anthropic_api_key_masking(test_handler):
logger, stream = test_handler
api_key = 'sk-ant-1234567890abcdef-some-more-stuff-here'
message = f"Anthropic API key: api_key='{api_key}' and there's some 'stuff' here"
logger.info(message)
log_output = stream.getvalue()
assert api_key not in log_output
def test_llm_config_attributes_masking(test_handler):
logger, stream = test_handler
llm_config = LLMConfig(
api_key='sk-abc123',
aws_access_key_id='AKIAIOSFODNN7EXAMPLE',
aws_secret_access_key='wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
)
logger.info(f'LLM Config: {llm_config}')
log_output = stream.getvalue()
assert 'sk-abc123' not in log_output
assert 'AKIAIOSFODNN7EXAMPLE' not in log_output
assert 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY' not in log_output
def test_app_config_attributes_masking(test_handler):
logger, stream = test_handler
app_config = AppConfig(e2b_api_key='e2b-xyz789')
logger.info(f'App Config: {app_config}')
log_output = stream.getvalue()
assert 'github_token' not in log_output
assert 'e2b-xyz789' not in log_output
assert 'ghp_abcdefghijklmnopqrstuvwxyz' not in log_output
def test_sensitive_env_vars_masking(test_handler):
logger, stream = test_handler
environ = {
'API_KEY': 'API_KEY_VALUE',
'AWS_ACCESS_KEY_ID': 'AWS_ACCESS_KEY_ID_VALUE',
'AWS_SECRET_ACCESS_KEY': 'AWS_SECRET_ACCESS_KEY_VALUE',
'E2B_API_KEY': 'E2B_API_KEY_VALUE',
'GITHUB_TOKEN': 'GITHUB_TOKEN_VALUE',
'JWT_SECRET': 'JWT_SECRET_VALUE',
}
with patch.dict('openhands.core.logger.os.environ', environ, clear=True):
log_message = ' '.join(f"{attr}='{value}'" for attr, value in environ.items())
logger.info(log_message)
log_output = stream.getvalue()
for _, value in environ.items():
assert value not in log_output
def test_special_cases_masking(test_handler):
logger, stream = test_handler
environ = {
'LLM_API_KEY': 'LLM_API_KEY_VALUE',
'SANDBOX_ENV_GITHUB_TOKEN': 'SANDBOX_ENV_GITHUB_TOKEN_VALUE',
}
with patch.dict('openhands.core.logger.os.environ', environ, clear=True):
log_message = ' '.join(
f"{attr}={value} with no single quotes' and something"
for attr, value in environ.items()
)
logger.info(log_message)
log_output = stream.getvalue()
for attr, value in environ.items():
assert value not in log_output