Skip to content

Commit

Permalink
fix: update grpc x-goog-user-project handling gracefulness
Browse files Browse the repository at this point in the history
When an instance of credentials that hasRequestMetadata() but can't refresh an IllegalStateException can be thrown. Add new tests to force failure and update handling to be graceful of this.
  • Loading branch information
BenWhitehead committed Apr 14, 2023
1 parent 96a9294 commit c202362
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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.InstantiatingGrpcChannelProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.retrying.StreamResumptionStrategy;
Expand All @@ -33,6 +32,8 @@
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials;
import com.google.auth.Credentials;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.OAuth2Credentials;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceFactory;
import com.google.cloud.ServiceOptions;
Expand All @@ -54,6 +55,8 @@
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.net.URI;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -160,21 +163,35 @@ private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throw
Opts<UserProject> defaultOpts = Opts.empty();
CredentialsProvider credentialsProvider;
if (credentials instanceof NoCredentials) {
credentialsProvider = NoCredentialsProvider.create();
// com.google.api.gax.core.NoCredentialsProvider returns null as its credentials instance
// the returned value is passed blindly to gRPC credentials, which in the case of directpath
// will end up asserting non-null.
// To avoid this obscure NPE, create a factory that will return the NoCredentials instance
credentialsProvider = FixedCredentialsProvider.create(credentials);
} else {
boolean foundQuotaProject = false;
if (credentials.hasRequestMetadata()) {
Map<String, List<String>> requestMetadata = credentials.getRequestMetadata(uri);
for (Entry<String, List<String>> e : requestMetadata.entrySet()) {
String key = e.getKey();
if ("x-goog-user-project".equals(key.trim().toLowerCase(Locale.ENGLISH))) {
List<String> value = e.getValue();
if (!value.isEmpty()) {
foundQuotaProject = true;
defaultOpts = Opts.from(UnifiedOpts.userProject(value.get(0)));
break;
try {
Map<String, List<String>> requestMetadata = credentials.getRequestMetadata(uri);
for (Entry<String, List<String>> e : requestMetadata.entrySet()) {
String key = e.getKey();
if ("x-goog-user-project".equals(key.trim().toLowerCase(Locale.ENGLISH))) {
List<String> value = e.getValue();
if (!value.isEmpty()) {
foundQuotaProject = true;
defaultOpts = Opts.from(UnifiedOpts.userProject(value.get(0)));
break;
}
}
}
} catch (IllegalStateException e) {
// This happens when an instance of OAuth2Credentials attempts to refresh its
// access token during our attempt at getting request metadata.
// This is most easily reproduced by OAuth2Credentials.create(null);
// see com.google.auth.oauth2.OAuth2Credentials.refreshAccessToken
if (!e.getMessage().startsWith("OAuth2Credentials")) {
throw e;
}
}
}
if (foundQuotaProject) {
Expand Down Expand Up @@ -680,4 +697,38 @@ protected StorageSettings.Builder setInternalHeaderProvider(
return super.setInternalHeaderProvider(internalHeaderProvider);
}
}

private static final class DirectPathFriendlyNoCredentials extends OAuth2Credentials {
private static final DirectPathFriendlyNoCredentials INSTANCE =
new DirectPathFriendlyNoCredentials();

private static final AccessToken TOKEN =
AccessToken.newBuilder()
.setTokenValue("")
.setExpirationTime(Date.from(Instant.EPOCH))
.build();

DirectPathFriendlyNoCredentials() {
super(TOKEN, java.time.Duration.ZERO, java.time.Duration.ZERO);
}

@Override
public boolean hasRequestMetadata() {
return false;
}

@Override
public AccessToken refreshAccessToken() throws IOException {
return TOKEN;
}

public static DirectPathFriendlyNoCredentials getInstance() {
return INSTANCE;
}

/** prevent java serialization from using a new instance */
private Object readResolve() {
return INSTANCE;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.storage.it;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.OAuth2Credentials;
import com.google.cloud.NoCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import org.junit.Test;

public final class ITStorageOptionsTest {

@Test
public void clientShouldConstructCleanly_WithNoCredentials_http() throws Exception {
StorageOptions options =
StorageOptions.http().setCredentials(NoCredentials.getInstance()).build();
doTest(options);
}

@Test
public void clientShouldConstructCleanly_WithNoCredentials_grpc() throws Exception {
StorageOptions options =
StorageOptions.grpc().setCredentials(NoCredentials.getInstance()).build();
doTest(options);
}

@Test
public void clientShouldConstructCleanly_nullAccessToken_google_http() throws Exception {
GoogleCredentials cred = GoogleCredentials.create(/* accessToken= */ null);
StorageOptions options = StorageOptions.http().setCredentials(cred).build();
doTest(options);
}

@Test
public void clientShouldConstructCleanly_nullAccessToken_google_grpc() throws Exception {
GoogleCredentials cred = GoogleCredentials.create(/* accessToken= */ null);
StorageOptions options = StorageOptions.grpc().setCredentials(cred).build();
doTest(options);
}

@Test
public void clientShouldConstructCleanly_nullAccessToken_oauth_http() throws Exception {
OAuth2Credentials cred = OAuth2Credentials.create(null);
StorageOptions options = StorageOptions.http().setCredentials(cred).build();
doTest(options);
}

@Test
public void clientShouldConstructCleanly_nullAccessToken_oauth_grpc() throws Exception {
OAuth2Credentials cred = OAuth2Credentials.create(null);
StorageOptions options = StorageOptions.grpc().setCredentials(cred).build();
doTest(options);
}

private static void doTest(StorageOptions options) throws Exception {
//noinspection EmptyTryBlock
try (Storage ignore = options.getService()) {}
}
}

0 comments on commit c202362

Please sign in to comment.