From f3db452e73cd9b17457644f52efff2c3c4ab248b Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 7 Jun 2020 06:34:15 -0700 Subject: [PATCH] Remove deprecated GS_AUTO_CREATE_BUCKET The removal was targeted for the 1.10 release. Best to include in the existing batch of removed deprecated behaviors. --- CHANGELOG.rst | 5 +++++ docs/backends/gcloud.rst | 12 ------------ storages/backends/gcloud.py | 31 ++----------------------------- tests/test_gcloud.py | 36 +----------------------------------- 4 files changed, 8 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ba4dc914b..b1e1620d1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -29,6 +29,11 @@ UNRELEASED - The ``S3Boto3Storage`` backend no longer supports the undocumented ``AWS_PRELOAD_METADATA`` setting. +- The ``GoogleCloudStorage`` backend no longer automatically creates the + bucket. Doing so had encouraged using overly broad credentials. As a result, + the ``GS_AUTO_CREATE_BUCKET`` setting has been removed. + + 1.9.1 (2020-02-03) ****************** diff --git a/docs/backends/gcloud.rst b/docs/backends/gcloud.rst index 798c72012..28ef258ee 100644 --- a/docs/backends/gcloud.rst +++ b/docs/backends/gcloud.rst @@ -92,18 +92,6 @@ back to the default inferred from the environment "path/to/credentials.json" ) - -``GS_AUTO_CREATE_BUCKET`` (optional, default is ``False``) - -If True, attempt to create the bucket if it does not exist. - -.. deprecated:: 1.9 - - The ability to automatically create a bucket will be removed in version 1.10. The permissions needed - to do so are incongruent with the requirements of the rest of this library. Either create it yourself - or use one of the popular configuration management tools. - - ``GS_AUTO_CREATE_ACL`` (optional, default is ``projectPrivate``) ACL used when creating a new bucket, from the diff --git a/storages/backends/gcloud.py b/storages/backends/gcloud.py index 0916cc8da..a0e32b704 100644 --- a/storages/backends/gcloud.py +++ b/storages/backends/gcloud.py @@ -1,5 +1,4 @@ import mimetypes -import warnings from datetime import timedelta from tempfile import SpooledTemporaryFile @@ -18,7 +17,7 @@ try: from google.cloud.storage import Blob, Client from google.cloud.storage.blob import _quote - from google.cloud.exceptions import Conflict, NotFound + from google.cloud.exceptions import NotFound except ImportError: raise ImproperlyConfigured("Could not load Google Cloud Storage bindings.\n" "See https://github.com/GoogleCloudPlatform/gcloud-python") @@ -92,15 +91,6 @@ def __init__(self, **settings): check_location(self) - if self.auto_create_bucket: - warnings.warn( - "Automatic bucket creation will be removed in version 1.10. It encourages " - "using overly broad credentials with this library. Either create it before " - "manually or use one of a myriad of automatic configuration management tools. " - "Unset GS_AUTO_CREATE_BUCKET (it defaults to False) to silence this warning.", - DeprecationWarning, - ) - self._bucket = None self._client = None @@ -111,7 +101,6 @@ def get_default_settings(self): "bucket_name": setting('GS_BUCKET_NAME'), "custom_endpoint": setting('GS_CUSTOM_ENDPOINT', None), "location": setting('GS_LOCATION', ''), - "auto_create_bucket": setting('GS_AUTO_CREATE_BUCKET', False), "auto_create_acl": setting('GS_AUTO_CREATE_ACL', 'projectPrivate'), "default_acl": setting('GS_DEFAULT_ACL'), "expiration": setting('GS_EXPIRATION', timedelta(seconds=86400)), @@ -136,25 +125,9 @@ def client(self): @property def bucket(self): if self._bucket is None: - self._bucket = self._get_or_create_bucket(self.bucket_name) + self._bucket = self.client.bucket(self.bucket_name) return self._bucket - def _get_or_create_bucket(self, name): - """ - Returns bucket. If auto_create_bucket is True, creates bucket if it - doesn't exist. - """ - bucket = self.client.bucket(name) - if self.auto_create_bucket: - try: - new_bucket = self.client.create_bucket(name) - new_bucket.acl.save_predefined(self.auto_create_acl) - return new_bucket - except Conflict: - # Bucket already exists - pass - return bucket - def _normalize_name(self, name): """ Normalizes the name so that paths like /path/to/ignored/../something.txt diff --git a/tests/test_gcloud.py b/tests/test_gcloud.py index 4681c8497..68a926659 100644 --- a/tests/test_gcloud.py +++ b/tests/test_gcloud.py @@ -1,5 +1,4 @@ import mimetypes -import warnings from datetime import datetime, timedelta from unittest import mock @@ -7,7 +6,7 @@ from django.core.files.base import ContentFile from django.test import TestCase, override_settings from django.utils import timezone -from google.cloud.exceptions import Conflict, NotFound +from google.cloud.exceptions import NotFound from google.cloud.storage.blob import Blob from storages.backends import gcloud @@ -159,26 +158,6 @@ def test_exists_bucket(self): # exists('') should return True if the bucket exists self.assertTrue(self.storage.exists('')) - def test_exists_no_bucket_auto_create(self): - # exists('') should return true when auto_create_bucket is configured - # and bucket already exists - # exists('') should automatically create the bucket if - # auto_create_bucket is configured - self.storage.auto_create_bucket = True - self.storage._client = mock.MagicMock() - self.storage._client.create_bucket.side_effect = Conflict('dang') - - self.assertTrue(self.storage.exists('')) - - def test_exists_bucket_auto_create(self): - # exists('') should automatically create the bucket if - # auto_create_bucket is configured - self.storage.auto_create_bucket = True - self.storage._client = mock.MagicMock() - - self.assertTrue(self.storage.exists('')) - self.storage._client.create_bucket.assert_called_with(self.bucket_name) - def test_listdir(self): file_names = ["some/path/1.txt", "2.txt", "other/path/3.txt", "4.txt"] subdir = "" @@ -420,19 +399,6 @@ def test_location_leading_slash(self): with self.assertRaises(ImproperlyConfigured, msg=msg): gcloud.GoogleCloudStorage(location='/') - def test_deprecated_autocreate_bucket(self): - with warnings.catch_warnings(record=True) as w: - gcloud.GoogleCloudStorage(auto_create_bucket=True) - assert len(w) == 1 - assert issubclass(w[-1].category, DeprecationWarning) - message = ( - "Automatic bucket creation will be removed in version 1.10. It encourages " - "using overly broad credentials with this library. Either create it before " - "manually or use one of a myriad of automatic configuration management tools. " - "Unset GS_AUTO_CREATE_BUCKET (it defaults to False) to silence this warning." - ) - assert str(w[-1].message) == message - def test_override_settings(self): with override_settings(GS_LOCATION='foo1'): storage = gcloud.GoogleCloudStorage()