Repository navigation
Conversation
…aders Send signed URL upload requests without client-level headers.
There was a problem hiding this comment.
Code Review
This pull request adds support for signed URLs in resumable uploads, ensuring that client headers (such as authorization and custom headers) are excluded when writing to a signed URL. To support this, a new forSignedUrl factory method is added to HttpClientContext, and HttpStorageRpc is updated to manage a dedicated signedUrlRequestFactory. However, a critical issue was identified in HttpClientContext.forSignedUrl: the method accepts a StorageRpc parameter but attempts to call getStorage(), which is not defined on the StorageRpc interface. It is recommended to change the parameter type to HttpStorageRpc directly to ensure compile-time safety.
Signed URL upload requests now keep the headers configured on the client, and omit only the client's credentials and quota project headers.
cd89171 to
644b865
Compare
nidhiii-27
left a comment
There was a problem hiding this comment.
Thanks for the fix — dropping Authorization/x-goog-user-project from signed URL uploads looks correct. Left a few comments inline. Also, could you update the PR title and description to reflect the latest changes? After 04cfa0b, header-provider headers are kept and only credentials and the quota project are dropped, so "without client-level headers" is no longer accurate.
| 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()); | ||
| } |
There was a problem hiding this comment.
[Medium] For any StorageRpc that isn't an HttpStorageRpc (e.g. one from a custom setServiceRpcFactory), the fallback builds a bare transport.createRequestFactory(). That drops the timeouts, Census tracing, invocation-id/idempotency headers, and x-goog-api-client/user-agent. Before this PR, those callers got from(storageRpc), and no test covers this branch. Could we keep the previous behavior for non-HTTP RPCs?
| 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()); | |
| } | |
| static HttpClientContext forSignedUrl(StorageRpc storageRpc) { | |
| if (!(storageRpc instanceof HttpStorageRpc)) { | |
| return from(storageRpc); | |
| } | |
| HttpStorageRpc httpStorageRpc = (HttpStorageRpc) storageRpc; | |
| return new HttpClientContext( | |
| httpStorageRpc.getSignedUrlRequestFactory(), | |
| httpStorageRpc.getStorage().getObjectParser(), | |
| Tracing.getTracer()); | |
| } |
| 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()) { | ||
| // headers declared as fields on HttpHeaders (e.g. User-Agent) are list valued | ||
| boolean listValued = headers.getClassInfo().getFieldInfo(e.getKey()) != null; | ||
| headers.set(e.getKey(), listValued ? ImmutableList.of(e.getValue()) : e.getValue()); | ||
| } | ||
| }; |
There was a problem hiding this comment.
[Medium] This initializer replaces HttpTransportOptions#getHttpRequestInitializer(ServiceOptions) for signed-URL requests. That method is public and can be overridden; this repo's own it/RequestAuditing overrides it. Any subclass customization (auditing, logging, interceptors) now silently stops applying to Storage#writer(URL). This block also copies the timeout and header handling from core-http, and L222–227 copies getInternalHeaderProviderBuilder. Those copies can drift from the originals. If bypassing is intended, could we document it? For example:
/**
* 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 below mirrors that initializer and must be kept in sync with it.
*/
private final HttpRequestFactory signedUrlRequestFactory;If you'd rather keep the hook, note that deriving from the core initializer with NoCredentials would trip its universe-domain check for non-default universes.
| 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(); |
There was a problem hiding this comment.
[High] The tests never exercise this authorization / x-goog-user-project filter. The test header provider only supplies x-custom, and the signed-URL assertions pass only because the internal provider omits the quota token and no credentials adapter is installed. Deleting the filter would leave the suite green. It also silently drops headers a user set explicitly through setHeaderProvider, and that isn't documented. Could you either (A) remove it, if dropping user-supplied headers isn't intended:
| 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(); | |
| return options.getMergedHeaderProvider(internalHeaderProvider).getHeaders(); |
or (B) keep it, document it in the Javadoc, and cover it in a test (see the comment on ResumableMediaTest.java L126)?
| .setProjectId("p") | ||
| .setHost("http://localhost") | ||
| .setQuotaProjectId("q") | ||
| .setHeaderProvider(FixedHeaderProvider.create("x-custom", "v")) |
There was a problem hiding this comment.
[High] To make the filter in signedUrlHeaders pull its weight, have the header provider also supply the headers that should be stripped; assertNoCredentials will then actually exercise it. The non-signed test stays green. Keep X-Goog-User-Project mixed-case, because the merged header provider builds an ImmutableMap and throws on an exact duplicate of the internal lowercase key.
| .setHeaderProvider(FixedHeaderProvider.create("x-custom", "v")) | |
| .setHeaderProvider( | |
| FixedHeaderProvider.create( | |
| "x-custom", "v", "Authorization", "Bearer user", "X-Goog-User-Project", "up")) |
| .getService(); | ||
| } | ||
|
|
||
| private static void assertNoCredentials(RecordingTransport transport) { |
There was a problem hiding this comment.
nit: assertNoCredentials also checks that x-custom is present and the quota-project header is absent, so a name like assertSignedUrlHeaders would describe it better (update the call sites on L89 and L102). Optionally, the fresh and restored-writer tests could share a parameterized body.
| private static void assertNoCredentials(RecordingTransport transport) { | |
| private static void assertSignedUrlHeaders(RecordingTransport transport) { |
| private static final class RecordingTransport extends HttpTransport { | ||
| private final List<String> methods = Collections.synchronizedList(new ArrayList<>()); | ||
| private final List<Map<String, List<String>>> headers = | ||
| Collections.synchronizedList(new ArrayList<>()); | ||
|
|
||
| @Override | ||
| protected LowLevelHttpRequest buildRequest(String method, String url) { | ||
| return new MockLowLevelHttpRequest(url) { | ||
| @Override | ||
| public MockLowLevelHttpResponse execute() { | ||
| methods.add(method); | ||
| headers.add(getHeaders()); | ||
| if (method.equals("POST")) { | ||
| return new MockLowLevelHttpResponse() | ||
| .setStatusCode(url.contains("Signature=") ? 201 : 200) | ||
| .addHeader("Location", "http://localhost/upload?upload_id=u"); | ||
| } | ||
| return new MockLowLevelHttpResponse() | ||
| .addHeader("Content-Type", "application/json; charset=UTF-8") | ||
| .setContent("{\"bucket\":\"b\",\"name\":\"o\",\"size\":\"10\"}"); | ||
| } | ||
| }; | ||
| } | ||
| } |
There was a problem hiding this comment.
[Medium] This custom transport largely duplicates the existing spi/v1/AuditingHttpTransport fixture. spi/v1/HttpRpcContextTest (doTestInvocationIdNotInSignedURL) already holds the signed-URL writer(URL) header tests and covers both V2 and V4 URLs. These new tests are outside ResumableMediaTest's scope (startUploadForSignedUrl validation) and only use V2. Could you give AuditingHttpTransport a (method, url) -> LowLevelHttpResponse constructor, move these tests next to HttpRpcContextTest, and run them for both V2 and V4?
Send signed URL upload requests (Storage#writer(URL)) without client credentials (Authorization) or quota project (x-goog-user-project) headers. Other headers configured through HeaderProvider, along with timeouts and tracing, stay intact.