s2a: inject Optional<AccessTokenManager> in tests

This commit is contained in:
Riya Mehta 2025-02-14 12:55:42 -08:00 committed by GitHub
parent 41dd0c6d73
commit a5347b2bc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 7 deletions

View File

@ -24,19 +24,21 @@ import java.util.Optional;
/** Retrieves the authentication mechanism for a given local identity. */ /** Retrieves the authentication mechanism for a given local identity. */
@Immutable @Immutable
final class GetAuthenticationMechanisms { 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. * Retrieves the authentication mechanism for a given local identity.
* *
* @param localIdentity the identity for which to fetch a token. * @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. * @return an {@link AuthenticationMechanism} for the given local identity.
*/ */
static Optional<AuthenticationMechanism> getAuthMechanism(Optional<S2AIdentity> localIdentity) { static Optional<AuthenticationMechanism> getAuthMechanism(Optional<S2AIdentity> localIdentity,
if (!TOKEN_MANAGER.isPresent()) { Optional<AccessTokenManager> tokenManager) {
if (!tokenManager.isPresent()) {
return Optional.empty(); 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 // If no identity is provided, fetch the default access token and DO NOT attach an identity
// to the request. // to the request.
if (!localIdentity.isPresent()) { if (!localIdentity.isPresent()) {

View File

@ -105,7 +105,8 @@ final class SslContextFactory {
reqBuilder.setLocalIdentity(localIdentity.get().getIdentity()); reqBuilder.setLocalIdentity(localIdentity.get().getIdentity());
} }
Optional<AuthenticationMechanism> authMechanism = Optional<AuthenticationMechanism> authMechanism =
GetAuthenticationMechanisms.getAuthMechanism(localIdentity); GetAuthenticationMechanisms.getAuthMechanism(localIdentity,
GetAuthenticationMechanisms.TOKEN_MANAGER);
if (authMechanism.isPresent()) { if (authMechanism.isPresent()) {
reqBuilder.addAuthenticationMechanisms(authMechanism.get()); reqBuilder.addAuthenticationMechanisms(authMechanism.get());
} }

View File

@ -18,9 +18,11 @@ package io.grpc.s2a.internal.handshaker;
import com.google.common.truth.Expect; import com.google.common.truth.Expect;
import io.grpc.s2a.internal.handshaker.S2AIdentity; import io.grpc.s2a.internal.handshaker.S2AIdentity;
import io.grpc.s2a.internal.handshaker.tokenmanager.AccessTokenManager;
import io.grpc.s2a.internal.handshaker.tokenmanager.SingleTokenFetcher; import io.grpc.s2a.internal.handshaker.tokenmanager.SingleTokenFetcher;
import java.util.Optional; import java.util.Optional;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -33,6 +35,7 @@ public final class GetAuthenticationMechanismsTest {
@Rule public final Expect expect = Expect.create(); @Rule public final Expect expect = Expect.create();
private static final String TOKEN = "access_token"; private static final String TOKEN = "access_token";
private static String originalAccessToken; private static String originalAccessToken;
private Optional<AccessTokenManager> tokenManager;
@BeforeClass @BeforeClass
public static void setUpClass() { public static void setUpClass() {
@ -41,6 +44,11 @@ public final class GetAuthenticationMechanismsTest {
SingleTokenFetcher.setAccessToken(TOKEN); SingleTokenFetcher.setAccessToken(TOKEN);
} }
@Before
public void setUp() {
tokenManager = AccessTokenManager.create();
}
@AfterClass @AfterClass
public static void tearDownClass() { public static void tearDownClass() {
SingleTokenFetcher.setAccessToken(originalAccessToken); SingleTokenFetcher.setAccessToken(originalAccessToken);
@ -49,7 +57,7 @@ public final class GetAuthenticationMechanismsTest {
@Test @Test
public void getAuthMechanisms_emptyIdentity_success() { public void getAuthMechanisms_emptyIdentity_success() {
expect expect
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.empty())) .that(GetAuthenticationMechanisms.getAuthMechanism(Optional.empty(), tokenManager))
.isEqualTo( .isEqualTo(
Optional.of(AuthenticationMechanism.newBuilder().setToken("access_token").build())); Optional.of(AuthenticationMechanism.newBuilder().setToken("access_token").build()));
} }
@ -58,7 +66,7 @@ public final class GetAuthenticationMechanismsTest {
public void getAuthMechanisms_nonEmptyIdentity_success() { public void getAuthMechanisms_nonEmptyIdentity_success() {
S2AIdentity fakeIdentity = S2AIdentity.fromSpiffeId("fake-spiffe-id"); S2AIdentity fakeIdentity = S2AIdentity.fromSpiffeId("fake-spiffe-id");
expect expect
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.of(fakeIdentity))) .that(GetAuthenticationMechanisms.getAuthMechanism(Optional.of(fakeIdentity), tokenManager))
.isEqualTo( .isEqualTo(
Optional.of( Optional.of(
AuthenticationMechanism.newBuilder() AuthenticationMechanism.newBuilder()