test_logging.py 4.2 KB

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