From 17ea5c08eff9002cf69e7fda92d32e7660f6e0dd Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Thu, 8 Jul 2021 15:15:01 -0700 Subject: [PATCH 1/2] feat: fix signed url mismatch in BlobWriteChannel --- .../google/cloud/storage/spi/v1/HttpStorageRpc.java | 4 ++++ .../com/google/cloud/storage/it/ITStorageTest.java | 12 ++++++++++-- logging.properties | 10 ++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 logging.properties diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index 23251edc53..11d4b98768 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -956,6 +956,10 @@ public String open(String signedURL) { HttpHeaders requestHeaders = httpRequest.getHeaders(); requestHeaders.set("X-Upload-Content-Type", ""); requestHeaders.set("x-goog-resumable", "start"); + // Using the x-goog-api-client header causes a signature mismatch with signed URLs generated + // outside the Java storage client + requestHeaders.remove("x-goog-api-client"); + HttpResponse response = httpRequest.execute(); if (response.getStatusCode() != 201) { GoogleJsonError error = new GoogleJsonError(); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 3055802045..7ed1239b21 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -3421,15 +3421,23 @@ public void testUploadUsingSignedURL() throws Exception { String blobName = "test-signed-url-upload"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); assertNotNull(storage.create(blob)); + Map extensionHeaders = new HashMap<>(); + extensionHeaders.put("x-goog-resumable", "start"); for (Storage.SignUrlOption urlStyle : Arrays.asList( Storage.SignUrlOption.withPathStyle(), Storage.SignUrlOption.withVirtualHostedStyle())) { URL signUrl = storage.signUrl( - blob, 1, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.POST), urlStyle); + blob, + 1, + TimeUnit.HOURS, + Storage.SignUrlOption.httpMethod(HttpMethod.POST), + Storage.SignUrlOption.withExtHeaders(extensionHeaders), + urlStyle); byte[] bytesArrayToUpload = BLOB_STRING_CONTENT.getBytes(); - try (WriteChannel writer = storage.writer(signUrl)) { + Storage unauthenticatedStorage = StorageOptions.getUnauthenticatedInstance().getService(); + try (WriteChannel writer = unauthenticatedStorage.writer(signUrl)) { writer.write(ByteBuffer.wrap(bytesArrayToUpload, 0, bytesArrayToUpload.length)); } diff --git a/logging.properties b/logging.properties new file mode 100644 index 0000000000..81a8f29c6d --- /dev/null +++ b/logging.properties @@ -0,0 +1,10 @@ +# Properties file which configures the operation of the JDK logging facility. +# The system will look for this config file to be specified as a system property: +# -Djava.util.logging.config.file=${project_loc:googleplus-simple-cmdline-sample}/logging.properties + +# Set up the console handler (uncomment "level" to show more fine-grained messages) +handlers = java.util.logging.ConsoleHandler +java.util.logging.ConsoleHandler.level = CONFIG + +# Set up logging of HTTP requests and responses (uncomment "level" to show) +com.google.api.client.http.level = CONFIG \ No newline at end of file From 50aab1e3edaf33e129a43d8fb046349c158330d7 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Thu, 8 Jul 2021 17:20:27 -0700 Subject: [PATCH 2/2] remove leftover file --- logging.properties | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 logging.properties diff --git a/logging.properties b/logging.properties deleted file mode 100644 index 81a8f29c6d..0000000000 --- a/logging.properties +++ /dev/null @@ -1,10 +0,0 @@ -# Properties file which configures the operation of the JDK logging facility. -# The system will look for this config file to be specified as a system property: -# -Djava.util.logging.config.file=${project_loc:googleplus-simple-cmdline-sample}/logging.properties - -# Set up the console handler (uncomment "level" to show more fine-grained messages) -handlers = java.util.logging.ConsoleHandler -java.util.logging.ConsoleHandler.level = CONFIG - -# Set up logging of HTTP requests and responses (uncomment "level" to show) -com.google.api.client.http.level = CONFIG \ No newline at end of file