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

s2a: inject Optional<AccessTokenManager> in tests. #11898

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@
/** Retrieves the authentication mechanism for a given local identity. */
@Immutable
final class GetAuthenticationMechanisms {
private static final Optional<AccessTokenManager> TOKEN_MANAGER = AccessTokenManager.create();
static final Optional<AccessTokenManager> TOKEN_MANAGER = AccessTokenManager.create();

/**
* Retrieves the authentication mechanism for a given local identity.
*
* @param localIdentity the identity for which to fetch a token.
* @param tokenManager the token manager to use for fetching tokens.
* @return an {@link AuthenticationMechanism} for the given local identity.
*/
static Optional<AuthenticationMechanism> getAuthMechanism(Optional<S2AIdentity> localIdentity) {
if (!TOKEN_MANAGER.isPresent()) {
static Optional<AuthenticationMechanism> getAuthMechanism(Optional<S2AIdentity> localIdentity,
Optional<AccessTokenManager> tokenManager) {
if (!tokenManager.isPresent()) {
return Optional.empty();
}
AccessTokenManager manager = TOKEN_MANAGER.get();
AccessTokenManager manager = tokenManager.get();
// If no identity is provided, fetch the default access token and DO NOT attach an identity
// to the request.
if (!localIdentity.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ private static GetTlsConfigurationResp.ClientTlsConfiguration getClientTlsConfig
reqBuilder.setLocalIdentity(localIdentity.get().getIdentity());
}
Optional<AuthenticationMechanism> authMechanism =
GetAuthenticationMechanisms.getAuthMechanism(localIdentity);
GetAuthenticationMechanisms.getAuthMechanism(localIdentity,
GetAuthenticationMechanisms.TOKEN_MANAGER);
if (authMechanism.isPresent()) {
reqBuilder.addAuthenticationMechanisms(authMechanism.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import com.google.common.truth.Expect;
import io.grpc.s2a.internal.handshaker.S2AIdentity;
import io.grpc.s2a.internal.handshaker.tokenmanager.AccessTokenManager;
import io.grpc.s2a.internal.handshaker.tokenmanager.SingleTokenFetcher;
import java.util.Optional;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -33,6 +35,7 @@ public final class GetAuthenticationMechanismsTest {
@Rule public final Expect expect = Expect.create();
private static final String TOKEN = "access_token";
private static String originalAccessToken;
private Optional<AccessTokenManager> tokenManager;

@BeforeClass
public static void setUpClass() {
Expand All @@ -41,6 +44,11 @@ public static void setUpClass() {
SingleTokenFetcher.setAccessToken(TOKEN);
}

@Before
public void setUp() {
tokenManager = AccessTokenManager.create();
}

@AfterClass
public static void tearDownClass() {
SingleTokenFetcher.setAccessToken(originalAccessToken);
Expand All @@ -49,7 +57,7 @@ public static void tearDownClass() {
@Test
public void getAuthMechanisms_emptyIdentity_success() {
expect
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.empty()))
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.empty(), tokenManager))
.isEqualTo(
Optional.of(AuthenticationMechanism.newBuilder().setToken("access_token").build()));
}
Expand All @@ -58,7 +66,7 @@ public void getAuthMechanisms_emptyIdentity_success() {
public void getAuthMechanisms_nonEmptyIdentity_success() {
S2AIdentity fakeIdentity = S2AIdentity.fromSpiffeId("fake-spiffe-id");
expect
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.of(fakeIdentity)))
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.of(fakeIdentity), tokenManager))
.isEqualTo(
Optional.of(
AuthenticationMechanism.newBuilder()
Expand Down