Skip to content

Commit 0f14a55

Browse files
committed
xds: Move JwtTokenFileCallCredentials to xds.client package and make package-private
Move `JwtTokenFileCallCredentials` from `io.grpc.auth` to `io.grpc.xds.client` and make it package-private as it is only used in xds. To support this move without breaking tests: - Moved `JwtTokenFileCallCredentialsTest` to the same package (`io.grpc.xds.client`). - Created `BootstrapperImplTest` in `io.grpc.xds.client` to test the base `BootstrapperImpl` parsing logic. - Moved `call_creds` parsing tests (which reference `JwtTokenFileCallCredentials`) from `GrpcBootstrapperImplTest` to the new `BootstrapperImplTest`.
1 parent e9a8c2b commit 0f14a55

5 files changed

Lines changed: 345 additions & 260 deletions

File tree

xds/src/main/java/io/grpc/xds/client/BootstrapperImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.grpc.CompositeCallCredentials;
2424
import io.grpc.Internal;
2525
import io.grpc.InternalLogId;
26-
import io.grpc.auth.JwtTokenFileCallCredentials;
2726
import io.grpc.internal.GrpcUtil;
2827
import io.grpc.internal.GrpcUtil.GrpcBuildVersion;
2928
import io.grpc.internal.JsonParser;

auth/src/main/java/io/grpc/auth/JwtTokenFileCallCredentials.java renamed to xds/src/main/java/io/grpc/xds/client/JwtTokenFileCallCredentials.java

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

17-
package io.grpc.auth;
17+
package io.grpc.xds.client;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

@@ -45,7 +45,7 @@
4545
* A {@link CallCredentials} implementation that loads a JWT token from a file,
4646
* parses it to extract its expiration time, and caches/refreshes it.
4747
*/
48-
public final class JwtTokenFileCallCredentials extends CallCredentials {
48+
final class JwtTokenFileCallCredentials extends CallCredentials {
4949
private static final int MAX_FILE_SIZE_BYTES = 1048576;
5050

5151
private static final Logger log = Logger.getLogger(JwtTokenFileCallCredentials.class.getName());
@@ -87,7 +87,7 @@ public long currentTimeMillis() {
8787
}
8888
};
8989

90-
public JwtTokenFileCallCredentials(String filePath) {
90+
JwtTokenFileCallCredentials(String filePath) {
9191
this(filePath, SYSTEM_TIME_PROVIDER);
9292
}
9393

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

Lines changed: 0 additions & 255 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424

2525
import com.google.common.collect.ImmutableMap;
2626
import com.google.common.collect.Iterables;
27-
import io.grpc.CallCredentials;
2827
import io.grpc.InsecureChannelCredentials;
2928
import io.grpc.TlsChannelCredentials;
30-
import io.grpc.auth.JwtTokenFileCallCredentials;
3129
import io.grpc.internal.GrpcUtil;
3230
import io.grpc.internal.GrpcUtil.GrpcBuildVersion;
3331
import io.grpc.xds.client.AllowedGrpcServices;
@@ -1061,257 +1059,4 @@ private static Node.Builder getNodeBuilder() {
10611059
.addClientFeatures(GrpcBootstrapperImpl.CLIENT_FEATURE_RESOURCE_IN_SOTW);
10621060
}
10631061

1064-
private static void setEnableXdsBootstrapCallCreds(boolean enable) {
1065-
io.grpc.xds.client.BootstrapperImpl.enableXdsBootstrapCallCreds = enable;
1066-
}
1067-
1068-
private static String getFilePath(JwtTokenFileCallCredentials credentials) {
1069-
try {
1070-
java.lang.reflect.Field field =
1071-
JwtTokenFileCallCredentials.class.getDeclaredField("filePath");
1072-
field.setAccessible(true);
1073-
return (String) field.get(credentials);
1074-
} catch (Exception e) {
1075-
throw new RuntimeException(e);
1076-
}
1077-
}
1078-
1079-
@Test
1080-
public void parseBootstrap_callCreds_flagDisabled() throws Exception {
1081-
setEnableXdsBootstrapCallCreds(false);
1082-
try {
1083-
String rawData = "{\n"
1084-
+ " \"xds_servers\": [\n"
1085-
+ " {\n"
1086-
+ " \"server_uri\": \"" + SERVER_URI + "\",\n"
1087-
+ " \"channel_creds\": [{\"type\": \"insecure\"}],\n"
1088-
+ " \"call_creds\": [\n"
1089-
+ " {\n"
1090-
+ " \"type\": \"jwt_token_file\",\n"
1091-
+ " \"config\": {\n"
1092-
+ " \"jwt_token_file\": \"/var/run/secrets/token\"\n"
1093-
+ " }\n"
1094-
+ " }\n"
1095-
+ " ]\n"
1096-
+ " }\n"
1097-
+ " ]\n"
1098-
+ "}";
1099-
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
1100-
BootstrapInfo info = bootstrapper.bootstrap();
1101-
assertThat(info.servers()).hasSize(1);
1102-
ServerInfo serverInfo = Iterables.getOnlyElement(info.servers());
1103-
assertThat(serverInfo.callCredentials()).isNull();
1104-
} finally {
1105-
setEnableXdsBootstrapCallCreds(false);
1106-
}
1107-
}
1108-
1109-
@Test
1110-
public void parseBootstrap_xdsServers_jwtTokenFileCallCreds() throws Exception {
1111-
setEnableXdsBootstrapCallCreds(true);
1112-
try {
1113-
String rawData = "{\n"
1114-
+ " \"xds_servers\": [\n"
1115-
+ " {\n"
1116-
+ " \"server_uri\": \"" + SERVER_URI + "\",\n"
1117-
+ " \"channel_creds\": [{\"type\": \"insecure\"}],\n"
1118-
+ " \"call_creds\": [\n"
1119-
+ " {\n"
1120-
+ " \"type\": \"jwt_token_file\",\n"
1121-
+ " \"config\": {\n"
1122-
+ " \"jwt_token_file\": \"/var/run/secrets/token\"\n"
1123-
+ " }\n"
1124-
+ " }\n"
1125-
+ " ]\n"
1126-
+ " }\n"
1127-
+ " ]\n"
1128-
+ "}";
1129-
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
1130-
BootstrapInfo info = bootstrapper.bootstrap();
1131-
assertThat(info.servers()).hasSize(1);
1132-
ServerInfo serverInfo = Iterables.getOnlyElement(info.servers());
1133-
assertThat(serverInfo.callCredentials())
1134-
.isInstanceOf(JwtTokenFileCallCredentials.class);
1135-
assertThat(getFilePath((JwtTokenFileCallCredentials) serverInfo.callCredentials()))
1136-
.isEqualTo("/var/run/secrets/token");
1137-
} finally {
1138-
setEnableXdsBootstrapCallCreds(false);
1139-
}
1140-
}
1141-
1142-
@Test
1143-
public void parseBootstrap_authorities_jwtTokenFileCallCreds() throws Exception {
1144-
setEnableXdsBootstrapCallCreds(true);
1145-
try {
1146-
String rawData = "{\n"
1147-
+ " \"authorities\": {\n"
1148-
+ " \"a.com\": {\n"
1149-
+ " \"xds_servers\": [\n"
1150-
+ " {\n"
1151-
+ " \"server_uri\": \"td2.googleapis.com:443\",\n"
1152-
+ " \"channel_creds\": [\n"
1153-
+ " {\"type\": \"insecure\"}\n"
1154-
+ " ],\n"
1155-
+ " \"call_creds\": [\n"
1156-
+ " {\n"
1157-
+ " \"type\": \"jwt_token_file\",\n"
1158-
+ " \"config\": {\n"
1159-
+ " \"jwt_token_file\": \"/var/run/secrets/authority_token\"\n"
1160-
+ " }\n"
1161-
+ " }\n"
1162-
+ " ]\n"
1163-
+ " }\n"
1164-
+ " ]\n"
1165-
+ " }\n"
1166-
+ " },\n"
1167-
+ " \"xds_servers\": [\n"
1168-
+ " {\n"
1169-
+ " \"server_uri\": \"" + SERVER_URI + "\",\n"
1170-
+ " \"channel_creds\": [\n"
1171-
+ " {\"type\": \"insecure\"}\n"
1172-
+ " ]\n"
1173-
+ " }\n"
1174-
+ " ]\n"
1175-
+ "}";
1176-
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
1177-
BootstrapInfo info = bootstrapper.bootstrap();
1178-
assertThat(info.authorities()).hasSize(1);
1179-
AuthorityInfo authorityInfo = info.authorities().get("a.com");
1180-
assertThat(authorityInfo.xdsServers()).hasSize(1);
1181-
ServerInfo serverInfo = authorityInfo.xdsServers().get(0);
1182-
assertThat(serverInfo.callCredentials())
1183-
.isInstanceOf(JwtTokenFileCallCredentials.class);
1184-
assertThat(getFilePath((JwtTokenFileCallCredentials) serverInfo.callCredentials()))
1185-
.isEqualTo("/var/run/secrets/authority_token");
1186-
} finally {
1187-
setEnableXdsBootstrapCallCreds(false);
1188-
}
1189-
}
1190-
1191-
@Test
1192-
public void parseBootstrap_unsupportedCallCredsType_ignored() throws Exception {
1193-
setEnableXdsBootstrapCallCreds(true);
1194-
try {
1195-
String rawData = "{\n"
1196-
+ " \"xds_servers\": [\n"
1197-
+ " {\n"
1198-
+ " \"server_uri\": \"" + SERVER_URI + "\",\n"
1199-
+ " \"channel_creds\": [{\"type\": \"insecure\"}],\n"
1200-
+ " \"call_creds\": [\n"
1201-
+ " {\n"
1202-
+ " \"type\": \"unsupported_type\",\n"
1203-
+ " \"config\": {\n"
1204-
+ " \"some_field\": \"some_val\"\n"
1205-
+ " }\n"
1206-
+ " },\n"
1207-
+ " {\n"
1208-
+ " \"type\": \"jwt_token_file\",\n"
1209-
+ " \"config\": {\n"
1210-
+ " \"jwt_token_file\": \"/var/run/secrets/token\"\n"
1211-
+ " }\n"
1212-
+ " }\n"
1213-
+ " ]\n"
1214-
+ " }\n"
1215-
+ " ]\n"
1216-
+ "}";
1217-
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
1218-
BootstrapInfo info = bootstrapper.bootstrap();
1219-
assertThat(info.servers()).hasSize(1);
1220-
ServerInfo serverInfo = Iterables.getOnlyElement(info.servers());
1221-
assertThat(serverInfo.callCredentials())
1222-
.isInstanceOf(JwtTokenFileCallCredentials.class);
1223-
assertThat(getFilePath((JwtTokenFileCallCredentials) serverInfo.callCredentials()))
1224-
.isEqualTo("/var/run/secrets/token");
1225-
} finally {
1226-
setEnableXdsBootstrapCallCreds(false);
1227-
}
1228-
}
1229-
1230-
@Test
1231-
public void parseBootstrap_malformedCallCreds_throws() throws Exception {
1232-
setEnableXdsBootstrapCallCreds(true);
1233-
try {
1234-
String rawData = "{\n"
1235-
+ " \"xds_servers\": [\n"
1236-
+ " {\n"
1237-
+ " \"server_uri\": \"" + SERVER_URI + "\",\n"
1238-
+ " \"channel_creds\": [{\"type\": \"insecure\"}],\n"
1239-
+ " \"call_creds\": [\n"
1240-
+ " {\n"
1241-
+ " \"type\": \"jwt_token_file\",\n"
1242-
+ " \"config\": {}\n"
1243-
+ " }\n"
1244-
+ " ]\n"
1245-
+ " }\n"
1246-
+ " ]\n"
1247-
+ "}";
1248-
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
1249-
XdsInitializationException e = assertThrows(XdsInitializationException.class,
1250-
bootstrapper::bootstrap);
1251-
assertThat(e).hasMessageThat().contains("jwt_token_file' jwt_token_file missing or empty");
1252-
} finally {
1253-
setEnableXdsBootstrapCallCreds(false);
1254-
}
1255-
}
1256-
1257-
@Test
1258-
public void parseBootstrap_xdsServers_multipleValidCallCreds() throws Exception {
1259-
setEnableXdsBootstrapCallCreds(true);
1260-
try {
1261-
String rawData = "{\n"
1262-
+ " \"xds_servers\": [\n"
1263-
+ " {\n"
1264-
+ " \"server_uri\": \"" + SERVER_URI + "\",\n"
1265-
+ " \"channel_creds\": [{\"type\": \"insecure\"}],\n"
1266-
+ " \"call_creds\": [\n"
1267-
+ " {\n"
1268-
+ " \"type\": \"jwt_token_file\",\n"
1269-
+ " \"config\": { \"jwt_token_file\": \"/var/run/secrets/token1\" }\n"
1270-
+ " },\n"
1271-
+ " {\n"
1272-
+ " \"type\": \"jwt_token_file\",\n"
1273-
+ " \"config\": { \"jwt_token_file\": \"/var/run/secrets/token2\" }\n"
1274-
+ " }\n"
1275-
+ " ]\n"
1276-
+ " }\n"
1277-
+ " ]\n"
1278-
+ "}";
1279-
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
1280-
BootstrapInfo info = bootstrapper.bootstrap();
1281-
assertThat(info.servers()).hasSize(1);
1282-
ServerInfo serverInfo = info.servers().get(0);
1283-
CallCredentials creds = serverInfo.callCredentials();
1284-
assertThat(creds).isNotNull();
1285-
assertThat(creds).isInstanceOf(io.grpc.CompositeCallCredentials.class);
1286-
} finally {
1287-
setEnableXdsBootstrapCallCreds(false);
1288-
}
1289-
}
1290-
1291-
@Test
1292-
public void parseBootstrap_xdsServers_missingTypeCallCreds() throws Exception {
1293-
setEnableXdsBootstrapCallCreds(true);
1294-
try {
1295-
String rawData = "{\n"
1296-
+ " \"xds_servers\": [\n"
1297-
+ " {\n"
1298-
+ " \"server_uri\": \"" + SERVER_URI + "\",\n"
1299-
+ " \"channel_creds\": [{\"type\": \"insecure\"}],\n"
1300-
+ " \"call_creds\": [\n"
1301-
+ " {\n"
1302-
+ " \"config\": { \"jwt_token_file\": \"/var/run/secrets/token\" }\n"
1303-
+ " }\n"
1304-
+ " ]\n"
1305-
+ " }\n"
1306-
+ " ]\n"
1307-
+ "}";
1308-
bootstrapper.setFileReader(createFileReader(BOOTSTRAP_FILE_PATH, rawData));
1309-
bootstrapper.bootstrap();
1310-
fail("Expected exception");
1311-
} catch (XdsInitializationException e) {
1312-
assertThat(e).hasMessageThat().contains("with 'call_creds' type unspecified");
1313-
} finally {
1314-
setEnableXdsBootstrapCallCreds(false);
1315-
}
1316-
}
13171062
}

0 commit comments

Comments
 (0)