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: update Signed URL default scheme to resolve from storage options host #2880

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -104,15 +105,9 @@ final class StorageImpl extends BaseService<StorageOptions> implements Storage,
private static final String EMPTY_BYTE_ARRAY_MD5 = "1B2M2Y8AsgTpgAmY7PhCfg==";
private static final String EMPTY_BYTE_ARRAY_CRC32C = "AAAAAA==";
private static final String PATH_DELIMITER = "/";
/** Signed URLs are only supported through the GCS XML API endpoint. */
private static final String STORAGE_XML_URI_SCHEME = "https";

// TODO: in the future, this can be replaced by getOptions().getHost()
private final String STORAGE_XML_URI_HOST_NAME =
getOptions()
.getResolvedApiaryHost("storage")
.replaceFirst("http(s)?://", "")
.replace("/", "");
private final String STORAGE_XML_URI_SCHEME;
private final String STORAGE_XML_URI_HOST_NAME;

private static final int DEFAULT_BUFFER_SIZE = 15 * 1024 * 1024;
private static final int MIN_BUFFER_SIZE = 256 * 1024;
Expand All @@ -128,6 +123,14 @@ final class StorageImpl extends BaseService<StorageOptions> implements Storage,
this.retryAlgorithmManager = options.getRetryAlgorithmManager();
this.storageRpc = options.getStorageRpcV1();
this.writerFactory = writerFactory;
try {
String resolvedApiaryHost = options.getResolvedApiaryHost("storage");
URI uri = new URI(resolvedApiaryHost);
STORAGE_XML_URI_HOST_NAME = uri.getHost();
STORAGE_XML_URI_SCHEME = firstNonNull(uri.getScheme(), "https");
} catch (URISyntaxException e) {
throw StorageException.coalesce(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.cloud.storage.it;

import static com.google.cloud.storage.TestUtils.assertAll;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -33,6 +35,7 @@
import com.google.cloud.storage.PostPolicyV4;
import com.google.cloud.storage.PostPolicyV4.PostFieldsV4;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.SignUrlOption;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.TransportCompatibility.Transport;
import com.google.cloud.storage.it.runner.StorageITRunner;
Expand All @@ -45,6 +48,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -261,4 +265,24 @@ public void testUploadUsingSignedURL() throws Exception {
assertTrue(storage.delete(bucketName, blobName));
}
}

@Test
public void generatingSignedURLForHttpProducesTheCorrectScheme() throws Exception {
StorageOptions options =
storage.getOptions().toBuilder().setHost("http://[::1]").setProjectId("no-project").build();
try (Storage s = options.getService()) {
BlobInfo info = BlobInfo.newBuilder("no-bucket", "no-object").build();
URL urlV2 = s.signUrl(info, 5, TimeUnit.MINUTES, SignUrlOption.withV2Signature());
URL urlV4 = s.signUrl(info, 5, TimeUnit.MINUTES, SignUrlOption.withV4Signature());
URI uriV2 = urlV2.toURI();
URI uriV4 = urlV4.toURI();
assertAll(
() -> assertThat(uriV2.getScheme()).isEqualTo("http"),
() -> assertThat(uriV2.getHost()).isEqualTo("[::1]"),
() -> assertThat(uriV2.getPath()).contains("no-bucket/no-object"),
() -> assertThat(uriV4.getScheme()).isEqualTo("http"),
() -> assertThat(uriV4.getHost()).isEqualTo("[::1]"),
() -> assertThat(uriV4.getPath()).contains("no-bucket/no-object"));
}
}
}
Loading