From e5d7005984ab92c79fe6621d7b039c11aee9c6bb Mon Sep 17 00:00:00 2001 From: Arunkumar Chacko Date: Wed, 13 Mar 2024 09:12:07 +0530 Subject: [PATCH] Set user agent in gRPC requests (#1126) --- gcs/CHANGES.md | 1 + .../gcsio/GoogleCloudStorageClientImpl.java | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gcs/CHANGES.md b/gcs/CHANGES.md index 7eee8a653d..f8505853c0 100644 --- a/gcs/CHANGES.md +++ b/gcs/CHANGES.md @@ -1,6 +1,7 @@ # Release Notes ## Next +1. [Bug Fix] Set user agent in gRPC requests ## 2.2.20 - 2024-02-21 1. Fix downscoping not working when gRPC is enabled issue diff --git a/gcsio/src/main/java/com/google/cloud/hadoop/gcsio/GoogleCloudStorageClientImpl.java b/gcsio/src/main/java/com/google/cloud/hadoop/gcsio/GoogleCloudStorageClientImpl.java index eb91b7f359..427e03fc99 100644 --- a/gcsio/src/main/java/com/google/cloud/hadoop/gcsio/GoogleCloudStorageClientImpl.java +++ b/gcsio/src/main/java/com/google/cloud/hadoop/gcsio/GoogleCloudStorageClientImpl.java @@ -40,8 +40,10 @@ import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.MoreObjects; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.flogger.GoogleLogger; import com.google.common.util.concurrent.ThreadFactoryBuilder; @@ -67,6 +69,7 @@ */ @VisibleForTesting public class GoogleCloudStorageClientImpl extends ForwardingGoogleCloudStorage { + private static final String USER_AGENT = "user-agent"; private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); private final GoogleCloudStorageOptions storageOptions; @@ -231,9 +234,10 @@ private Storage createStorage( ExecutorService pCUExecutorService, Function, String> downscopedAccessTokenFn) throws IOException { + final ImmutableMap headers = getUpdatedHeadersWithUserAgent(storageOptions); return StorageOptions.grpc() .setAttemptDirectPath(storageOptions.isDirectPathPreferred()) - .setHeaderProvider(() -> storageOptions.getHttpRequestHeaders()) + .setHeaderProvider(() -> headers) .setGrpcInterceptorProvider( () -> { List list = new ArrayList<>(); @@ -264,6 +268,22 @@ private Storage createStorage( .getService(); } + private static ImmutableMap getUpdatedHeadersWithUserAgent( + GoogleCloudStorageOptions storageOptions) { + ImmutableMap httpRequestHeaders = + MoreObjects.firstNonNull(storageOptions.getHttpRequestHeaders(), ImmutableMap.of()); + String appName = storageOptions.getAppName(); + if (!httpRequestHeaders.containsKey(USER_AGENT) && !Strings.isNullOrEmpty(appName)) { + logger.atFiner().log("Setting useragent %s", appName); + return ImmutableMap.builder() + .putAll(httpRequestHeaders) + .put(USER_AGENT, appName) + .build(); + } + + return httpRequestHeaders; + } + private Credentials getNoCredentials( Function, String> downscopedAccessTokenFn) { if (downscopedAccessTokenFn == null) {