Skip to content

Commit

Permalink
feat: add support for new setAllowHardBoundTokens field. (#3467)
Browse files Browse the repository at this point in the history
Introduce new `setAllowHardBoundTokens` field.
  • Loading branch information
rmehta19 authored Jan 22, 2025
1 parent afec970 commit 38431a2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,35 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
@Nullable private final Boolean allowNonDefaultServiceAccount;
@VisibleForTesting final ImmutableMap<String, ?> directPathServiceConfig;
@Nullable private final MtlsProvider mtlsProvider;
@Nullable private final List<HardBoundTokenTypes> allowedHardBoundTokenTypes;
@VisibleForTesting final Map<String, String> headersWithDuplicatesRemoved = new HashMap<>();

@Nullable
private final ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> channelConfigurator;

/*
* Experimental feature
*
* <p>{@link HardBoundTokenTypes} specifies if hard bound tokens should be used if DirectPath
* or S2A is used to estabilsh a connection to Google APIs.
*
*/
@InternalApi
public enum HardBoundTokenTypes {
// If DirectPath is used to create the channel, use hard ALTS-bound tokens for requests sent on
// that channel.
ALTS,
// If MTLS via S2A is used to create the channel, use hard MTLS-bound tokens for requests sent
// on that channel.
MTLS_S2A
}

private InstantiatingGrpcChannelProvider(Builder builder) {
this.processorCount = builder.processorCount;
this.executor = builder.executor;
this.headerProvider = builder.headerProvider;
this.endpoint = builder.endpoint;
this.allowedHardBoundTokenTypes = builder.allowedHardBoundTokenTypes;
this.mtlsProvider = builder.mtlsProvider;
this.envProvider = builder.envProvider;
this.interceptorProvider = builder.interceptorProvider;
Expand Down Expand Up @@ -620,6 +639,7 @@ public static final class Builder {
@Nullable private Boolean attemptDirectPathXds;
@Nullable private Boolean allowNonDefaultServiceAccount;
@Nullable private ImmutableMap<String, ?> directPathServiceConfig;
@Nullable private List<HardBoundTokenTypes> allowedHardBoundTokenTypes;

private Builder() {
processorCount = Runtime.getRuntime().availableProcessors();
Expand Down Expand Up @@ -700,6 +720,19 @@ public Builder setEndpoint(String endpoint) {
return this;
}

/*
* Sets the allowed hard bound token types for this TransportChannelProvider.
*
* <p>The list of
* {@link HardBoundTokenTypes} indicates for which methods of connecting to Google APIs hard bound tokens should
* be used. This is optional; if it is not provided, bearer tokens will be used.
*/
@InternalApi
public Builder setAllowHardBoundTokenTypes(List<HardBoundTokenTypes> allowedValues) {
this.allowedHardBoundTokenTypes = allowedValues;
return this;
}

@VisibleForTesting
Builder setMtlsProvider(MtlsProvider mtlsProvider) {
this.mtlsProvider = mtlsProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ void testToBuilder() {
throw new UnsupportedOperationException();
};
Map<String, ?> directPathServiceConfig = ImmutableMap.of("loadbalancingConfig", "grpclb");
List<InstantiatingGrpcChannelProvider.HardBoundTokenTypes> hardBoundTokenTypes =
new ArrayList<>();
hardBoundTokenTypes.add(InstantiatingGrpcChannelProvider.HardBoundTokenTypes.ALTS);
hardBoundTokenTypes.add(InstantiatingGrpcChannelProvider.HardBoundTokenTypes.MTLS_S2A);

InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
Expand All @@ -238,6 +242,7 @@ void testToBuilder() {
.setChannelConfigurator(channelConfigurator)
.setChannelsPerCpu(2.5)
.setDirectPathServiceConfig(directPathServiceConfig)
.setAllowHardBoundTokenTypes(hardBoundTokenTypes)
.build();

InstantiatingGrpcChannelProvider.Builder builder = provider.toBuilder();
Expand Down

0 comments on commit 38431a2

Please sign in to comment.