diff --git a/.github/workflows/sonar.yaml b/.github/workflows/sonar.yaml new file mode 100644 index 000000000..575011e6a --- /dev/null +++ b/.github/workflows/sonar.yaml @@ -0,0 +1,36 @@ +name: Build +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened] +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: 11 + - name: Cache SonarCloud packages + uses: actions/cache@v2 + with: + path: ~/.sonar/cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar + - name: Cache Gradle packages + uses: actions/cache@v2 + with: + path: ~/.gradle/caches + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }} + restore-keys: ${{ runner.os }}-gradle + - name: Build and analyze + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: ./gradlew build sonarqube --info \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a2bbb56d..ed48c3460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +### [2.12.1](https://github.com/googleapis/gax-java/compare/v2.12.0...v2.12.1) (2022-02-09) + + +### Bug Fixes + +* revert "feat: add api key support ([#1436](https://github.com/googleapis/gax-java/issues/1436))" ([#1617](https://github.com/googleapis/gax-java/issues/1617)) ([47f98b8](https://github.com/googleapis/gax-java/commit/47f98b872c6896ad2af37e30db440321c2adb492)) + ## [2.12.0](https://github.com/googleapis/gax-java/compare/v2.11.0...v2.12.0) (2022-01-28) diff --git a/build.gradle b/build.gradle index 0867bd0b3..d47069d63 100644 --- a/build.gradle +++ b/build.gradle @@ -4,14 +4,16 @@ plugins { id 'java' id 'io.github.gradle-nexus.publish-plugin' version '1.1.0' - id 'com.diffplug.eclipse.apt' version '3.34.1' apply false + id 'com.diffplug.eclipse.apt' version '3.35.0' apply false id 'com.dorongold.task-tree' version '2.1.0' apply false id 'com.github.johnrengelman.shadow' version '7.1.2' apply false id 'com.github.sherter.google-java-format' version '0.9' apply false + + id 'org.sonarqube' version '3.3' } // TODO: Populate this from dependencies.properties version property (for proper Gradle-Bazel sync) -project.version = "2.12.0" // {x-version-update:gax:current} +project.version = "2.12.1" // {x-version-update:gax:current} allprojects { group = 'com.google.api' @@ -313,3 +315,11 @@ task javadocCombinedV3(type: Javadoc) { clean { delete 'tmp_docs/' } + +sonarqube { + properties { + property 'sonar.projectKey', 'googleapis_gax-java' + property 'sonar.organization', 'googleapis' + property 'sonar.host.url', 'https://sonarcloud.io' + } +} \ No newline at end of file diff --git a/dependencies.properties b/dependencies.properties index a38dc9ba6..fd6e933e8 100644 --- a/dependencies.properties +++ b/dependencies.properties @@ -8,16 +8,16 @@ # Versions of oneself # {x-version-update-start:gax:current} -version.gax=2.12.0 +version.gax=2.12.1 # {x-version-update-end} # {x-version-update-start:gax:current} -version.gax_grpc=2.12.0 +version.gax_grpc=2.12.1 # {x-version-update-end} # {x-version-update-start:gax:current} -version.gax_bom=2.12.0 +version.gax_bom=2.12.1 # {x-version-update-end} # {x-version-update-start:gax-httpjson:current} -version.gax_httpjson=0.97.0 +version.gax_httpjson=0.97.1 # {x-version-update-end} # Versions for dependencies which actual artifacts differ between Bazel and Gradle. @@ -62,7 +62,7 @@ maven.com_google_errorprone_error_prone_annotations=com.google.errorprone:error_ maven.com_google_j2objc_j2objc_annotations=com.google.j2objc:j2objc-annotations:1.3 maven.com_google_auto_value_auto_value=com.google.auto.value:auto-value:1.9 maven.com_google_auto_value_auto_value_annotations=com.google.auto.value:auto-value-annotations:1.9 -maven.com_google_api_api_common=com.google.api:api-common:2.1.3 +maven.com_google_api_api_common=com.google.api:api-common:2.1.4 maven.org_threeten_threetenbp=org.threeten:threetenbp:1.5.0 maven.com_google_api_grpc_grpc_google_iam_v1=com.google.api.grpc:grpc-google-iam-v1:1.0.9 maven.com_google_api_grpc_proto_google_iam_v1=com.google.api.grpc:proto-google-iam-v1:1.0.9 diff --git a/gax-bom/build.gradle b/gax-bom/build.gradle index c4ded5e11..983ff7699 100644 --- a/gax-bom/build.gradle +++ b/gax-bom/build.gradle @@ -5,7 +5,7 @@ plugins { archivesBaseName = 'gax-bom' -project.version = "2.12.0" // {x-version-update:gax-bom:current} +project.version = "2.12.1" // {x-version-update:gax-bom:current} def mavenJavaDir = "$buildDir/publications/mavenJava" def mavenJavaBomOutputFile = file(mavenJavaDir + '/pom-default.xml') diff --git a/gax-bom/pom.xml b/gax-bom/pom.xml index f4d220716..d4cdaaec2 100644 --- a/gax-bom/pom.xml +++ b/gax-bom/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.google.api gax-bom - 2.12.0 + 2.12.1 pom GAX (Google Api eXtensions) for Java Google Api eXtensions for Java @@ -33,34 +33,34 @@ com.google.api gax - 2.12.0 + 2.12.1 com.google.api gax - 2.12.0 + 2.12.1 testlib com.google.api gax-grpc - 2.12.0 + 2.12.1 com.google.api gax-grpc - 2.12.0 + 2.12.1 testlib com.google.api gax-httpjson - 0.97.0 + 0.97.1 com.google.api gax-httpjson - 0.97.0 + 0.97.1 testlib diff --git a/gax-grpc/build.gradle b/gax-grpc/build.gradle index cefc1121e..29f487785 100644 --- a/gax-grpc/build.gradle +++ b/gax-grpc/build.gradle @@ -1,7 +1,7 @@ archivesBaseName = 'gax-grpc' // TODO: Populate this from dependencies.properties version property (for proper Gradle-Bazel sync) -project.version = "2.12.0" // {x-version-update:gax-grpc:current} +project.version = "2.12.1" // {x-version-update:gax-grpc:current} dependencies { api(project(':gax'), diff --git a/gax-httpjson/build.gradle b/gax-httpjson/build.gradle index 691d60c60..864e40dbe 100644 --- a/gax-httpjson/build.gradle +++ b/gax-httpjson/build.gradle @@ -1,7 +1,7 @@ archivesBaseName = 'gax-httpjson' // TODO: Populate this from dependencies.properties version property (for proper Gradle-Bazel sync) -project.version = "0.97.0" // {x-version-update:gax-httpjson:current} +project.version = "0.97.1" // {x-version-update:gax-httpjson:current} dependencies { api(project(':gax'), diff --git a/gax/build.gradle b/gax/build.gradle index 59dbb5fc8..bb1c1b619 100644 --- a/gax/build.gradle +++ b/gax/build.gradle @@ -1,7 +1,7 @@ archivesBaseName = "gax" // TODO: Populate this from dependencies.properties version property (for proper Gradle-Bazel sync) -project.version = "2.12.0" // {x-version-update:gax:current} +project.version = "2.12.1" // {x-version-update:gax:current} dependencies { api(libraries['maven.com_google_api_api_common'], diff --git a/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 2afd5e2a8..ff18e77d3 100644 --- a/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -35,14 +35,12 @@ import com.google.api.gax.core.BackgroundResource; import com.google.api.gax.core.ExecutorAsBackgroundResource; import com.google.api.gax.core.ExecutorProvider; -import com.google.api.gax.rpc.internal.EnvironmentProvider; import com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials; import com.google.api.gax.rpc.mtls.MtlsProvider; import com.google.api.gax.tracing.ApiTracerFactory; import com.google.api.gax.tracing.BaseApiTracerFactory; import com.google.auth.Credentials; import com.google.auto.value.AutoValue; -import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; @@ -67,7 +65,6 @@ @AutoValue public abstract class ClientContext { private static final String QUOTA_PROJECT_ID_HEADER_KEY = "x-goog-user-project"; - private static final String API_KEY_HEADER_KEY = "x-goog-api-key"; /** * The objects that need to be closed in order to clean up the resources created in the process of @@ -162,32 +159,6 @@ static String getEndpoint( return endpoint; } - /** - * Retrieves the API key value and add it to the headers if API key exists. It first tries to - * retrieve the value from the stub settings. If not found, it then tries the load the - * GOOGLE_API_KEY environment variable. An IOException will be thrown if both GOOGLE_API_KEY and - * GOOGLE_APPLICATION_CREDENTIALS environment variables are set. - */ - @VisibleForTesting - static void addApiKeyToHeaders( - StubSettings settings, EnvironmentProvider environmentProvider, Map headers) - throws IOException { - if (settings.getApiKey() != null) { - headers.put(API_KEY_HEADER_KEY, settings.getApiKey()); - return; - } - - String apiKey = environmentProvider.getenv("GOOGLE_API_KEY"); - String applicationCredentials = environmentProvider.getenv("GOOGLE_APPLICATION_CREDENTIALS"); - if (apiKey != null && applicationCredentials != null) { - throw new IOException( - "Environment variables GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS are mutually exclusive"); - } - if (apiKey != null) { - headers.put(API_KEY_HEADER_KEY, apiKey); - } - } - /** * Instantiates the executor, credentials, and transport context based on the given client * settings. @@ -198,21 +169,14 @@ public static ClientContext create(StubSettings settings) throws IOException { ExecutorProvider backgroundExecutorProvider = settings.getBackgroundExecutorProvider(); final ScheduledExecutorService backgroundExecutor = backgroundExecutorProvider.getExecutor(); - Credentials credentials = null; - Map headers = getHeadersFromSettingsAndEnvironment(settings, System::getenv); - - boolean hasApiKey = headers.containsKey(API_KEY_HEADER_KEY); - if (!hasApiKey) { - credentials = settings.getCredentialsProvider().getCredentials(); + Credentials credentials = settings.getCredentialsProvider().getCredentials(); - if (settings.getQuotaProjectId() != null) { - // If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as - // QuotaProjectIdHidingCredentials. - // Ensure that a custom set quota project id takes priority over one detected by - // credentials. - // Avoid the backend receiving possibly conflict values of quotaProjectId - credentials = new QuotaProjectIdHidingCredentials(credentials); - } + if (settings.getQuotaProjectId() != null) { + // If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as + // QuotaProjectIdHidingCredentials. + // Ensure that a custom set quota project id takes priority over one detected by credentials. + // Avoid the backend receiving possibly conflict values of quotaProjectId + credentials = new QuotaProjectIdHidingCredentials(credentials); } TransportChannelProvider transportChannelProvider = settings.getTransportChannelProvider(); @@ -222,11 +186,11 @@ public static ClientContext create(StubSettings settings) throws IOException { if (transportChannelProvider.needsExecutor() && settings.getExecutorProvider() != null) { transportChannelProvider = transportChannelProvider.withExecutor(backgroundExecutor); } - + Map headers = getHeadersFromSettings(settings); if (transportChannelProvider.needsHeaders()) { transportChannelProvider = transportChannelProvider.withHeaders(headers); } - if (!hasApiKey && transportChannelProvider.needsCredentials()) { + if (transportChannelProvider.needsCredentials() && credentials != null) { transportChannelProvider = transportChannelProvider.withCredentials(credentials); } String endpoint = @@ -296,8 +260,7 @@ public static ClientContext create(StubSettings settings) throws IOException { * Getting a header map from HeaderProvider and InternalHeaderProvider from settings with Quota * Project Id. */ - private static Map getHeadersFromSettingsAndEnvironment( - StubSettings settings, EnvironmentProvider environmentProvider) throws IOException { + private static Map getHeadersFromSettings(StubSettings settings) { // Resolve conflicts when merging headers from multiple sources Map userHeaders = settings.getHeaderProvider().getHeaders(); Map internalHeaders = settings.getInternalHeaderProvider().getHeaders(); @@ -323,7 +286,6 @@ private static Map getHeadersFromSettingsAndEnvironment( effectiveHeaders.putAll(internalHeaders); effectiveHeaders.putAll(userHeaders); effectiveHeaders.putAll(conflictResolution); - addApiKeyToHeaders(settings, environmentProvider, effectiveHeaders); return ImmutableMap.copyOf(effectiveHeaders); } diff --git a/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java b/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java index 2fc25f566..04f1d59c9 100644 --- a/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java +++ b/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java @@ -73,7 +73,6 @@ public abstract class StubSettings> { private final String endpoint; private final String mtlsEndpoint; private final String quotaProjectId; - private final String apiKey; @Nullable private final WatchdogProvider streamWatchdogProvider; @Nonnull private final Duration streamWatchdogCheckInterval; @Nonnull private final ApiTracerFactory tracerFactory; @@ -100,7 +99,6 @@ protected StubSettings(Builder builder) { this.mtlsEndpoint = builder.mtlsEndpoint; this.switchToMtlsEndpointAllowed = builder.switchToMtlsEndpointAllowed; this.quotaProjectId = builder.quotaProjectId; - this.apiKey = builder.apiKey; this.streamWatchdogProvider = builder.streamWatchdogProvider; this.streamWatchdogCheckInterval = builder.streamWatchdogCheckInterval; this.tracerFactory = builder.tracerFactory; @@ -156,10 +154,6 @@ public final String getQuotaProjectId() { return quotaProjectId; } - public final String getApiKey() { - return apiKey; - } - @BetaApi("The surface for streaming is not stable yet and may change in the future.") @Nullable public final WatchdogProvider getStreamWatchdogProvider() { @@ -195,7 +189,6 @@ public String toString() { .add("mtlsEndpoint", mtlsEndpoint) .add("switchToMtlsEndpointAllowed", switchToMtlsEndpointAllowed) .add("quotaProjectId", quotaProjectId) - .add("apiKey", apiKey) .add("streamWatchdogProvider", streamWatchdogProvider) .add("streamWatchdogCheckInterval", streamWatchdogCheckInterval) .add("tracerFactory", tracerFactory) @@ -216,7 +209,6 @@ public abstract static class Builder< private String endpoint; private String mtlsEndpoint; private String quotaProjectId; - private String apiKey; @Nullable private WatchdogProvider streamWatchdogProvider; @Nonnull private Duration streamWatchdogCheckInterval; @Nonnull private ApiTracerFactory tracerFactory; @@ -242,7 +234,6 @@ protected Builder(StubSettings settings) { this.mtlsEndpoint = settings.mtlsEndpoint; this.switchToMtlsEndpointAllowed = settings.switchToMtlsEndpointAllowed; this.quotaProjectId = settings.quotaProjectId; - this.apiKey = settings.apiKey; this.streamWatchdogProvider = settings.streamWatchdogProvider; this.streamWatchdogCheckInterval = settings.streamWatchdogCheckInterval; this.tracerFactory = settings.tracerFactory; @@ -267,7 +258,6 @@ private static String getQuotaProjectIdFromClientContext(ClientContext clientCon } protected Builder(ClientContext clientContext) { - this.apiKey = null; if (clientContext == null) { this.backgroundExecutorProvider = InstantiatingExecutorProvider.newBuilder().build(); this.transportChannelProvider = null; @@ -442,11 +432,6 @@ public B setQuotaProjectId(String quotaProjectId) { return self(); } - public B setApiKey(String apiKey) { - this.apiKey = apiKey; - return self(); - } - /** * Sets how often the {@link Watchdog} will check ongoing streaming RPCs. Defaults to 10 secs. * Use {@link Duration#ZERO} to disable. @@ -528,10 +513,6 @@ public String getQuotaProjectId() { return quotaProjectId; } - public String getApiKey() { - return apiKey; - } - @BetaApi("The surface for streaming is not stable yet and may change in the future.") @Nonnull public Duration getStreamWatchdogCheckInterval() { @@ -568,7 +549,6 @@ public String toString() { .add("mtlsEndpoint", mtlsEndpoint) .add("switchToMtlsEndpointAllowed", switchToMtlsEndpointAllowed) .add("quotaProjectId", quotaProjectId) - .add("apiKey", apiKey) .add("streamWatchdogProvider", streamWatchdogProvider) .add("streamWatchdogCheckInterval", streamWatchdogCheckInterval) .add("tracerFactory", tracerFactory) diff --git a/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index b90e1e422..7fe418dd6 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -32,7 +32,6 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -42,7 +41,6 @@ import com.google.api.gax.core.ExecutorProvider; import com.google.api.gax.core.FixedCredentialsProvider; import com.google.api.gax.core.FixedExecutorProvider; -import com.google.api.gax.rpc.internal.EnvironmentProvider; import com.google.api.gax.rpc.mtls.MtlsProvider; import com.google.api.gax.rpc.mtls.MtlsProvider.MtlsEndpointUsagePolicy; import com.google.api.gax.rpc.testing.FakeChannel; @@ -56,7 +54,6 @@ import com.google.common.truth.Truth; import java.io.IOException; import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Executor; @@ -179,7 +176,7 @@ public TransportChannelProvider withPoolSize(int size) { @Override public TransportChannel getTransportChannel() throws IOException { - if (needsCredentials() && !headers.containsKey("x-goog-api-key")) { + if (needsCredentials()) { throw new IllegalStateException("Needs Credentials"); } transport.setExecutor(executor); @@ -772,73 +769,4 @@ public void testExecutorSettings() throws Exception { transportChannel = (FakeTransportChannel) context.getTransportChannel(); assertThat(transportChannel.getExecutor()).isSameInstanceAs(executorProvider.getExecutor()); } - - @Test - public void testAddApiKeyToHeadersFromStubSettings() throws IOException { - StubSettings settings = new FakeStubSettings.Builder().setApiKey("stub-setting-key").build(); - EnvironmentProvider environmentProvider = - name -> name.equals("GOOGLE_API_KEY") ? "env-key" : null; - Map headers = new HashMap<>(); - ClientContext.addApiKeyToHeaders(settings, environmentProvider, headers); - assertThat(headers).containsEntry("x-goog-api-key", "stub-setting-key"); - } - - @Test - public void testAddApiKeyToHeadersFromEnvironmentProvider() throws IOException { - StubSettings settings = new FakeStubSettings.Builder().build(); - EnvironmentProvider environmentProvider = - name -> name.equals("GOOGLE_API_KEY") ? "env-key" : null; - Map headers = new HashMap<>(); - ClientContext.addApiKeyToHeaders(settings, environmentProvider, headers); - assertThat(headers).containsEntry("x-goog-api-key", "env-key"); - } - - @Test - public void testAddApiKeyToHeadersNoApiKey() throws IOException { - StubSettings settings = new FakeStubSettings.Builder().build(); - EnvironmentProvider environmentProvider = name -> null; - Map headers = new HashMap<>(); - ClientContext.addApiKeyToHeaders(settings, environmentProvider, headers); - assertThat(headers).doesNotContainKey("x-goog-api-key"); - } - - @Test - public void testAddApiKeyToHeadersThrows() throws IOException { - StubSettings settings = new FakeStubSettings.Builder().build(); - EnvironmentProvider environmentProvider = - name -> name.equals("GOOGLE_API_KEY") ? "env-key" : "/path/to/adc/json"; - Map headers = new HashMap<>(); - Exception ex = - assertThrows( - IOException.class, - () -> ClientContext.addApiKeyToHeaders(settings, environmentProvider, headers)); - assertThat(ex) - .hasMessageThat() - .contains( - "Environment variables GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS are mutually exclusive"); - } - - @Test - public void testApiKey() throws IOException { - FakeStubSettings.Builder builder = new FakeStubSettings.Builder(); - - FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel()); - FakeTransportProvider transportProvider = - new FakeTransportProvider(transportChannel, null, true, null, null); - builder.setTransportChannelProvider(transportProvider); - - HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); - Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); - builder.setHeaderProvider(headerProvider); - - // Set API key. - builder.setApiKey("key"); - - ClientContext context = ClientContext.create(builder.build()); - - // Check API key is in the transport channel's header. - List resources = context.getBackgroundResources(); - FakeTransportChannel fakeTransportChannel = (FakeTransportChannel) resources.get(0); - assertThat(fakeTransportChannel.getHeaders()).containsEntry("x-goog-api-key", "key"); - } } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 7454180f2..41d9927a4 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2e6e5897b..41dfb8790 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/versions.txt b/versions.txt index 892a59bd4..21ce428cb 100644 --- a/versions.txt +++ b/versions.txt @@ -1,7 +1,7 @@ # Format: # module:released-version:current-version -gax:2.12.0:2.12.0 -gax-bom:2.12.0:2.12.0 -gax-grpc:2.12.0:2.12.0 -gax-httpjson:0.97.0:0.97.0 +gax:2.12.1:2.12.1 +gax-bom:2.12.1:2.12.1 +gax-grpc:2.12.1:2.12.1 +gax-httpjson:0.97.1:0.97.1