Skip to content

Commit

Permalink
remove Jcommander dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmehta19 committed Apr 30, 2024
1 parent b35f145 commit b0858c0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 31 deletions.
1 change: 0 additions & 1 deletion repositories.bzl
Expand Up @@ -47,7 +47,6 @@ IO_GRPC_GRPC_JAVA_ARTIFACTS = [
"org.apache.tomcat:annotations-api:6.0.53",
"org.checkerframework:checker-qual:3.12.0",
"org.codehaus.mojo:animal-sniffer-annotations:1.23",
"org.jcommander:jcommander:1.83",
]
# GRPC_DEPS_END

Expand Down
1 change: 0 additions & 1 deletion s2a/BUILD.bazel
Expand Up @@ -59,7 +59,6 @@ java_library(
deps = [
":s2a_identity",
":token_fetcher",
artifact("org.jcommander:jcommander"),
],
)

Expand Down
2 changes: 0 additions & 2 deletions s2a/build.gradle
Expand Up @@ -26,7 +26,6 @@ dependencies {
libraries.protobuf.java,
libraries.conscrypt,
libraries.guava.jre // JRE required by protobuf-java-util from grpclb
compileOnly 'org.jcommander:jcommander:1.83'
def nettyDependency = implementation project(':grpc-netty')
compileOnly libraries.javax.annotation

Expand All @@ -44,7 +43,6 @@ dependencies {
libraries.conscrypt,
libraries.netty.transport.epoll

testImplementation 'org.jcommander:jcommander:1.83'
testImplementation 'com.google.truth:truth:1.4.2'
testImplementation 'com.google.truth.extensions:truth-proto-extension:1.4.2'
testImplementation libraries.guava.testlib
Expand Down
Expand Up @@ -16,27 +16,15 @@

package io.grpc.s2a.handshaker.tokenmanager;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.s2a.handshaker.S2AIdentity;
import java.util.Optional;

/** Fetches a single access token via an environment variable. */
@SuppressWarnings("NonFinalStaticField")
public final class SingleTokenFetcher implements TokenFetcher {
private static final String ENVIRONMENT_VARIABLE = "S2A_ACCESS_TOKEN";

/** Set an access token via a flag. */
@Parameters(separators = "=")
public static class Flags {
@Parameter(
names = "--s2a_access_token",
description = "The access token used to authenticate to S2A.")
private static String accessToken = System.getenv(ENVIRONMENT_VARIABLE);

public synchronized void reset() {
accessToken = null;
}
}
private static String accessToken = System.getenv(ENVIRONMENT_VARIABLE);

private final String token;

Expand All @@ -45,7 +33,12 @@ public synchronized void reset() {
* {@code Optional} instance if the token could not be fetched.
*/
public static Optional<TokenFetcher> create() {
return Optional.ofNullable(Flags.accessToken).map(SingleTokenFetcher::new);
return Optional.ofNullable(accessToken).map(SingleTokenFetcher::new);
}

@VisibleForTesting
public static void setAccessToken(String token) {
accessToken = token;
}

private SingleTokenFetcher(String token) {
Expand Down
Expand Up @@ -16,7 +16,6 @@

package io.grpc.s2a.handshaker;

import com.beust.jcommander.JCommander;
import com.google.common.truth.Expect;
import io.grpc.s2a.handshaker.S2AIdentity;
import io.grpc.s2a.handshaker.tokenmanager.SingleTokenFetcher;
Expand All @@ -32,13 +31,11 @@
public final class GetAuthenticationMechanismsTest {
@Rule public final Expect expect = Expect.create();
private static final String TOKEN = "access_token";
private static final String[] SET_TOKEN = {"--s2a_access_token", TOKEN};
private static final SingleTokenFetcher.Flags FLAGS = new SingleTokenFetcher.Flags();

@BeforeClass
public static void setUpClass() {
// Set the token that the client will use to authenticate to the S2A.
JCommander.newBuilder().addObject(FLAGS).build().parse(SET_TOKEN);
SingleTokenFetcher.setAccessToken(TOKEN);
}

@Test
Expand Down
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.truth.Truth.assertThat;

import com.beust.jcommander.JCommander;
import io.grpc.s2a.handshaker.S2AIdentity;
import java.util.Optional;
import org.junit.Before;
Expand All @@ -30,25 +29,23 @@
public final class SingleTokenAccessTokenManagerTest {
private static final S2AIdentity IDENTITY = S2AIdentity.fromSpiffeId("spiffe_id");
private static final String TOKEN = "token";
private static final String[] SET_TOKEN = {"--s2a_access_token", TOKEN};
private static final SingleTokenFetcher.Flags FLAGS = new SingleTokenFetcher.Flags();

@Before
public void setUp() {
FLAGS.reset();
SingleTokenFetcher.setAccessToken(null);
}

@Test
public void getDefaultToken_success() throws Exception {
JCommander.newBuilder().addObject(FLAGS).build().parse(SET_TOKEN);
SingleTokenFetcher.setAccessToken(TOKEN);
Optional<AccessTokenManager> manager = AccessTokenManager.create();
assertThat(manager).isPresent();
assertThat(manager.get().getDefaultToken()).isEqualTo(TOKEN);
}

@Test
public void getToken_success() throws Exception {
JCommander.newBuilder().addObject(FLAGS).build().parse(SET_TOKEN);
SingleTokenFetcher.setAccessToken(TOKEN);
Optional<AccessTokenManager> manager = AccessTokenManager.create();
assertThat(manager).isPresent();
assertThat(manager.get().getToken(IDENTITY)).isEqualTo(TOKEN);
Expand All @@ -61,7 +58,7 @@ public void getToken_noEnvironmentVariable() throws Exception {

@Test
public void create_success() throws Exception {
JCommander.newBuilder().addObject(FLAGS).build().parse(SET_TOKEN);
SingleTokenFetcher.setAccessToken(TOKEN);
Optional<AccessTokenManager> manager = AccessTokenManager.create();
assertThat(manager).isPresent();
assertThat(manager.get().getToken(IDENTITY)).isEqualTo(TOKEN);
Expand Down

0 comments on commit b0858c0

Please sign in to comment.