1. import json
  2. import time
  3. import re
  4. class ArtifactCleaner:
  5. def __init__(self, cleanup_interval=60):
  6. """
  7. Initializes the ArtifactCleaner.
  8. Args:
  9. cleanup_interval (int): Time in seconds to run cleanup. Defaults to 60 seconds.
  10. """
  11. self.cleanup_interval = cleanup_interval
  12. self.artifacts = [] # Store artifacts (e.g., response bodies)
  13. self.last_cleanup = 0
  14. def store_artifact(self, artifact):
  15. """
  16. Stores an artifact.
  17. Args:
  18. artifact (any): The artifact to store.
  19. """
  20. self.artifacts.append(artifact)
  21. def cleanup(self):
  22. """
  23. Cleans up artifacts older than the specified interval.
  24. """
  25. now = time.time()
  26. artifacts_to_remove = []
  27. for artifact in self.artifacts:
  28. if (now - artifact['timestamp']) > self.cleanup_interval:
  29. artifacts_to_remove.append(artifact)
  30. for artifact in artifacts_to_remove:
  31. self.artifacts.remove(artifact)
  32. print(f"Cleaned up {len(artifacts_to_remove)} artifacts.")
  33. def run(self):
  34. """
  35. Runs the cleanup process periodically.
  36. """
  37. while True:
  38. self.cleanup()
  39. self.last_cleanup = time.time()
  40. time.sleep(self.cleanup_interval)
  41. def clean_response(response_text):
  42. """
  43. Cleans a response text by removing sensitive information.
  44. Args:
  45. response_text (str): The raw response text.
  46. Returns:
  47. str: The cleaned response text.
  48. """
  49. # Remove potential API keys or sensitive data using regex
  50. response_text = re.sub(r'\b(API_KEY|password|secret)\b', '[REDACTED]', response_text, flags=re.IGNORECASE)
  51. # Remove JSON formatting (optional, for brevity)
  52. response_text = re.sub(r'\{.*\}', '', response_text)
  53. response_text = re.sub(r'\[.*\]', '', response_text)
  54. return response_text
  55. if __name__ == '__main__':
  56. cleaner = ArtifactCleaner(cleanup_interval=10) # Cleanup every 10 seconds
  57. # Simulate API calls and artifact storage
  58. for i in range(15):
  59. # Simulate an API response
  60. response_data = {"timestamp": time.time(), "data": f"This is some data {i}"}
  61. response_text = json.dumps(response_data)
  62. cleaned_response = clean_response(response_text)
  63. cleaner.store_artifact(response_text)
  64. time.sleep(2) # Simulate API call frequency
  65. # Start the cleanup process in the background
  66. cleanup_thread = threading.Thread(target=cleaner.run)
  67. cleanup_thread.daemon = True # Allow main thread to exit even if cleanup is running
  68. cleanup_thread.start()
  69. time.sleep(15) #Let the cleanup thread run for a bit
  70. print("Done.")

Add your comment