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(storage): enable CSEK w/ V4 signed URLs #9450

Merged
merged 2 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ def generate_signed_url(
else:
helper = generate_signed_url_v4

if self._encryption_key is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should only be done if v4.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per chat, we still need to add the X-Goog-Encryption-Algorithm header for V2 (but not the key or hash, oddly enough). See: https://cloud.google.com/storage/docs/access-control/signed-urls-v2#about-canonical-extension-headers

if headers is None:
headers = {}
headers.update(_get_encryption_headers(self._encryption_key))

return helper(
credentials,
resource=resource,
Expand Down
35 changes: 33 additions & 2 deletions storage/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import datetime
import hashlib
import os
import re
import tempfile
Expand Down Expand Up @@ -860,11 +862,12 @@ def _create_signed_read_url_helper(
version="v2",
payload=None,
expiration=None,
encryption_key=None,
):
expiration = self._morph_expiration(version, expiration)

if payload is not None:
blob = self.bucket.blob(blob_name)
blob = self.bucket.blob(blob_name, encryption_key=encryption_key)
blob.upload_from_string(payload)
else:
blob = self.blob
Expand All @@ -873,7 +876,17 @@ def _create_signed_read_url_helper(
expiration=expiration, method=method, client=Config.CLIENT, version=version
)

response = requests.get(signed_url)
headers = {}

if encryption_key is not None:
headers["x-goog-encryption-algorithm"] = "AES256"
encoded_key = base64.b64encode(encryption_key).decode("utf-8")
headers["x-goog-encryption-key"] = encoded_key
key_hash = hashlib.sha256(encryption_key).digest()
key_hash = base64.b64encode(key_hash).decode("utf-8")
headers["x-goog-encryption-key-sha256"] = key_hash

response = requests.get(signed_url, headers=headers)
self.assertEqual(response.status_code, 200)
if payload is not None:
self.assertEqual(response.content, payload)
Expand Down Expand Up @@ -916,6 +929,24 @@ def test_create_signed_read_url_v4_w_non_ascii_name(self):
version="v4",
)

@unittest.skip("V2 signing does not work with CSEK")
def test_create_signed_read_url_v2_w_csek(self):
encryption_key = os.urandom(32)
self._create_signed_read_url_helper(
blob_name="v2-w-csek.txt",
payload=b"Test signed URL for blob w/ CSEK",
encryption_key=encryption_key,
)

def test_create_signed_read_url_v4_w_csek(self):
encryption_key = os.urandom(32)
self._create_signed_read_url_helper(
blob_name="v2-w-csek.txt",
payload=b"Test signed URL for blob w/ CSEK",
encryption_key=encryption_key,
version="v4",
)

def _create_signed_delete_url_helper(self, version="v2", expiration=None):
expiration = self._morph_expiration(version, expiration)

Expand Down
28 changes: 26 additions & 2 deletions storage/tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,12 @@ def _generate_signed_url_helper(
query_parameters=None,
credentials=None,
expiration=None,
encryption_key=None,
):
from six.moves.urllib import parse
from google.cloud._helpers import UTC
from google.cloud.storage.blob import _API_ACCESS_ENDPOINT
from google.cloud.storage.blob import _get_encryption_headers

api_access_endpoint = api_access_endpoint or _API_ACCESS_ENDPOINT

Expand All @@ -406,7 +408,7 @@ def _generate_signed_url_helper(
connection = _Connection()
client = _Client(connection)
bucket = _Bucket(client)
blob = self._make_one(blob_name, bucket=bucket)
blob = self._make_one(blob_name, bucket=bucket, encryption_key=encryption_key)

if version is None:
effective_version = "v2"
Expand Down Expand Up @@ -442,6 +444,12 @@ def _generate_signed_url_helper(

encoded_name = blob_name.encode("utf-8")
expected_resource = "/name/{}".format(parse.quote(encoded_name, safe=b"/~"))
if encryption_key is not None:
expected_headers = headers or {}
expected_headers.update(_get_encryption_headers(encryption_key))
else:
expected_headers = headers

expected_kwargs = {
"resource": expected_resource,
"expiration": expiration,
Expand All @@ -452,7 +460,7 @@ def _generate_signed_url_helper(
"response_type": response_type,
"response_disposition": response_disposition,
"generation": generation,
"headers": headers,
"headers": expected_headers,
"query_parameters": query_parameters,
}
signer.assert_called_once_with(expected_creds, **expected_kwargs)
Expand Down Expand Up @@ -514,6 +522,14 @@ def test_generate_signed_url_v2_w_generation(self):
def test_generate_signed_url_v2_w_headers(self):
self._generate_signed_url_v2_helper(headers={"x-goog-foo": "bar"})

def test_generate_signed_url_v2_w_csek(self):
self._generate_signed_url_v2_helper(encryption_key=os.urandom(32))

def test_generate_signed_url_v2_w_csek_and_headers(self):
self._generate_signed_url_v2_helper(
encryption_key=os.urandom(32), headers={"x-goog-foo": "bar"}
)

def test_generate_signed_url_v2_w_credentials(self):
credentials = object()
self._generate_signed_url_v2_helper(credentials=credentials)
Expand Down Expand Up @@ -566,6 +582,14 @@ def test_generate_signed_url_v4_w_generation(self):
def test_generate_signed_url_v4_w_headers(self):
self._generate_signed_url_v4_helper(headers={"x-goog-foo": "bar"})

def test_generate_signed_url_v4_w_csek(self):
self._generate_signed_url_v4_helper(encryption_key=os.urandom(32))

def test_generate_signed_url_v4_w_csek_and_headers(self):
self._generate_signed_url_v4_helper(
encryption_key=os.urandom(32), headers={"x-goog-foo": "bar"}
)

def test_generate_signed_url_v4_w_credentials(self):
credentials = object()
self._generate_signed_url_v4_helper(credentials=credentials)
Expand Down