From c89d27508039a014ea5a6dd8d4889f63d07db73f Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Wed, 23 Aug 2023 13:33:42 -0400 Subject: [PATCH] fix: update version resolution logic to be more resilient (#2169) Update StorageOptions to resolve their version from maven metadata rather than manifest version. A jar can only have on MANIFEST.mf thereby leading to storage thinking it is a version it is not. In particular, if a program is shaded, this change will now allow for the actual version of storage to be carried through. Related https://github.com/googleapis/google-api-java-client/pull/1419 --- .../cloud/storage/GrpcStorageOptions.java | 5 +-- .../google/cloud/storage/StorageOptions.java | 40 +++++++++++++++++++ .../transfermanager/TransferManagerImpl.java | 4 +- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java index a6aa331350..09dfaf5dc0 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java @@ -23,7 +23,6 @@ import com.google.api.core.InternalApi; import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.core.FixedCredentialsProvider; -import com.google.api.gax.core.GaxProperties; import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.gax.grpc.GrpcInterceptorProvider; import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; @@ -210,9 +209,7 @@ private Tuple> resolveSettingsAndOpts() throw HeaderProvider internalHeaderProvider = StorageSettings.defaultApiClientHeaderProviderBuilder() - .setClientLibToken( - ServiceOptions.getGoogApiClientLibName(), - GaxProperties.getLibraryVersion(this.getClass())) + .setClientLibToken(ServiceOptions.getGoogApiClientLibName(), getLibraryVersion()) .build(); StorageSettings.Builder builder = diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageOptions.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageOptions.java index 86f2f92b07..74a6ffb3df 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageOptions.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageOptions.java @@ -17,6 +17,7 @@ package com.google.cloud.storage; import com.google.api.core.BetaApi; +import com.google.api.core.InternalApi; import com.google.cloud.NoCredentials; import com.google.cloud.ServiceDefaults; import com.google.cloud.ServiceOptions; @@ -26,10 +27,38 @@ import com.google.cloud.storage.HttpStorageOptions.HttpStorageRpcFactory; import com.google.cloud.storage.TransportCompatibility.Transport; import com.google.cloud.storage.spi.StorageRpcFactory; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; public abstract class StorageOptions extends ServiceOptions { private static final long serialVersionUID = -7295846567928013233L; + private static final String VERSION; + + static { + String tmp = "unresolved"; + final Properties props = new Properties(); + try { + String resourcePath = + String.format( + "/META-INF/maven/%s/%s/pom.properties", "com.google.cloud", "google-cloud-storage"); + InputStream resourceAsStream = StorageOptions.class.getResourceAsStream(resourcePath); + if (resourceAsStream == null) { + // some classloaders don't like a leading slash + resourceAsStream = StorageOptions.class.getResourceAsStream(resourcePath.substring(1)); + } + if (resourceAsStream != null) { + props.load(resourceAsStream); + resourceAsStream.close(); + + tmp = props.getProperty("version", "unknown-version"); + } + } catch (IOException ignore) { + // ignored + } + VERSION = tmp; + } /** @deprecated Use {@link HttpStorageFactory} */ @Deprecated @@ -86,6 +115,17 @@ protected boolean projectIdRequired() { return false; } + @Override + public String getLibraryVersion() { + return VERSION; + } + + /* This can break at any time, the value produce is intended to be informative not authoritative */ + @InternalApi + public static String version() { + return VERSION; + } + @SuppressWarnings("unchecked") @Override public abstract StorageOptions.Builder toBuilder(); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/transfermanager/TransferManagerImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/transfermanager/TransferManagerImpl.java index 3d5845cf55..2807d7923f 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/transfermanager/TransferManagerImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/transfermanager/TransferManagerImpl.java @@ -20,7 +20,6 @@ import com.google.api.core.ApiFutures; import com.google.api.core.BetaApi; import com.google.api.core.ListenableFutureToApiFuture; -import com.google.api.gax.core.GaxProperties; import com.google.api.gax.rpc.FixedHeaderProvider; import com.google.cloud.storage.BlobId; import com.google.cloud.storage.BlobInfo; @@ -45,8 +44,7 @@ final class TransferManagerImpl implements TransferManager { private static final String USER_AGENT_ENTRY = "gcloud-tm/"; - private static final String LIBRARY_VERSION = - GaxProperties.getLibraryVersion(TransferManagerConfig.class); + private static final String LIBRARY_VERSION = StorageOptions.version(); private final TransferManagerConfig transferManagerConfig; private final ListeningExecutorService executor; private final Qos qos;