use of org.apache.hive.jdbc.HiveConnection in project oozie by apache.
the class Hive2Credentials method updateCredentials.
@Override
public void updateCredentials(Credentials credentials, Configuration config, CredentialsProperties props, Context context) throws Exception {
try {
// load the driver
Class.forName("org.apache.hive.jdbc.HiveDriver");
String url = props.getProperties().get(HIVE2_JDBC_URL);
if (url == null || url.isEmpty()) {
throw new CredentialException(ErrorCode.E0510, HIVE2_JDBC_URL + " is required to get hive server 2 credential");
}
String principal = props.getProperties().get(HIVE2_SERVER_PRINCIPAL);
if (principal == null || principal.isEmpty()) {
throw new CredentialException(ErrorCode.E0510, HIVE2_SERVER_PRINCIPAL + " is required to get hive server 2 credentials");
}
url = url + ";principal=" + principal;
Connection con = null;
String tokenStr = null;
try {
con = DriverManager.getConnection(url);
XLog.getLog(getClass()).debug("Connected successfully to " + url);
// get delegation token for the given proxy user
tokenStr = ((HiveConnection) con).getDelegationToken(config.get(USER_NAME), principal);
} finally {
if (con != null) {
con.close();
}
}
XLog.getLog(getClass()).debug("Got token");
Token<DelegationTokenIdentifier> hive2Token = new Token<DelegationTokenIdentifier>();
hive2Token.decodeFromUrlString(tokenStr);
credentials.addToken(CredentialsProviderFactory.getUniqueAlias(hive2Token), hive2Token);
XLog.getLog(getClass()).debug("Added the Hive Server 2 token to launcher's credential");
} catch (Exception e) {
XLog.getLog(getClass()).warn("Exception in obtaining Hive2 token", e);
throw e;
}
}
use of org.apache.hive.jdbc.HiveConnection in project hive by apache.
the class TempletonControllerJob method buildHS2DelegationToken.
private String buildHS2DelegationToken(String user) throws IOException, InterruptedException, TException {
final HiveConf c = new HiveConf();
LOG.debug("Creating hiveserver2 delegation token for user " + user);
final UserGroupInformation ugi = UgiFactory.getUgi(user);
UserGroupInformation real = ugi.getRealUser();
return real.doAs(new PrivilegedExceptionAction<String>() {
@Override
public String run() throws IOException, TException, InterruptedException {
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
String hs2Url = appConf.get(AppConfig.HIVE_SERVER2_URL);
final HiveConnection con;
try {
con = (HiveConnection) DriverManager.getConnection(hs2Url);
} catch (SQLException e) {
throw new IOException(e);
}
String token = ugi.doAs(new PrivilegedExceptionAction<String>() {
@Override
public String run() throws SQLException {
String u = ugi.getUserName();
return con.getDelegationToken(u, u);
}
});
return token;
}
});
}
use of org.apache.hive.jdbc.HiveConnection in project hive by apache.
the class TestJdbcWithMiniKdc method testCancelRenewTokenFlow.
@Test
public void testCancelRenewTokenFlow() throws Exception {
miniHiveKdc.loginUser(MiniHiveKdc.HIVE_TEST_SUPER_USER);
hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL());
// retrieve token and store in the cache
String token = ((HiveConnection) hs2Conn).getDelegationToken(MiniHiveKdc.HIVE_TEST_USER_1, MiniHiveKdc.HIVE_SERVICE_PRINCIPAL);
assertTrue(token != null && !token.isEmpty());
Exception ex = null;
((HiveConnection) hs2Conn).cancelDelegationToken(token);
try {
((HiveConnection) hs2Conn).renewDelegationToken(token);
} catch (Exception SQLException) {
ex = SQLException;
}
assertTrue(ex != null && ex instanceof HiveSQLException);
// retrieve token and store in the cache
token = ((HiveConnection) hs2Conn).getDelegationToken(MiniHiveKdc.HIVE_TEST_USER_1, MiniHiveKdc.HIVE_SERVICE_PRINCIPAL);
assertTrue(token != null && !token.isEmpty());
hs2Conn.close();
}
use of org.apache.hive.jdbc.HiveConnection in project hive by apache.
the class TestJdbcWithMiniKdc method testRenewDelegationToken.
@Test
public void testRenewDelegationToken() throws Exception {
UserGroupInformation currentUGI = miniHiveKdc.loginUser(MiniHiveKdc.HIVE_TEST_SUPER_USER);
hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL());
String currentUser = currentUGI.getUserName();
// retrieve token and store in the cache
String token = ((HiveConnection) hs2Conn).getDelegationToken(MiniHiveKdc.HIVE_TEST_USER_1, miniHiveKdc.getFullyQualifiedServicePrincipal(MiniHiveKdc.HIVE_TEST_SUPER_USER));
assertTrue(token != null && !token.isEmpty());
((HiveConnection) hs2Conn).renewDelegationToken(token);
hs2Conn.close();
}
use of org.apache.hive.jdbc.HiveConnection in project hive by apache.
the class TestHttpSamlAuthentication method testTokenReuse.
/**
* Test make sure that a token which is issued for a different connection cannot be
* reused.
*/
@Test(expected = SQLException.class)
public void testTokenReuse() throws Exception {
setupIDP(true, USER_PASS_MODE, null, null);
String token = null;
try (HiveConnection connection = new TestHiveConnection(getSamlJdbcConnectionUrl(), new Properties(), USER1, USER1_PASSWORD)) {
token = connection.getBrowserClient().getServerResponse().getToken();
}
assertNotNull(token);
// inject the token using http.header url param
String bearerToken = "Bearer%20" + token;
String jdbcUrl = getSamlJdbcConnectionUrl(10) + ";http.header.Authorization=" + bearerToken;
try (HiveConnection connection = new HiveConnection(jdbcUrl, new Properties())) {
fail("User should not be able to login just using the token");
}
}
Aggregations