Skip to content

Commit

Permalink
Bigtable: enable JWT tokens (#3351)
Browse files Browse the repository at this point in the history
This will remove the periodic stop the world OAuth token refresh.
JWT tokens will be used instead of OAuth tokens when the user uses
a service account and the default scopes.
  • Loading branch information
igorbernstein2 authored and pongad committed Jun 8, 2018
1 parent 3b34f8a commit 33eb9b5
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.api.gax.batching.BatchingSettings;
import com.google.api.gax.batching.FlowControlSettings;
import com.google.api.gax.batching.FlowController.LimitExceededBehavior;
import com.google.api.gax.core.GoogleCredentialsProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.BatchingCallSettings;
Expand All @@ -35,6 +36,7 @@
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -92,6 +94,19 @@ public class EnhancedBigtableStubSettings extends StubSettings<EnhancedBigtableS
.setTotalTimeout(Duration.ofMillis(600000L))
.build();

/**
* Scopes that are equivalent to JWT's audience.
*
* <p>When the credentials provider contains any of these scopes (default behavior) and the
* application default credentials point to a service account, then OAuth2 tokens will be replaced
* with JWT tokens. This removes the need for access token refreshes. */
private static final ImmutableList<String> JWT_ENABLED_SCOPES =
ImmutableList.<String>builder()
.add("https://www.googleapis.com/auth/bigtable.data")
.add("https://www.googleapis.com/auth/cloud-bigtable.data")
.add("https://www.googleapis.com/auth/cloud-platform")
.build();

private final InstanceName instanceName;
private final String appProfileId;

Expand Down Expand Up @@ -131,6 +146,12 @@ public String getAppProfileId() {
return appProfileId;
}

/** Returns a builder for the default credentials for this service. */
public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() {
return BigtableStubSettings.defaultCredentialsProviderBuilder()
.setJwtEnabledScopes(JWT_ENABLED_SCOPES);
}

/** Returns the object with the settings used for calls to ReadRows. */
public ServerStreamingCallSettings<Query, Row> readRowsSettings() {
return readRowsSettings;
Expand Down Expand Up @@ -195,12 +216,12 @@ public static class Builder extends StubSettings.Builder<EnhancedBigtableStubSet
*/
private Builder() {
this.appProfileId = SERVER_DEFAULT_APP_PROFILE_ID;
setCredentialsProvider(defaultCredentialsProviderBuilder().build());

// Defaults provider
BigtableStubSettings.Builder baseDefaults = BigtableStubSettings.newBuilder();

setEndpoint(baseDefaults.getEndpoint());
setCredentialsProvider(baseDefaults.getCredentialsProvider());
setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder()
// TODO: tune channels
Expand Down

0 comments on commit 33eb9b5

Please sign in to comment.