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

Move CredentialProvider up to ClientSettings #305

Merged
merged 8 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2016, Google Inc. All rights reserved.

This comment was marked as spam.

This comment was marked as spam.

*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.grpc;

import com.google.api.core.ApiFuture;
import com.google.common.base.Preconditions;
import io.grpc.CallCredentials;

class AuthCallable<RequestT, ResponseT> implements FutureCallable<RequestT, ResponseT> {

This comment was marked as spam.

This comment was marked as spam.

private final FutureCallable<RequestT, ResponseT> callable;
private final CallCredentials credentials;

AuthCallable(FutureCallable<RequestT, ResponseT> callable, CallCredentials credentials) {
this.callable = Preconditions.checkNotNull(callable);
this.credentials = Preconditions.checkNotNull(credentials);
}

@Override
public ApiFuture<ResponseT> futureCall(RequestT request, CallContext context) {
if (context.getCallOptions().getCredentials() == null) {
context = context.withCallOptions(context.getCallOptions().withCallCredentials(credentials));
}
return callable.futureCall(request, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
package com.google.api.gax.grpc;

import com.google.api.core.BetaApi;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.retrying.RetrySettings;
import io.grpc.Status;
import java.io.IOException;
import java.util.Set;
import javax.annotation.Nullable;

/**
* A base settings class to configure a service API class.
Expand Down Expand Up @@ -65,11 +67,21 @@ public abstract class ClientSettings {

private final ExecutorProvider executorProvider;
private final ChannelProvider channelProvider;
@Nullable private final CredentialsProvider credentialsProvider;

/** Constructs an instance of ClientSettings. */
@Deprecated
protected ClientSettings(ExecutorProvider executorProvider, ChannelProvider channelProvider) {
this(executorProvider, channelProvider, null);
}

/** Constructs an instance of ClientSettings. */
protected ClientSettings(
ExecutorProvider executorProvider,
ChannelProvider channelProvider,
CredentialsProvider credentialsProvider) {
this.executorProvider = executorProvider;
this.channelProvider = channelProvider;
this.credentialsProvider = credentialsProvider;
}

/** Gets a channel and an executor for making calls. */
Expand All @@ -85,10 +97,16 @@ public final ChannelProvider getChannelProvider() {
return channelProvider;
}

@Nullable
public final CredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}

public abstract static class Builder {

private ExecutorProvider executorProvider;
private ChannelProvider channelProvider;
private CredentialsProvider credentialsProvider;

/** Create a builder from a ClientSettings object. */
protected Builder(ClientSettings settings) {
Expand Down Expand Up @@ -118,6 +136,12 @@ public Builder setChannelProvider(ChannelProvider channelProvider) {
return this;
}

/** Sets the CredentialsProvider to use for getting the channel to make calls with. */
public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
return this;
}

/** Gets the ExecutorProvider that was previously set on this Builder. */
public ExecutorProvider getExecutorProvider() {
return executorProvider;
Expand All @@ -128,6 +152,11 @@ public ChannelProvider getChannelProvider() {
return channelProvider;
}

/** Gets the CredentialsProvider that was previously set on this Builder. */
public CredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}

/** Performs a merge, using only non-null fields */
protected Builder applyToAllUnaryMethods(
Iterable<UnaryCallSettings.Builder> methodSettingsBuilders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private ManagedChannel createChannel(Executor executor) throws IOException {
* Gets the credentials which will be used to call the service. If the credentials have not been
* acquired yet, then they will be acquired when this function is called.
*/
@Deprecated
public Credentials getCredentials() throws IOException {
return getCredentialsProvider().getCredentials();
}
Expand All @@ -147,6 +148,7 @@ public Credentials getCredentials() throws IOException {
* The credentials to use in order to call the service. Credentials will not be acquired until
* they are required.
*/
@Deprecated
public CredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}
Expand Down Expand Up @@ -261,12 +263,14 @@ public Builder setExecutorProvider(ExecutorProvider executorProvider) {
* Sets the CredentialsProvider which will acquire the credentials for making calls to the
* service. Credentials will not be acquired until they are required.
*/
@Deprecated
public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) {

This comment was marked as spam.

This comment was marked as spam.

this.credentialsProvider = credentialsProvider;
return this;
}

/** The previously set CredentialsProvider. */
@Deprecated
public CredentialsProvider getCredentialsProvider() {
return credentialsProvider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import io.grpc.CallCredentials;
import io.grpc.Channel;
import io.grpc.Status;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -328,4 +329,8 @@ UnaryCallable<RequestT, ResponseT> batching(
return new UnaryCallable<>(
new BatchingCallable<>(callable, batchingDescriptor, batcherFactory), channel, settings);
}

UnaryCallable<RequestT, ResponseT> auth(CallCredentials credentials) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

return new UnaryCallable<>(new AuthCallable<>(callable, credentials));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2016, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.grpc;

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.common.truth.Truth;
import io.grpc.CallCredentials;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;

@RunWith(JUnit4.class)
public class AuthCallableTest {
private static class StashCallable implements FutureCallable<Integer, Integer> {
CallCredentials lastCredentials;

@Override
public ApiFuture<Integer> futureCall(Integer request, CallContext context) {
lastCredentials = context.getCallOptions().getCredentials();
return ApiFutures.<Integer>immediateFuture(42);
}
}

@Test
public void testAuth() throws InterruptedException, ExecutionException, CancellationException {
StashCallable stash = new StashCallable();
Truth.assertThat(UnaryCallable.create(stash).futureCall(0).get()).isEqualTo(42);
Truth.assertThat(stash.lastCredentials).isNull();

CallCredentials cred = Mockito.mock(CallCredentials.class);
Truth.assertThat(UnaryCallable.create(stash).auth(cred).futureCall(0).get()).isEqualTo(42);
Truth.assertThat(stash.lastCredentials).isEqualTo(cred);
}
}