|
| 1 | +# Copyright 2018 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Implement a class to simulate GCS buckets.""" |
| 15 | + |
| 16 | +import base64 |
| 17 | +import error_response |
| 18 | +import flask |
| 19 | +import gcs_object |
| 20 | +import json |
| 21 | +import re |
| 22 | +import testbench_utils |
| 23 | +import time |
| 24 | + |
| 25 | + |
| 26 | +class GcsBucket: |
| 27 | + """Represent a GCS Bucket.""" |
| 28 | + |
| 29 | + def __init__(self, gcs_url, name): |
| 30 | + self.name = name |
| 31 | + self.gcs_url = gcs_url |
| 32 | + now = time.gmtime(time.time()) |
| 33 | + timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", now) |
| 34 | + self.metadata = { |
| 35 | + "timeCreated": timestamp, |
| 36 | + "updated": timestamp, |
| 37 | + "metageneration": "0", |
| 38 | + "name": self.name, |
| 39 | + "location": "US", |
| 40 | + "storageClass": "STANDARD", |
| 41 | + "etag": "XYZ=", |
| 42 | + "labels": {"foo": "bar", "baz": "qux"}, |
| 43 | + "owner": {"entity": "project-owners-123456789", "entityId": ""}, |
| 44 | + } |
| 45 | + self.resumable_uploads = {} |
| 46 | + |
| 47 | + def versioning_enabled(self): |
| 48 | + """Return True if versioning is enabled for this Bucket.""" |
| 49 | + v = self.metadata.get("versioning", None) |
| 50 | + if v is None: |
| 51 | + return False |
| 52 | + return v.get("enabled", False) |
| 53 | + |
| 54 | + def check_preconditions(self, request): |
| 55 | + """Verify that the preconditions in request are met. |
| 56 | +
|
| 57 | + :param request:flask.Request the contents of the HTTP request. |
| 58 | + :rtype:NoneType |
| 59 | + :raises:ErrorResponse if the request does not pass the preconditions, |
| 60 | + for example, the request has a `ifMetagenerationMatch` restriction |
| 61 | + that is not met. |
| 62 | + """ |
| 63 | + |
| 64 | + metageneration_match = request.args.get("ifMetagenerationMatch") |
| 65 | + metageneration_not_match = request.args.get("ifMetagenerationNotMatch") |
| 66 | + metageneration = self.metadata.get("metageneration") |
| 67 | + |
| 68 | + if ( |
| 69 | + metageneration_not_match is not None |
| 70 | + and metageneration_not_match == metageneration |
| 71 | + ): |
| 72 | + raise error_response.ErrorResponse( |
| 73 | + "Precondition Failed (metageneration = %s)" % metageneration, |
| 74 | + status_code=412, |
| 75 | + ) |
| 76 | + |
| 77 | + if metageneration_match is not None and metageneration_match != metageneration: |
| 78 | + raise error_response.ErrorResponse( |
| 79 | + "Precondition Failed (metageneration = %s)" % metageneration, |
| 80 | + status_code=412, |
| 81 | + ) |
| 82 | + |
| 83 | + def create_resumable_upload(self, upload_url, request): |
| 84 | + """Capture the details for a resumable upload. |
| 85 | +
|
| 86 | + :param upload_url: str the base URL for uploads. |
| 87 | + :param request: flask.Request the original http request. |
| 88 | + :return: the HTTP response to send back. |
| 89 | + """ |
| 90 | + x_upload_content_type = request.headers.get( |
| 91 | + "x-upload-content-type", "application/octet-stream" |
| 92 | + ) |
| 93 | + x_upload_content_length = request.headers.get("x-upload-content-length") |
| 94 | + expected_bytes = None |
| 95 | + if x_upload_content_length: |
| 96 | + expected_bytes = int(x_upload_content_length) |
| 97 | + |
| 98 | + if request.args.get("name") is not None and len(request.data): |
| 99 | + raise error_response.ErrorResponse( |
| 100 | + "The name argument is only supported for empty payloads", |
| 101 | + status_code=400, |
| 102 | + ) |
| 103 | + if len(request.data): |
| 104 | + metadata = json.loads(request.data) |
| 105 | + else: |
| 106 | + metadata = {"name": request.args.get("name")} |
| 107 | + |
| 108 | + if metadata.get("name") is None: |
| 109 | + raise error_response.ErrorResponse( |
| 110 | + "Missing object name argument", status_code=400 |
| 111 | + ) |
| 112 | + metadata.setdefault("contentType", x_upload_content_type) |
| 113 | + upload = { |
| 114 | + "metadata": metadata, |
| 115 | + "instructions": request.headers.get("x-goog-testbench-instructions"), |
| 116 | + "fields": request.args.get("fields"), |
| 117 | + "next_byte": 0, |
| 118 | + "expected_bytes": expected_bytes, |
| 119 | + "object_name": metadata.get("name"), |
| 120 | + "media": b"", |
| 121 | + "transfer": set(), |
| 122 | + "done": False, |
| 123 | + } |
| 124 | + # Capture the preconditions, including those that are None. |
| 125 | + for precondition in [ |
| 126 | + "ifGenerationMatch", |
| 127 | + "ifGenerationNotMatch", |
| 128 | + "ifMetagenerationMatch", |
| 129 | + "ifMetagenerationNotMatch", |
| 130 | + ]: |
| 131 | + upload[precondition] = request.args.get(precondition) |
| 132 | + upload_id = base64.b64encode(bytearray(metadata.get("name"), "utf-8")).decode( |
| 133 | + "utf-8" |
| 134 | + ) |
| 135 | + self.resumable_uploads[upload_id] = upload |
| 136 | + location = "{}?uploadType=resumable&upload_id={}".format(upload_url, upload_id) |
| 137 | + response = flask.make_response("") |
| 138 | + response.headers["Location"] = location |
| 139 | + return response |
| 140 | + |
| 141 | + def receive_upload_chunk(self, gcs_url, request): |
| 142 | + """Receive a new upload chunk. |
| 143 | +
|
| 144 | + :param gcs_url: str the base URL for the service. |
| 145 | + :param request: flask.Request the original http request. |
| 146 | + :return: the HTTP response. |
| 147 | + """ |
| 148 | + upload_id = request.args.get("upload_id") |
| 149 | + if upload_id is None: |
| 150 | + raise error_response.ErrorResponse( |
| 151 | + "Missing upload_id in resumable_upload_chunk", status_code=400 |
| 152 | + ) |
| 153 | + upload = self.resumable_uploads.get(upload_id) |
| 154 | + if upload is None: |
| 155 | + raise error_response.ErrorResponse( |
| 156 | + "Cannot find resumable upload %s" % upload_id, status_code=404 |
| 157 | + ) |
| 158 | + # Be gracious in what you accept, if the Content-Range header is not |
| 159 | + # set we assume it is a good header and it is the end of the file. |
| 160 | + next_byte = upload["next_byte"] |
| 161 | + upload["transfer"].add(request.environ.get("HTTP_TRANSFER_ENCODING", "")) |
| 162 | + end = next_byte + len(request.data) |
| 163 | + total = end |
| 164 | + final_chunk = False |
| 165 | + payload = testbench_utils.extract_media(request) |
| 166 | + content_range = request.headers.get("content-range") |
| 167 | + if content_range is not None: |
| 168 | + if content_range.startswith("bytes */*"): |
| 169 | + # This is just a query to resume an upload, if it is done, return |
| 170 | + # the completed upload payload and an empty range header. |
| 171 | + response = flask.make_response(upload.get("payload", "")) |
| 172 | + if next_byte > 1 and not upload["done"]: |
| 173 | + response.headers["Range"] = "bytes=0-%d" % (next_byte - 1) |
| 174 | + response.status_code = 200 if upload["done"] else 308 |
| 175 | + return response |
| 176 | + match = re.match(r"bytes \*/(\*|[0-9]+)", content_range) |
| 177 | + if match: |
| 178 | + if match.group(1) == "*": |
| 179 | + total = 0 |
| 180 | + else: |
| 181 | + total = int(match.group(1)) |
| 182 | + final_chunk = True |
| 183 | + else: |
| 184 | + match = re.match(r"bytes ([0-9]+)-([0-9]+)\/(\*|[0-9]+)", content_range) |
| 185 | + if not match: |
| 186 | + raise error_response.ErrorResponse( |
| 187 | + "Invalid Content-Range in upload %s" % content_range, |
| 188 | + status_code=400, |
| 189 | + ) |
| 190 | + begin = int(match.group(1)) |
| 191 | + end = int(match.group(2)) |
| 192 | + if match.group(3) == "*": |
| 193 | + total = 0 |
| 194 | + else: |
| 195 | + total = int(match.group(3)) |
| 196 | + final_chunk = True |
| 197 | + |
| 198 | + if begin != next_byte: |
| 199 | + raise error_response.ErrorResponse( |
| 200 | + "Mismatched data range, expected data at %d, got %d" |
| 201 | + % (next_byte, begin), |
| 202 | + status_code=400, |
| 203 | + ) |
| 204 | + if len(payload) != end - begin + 1: |
| 205 | + raise error_response.ErrorResponse( |
| 206 | + "Mismatched data range (%d) vs. received data (%d)" |
| 207 | + % (end - begin + 1, len(payload)), |
| 208 | + status_code=400, |
| 209 | + ) |
| 210 | + |
| 211 | + upload["media"] = upload.get("media", b"") + payload |
| 212 | + next_byte = len(upload.get("media", "")) |
| 213 | + upload["next_byte"] = next_byte |
| 214 | + response_payload = "" |
| 215 | + if final_chunk and next_byte >= total: |
| 216 | + expected_bytes = upload["expected_bytes"] |
| 217 | + if expected_bytes is not None and expected_bytes != total: |
| 218 | + raise error_response.ErrorResponse( |
| 219 | + "X-Upload-Content-Length" |
| 220 | + "validation failed. Expected=%d, got %d." % (expected_bytes, total) |
| 221 | + ) |
| 222 | + upload["done"] = True |
| 223 | + object_name = upload.get("object_name") |
| 224 | + object_path, blob = testbench_utils.get_object( |
| 225 | + self.name, object_name, gcs_object.GcsObject(self.name, object_name) |
| 226 | + ) |
| 227 | + # Release a few resources to control memory usage. |
| 228 | + original_metadata = upload.pop("metadata", None) |
| 229 | + media = upload.pop("media", None) |
| 230 | + blob.check_preconditions_by_value( |
| 231 | + upload.get("ifGenerationMatch"), |
| 232 | + upload.get("ifGenerationNotMatch"), |
| 233 | + upload.get("ifMetagenerationMatch"), |
| 234 | + upload.get("ifMetagenerationNotMatch"), |
| 235 | + ) |
| 236 | + if upload.pop("instructions", None) == "inject-upload-data-error": |
| 237 | + media = testbench_utils.corrupt_media(media) |
| 238 | + revision = blob.insert_resumable(gcs_url, request, media, original_metadata) |
| 239 | + revision.metadata.setdefault("metadata", {}) |
| 240 | + revision.metadata["metadata"]["x_testbench_transfer_encoding"] = ":".join( |
| 241 | + upload["transfer"] |
| 242 | + ) |
| 243 | + response_payload = testbench_utils.filter_fields_from_response( |
| 244 | + upload.get("fields"), revision.metadata |
| 245 | + ) |
| 246 | + upload["payload"] = response_payload |
| 247 | + testbench_utils.insert_object(object_path, blob) |
| 248 | + |
| 249 | + response = flask.make_response(response_payload) |
| 250 | + if next_byte == 0: |
| 251 | + response.headers["Range"] = "bytes=0-0" |
| 252 | + else: |
| 253 | + response.headers["Range"] = "bytes=0-%d" % (next_byte - 1) |
| 254 | + if upload.get("done", False): |
| 255 | + response.status_code = 200 |
| 256 | + else: |
| 257 | + response.status_code = 308 |
| 258 | + return response |
0 commit comments