Skip to content

Commit 49dca72

Browse files
committed
Fix Tests using Reflections
1 parent 3ffde6e commit 49dca72

File tree

4 files changed

+238
-349
lines changed

4 files changed

+238
-349
lines changed

itests/hive-unit/src/test/java/org/apache/hive/service/auth/jwt/TestHttpJwtAuthentication.java

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,14 @@
2828
import com.nimbusds.jwt.SignedJWT;
2929
import org.apache.hadoop.hive.conf.HiveConf;
3030
import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
31-
import org.apache.hive.common.util.ReflectionUtil;
3231
import org.apache.hive.jdbc.HiveConnection;
33-
import org.apache.hive.jdbc.Utils;
3432
import org.apache.hive.jdbc.miniHS2.MiniHS2;
3533
import org.junit.AfterClass;
36-
import org.junit.Before;
3734
import org.junit.BeforeClass;
3835
import org.junit.ClassRule;
3936
import org.junit.Test;
4037

4138
import java.io.File;
42-
import java.lang.invoke.MethodHandles;
43-
import java.lang.invoke.VarHandle;
44-
import java.lang.reflect.Field;
45-
import java.lang.reflect.Modifier;
4639
import java.nio.charset.StandardCharsets;
4740
import java.nio.file.Files;
4841
import java.nio.file.Path;
@@ -53,7 +46,6 @@
5346
import java.sql.Statement;
5447
import java.util.Date;
5548
import java.util.HashMap;
56-
import java.util.Map;
5749
import java.util.UUID;
5850
import java.util.concurrent.TimeUnit;
5951

@@ -62,15 +54,9 @@
6254
import static org.junit.Assert.assertEquals;
6355
import static org.junit.Assert.assertTrue;
6456

65-
6657
public class TestHttpJwtAuthentication {
67-
private static final Map<String, String> DEFAULTS = new HashMap<>(System.getenv());
68-
private static Map<String, String> envMap;
69-
70-
private static final File jwtAuthorizedKeyFile =
71-
new File("src/test/resources/auth.jwt/jwt-authorized-key.json");
72-
private static final File jwtUnauthorizedKeyFile =
73-
new File("src/test/resources/auth.jwt/jwt-unauthorized-key.json");
58+
private static final File jwtAuthorizedKeyFile = new File("src/test/resources/auth.jwt/jwt-authorized-key.json");
59+
private static final File jwtUnauthorizedKeyFile = new File("src/test/resources/auth.jwt/jwt-unauthorized-key.json");
7460
private static final File jwtVerificationJWKSFile =
7561
new File("src/test/resources/auth.jwt/jwt-verification-jwks.json");
7662

@@ -82,43 +68,19 @@ public class TestHttpJwtAuthentication {
8268
@ClassRule
8369
public static final WireMockRule MOCK_JWKS_SERVER = new WireMockRule(MOCK_JWKS_SERVER_PORT);
8470

85-
/**
86-
* This is a hack to make environment variables modifiable.
87-
* Ref: https://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java.
88-
*/
89-
@BeforeClass
90-
public static void makeEnvModifiable() throws Exception {
91-
envMap = new HashMap<>();
92-
Class<?> envClass = Class.forName("java.lang.ProcessEnvironment");
93-
Field theUnmodifiableEnvironmentField = envClass.getDeclaredField("theUnmodifiableEnvironment");
94-
removeStaticFinalAndSetValue(theUnmodifiableEnvironmentField, envMap);
95-
}
96-
97-
private static void removeStaticFinalAndSetValue(Field field, Object value) throws Exception {
98-
ReflectionUtil.setStaticFinalFieldsModifiable(field);
99-
field.set(null, value);
100-
}
101-
102-
@Before
103-
public void initEnvMap() {
104-
envMap.clear();
105-
envMap.putAll(DEFAULTS);
106-
}
107-
10871
@BeforeClass
10972
public static void setupHS2() throws Exception {
110-
MOCK_JWKS_SERVER.stubFor(get("/jwks")
111-
.willReturn(ok()
112-
.withBody(Files.readAllBytes(jwtVerificationJWKSFile.toPath()))));
73+
MOCK_JWKS_SERVER.stubFor(
74+
get("/jwks").willReturn(ok().withBody(Files.readAllBytes(jwtVerificationJWKSFile.toPath()))));
11375

11476
HiveConf conf = new HiveConf();
11577
conf.setBoolVar(ConfVars.HIVE_SUPPORT_CONCURRENCY, false);
11678
conf.setBoolVar(ConfVars.HIVE_SERVER2_LOGGING_OPERATION_ENABLED, false);
11779
conf.setBoolVar(ConfVars.HIVE_STATS_COL_AUTOGATHER, false);
11880
conf.setVar(ConfVars.HIVE_SERVER2_AUTHENTICATION, "JWT");
11981
// the content of the URL below is the same as jwtVerificationJWKSFile
120-
conf.setVar(ConfVars.HIVE_SERVER2_AUTHENTICATION_JWT_JWKS_URL, "http://localhost:" + MOCK_JWKS_SERVER_PORT +
121-
"/jwks");
82+
conf.setVar(ConfVars.HIVE_SERVER2_AUTHENTICATION_JWT_JWKS_URL,
83+
"http://localhost:" + MOCK_JWKS_SERVER_PORT + "/jwks");
12284
miniHS2 = new MiniHS2.Builder().withConf(conf).withHTTPTransport().build();
12385

12486
miniHS2.start(new HashMap<>());
@@ -137,38 +99,38 @@ public static void stopServices() throws Exception {
13799
@Test
138100
public void testAuthorizedUser() throws Exception {
139101
String jwt = generateJWT(USER_1, jwtAuthorizedKeyFile.toPath(), TimeUnit.MINUTES.toMillis(5));
140-
HiveConnection connection = getConnection(jwt, true);
141-
assertLoggedInUser(connection, USER_1);
142-
connection.close();
143-
144-
connection = getConnection(jwt, false);
145-
assertLoggedInUser(connection, USER_1);
146-
connection.close();
102+
try (HiveConnection connection = getConnection(jwt)) {
103+
assertLoggedInUser(connection, USER_1);
104+
}
147105
}
148106

149107
@Test(expected = SQLException.class)
150108
public void testExpiredJwt() throws Exception {
151109
String jwt = generateJWT(USER_1, jwtAuthorizedKeyFile.toPath(), 1);
152110
Thread.sleep(1);
153-
HiveConnection connection = getConnection(jwt, true);
111+
try (HiveConnection connection = getConnection(jwt)) {
112+
// Should throw SQLException
113+
}
154114
}
155115

156116
@Test(expected = SQLException.class)
157117
public void testUnauthorizedUser() throws Exception {
158118
String unauthorizedJwt = generateJWT(USER_1, jwtUnauthorizedKeyFile.toPath(), TimeUnit.MINUTES.toMillis(5));
159-
HiveConnection connection = getConnection(unauthorizedJwt, true);
119+
try (HiveConnection connection = getConnection(unauthorizedJwt)) {
120+
// Should throw SQLException
121+
}
160122
}
161123

162124
@Test(expected = SQLException.class)
163125
public void testWithoutJwtProvided() throws Exception {
164-
HiveConnection connection = getConnection(null, true);
126+
try (HiveConnection connection = getConnection(null)) {
127+
// Should throw SQLException
128+
}
165129
}
166130

167-
private HiveConnection getConnection(String jwt, Boolean putJwtInEnv) throws Exception {
131+
private HiveConnection getConnection(String jwt) throws Exception {
168132
String url = getJwtJdbcConnectionUrl();
169-
if (jwt != null && putJwtInEnv) {
170-
System.getenv().put(Utils.JdbcConnectionParams.AUTH_JWT_ENV, jwt);
171-
} else if (jwt != null) {
133+
if (jwt != null) {
172134
url += "jwt=" + jwt;
173135
}
174136
Class.forName("org.apache.hive.jdbc.HiveDriver");
@@ -218,4 +180,4 @@ private void assertLoggedInUser(HiveConnection connection, String expectedUser)
218180
String loggedInUser = resultSet.getString(1);
219181
assertEquals(expectedUser, loggedInUser);
220182
}
221-
}
183+
}

0 commit comments

Comments
 (0)