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: support virtual_hosted_style and bucket_bound_hostname in v2 signing #1180

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,11 @@ def generate_signed_url(
resource = f"/{self.bucket.name}/{quoted_name}"

if virtual_hosted_style or bucket_bound_hostname:
resource = f"/{quoted_name}"
if version == "v4":
resource = f"/{quoted_name}"
else:
# v2 signing requires the resource path to include the bucket name
resource = f"/{self.bucket.name}/{quoted_name}"

if credentials is None:
client = self._require_client(client)
Expand All @@ -600,7 +604,7 @@ def generate_signed_url(
else:
headers.update(encryption_headers)

return helper(
signed_url = helper(
credentials,
resource=resource,
expiration=expiration,
Expand All @@ -616,6 +620,13 @@ def generate_signed_url(
service_account_email=service_account_email,
access_token=access_token,
)
if version == "v2" and (virtual_hosted_style or bucket_bound_hostname):
# v2 signing does not sign the host, so the host can be updated without breaking
# the signature. Update resource in host to accomodate non path-styled endpoints.
unsigned_host = f"{api_access_endpoint}{resource}"
special_style_host = f"{api_access_endpoint}/{quoted_name}"
signed_url = signed_url.replace(unsigned_host, special_style_host)
return signed_url

def exists(
self,
Expand Down
31 changes: 28 additions & 3 deletions tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ def _generate_signed_url_helper(
virtual_hosted_style=False,
bucket_bound_hostname=None,
scheme="http",
handle_v2_non_path_style=False,
):
from urllib import parse
from google.cloud._helpers import UTC
Expand Down Expand Up @@ -508,8 +509,6 @@ def _generate_signed_url_helper(
bucket_bound_hostname=bucket_bound_hostname,
)

self.assertEqual(signed_uri, signer.return_value)

encoded_name = blob_name.encode("utf-8")
quoted_name = parse.quote(encoded_name, safe=b"/~")

Expand All @@ -526,7 +525,10 @@ def _generate_signed_url_helper(
expected_resource = f"/{bucket.name}/{quoted_name}"

if virtual_hosted_style or bucket_bound_hostname:
expected_resource = f"/{quoted_name}"
if version == "v4":
expected_resource = f"/{quoted_name}"
else:
expected_resource = f"/{bucket.name}/{quoted_name}"

if encryption_key is not None:
expected_headers = headers or {}
Expand Down Expand Up @@ -554,6 +556,13 @@ def _generate_signed_url_helper(
}
signer.assert_called_once_with(expected_creds, **expected_kwargs)

return_value = signer.return_value
if handle_v2_non_path_style:
unsigned_host = f"{api_access_endpoint}{expected_resource}"
special_style_host = f"{api_access_endpoint}/{quoted_name}"
return_value = return_value.replace(unsigned_host, special_style_host)
self.assertEqual(signed_uri, return_value)

def test_generate_signed_url_no_version_passed_warning(self):
self._generate_signed_url_helper()

Expand Down Expand Up @@ -623,6 +632,22 @@ def test_generate_signed_url_v2_w_credentials(self):
credentials = object()
self._generate_signed_url_v2_helper(credentials=credentials)

def test_generate_signed_url_v2_w_virtual_hostname(self):
self._generate_signed_url_v2_helper(
virtual_hosted_style=True, handle_v2_non_path_style=True
)

def test_generate_signed_url_v2_w_bucket_bound_hostname_w_scheme(self):
self._generate_signed_url_v2_helper(
bucket_bound_hostname="http://cdn.example.com",
handle_v2_non_path_style=True,
)

def test_generate_signed_url_v2_w_bucket_bound_hostname_w_bare_hostname(self):
self._generate_signed_url_v2_helper(
bucket_bound_hostname="cdn.example.com", handle_v2_non_path_style=True
)

def _generate_signed_url_v4_helper(self, **kw):
version = "v4"
self._generate_signed_url_helper(version, **kw)
Expand Down