Skip to content

Commit 2064531

Browse files
committed
Moving JwtToken related classes to xds package
1 parent e309c89 commit 2064531

File tree

6 files changed

+26
-63
lines changed

6 files changed

+26
-63
lines changed

alts/build.gradle

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
plugins {
22
id "java-library"
3-
id "java-test-fixtures"
43
id "maven-publish"
54

65
id "com.google.protobuf"
@@ -37,9 +36,6 @@ dependencies {
3736
libraries.mockito.core,
3837
libraries.truth
3938

40-
testFixturesImplementation libraries.junit,
41-
libraries.guava
42-
4339
testImplementation libraries.guava.testlib
4440
testRuntimeOnly libraries.netty.tcnative,
4541
libraries.netty.tcnative.classes
@@ -109,6 +105,3 @@ publishing {
109105
}
110106
}
111107
}
112-
113-
components.java.withVariantsFromConfiguration(configurations.testFixturesApiElements) { skip() }
114-
components.java.withVariantsFromConfiguration(configurations.testFixturesRuntimeElements) { skip() }

alts/src/main/java/io/grpc/alts/JwtTokenFileCallCredentials.java renamed to xds/src/main/java/io/grpc/xds/JwtTokenFileCallCredentials.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.grpc.alts;
17+
package io.grpc.xds;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
1820

1921
import com.google.api.client.json.gson.GsonFactory;
2022
import com.google.api.client.json.webtoken.JsonWebSignature;
@@ -33,11 +35,11 @@
3335
* See gRFC A97 (https://github.com/grpc/proposal/pull/492).
3436
*/
3537
public final class JwtTokenFileCallCredentials extends OAuth2Credentials {
36-
private static final long serialVersionUID = 452556614608513984L;
37-
private String path = null;
38+
private static final long serialVersionUID = 0L;
39+
private final String path;
3840

3941
private JwtTokenFileCallCredentials(String path) {
40-
this.path = path;
42+
this.path = checkNotNull(path, "path");
4143
}
4244

4345
@Override

xds/src/main/java/io/grpc/xds/internal/JwtTokenFileXdsCredentialsProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import io.grpc.CallCredentials;
2020
import io.grpc.ChannelCredentials;
21-
import io.grpc.alts.JwtTokenFileCallCredentials;
2221
import io.grpc.internal.JsonUtil;
22+
import io.grpc.xds.JwtTokenFileCallCredentials;
2323
import io.grpc.xds.XdsCredentialsProvider;
2424
import java.io.File;
2525
import java.util.Map;
@@ -28,7 +28,7 @@
2828
* A wrapper class that supports {@link JwtTokenFileXdsCredentialsProvider} for
2929
* Xds by implementing {@link XdsCredentialsProvider}.
3030
*/
31-
public class JwtTokenFileXdsCredentialsProvider extends XdsCredentialsProvider {
31+
public final class JwtTokenFileXdsCredentialsProvider extends XdsCredentialsProvider {
3232
private static final String CREDS_NAME = "jwt_token_file";
3333

3434
@Override

alts/src/test/java/io/grpc/alts/JwtTokenFileCallCredentialsTest.java renamed to xds/src/test/java/io/grpc/xds/JwtTokenFileCallCredentialsTest.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.grpc.alts;
17+
package io.grpc.xds;
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertThrows;
@@ -30,10 +30,8 @@
3030
import java.util.Date;
3131
import java.util.concurrent.TimeUnit;
3232
import org.junit.Before;
33-
import org.junit.Rule;
3433
import org.junit.Test;
3534
import org.junit.experimental.runners.Enclosed;
36-
import org.junit.rules.TemporaryFolder;
3735
import org.junit.runner.RunWith;
3836
import org.junit.runners.JUnit4;
3937

@@ -42,15 +40,12 @@
4240
public class JwtTokenFileCallCredentialsTest {
4341
@RunWith(JUnit4.class)
4442
public static class WithEmptyJwtTokenTest {
45-
@Rule
46-
public TemporaryFolder tempFolder = new TemporaryFolder();
47-
4843
private File jwtTokenFile;
4944
private JwtTokenFileCallCredentials unit;
5045

5146
@Before
5247
public void setUp() throws Exception {
53-
this.jwtTokenFile = JwtTokenFileTestUtils.createEmptyJwtToken(tempFolder);
48+
this.jwtTokenFile = JwtTokenFileTestUtils.createEmptyJwtToken();
5449

5550
Constructor<JwtTokenFileCallCredentials> ctor =
5651
JwtTokenFileCallCredentials.class.getDeclaredConstructor(String.class);
@@ -68,15 +63,12 @@ public void givenJwtTokenFileEmpty_WhenTokenRefreshed_ExpectException() {
6863

6964
@RunWith(JUnit4.class)
7065
public static class WithInvalidJwtTokenTest {
71-
@Rule
72-
public TemporaryFolder tempFolder = new TemporaryFolder();
73-
7466
private File jwtTokenFile;
7567
private JwtTokenFileCallCredentials unit;
7668

7769
@Before
7870
public void setUp() throws Exception {
79-
this.jwtTokenFile = JwtTokenFileTestUtils.createJwtTokenWithoutExpiration(tempFolder);
71+
this.jwtTokenFile = JwtTokenFileTestUtils.createJwtTokenWithoutExpiration();
8072

8173
Constructor<JwtTokenFileCallCredentials> ctor =
8274
JwtTokenFileCallCredentials.class.getDeclaredConstructor(String.class);
@@ -100,9 +92,6 @@ public void givenJwtTokenFileWithoutExpiration_WhenTokenRefreshed_ExpectExceptio
10092

10193
@RunWith(JUnit4.class)
10294
public static class WithValidJwtTokenTest {
103-
@Rule
104-
public TemporaryFolder tempFolder = new TemporaryFolder();
105-
10695
private File jwtTokenFile;
10796
private JwtTokenFileCallCredentials unit;
10897
private Long givenExpTimeInSeconds;
@@ -111,8 +100,7 @@ public static class WithValidJwtTokenTest {
111100
public void setUp() throws Exception {
112101
this.givenExpTimeInSeconds = Instant.now().getEpochSecond() + TimeUnit.HOURS.toSeconds(1);
113102

114-
this.jwtTokenFile = JwtTokenFileTestUtils.createValidJwtToken(
115-
tempFolder, givenExpTimeInSeconds);
103+
this.jwtTokenFile = JwtTokenFileTestUtils.createValidJwtToken(givenExpTimeInSeconds);
116104

117105
Constructor<JwtTokenFileCallCredentials> ctor =
118106
JwtTokenFileCallCredentials.class.getDeclaredConstructor(String.class);

alts/src/testFixtures/java/io/grpc/alts/JwtTokenFileTestUtils.java renamed to xds/src/test/java/io/grpc/xds/JwtTokenFileTestUtils.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.grpc.alts;
17+
package io.grpc.xds;
1818

1919
import com.google.common.io.BaseEncoding;
2020
import java.io.File;
2121
import java.io.FileOutputStream;
22+
import java.io.IOException;
2223
import java.nio.charset.StandardCharsets;
23-
import org.junit.rules.TemporaryFolder;
2424

2525
public class JwtTokenFileTestUtils {
26-
public static File createEmptyJwtToken(TemporaryFolder tempFolder) throws Exception {
27-
File jwtToken = tempFolder.newFile(new String("jwt.token"));
26+
public static File createEmptyJwtToken() throws IOException {
27+
File jwtToken = File.createTempFile(new String("jwt.token"), "");
28+
jwtToken.deleteOnExit();
2829
return jwtToken;
2930
}
3031

31-
public static File createJwtTokenWithoutExpiration(TemporaryFolder tempFolder) throws Exception {
32-
File jwtToken = tempFolder.newFile(new String("jwt.token"));
32+
public static File createJwtTokenWithoutExpiration() throws IOException {
33+
File jwtToken = File.createTempFile(new String("jwt.token"), "");
34+
jwtToken.deleteOnExit();
3335
FileOutputStream outputStream = new FileOutputStream(jwtToken);
3436
String content =
3537
BaseEncoding.base64().encode(
@@ -44,9 +46,10 @@ public static File createJwtTokenWithoutExpiration(TemporaryFolder tempFolder) t
4446
return jwtToken;
4547
}
4648

47-
public static File createValidJwtToken(TemporaryFolder tempFolder, Long expTime)
49+
public static File createValidJwtToken(long expTime)
4850
throws Exception {
49-
File jwtToken = tempFolder.newFile(new String("jwt.token"));
51+
File jwtToken = File.createTempFile(new String("jwt.token"), "");
52+
jwtToken.deleteOnExit();
5053
FileOutputStream outputStream = new FileOutputStream(jwtToken);
5154
String content =
5255
BaseEncoding.base64().encode(

xds/src/test/java/io/grpc/xds/SharedXdsClientPoolProviderTest.java

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import com.google.auth.oauth2.AccessToken;
2828
import com.google.auth.oauth2.OAuth2Credentials;
29-
import com.google.common.io.BaseEncoding;
3029
import com.google.common.util.concurrent.SettableFuture;
3130
import io.grpc.CallCredentials;
3231
import io.grpc.Grpc;
@@ -38,9 +37,10 @@
3837
import io.grpc.ServerCall;
3938
import io.grpc.ServerCallHandler;
4039
import io.grpc.ServerInterceptor;
41-
import io.grpc.alts.JwtTokenFileCallCredentials;
4240
import io.grpc.auth.MoreCallCredentials;
4341
import io.grpc.internal.ObjectPool;
42+
import io.grpc.xds.JwtTokenFileCallCredentials;
43+
import io.grpc.xds.JwtTokenFileTestUtils;
4444
import io.grpc.xds.SharedXdsClientPoolProvider.RefCountedXdsClientObjectPool;
4545
import io.grpc.xds.XdsListenerResource.LdsUpdate;
4646
import io.grpc.xds.client.Bootstrapper.BootstrapInfo;
@@ -50,15 +50,13 @@
5050
import io.grpc.xds.client.XdsClient.ResourceWatcher;
5151
import io.grpc.xds.client.XdsInitializationException;
5252
import java.io.File;
53-
import java.io.FileOutputStream;
5453
import java.nio.charset.StandardCharsets;
5554
import java.nio.file.Files;
5655
import java.time.Instant;
5756
import java.util.Collections;
5857
import java.util.concurrent.TimeUnit;
5958
import org.junit.Rule;
6059
import org.junit.Test;
61-
import org.junit.rules.TemporaryFolder;
6260
import org.junit.runner.RunWith;
6361
import org.junit.runners.JUnit4;
6462
import org.mockito.Mock;
@@ -68,9 +66,6 @@
6866
/** Tests for {@link SharedXdsClientPoolProvider}. */
6967
@RunWith(JUnit4.class)
7068
public class SharedXdsClientPoolProviderTest {
71-
@Rule
72-
public TemporaryFolder tempFolder = new TemporaryFolder();
73-
7469
private static final String SERVER_URI = "trafficdirector.googleapis.com";
7570
@Rule
7671
public final MockitoRule mocks = MockitoJUnit.rule();
@@ -84,24 +79,6 @@ public class SharedXdsClientPoolProviderTest {
8479
private GrpcBootstrapperImpl bootstrapper;
8580
@Mock private ResourceWatcher<LdsUpdate> ldsResourceWatcher;
8681

87-
// TODO (zgoda): How to include this method in alts/testFixtures. Running into build issues atm
88-
public static File createValidJwtToken(TemporaryFolder tempFolder, Long expTime)
89-
throws Exception {
90-
File jwtToken = tempFolder.newFile(new String("jwt.token"));
91-
FileOutputStream outputStream = new FileOutputStream(jwtToken);
92-
String content =
93-
BaseEncoding.base64().encode(
94-
new String("{\"typ\": \"JWT\", \"alg\": \"HS256\"}").getBytes(StandardCharsets.UTF_8))
95-
+ "."
96-
+ BaseEncoding.base64().encode(
97-
String.format("{\"exp\": %d}", expTime).getBytes(StandardCharsets.UTF_8))
98-
+ "."
99-
+ BaseEncoding.base64().encode(new String("signature").getBytes(StandardCharsets.UTF_8));
100-
outputStream.write(content.getBytes(StandardCharsets.UTF_8));
101-
outputStream.close();
102-
return jwtToken;
103-
}
104-
10582
@Test
10683
public void noServer() throws XdsInitializationException {
10784
BootstrapInfo bootstrapInfo =
@@ -246,7 +223,7 @@ public void xdsClient_usesJwtTokenFileCallCredentials() throws Exception {
246223
GrpcBootstrapperImpl.xdsBootstrapCallCredsEnabled = true;
247224

248225
Long givenExpTimeInSeconds = Instant.now().getEpochSecond() + TimeUnit.HOURS.toSeconds(1);
249-
File jwtToken = createValidJwtToken(tempFolder, givenExpTimeInSeconds);
226+
File jwtToken = JwtTokenFileTestUtils.createValidJwtToken(givenExpTimeInSeconds);
250227
String jwtTokenContent = new String(
251228
Files.readAllBytes(jwtToken.toPath()),
252229
StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)