Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for new setAllowHardBoundTokens field. #3467

Merged
merged 13 commits into from
Jan 22, 2025
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to also mark this as internal?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree on making this internal as well.
Thinking twice about it though, I see that it is an Experimental feature, is it that we will always set the tokens to certain values? Or it's just this feature is not stable yet, internal teams could still set this to different values? If it's the former, then we don't have to introduce another public enum since they would be obsolete soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this should be marked as Internal Api, since this is intended to be set by client libraries. Done in 591ef68

This is being marked as experimental for now, since we are in progress of adding the related logic (e.g #3548, #3572) and then piloting, as discussed in the internal doc + chat. When the feature is non-experimental, the field (allowedHardBoundTokenTypes) will be set for all gapics to include both (MTLS_S2A and ALTS), however handwritten libraries will continue to set this field (allowedHardBoundTokenTypes) themselves in their handwritten layer (e.g. GCS). Additionally, when it is non-experimental, gapics + handwritten libraries will have the option to override the default value of the allowedHardBoundTokenTypes. I think the enum helps to proves clarity on the options.

// 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
blakeli0 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading