Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 47f98b8

Browse files
fix: revert "feat: add api key support (#1436)" (#1617)
1 parent 173eff4 commit 47f98b8

File tree

3 files changed

+11
-141
lines changed

3 files changed

+11
-141
lines changed

gax/src/main/java/com/google/api/gax/rpc/ClientContext.java

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@
3535
import com.google.api.gax.core.BackgroundResource;
3636
import com.google.api.gax.core.ExecutorAsBackgroundResource;
3737
import com.google.api.gax.core.ExecutorProvider;
38-
import com.google.api.gax.rpc.internal.EnvironmentProvider;
3938
import com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials;
4039
import com.google.api.gax.rpc.mtls.MtlsProvider;
4140
import com.google.api.gax.tracing.ApiTracerFactory;
4241
import com.google.api.gax.tracing.BaseApiTracerFactory;
4342
import com.google.auth.Credentials;
4443
import com.google.auto.value.AutoValue;
45-
import com.google.common.annotations.VisibleForTesting;
4644
import com.google.common.collect.ImmutableList;
4745
import com.google.common.collect.ImmutableMap;
4846
import com.google.common.collect.Sets;
@@ -67,7 +65,6 @@
6765
@AutoValue
6866
public abstract class ClientContext {
6967
private static final String QUOTA_PROJECT_ID_HEADER_KEY = "x-goog-user-project";
70-
private static final String API_KEY_HEADER_KEY = "x-goog-api-key";
7168

7269
/**
7370
* 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(
162159
return endpoint;
163160
}
164161

165-
/**
166-
* Retrieves the API key value and add it to the headers if API key exists. It first tries to
167-
* retrieve the value from the stub settings. If not found, it then tries the load the
168-
* GOOGLE_API_KEY environment variable. An IOException will be thrown if both GOOGLE_API_KEY and
169-
* GOOGLE_APPLICATION_CREDENTIALS environment variables are set.
170-
*/
171-
@VisibleForTesting
172-
static void addApiKeyToHeaders(
173-
StubSettings settings, EnvironmentProvider environmentProvider, Map<String, String> headers)
174-
throws IOException {
175-
if (settings.getApiKey() != null) {
176-
headers.put(API_KEY_HEADER_KEY, settings.getApiKey());
177-
return;
178-
}
179-
180-
String apiKey = environmentProvider.getenv("GOOGLE_API_KEY");
181-
String applicationCredentials = environmentProvider.getenv("GOOGLE_APPLICATION_CREDENTIALS");
182-
if (apiKey != null && applicationCredentials != null) {
183-
throw new IOException(
184-
"Environment variables GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS are mutually exclusive");
185-
}
186-
if (apiKey != null) {
187-
headers.put(API_KEY_HEADER_KEY, apiKey);
188-
}
189-
}
190-
191162
/**
192163
* Instantiates the executor, credentials, and transport context based on the given client
193164
* settings.
@@ -198,21 +169,14 @@ public static ClientContext create(StubSettings settings) throws IOException {
198169
ExecutorProvider backgroundExecutorProvider = settings.getBackgroundExecutorProvider();
199170
final ScheduledExecutorService backgroundExecutor = backgroundExecutorProvider.getExecutor();
200171

201-
Credentials credentials = null;
202-
Map<String, String> headers = getHeadersFromSettingsAndEnvironment(settings, System::getenv);
203-
204-
boolean hasApiKey = headers.containsKey(API_KEY_HEADER_KEY);
205-
if (!hasApiKey) {
206-
credentials = settings.getCredentialsProvider().getCredentials();
172+
Credentials credentials = settings.getCredentialsProvider().getCredentials();
207173

208-
if (settings.getQuotaProjectId() != null) {
209-
// If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as
210-
// QuotaProjectIdHidingCredentials.
211-
// Ensure that a custom set quota project id takes priority over one detected by
212-
// credentials.
213-
// Avoid the backend receiving possibly conflict values of quotaProjectId
214-
credentials = new QuotaProjectIdHidingCredentials(credentials);
215-
}
174+
if (settings.getQuotaProjectId() != null) {
175+
// If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as
176+
// QuotaProjectIdHidingCredentials.
177+
// Ensure that a custom set quota project id takes priority over one detected by credentials.
178+
// Avoid the backend receiving possibly conflict values of quotaProjectId
179+
credentials = new QuotaProjectIdHidingCredentials(credentials);
216180
}
217181

218182
TransportChannelProvider transportChannelProvider = settings.getTransportChannelProvider();
@@ -222,11 +186,11 @@ public static ClientContext create(StubSettings settings) throws IOException {
222186
if (transportChannelProvider.needsExecutor() && settings.getExecutorProvider() != null) {
223187
transportChannelProvider = transportChannelProvider.withExecutor(backgroundExecutor);
224188
}
225-
189+
Map<String, String> headers = getHeadersFromSettings(settings);
226190
if (transportChannelProvider.needsHeaders()) {
227191
transportChannelProvider = transportChannelProvider.withHeaders(headers);
228192
}
229-
if (!hasApiKey && transportChannelProvider.needsCredentials()) {
193+
if (transportChannelProvider.needsCredentials() && credentials != null) {
230194
transportChannelProvider = transportChannelProvider.withCredentials(credentials);
231195
}
232196
String endpoint =
@@ -296,8 +260,7 @@ public static ClientContext create(StubSettings settings) throws IOException {
296260
* Getting a header map from HeaderProvider and InternalHeaderProvider from settings with Quota
297261
* Project Id.
298262
*/
299-
private static Map<String, String> getHeadersFromSettingsAndEnvironment(
300-
StubSettings settings, EnvironmentProvider environmentProvider) throws IOException {
263+
private static Map<String, String> getHeadersFromSettings(StubSettings settings) {
301264
// Resolve conflicts when merging headers from multiple sources
302265
Map<String, String> userHeaders = settings.getHeaderProvider().getHeaders();
303266
Map<String, String> internalHeaders = settings.getInternalHeaderProvider().getHeaders();
@@ -323,7 +286,6 @@ private static Map<String, String> getHeadersFromSettingsAndEnvironment(
323286
effectiveHeaders.putAll(internalHeaders);
324287
effectiveHeaders.putAll(userHeaders);
325288
effectiveHeaders.putAll(conflictResolution);
326-
addApiKeyToHeaders(settings, environmentProvider, effectiveHeaders);
327289

328290
return ImmutableMap.copyOf(effectiveHeaders);
329291
}

gax/src/main/java/com/google/api/gax/rpc/StubSettings.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public abstract class StubSettings<SettingsT extends StubSettings<SettingsT>> {
7373
private final String endpoint;
7474
private final String mtlsEndpoint;
7575
private final String quotaProjectId;
76-
private final String apiKey;
7776
@Nullable private final WatchdogProvider streamWatchdogProvider;
7877
@Nonnull private final Duration streamWatchdogCheckInterval;
7978
@Nonnull private final ApiTracerFactory tracerFactory;
@@ -100,7 +99,6 @@ protected StubSettings(Builder builder) {
10099
this.mtlsEndpoint = builder.mtlsEndpoint;
101100
this.switchToMtlsEndpointAllowed = builder.switchToMtlsEndpointAllowed;
102101
this.quotaProjectId = builder.quotaProjectId;
103-
this.apiKey = builder.apiKey;
104102
this.streamWatchdogProvider = builder.streamWatchdogProvider;
105103
this.streamWatchdogCheckInterval = builder.streamWatchdogCheckInterval;
106104
this.tracerFactory = builder.tracerFactory;
@@ -156,10 +154,6 @@ public final String getQuotaProjectId() {
156154
return quotaProjectId;
157155
}
158156

159-
public final String getApiKey() {
160-
return apiKey;
161-
}
162-
163157
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
164158
@Nullable
165159
public final WatchdogProvider getStreamWatchdogProvider() {
@@ -195,7 +189,6 @@ public String toString() {
195189
.add("mtlsEndpoint", mtlsEndpoint)
196190
.add("switchToMtlsEndpointAllowed", switchToMtlsEndpointAllowed)
197191
.add("quotaProjectId", quotaProjectId)
198-
.add("apiKey", apiKey)
199192
.add("streamWatchdogProvider", streamWatchdogProvider)
200193
.add("streamWatchdogCheckInterval", streamWatchdogCheckInterval)
201194
.add("tracerFactory", tracerFactory)
@@ -216,7 +209,6 @@ public abstract static class Builder<
216209
private String endpoint;
217210
private String mtlsEndpoint;
218211
private String quotaProjectId;
219-
private String apiKey;
220212
@Nullable private WatchdogProvider streamWatchdogProvider;
221213
@Nonnull private Duration streamWatchdogCheckInterval;
222214
@Nonnull private ApiTracerFactory tracerFactory;
@@ -242,7 +234,6 @@ protected Builder(StubSettings settings) {
242234
this.mtlsEndpoint = settings.mtlsEndpoint;
243235
this.switchToMtlsEndpointAllowed = settings.switchToMtlsEndpointAllowed;
244236
this.quotaProjectId = settings.quotaProjectId;
245-
this.apiKey = settings.apiKey;
246237
this.streamWatchdogProvider = settings.streamWatchdogProvider;
247238
this.streamWatchdogCheckInterval = settings.streamWatchdogCheckInterval;
248239
this.tracerFactory = settings.tracerFactory;
@@ -267,7 +258,6 @@ private static String getQuotaProjectIdFromClientContext(ClientContext clientCon
267258
}
268259

269260
protected Builder(ClientContext clientContext) {
270-
this.apiKey = null;
271261
if (clientContext == null) {
272262
this.backgroundExecutorProvider = InstantiatingExecutorProvider.newBuilder().build();
273263
this.transportChannelProvider = null;
@@ -442,11 +432,6 @@ public B setQuotaProjectId(String quotaProjectId) {
442432
return self();
443433
}
444434

445-
public B setApiKey(String apiKey) {
446-
this.apiKey = apiKey;
447-
return self();
448-
}
449-
450435
/**
451436
* Sets how often the {@link Watchdog} will check ongoing streaming RPCs. Defaults to 10 secs.
452437
* Use {@link Duration#ZERO} to disable.
@@ -528,10 +513,6 @@ public String getQuotaProjectId() {
528513
return quotaProjectId;
529514
}
530515

531-
public String getApiKey() {
532-
return apiKey;
533-
}
534-
535516
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
536517
@Nonnull
537518
public Duration getStreamWatchdogCheckInterval() {
@@ -568,7 +549,6 @@ public String toString() {
568549
.add("mtlsEndpoint", mtlsEndpoint)
569550
.add("switchToMtlsEndpointAllowed", switchToMtlsEndpointAllowed)
570551
.add("quotaProjectId", quotaProjectId)
571-
.add("apiKey", apiKey)
572552
.add("streamWatchdogProvider", streamWatchdogProvider)
573553
.add("streamWatchdogCheckInterval", streamWatchdogCheckInterval)
574554
.add("tracerFactory", tracerFactory)

gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import static com.google.common.truth.Truth.assertThat;
3333
import static org.junit.Assert.assertEquals;
3434
import static org.junit.Assert.assertFalse;
35-
import static org.junit.Assert.assertThrows;
3635
import static org.junit.Assert.assertTrue;
3736
import static org.junit.Assert.fail;
3837

@@ -42,7 +41,6 @@
4241
import com.google.api.gax.core.ExecutorProvider;
4342
import com.google.api.gax.core.FixedCredentialsProvider;
4443
import com.google.api.gax.core.FixedExecutorProvider;
45-
import com.google.api.gax.rpc.internal.EnvironmentProvider;
4644
import com.google.api.gax.rpc.mtls.MtlsProvider;
4745
import com.google.api.gax.rpc.mtls.MtlsProvider.MtlsEndpointUsagePolicy;
4846
import com.google.api.gax.rpc.testing.FakeChannel;
@@ -56,7 +54,6 @@
5654
import com.google.common.truth.Truth;
5755
import java.io.IOException;
5856
import java.util.Collections;
59-
import java.util.HashMap;
6057
import java.util.List;
6158
import java.util.Map;
6259
import java.util.concurrent.Executor;
@@ -179,7 +176,7 @@ public TransportChannelProvider withPoolSize(int size) {
179176

180177
@Override
181178
public TransportChannel getTransportChannel() throws IOException {
182-
if (needsCredentials() && !headers.containsKey("x-goog-api-key")) {
179+
if (needsCredentials()) {
183180
throw new IllegalStateException("Needs Credentials");
184181
}
185182
transport.setExecutor(executor);
@@ -772,73 +769,4 @@ public void testExecutorSettings() throws Exception {
772769
transportChannel = (FakeTransportChannel) context.getTransportChannel();
773770
assertThat(transportChannel.getExecutor()).isSameInstanceAs(executorProvider.getExecutor());
774771
}
775-
776-
@Test
777-
public void testAddApiKeyToHeadersFromStubSettings() throws IOException {
778-
StubSettings settings = new FakeStubSettings.Builder().setApiKey("stub-setting-key").build();
779-
EnvironmentProvider environmentProvider =
780-
name -> name.equals("GOOGLE_API_KEY") ? "env-key" : null;
781-
Map<String, String> headers = new HashMap<>();
782-
ClientContext.addApiKeyToHeaders(settings, environmentProvider, headers);
783-
assertThat(headers).containsEntry("x-goog-api-key", "stub-setting-key");
784-
}
785-
786-
@Test
787-
public void testAddApiKeyToHeadersFromEnvironmentProvider() throws IOException {
788-
StubSettings settings = new FakeStubSettings.Builder().build();
789-
EnvironmentProvider environmentProvider =
790-
name -> name.equals("GOOGLE_API_KEY") ? "env-key" : null;
791-
Map<String, String> headers = new HashMap<>();
792-
ClientContext.addApiKeyToHeaders(settings, environmentProvider, headers);
793-
assertThat(headers).containsEntry("x-goog-api-key", "env-key");
794-
}
795-
796-
@Test
797-
public void testAddApiKeyToHeadersNoApiKey() throws IOException {
798-
StubSettings settings = new FakeStubSettings.Builder().build();
799-
EnvironmentProvider environmentProvider = name -> null;
800-
Map<String, String> headers = new HashMap<>();
801-
ClientContext.addApiKeyToHeaders(settings, environmentProvider, headers);
802-
assertThat(headers).doesNotContainKey("x-goog-api-key");
803-
}
804-
805-
@Test
806-
public void testAddApiKeyToHeadersThrows() throws IOException {
807-
StubSettings settings = new FakeStubSettings.Builder().build();
808-
EnvironmentProvider environmentProvider =
809-
name -> name.equals("GOOGLE_API_KEY") ? "env-key" : "/path/to/adc/json";
810-
Map<String, String> headers = new HashMap<>();
811-
Exception ex =
812-
assertThrows(
813-
IOException.class,
814-
() -> ClientContext.addApiKeyToHeaders(settings, environmentProvider, headers));
815-
assertThat(ex)
816-
.hasMessageThat()
817-
.contains(
818-
"Environment variables GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS are mutually exclusive");
819-
}
820-
821-
@Test
822-
public void testApiKey() throws IOException {
823-
FakeStubSettings.Builder builder = new FakeStubSettings.Builder();
824-
825-
FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel());
826-
FakeTransportProvider transportProvider =
827-
new FakeTransportProvider(transportChannel, null, true, null, null);
828-
builder.setTransportChannelProvider(transportProvider);
829-
830-
HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class);
831-
Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of());
832-
builder.setHeaderProvider(headerProvider);
833-
834-
// Set API key.
835-
builder.setApiKey("key");
836-
837-
ClientContext context = ClientContext.create(builder.build());
838-
839-
// Check API key is in the transport channel's header.
840-
List<BackgroundResource> resources = context.getBackgroundResources();
841-
FakeTransportChannel fakeTransportChannel = (FakeTransportChannel) resources.get(0);
842-
assertThat(fakeTransportChannel.getHeaders()).containsEntry("x-goog-api-key", "key");
843-
}
844772
}

0 commit comments

Comments
 (0)