mirror of https://github.com/grpc/grpc-java.git
alts: Make GoogleDefaultChannelCredentials take a CallCredentials (#8548)
DirectPath is going to support non-default service account. This commit allows users to pass CallCredentials to GoogleDefaultChannelCredentials. See design in go/directpath-file-credential-google-default-creds
This commit is contained in:
parent
b746bab97b
commit
a2398ce5db
|
@ -44,18 +44,52 @@ public final class GoogleDefaultChannelCredentials {
|
|||
* as fallback.
|
||||
*/
|
||||
public static ChannelCredentials create() {
|
||||
return newBuilder().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new instance of {@link Builder}.
|
||||
*
|
||||
* @since 1.42.0
|
||||
*/
|
||||
public static Builder newBuilder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for {@link GoogleDefaultChannelCredentials} instances.
|
||||
*
|
||||
* @since 1.42.0
|
||||
*/
|
||||
public static final class Builder {
|
||||
private CallCredentials callCredentials;
|
||||
|
||||
private Builder() {}
|
||||
|
||||
/** Constructs GoogleDefaultChannelCredentials with a given call credential. */
|
||||
public Builder callCredentials(CallCredentials callCreds) {
|
||||
callCredentials = callCreds;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Builds a GoogleDefaultChannelCredentials instance. */
|
||||
public ChannelCredentials build() {
|
||||
ChannelCredentials nettyCredentials =
|
||||
InternalNettyChannelCredentials.create(createClientFactory());
|
||||
CallCredentials callCredentials;
|
||||
if (callCredentials != null) {
|
||||
return CompositeChannelCredentials.create(nettyCredentials, callCredentials);
|
||||
}
|
||||
CallCredentials callCreds;
|
||||
try {
|
||||
callCredentials = MoreCallCredentials.from(GoogleCredentials.getApplicationDefault());
|
||||
callCreds = MoreCallCredentials.from(GoogleCredentials.getApplicationDefault());
|
||||
} catch (IOException e) {
|
||||
callCredentials = new FailingCallCredentials(
|
||||
callCreds =
|
||||
new FailingCallCredentials(
|
||||
Status.UNAUTHENTICATED
|
||||
.withDescription("Failed to get Google default credentials")
|
||||
.withCause(e));
|
||||
}
|
||||
return CompositeChannelCredentials.create(nettyCredentials, callCredentials);
|
||||
return CompositeChannelCredentials.create(nettyCredentials, callCreds);
|
||||
}
|
||||
|
||||
private static InternalProtocolNegotiator.ClientFactory createClientFactory() {
|
||||
|
@ -71,3 +105,4 @@ public final class GoogleDefaultChannelCredentials {
|
|||
sslContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue