Skip to content

Commit a5347b2

Browse files
authored
s2a: inject Optional<AccessTokenManager> in tests
1 parent 41dd0c6 commit a5347b2

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

s2a/src/main/java/io/grpc/s2a/internal/handshaker/GetAuthenticationMechanisms.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@
2424
/** Retrieves the authentication mechanism for a given local identity. */
2525
@Immutable
2626
final class GetAuthenticationMechanisms {
27-
private static final Optional<AccessTokenManager> TOKEN_MANAGER = AccessTokenManager.create();
27+
static final Optional<AccessTokenManager> TOKEN_MANAGER = AccessTokenManager.create();
2828

2929
/**
3030
* Retrieves the authentication mechanism for a given local identity.
3131
*
3232
* @param localIdentity the identity for which to fetch a token.
33+
* @param tokenManager the token manager to use for fetching tokens.
3334
* @return an {@link AuthenticationMechanism} for the given local identity.
3435
*/
35-
static Optional<AuthenticationMechanism> getAuthMechanism(Optional<S2AIdentity> localIdentity) {
36-
if (!TOKEN_MANAGER.isPresent()) {
36+
static Optional<AuthenticationMechanism> getAuthMechanism(Optional<S2AIdentity> localIdentity,
37+
Optional<AccessTokenManager> tokenManager) {
38+
if (!tokenManager.isPresent()) {
3739
return Optional.empty();
3840
}
39-
AccessTokenManager manager = TOKEN_MANAGER.get();
41+
AccessTokenManager manager = tokenManager.get();
4042
// If no identity is provided, fetch the default access token and DO NOT attach an identity
4143
// to the request.
4244
if (!localIdentity.isPresent()) {

s2a/src/main/java/io/grpc/s2a/internal/handshaker/SslContextFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ private static GetTlsConfigurationResp.ClientTlsConfiguration getClientTlsConfig
105105
reqBuilder.setLocalIdentity(localIdentity.get().getIdentity());
106106
}
107107
Optional<AuthenticationMechanism> authMechanism =
108-
GetAuthenticationMechanisms.getAuthMechanism(localIdentity);
108+
GetAuthenticationMechanisms.getAuthMechanism(localIdentity,
109+
GetAuthenticationMechanisms.TOKEN_MANAGER);
109110
if (authMechanism.isPresent()) {
110111
reqBuilder.addAuthenticationMechanisms(authMechanism.get());
111112
}

s2a/src/test/java/io/grpc/s2a/internal/handshaker/GetAuthenticationMechanismsTest.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
import com.google.common.truth.Expect;
2020
import io.grpc.s2a.internal.handshaker.S2AIdentity;
21+
import io.grpc.s2a.internal.handshaker.tokenmanager.AccessTokenManager;
2122
import io.grpc.s2a.internal.handshaker.tokenmanager.SingleTokenFetcher;
2223
import java.util.Optional;
2324
import org.junit.AfterClass;
25+
import org.junit.Before;
2426
import org.junit.BeforeClass;
2527
import org.junit.Rule;
2628
import org.junit.Test;
@@ -33,6 +35,7 @@ public final class GetAuthenticationMechanismsTest {
3335
@Rule public final Expect expect = Expect.create();
3436
private static final String TOKEN = "access_token";
3537
private static String originalAccessToken;
38+
private Optional<AccessTokenManager> tokenManager;
3639

3740
@BeforeClass
3841
public static void setUpClass() {
@@ -41,6 +44,11 @@ public static void setUpClass() {
4144
SingleTokenFetcher.setAccessToken(TOKEN);
4245
}
4346

47+
@Before
48+
public void setUp() {
49+
tokenManager = AccessTokenManager.create();
50+
}
51+
4452
@AfterClass
4553
public static void tearDownClass() {
4654
SingleTokenFetcher.setAccessToken(originalAccessToken);
@@ -49,7 +57,7 @@ public static void tearDownClass() {
4957
@Test
5058
public void getAuthMechanisms_emptyIdentity_success() {
5159
expect
52-
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.empty()))
60+
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.empty(), tokenManager))
5361
.isEqualTo(
5462
Optional.of(AuthenticationMechanism.newBuilder().setToken("access_token").build()));
5563
}
@@ -58,7 +66,7 @@ public void getAuthMechanisms_emptyIdentity_success() {
5866
public void getAuthMechanisms_nonEmptyIdentity_success() {
5967
S2AIdentity fakeIdentity = S2AIdentity.fromSpiffeId("fake-spiffe-id");
6068
expect
61-
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.of(fakeIdentity)))
69+
.that(GetAuthenticationMechanisms.getAuthMechanism(Optional.of(fakeIdentity), tokenManager))
6270
.isEqualTo(
6371
Optional.of(
6472
AuthenticationMechanism.newBuilder()

0 commit comments

Comments
 (0)