28
28
import com .nimbusds .jwt .SignedJWT ;
29
29
import org .apache .hadoop .hive .conf .HiveConf ;
30
30
import org .apache .hadoop .hive .conf .HiveConf .ConfVars ;
31
- import org .apache .hive .common .util .ReflectionUtil ;
32
31
import org .apache .hive .jdbc .HiveConnection ;
33
- import org .apache .hive .jdbc .Utils ;
34
32
import org .apache .hive .jdbc .miniHS2 .MiniHS2 ;
35
33
import org .junit .AfterClass ;
36
- import org .junit .Before ;
37
34
import org .junit .BeforeClass ;
38
35
import org .junit .ClassRule ;
39
36
import org .junit .Test ;
40
37
41
38
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 ;
46
39
import java .nio .charset .StandardCharsets ;
47
40
import java .nio .file .Files ;
48
41
import java .nio .file .Path ;
53
46
import java .sql .Statement ;
54
47
import java .util .Date ;
55
48
import java .util .HashMap ;
56
- import java .util .Map ;
57
49
import java .util .UUID ;
58
50
import java .util .concurrent .TimeUnit ;
59
51
62
54
import static org .junit .Assert .assertEquals ;
63
55
import static org .junit .Assert .assertTrue ;
64
56
65
-
66
57
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" );
74
60
private static final File jwtVerificationJWKSFile =
75
61
new File ("src/test/resources/auth.jwt/jwt-verification-jwks.json" );
76
62
@@ -82,43 +68,19 @@ public class TestHttpJwtAuthentication {
82
68
@ ClassRule
83
69
public static final WireMockRule MOCK_JWKS_SERVER = new WireMockRule (MOCK_JWKS_SERVER_PORT );
84
70
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
-
108
71
@ BeforeClass
109
72
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 ()))));
113
75
114
76
HiveConf conf = new HiveConf ();
115
77
conf .setBoolVar (ConfVars .HIVE_SUPPORT_CONCURRENCY , false );
116
78
conf .setBoolVar (ConfVars .HIVE_SERVER2_LOGGING_OPERATION_ENABLED , false );
117
79
conf .setBoolVar (ConfVars .HIVE_STATS_COL_AUTOGATHER , false );
118
80
conf .setVar (ConfVars .HIVE_SERVER2_AUTHENTICATION , "JWT" );
119
81
// 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" );
122
84
miniHS2 = new MiniHS2 .Builder ().withConf (conf ).withHTTPTransport ().build ();
123
85
124
86
miniHS2 .start (new HashMap <>());
@@ -137,38 +99,38 @@ public static void stopServices() throws Exception {
137
99
@ Test
138
100
public void testAuthorizedUser () throws Exception {
139
101
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
+ }
147
105
}
148
106
149
107
@ Test (expected = SQLException .class )
150
108
public void testExpiredJwt () throws Exception {
151
109
String jwt = generateJWT (USER_1 , jwtAuthorizedKeyFile .toPath (), 1 );
152
110
Thread .sleep (1 );
153
- HiveConnection connection = getConnection (jwt , true );
111
+ try (HiveConnection connection = getConnection (jwt )) {
112
+ // Should throw SQLException
113
+ }
154
114
}
155
115
156
116
@ Test (expected = SQLException .class )
157
117
public void testUnauthorizedUser () throws Exception {
158
118
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
+ }
160
122
}
161
123
162
124
@ Test (expected = SQLException .class )
163
125
public void testWithoutJwtProvided () throws Exception {
164
- HiveConnection connection = getConnection (null , true );
126
+ try (HiveConnection connection = getConnection (null )) {
127
+ // Should throw SQLException
128
+ }
165
129
}
166
130
167
- private HiveConnection getConnection (String jwt , Boolean putJwtInEnv ) throws Exception {
131
+ private HiveConnection getConnection (String jwt ) throws Exception {
168
132
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 ) {
172
134
url += "jwt=" + jwt ;
173
135
}
174
136
Class .forName ("org.apache.hive.jdbc.HiveDriver" );
@@ -218,4 +180,4 @@ private void assertLoggedInUser(HiveConnection connection, String expectedUser)
218
180
String loggedInUser = resultSet .getString (1 );
219
181
assertEquals (expectedUser , loggedInUser );
220
182
}
221
- }
183
+ }
0 commit comments