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

Remove deprecated GS_AUTO_CREATE_BUCKET #894

Merged
merged 1 commit into from
Jun 7, 2020
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
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
******************

Expand Down
12 changes: 0 additions & 12 deletions docs/backends/gcloud.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 2 additions & 29 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import mimetypes
import warnings
from datetime import timedelta
from tempfile import SpooledTemporaryFile

Expand All @@ -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")
Expand Down Expand Up @@ -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

Expand All @@ -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)),
Expand All @@ -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
Expand Down
36 changes: 1 addition & 35 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import mimetypes
import warnings
from datetime import datetime, timedelta
from unittest import mock

from django.core.exceptions import ImproperlyConfigured
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
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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()
Expand Down