Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only try to create storage client if storage is enabled #9913

Merged
merged 2 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions posthog/storage/object_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

logger = structlog.get_logger(__name__)

from posthog.settings import (
OBJECT_STORAGE_ACCESS_KEY_ID,
OBJECT_STORAGE_BUCKET,
OBJECT_STORAGE_ENDPOINT,
OBJECT_STORAGE_SECRET_ACCESS_KEY,
)
from django.conf import settings

s3_client = None

Expand All @@ -18,22 +13,24 @@
# noinspection PyMissingTypeHints
def storage_client():
global s3_client
if not s3_client:
if settings.OBJECT_STORAGE_ENABLED and not s3_client:
s3_client = client(
"s3",
endpoint_url=OBJECT_STORAGE_ENDPOINT,
aws_access_key_id=OBJECT_STORAGE_ACCESS_KEY_ID,
aws_secret_access_key=OBJECT_STORAGE_SECRET_ACCESS_KEY,
config=Config(signature_version="s3v4"),
endpoint_url=settings.OBJECT_STORAGE_ENDPOINT,
aws_access_key_id=settings.OBJECT_STORAGE_ACCESS_KEY_ID,
aws_secret_access_key=settings.OBJECT_STORAGE_SECRET_ACCESS_KEY,
config=Config(signature_version="s3v4", connect_timeout=1, retries={"max_attempts": 1}),
region_name="us-east-1",
)

return s3_client


def health_check() -> bool:
# noinspection PyBroadException
try:
response = storage_client().head_bucket(Bucket=OBJECT_STORAGE_BUCKET)
client = storage_client()
response = client.head_bucket(Bucket=settings.OBJECT_STORAGE_BUCKET) if client else False
return bool(response)
except Exception as e:
logger.warn("object_storage.health_check_failed", error=e)
Expand Down
12 changes: 12 additions & 0 deletions posthog/storage/test/test_object_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from unittest.mock import patch

from posthog.storage.object_storage import health_check
from posthog.test.base import APIBaseTest


class TestStorage(APIBaseTest):
@patch("posthog.storage.object_storage.client")
def test_does_not_create_client_if_storage_is_disabled(self, patched_s3_client) -> None:
with self.settings(OBJECT_STORAGE_ENABLED=False):
self.assertFalse(health_check())
patched_s3_client.assert_not_called()