3535import com .google .api .gax .core .BackgroundResource ;
3636import com .google .api .gax .core .ExecutorAsBackgroundResource ;
3737import com .google .api .gax .core .ExecutorProvider ;
38- import com .google .api .gax .rpc .internal .EnvironmentProvider ;
3938import com .google .api .gax .rpc .internal .QuotaProjectIdHidingCredentials ;
4039import com .google .api .gax .rpc .mtls .MtlsProvider ;
4140import com .google .api .gax .tracing .ApiTracerFactory ;
4241import com .google .api .gax .tracing .BaseApiTracerFactory ;
4342import com .google .auth .Credentials ;
4443import com .google .auto .value .AutoValue ;
45- import com .google .common .annotations .VisibleForTesting ;
4644import com .google .common .collect .ImmutableList ;
4745import com .google .common .collect .ImmutableMap ;
4846import com .google .common .collect .Sets ;
6765@ AutoValue
6866public 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 }
0 commit comments