Repository navigation
fix(storage): omit credentials and quota project on signed URL uploads #15132
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
base: main
Are you sure you want to change the base?
Changes from all commits
fb3f7a9
644b865
04cfa0b
cd89171
df97921
492b80b
ef25449
89c47a5
b1f8c70
bb9e9a6
87d3c96
66f3c8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |||||||||||||||||||||||||||||||||||||
| import com.google.api.client.http.HttpRequestFactory; | ||||||||||||||||||||||||||||||||||||||
| import com.google.api.client.json.JsonObjectParser; | ||||||||||||||||||||||||||||||||||||||
| import com.google.api.client.util.ObjectParser; | ||||||||||||||||||||||||||||||||||||||
| import com.google.cloud.storage.spi.v1.HttpStorageRpc; | ||||||||||||||||||||||||||||||||||||||
| import com.google.cloud.storage.spi.v1.StorageRpc; | ||||||||||||||||||||||||||||||||||||||
| import io.opencensus.trace.Span; | ||||||||||||||||||||||||||||||||||||||
| import io.opencensus.trace.Tracer; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -78,6 +79,15 @@ static HttpClientContext from(StorageRpc storageRpc) { | |||||||||||||||||||||||||||||||||||||
| Tracing.getTracer()); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| static HttpClientContext forSignedUrl(StorageRpc storageRpc) { | ||||||||||||||||||||||||||||||||||||||
| HttpRequestFactory requestFactory = | ||||||||||||||||||||||||||||||||||||||
| storageRpc instanceof HttpStorageRpc | ||||||||||||||||||||||||||||||||||||||
| ? ((HttpStorageRpc) storageRpc).getSignedUrlRequestFactory() | ||||||||||||||||||||||||||||||||||||||
| : storageRpc.getStorage().getRequestFactory().getTransport().createRequestFactory(); | ||||||||||||||||||||||||||||||||||||||
| return new HttpClientContext( | ||||||||||||||||||||||||||||||||||||||
| requestFactory, storageRpc.getStorage().getObjectParser(), Tracing.getTracer()); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Dhriti07 marked this conversation as resolved.
Resolved
Comment on lines
+82
to
+89
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Medium] For any
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In practice |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public static HttpClientContext of( | ||||||||||||||||||||||||||||||||||||||
| HttpRequestFactory requestFactory, JsonObjectParser jsonObjectParser) { | ||||||||||||||||||||||||||||||||||||||
| return new HttpClientContext(requestFactory, jsonObjectParser, Tracing.getTracer()); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,10 @@ | |
| import com.google.api.client.json.JsonFactory; | ||
| import com.google.api.client.json.jackson2.JacksonFactory; | ||
| import com.google.api.client.util.Data; | ||
| import com.google.api.core.InternalApi; | ||
| import com.google.api.gax.core.GaxProperties; | ||
| import com.google.api.gax.rpc.ApiClientHeaderProvider; | ||
| import com.google.api.gax.rpc.HeaderProvider; | ||
| import com.google.api.services.storage.Storage; | ||
| import com.google.api.services.storage.Storage.BucketAccessControls; | ||
| import com.google.api.services.storage.Storage.Buckets; | ||
|
|
@@ -77,6 +81,7 @@ | |
| import com.google.api.services.storage.model.ServiceAccount; | ||
| import com.google.api.services.storage.model.StorageObject; | ||
| import com.google.api.services.storage.model.TestIamPermissionsResponse; | ||
| import com.google.cloud.ServiceOptions; | ||
| import com.google.cloud.Tuple; | ||
| import com.google.cloud.http.CensusHttpModule; | ||
| import com.google.cloud.http.HttpTransportOptions; | ||
|
|
@@ -132,6 +137,15 @@ public class HttpStorageRpc implements StorageRpc { | |
|
|
||
| private final StorageOptions options; | ||
| private final Storage storage; | ||
|
|
||
| /** | ||
| * Eagerly constructed request factory for requests sent to a signed URL. Requests built from this | ||
| * factory carry no client credentials or quota project and intentionally do not run {@link | ||
| * HttpTransportOptions#getHttpRequestInitializer(ServiceOptions)}; customizations made by | ||
| * subclasses of {@code HttpTransportOptions} therefore do not apply. The timeout and header | ||
| * handling in the constructor mirrors that initializer and must be kept in sync with it. | ||
| */ | ||
| private final HttpRequestFactory signedUrlRequestFactory; | ||
|
Dhriti07 marked this conversation as resolved.
Resolved
|
||
| private final Tracer tracer = Tracing.getTracer(); | ||
| private final HttpRequestInitializer batchRequestInitializer; | ||
|
|
||
|
|
@@ -184,12 +198,63 @@ public HttpStorageRpc(StorageOptions options, JsonFactory jsonFactory) { | |
| storageBuilder.setRootUrl(host); | ||
| } | ||
| storage = storageBuilder.build(); | ||
| Map<String, String> signedUrlHeaders = signedUrlHeaders(options); | ||
| HttpRequestInitializer signedUrlInitializer = | ||
| request -> { | ||
| if (transportOptions.getConnectTimeout() >= 0) { | ||
| request.setConnectTimeout(transportOptions.getConnectTimeout()); | ||
| } | ||
| if (transportOptions.getReadTimeout() >= 0) { | ||
| request.setReadTimeout(transportOptions.getReadTimeout()); | ||
| } | ||
| HttpHeaders headers = request.getHeaders(); | ||
| for (Entry<String, String> e : signedUrlHeaders.entrySet()) { | ||
| // Mirrors com.google.api.gax.httpjson.HttpHeadersUtils#setHeader (without Long | ||
| // conversion for Age/Content-Length): fields declared on HttpHeaders are list valued. | ||
| boolean listValued = headers.getClassInfo().getFieldInfo(e.getKey()) != null; | ||
| headers.set(e.getKey(), listValued ? ImmutableList.of(e.getValue()) : e.getValue()); | ||
| } | ||
|
Comment on lines
+211
to
+216
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This loop re-implements
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree it is unlikely to break anything. Added it to be extra careful |
||
| }; | ||
|
Dhriti07 marked this conversation as resolved.
Resolved
|
||
| signedUrlRequestFactory = | ||
| transport.createRequestFactory( | ||
| new InvocationIdInitializer( | ||
| censusHttpModule.getHttpRequestInitializer(signedUrlInitializer), | ||
| applicationName, | ||
| tm)); | ||
| } | ||
|
|
||
| /** | ||
| * Headers for requests sent to a signed URL. These are the same headers sent on other requests, | ||
| * except {@code Authorization} and {@code x-goog-user-project}, which are stripped even if | ||
| * configured through a user {@link HeaderProvider}. | ||
| */ | ||
| private static Map<String, String> signedUrlHeaders(StorageOptions options) { | ||
| HeaderProvider internalHeaderProvider = | ||
| ApiClientHeaderProvider.newBuilder() | ||
| .setClientLibToken( | ||
| ServiceOptions.getGoogApiClientLibName(), | ||
| GaxProperties.getLibraryVersion(options.getClass())) | ||
| .build(); | ||
| ImmutableMap.Builder<String, String> headers = ImmutableMap.builder(); | ||
| for (Entry<String, String> e : | ||
| options.getMergedHeaderProvider(internalHeaderProvider).getHeaders().entrySet()) { | ||
| String key = e.getKey(); | ||
| if (!"authorization".equalsIgnoreCase(key) && !"x-goog-user-project".equalsIgnoreCase(key)) { | ||
| headers.put(e); | ||
| } | ||
| } | ||
| return headers.build(); | ||
|
Dhriti07 marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| public Storage getStorage() { | ||
| return storage; | ||
| } | ||
|
|
||
| @InternalApi | ||
| public HttpRequestFactory getSignedUrlRequestFactory() { | ||
| return signedUrlRequestFactory; | ||
| } | ||
|
|
||
| private static final class InvocationIdInitializer implements HttpRequestInitializer { | ||
| @Nullable HttpRequestInitializer initializer; | ||
| @Nullable private final String applicationName; | ||
|
|
@@ -1224,9 +1289,8 @@ public String open(String signedURL) { | |
| url.set("uploadType", "resumable"); | ||
| String bytesArrayParameters = ""; | ||
| byte[] bytesArray = new byte[bytesArrayParameters.length()]; | ||
| HttpRequestFactory requestFactory = storage.getRequestFactory(); | ||
| HttpRequest httpRequest = | ||
| requestFactory.buildPostRequest( | ||
| signedUrlRequestFactory.buildPostRequest( | ||
|
Dhriti07 marked this conversation as resolved.
Resolved
|
||
| url, new ByteArrayContent("", bytesArray, 0, bytesArray.length)); | ||
| HttpHeaders requestHeaders = httpRequest.getHeaders(); | ||
| requestHeaders.set("X-Upload-Content-Type", ""); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.