Skip to content

Commit 7ae7e39

Browse files
authored
fix: update Signed URL default scheme to resolve from storage options host (#2880)
Fixes #2870
1 parent 1b74ecf commit 7ae7e39

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import java.io.UnsupportedEncodingException;
7373
import java.net.MalformedURLException;
7474
import java.net.URI;
75+
import java.net.URISyntaxException;
7576
import java.net.URL;
7677
import java.net.URLEncoder;
7778
import java.nio.ByteBuffer;
@@ -104,15 +105,9 @@ final class StorageImpl extends BaseService<StorageOptions> implements Storage,
104105
private static final String EMPTY_BYTE_ARRAY_MD5 = "1B2M2Y8AsgTpgAmY7PhCfg==";
105106
private static final String EMPTY_BYTE_ARRAY_CRC32C = "AAAAAA==";
106107
private static final String PATH_DELIMITER = "/";
107-
/** Signed URLs are only supported through the GCS XML API endpoint. */
108-
private static final String STORAGE_XML_URI_SCHEME = "https";
109108

110-
// TODO: in the future, this can be replaced by getOptions().getHost()
111-
private final String STORAGE_XML_URI_HOST_NAME =
112-
getOptions()
113-
.getResolvedApiaryHost("storage")
114-
.replaceFirst("http(s)?://", "")
115-
.replace("/", "");
109+
private final String STORAGE_XML_URI_SCHEME;
110+
private final String STORAGE_XML_URI_HOST_NAME;
116111

117112
private static final int DEFAULT_BUFFER_SIZE = 15 * 1024 * 1024;
118113
private static final int MIN_BUFFER_SIZE = 256 * 1024;
@@ -128,6 +123,14 @@ final class StorageImpl extends BaseService<StorageOptions> implements Storage,
128123
this.retryAlgorithmManager = options.getRetryAlgorithmManager();
129124
this.storageRpc = options.getStorageRpcV1();
130125
this.writerFactory = writerFactory;
126+
try {
127+
String resolvedApiaryHost = options.getResolvedApiaryHost("storage");
128+
URI uri = new URI(resolvedApiaryHost);
129+
STORAGE_XML_URI_HOST_NAME = uri.getHost();
130+
STORAGE_XML_URI_SCHEME = firstNonNull(uri.getScheme(), "https");
131+
} catch (URISyntaxException e) {
132+
throw StorageException.coalesce(e);
133+
}
131134
}
132135

133136
@Override

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITSignedUrlTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.storage.it;
1818

19+
import static com.google.cloud.storage.TestUtils.assertAll;
20+
import static com.google.common.truth.Truth.assertThat;
1921
import static org.junit.Assert.assertArrayEquals;
2022
import static org.junit.Assert.assertEquals;
2123
import static org.junit.Assert.assertNotNull;
@@ -33,6 +35,7 @@
3335
import com.google.cloud.storage.PostPolicyV4;
3436
import com.google.cloud.storage.PostPolicyV4.PostFieldsV4;
3537
import com.google.cloud.storage.Storage;
38+
import com.google.cloud.storage.Storage.SignUrlOption;
3639
import com.google.cloud.storage.StorageOptions;
3740
import com.google.cloud.storage.TransportCompatibility.Transport;
3841
import com.google.cloud.storage.it.runner.StorageITRunner;
@@ -45,6 +48,7 @@
4548
import java.io.ByteArrayInputStream;
4649
import java.io.IOException;
4750
import java.io.InputStream;
51+
import java.net.URI;
4852
import java.net.URL;
4953
import java.net.URLConnection;
5054
import java.nio.ByteBuffer;
@@ -261,4 +265,24 @@ public void testUploadUsingSignedURL() throws Exception {
261265
assertTrue(storage.delete(bucketName, blobName));
262266
}
263267
}
268+
269+
@Test
270+
public void generatingSignedURLForHttpProducesTheCorrectScheme() throws Exception {
271+
StorageOptions options =
272+
storage.getOptions().toBuilder().setHost("http://[::1]").setProjectId("no-project").build();
273+
try (Storage s = options.getService()) {
274+
BlobInfo info = BlobInfo.newBuilder("no-bucket", "no-object").build();
275+
URL urlV2 = s.signUrl(info, 5, TimeUnit.MINUTES, SignUrlOption.withV2Signature());
276+
URL urlV4 = s.signUrl(info, 5, TimeUnit.MINUTES, SignUrlOption.withV4Signature());
277+
URI uriV2 = urlV2.toURI();
278+
URI uriV4 = urlV4.toURI();
279+
assertAll(
280+
() -> assertThat(uriV2.getScheme()).isEqualTo("http"),
281+
() -> assertThat(uriV2.getHost()).isEqualTo("[::1]"),
282+
() -> assertThat(uriV2.getPath()).contains("no-bucket/no-object"),
283+
() -> assertThat(uriV4.getScheme()).isEqualTo("http"),
284+
() -> assertThat(uriV4.getHost()).isEqualTo("[::1]"),
285+
() -> assertThat(uriV4.getPath()).contains("no-bucket/no-object"));
286+
}
287+
}
264288
}

0 commit comments

Comments
 (0)