Skip to content

Commit

Permalink
fix: update version resolution logic to be more resilient (#2169)
Browse files Browse the repository at this point in the history
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 googleapis/google-api-java-client#1419
  • Loading branch information
BenWhitehead authored Aug 23, 2023
1 parent 665b714 commit c89d275
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -210,9 +209,7 @@ private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throw

HeaderProvider internalHeaderProvider =
StorageSettings.defaultApiClientHeaderProviderBuilder()
.setClientLibToken(
ServiceOptions.getGoogApiClientLibName(),
GaxProperties.getLibraryVersion(this.getClass()))
.setClientLibToken(ServiceOptions.getGoogApiClientLibName(), getLibraryVersion())
.build();

StorageSettings.Builder builder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Storage, StorageOptions> {

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
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit c89d275

Please sign in to comment.