Search in sources :

Example 36 with Connection

use of java.sql.Connection in project hive by apache.

the class TestHS2ImpersonationWithRemoteMS method testImpersonation.

@Test
public void testImpersonation() throws Exception {
    assertTrue("Test setup failed. MiniHS2 is not initialized", miniHS2 != null && miniHS2.isStarted());
    Class.forName(MiniHS2.getJdbcDriverName());
    // Create two tables one as user "foo" and other as user "bar"
    Connection hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL(), "foo", null);
    Statement stmt = hs2Conn.createStatement();
    String tableName = "foo_table";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (value string)");
    stmt.close();
    hs2Conn.close();
    hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL(), "bar", null);
    stmt = hs2Conn.createStatement();
    tableName = "bar_table";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (value string)");
    stmt.close();
    hs2Conn.close();
    MiniDFSShim dfs = miniHS2.getDfs();
    FileSystem fs = dfs.getFileSystem();
    FileStatus[] files = fs.listStatus(miniHS2.getWareHouseDir());
    boolean fooTableValidated = false;
    boolean barTableValidated = false;
    for (FileStatus file : files) {
        final String name = file.getPath().getName();
        final String owner = file.getOwner();
        if (name.equals("foo_table")) {
            fooTableValidated = owner.equals("foo");
            assertTrue(String.format("User 'foo' table has wrong ownership '%s'", owner), fooTableValidated);
        } else if (name.equals("bar_table")) {
            barTableValidated = owner.equals("bar");
            assertTrue(String.format("User 'bar' table has wrong ownership '%s'", owner), barTableValidated);
        } else {
            fail(String.format("Unexpected table directory '%s' in warehouse", name));
        }
        System.out.println(String.format("File: %s, Owner: %s", name, owner));
    }
    assertTrue("User 'foo' table not found in warehouse", fooTableValidated);
    assertTrue("User 'bar' table not found in warehouse", barTableValidated);
}
Also used : MiniDFSShim(org.apache.hadoop.hive.shims.HadoopShims.MiniDFSShim) FileStatus(org.apache.hadoop.fs.FileStatus) Statement(java.sql.Statement) FileSystem(org.apache.hadoop.fs.FileSystem) Connection(java.sql.Connection) Test(org.junit.Test)

Example 37 with Connection

use of java.sql.Connection in project hive by apache.

the class TestCustomAuthentication method testCustomAuthentication.

@Test
public void testCustomAuthentication() throws Exception {
    String url = "jdbc:hive2://localhost:10000/default";
    Class.forName("org.apache.hive.jdbc.HiveDriver");
    try {
        DriverManager.getConnection(url, "wronguser", "pwd");
        Assert.fail("Expected Exception");
    } catch (SQLException e) {
        Assert.assertNotNull(e.getMessage());
        Assert.assertTrue(e.getMessage(), e.getMessage().contains("Peer indicated failure: Error validating the login"));
    }
    Connection connection = DriverManager.getConnection(url, "hiveuser", "hive");
    connection.close();
    System.out.println(">>> PASSED testCustomAuthentication");
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Test(org.junit.Test)

Example 38 with Connection

use of java.sql.Connection in project hive by apache.

the class cbo_rp_TestJdbcDriver2 method setUpBeforeClass.

@BeforeClass
public static void setUpBeforeClass() throws SQLException, ClassNotFoundException {
    Class.forName(driverName);
    Connection con1 = getConnection("default");
    System.setProperty(ConfVars.HIVE_SERVER2_LOGGING_OPERATION_LEVEL.varname, "verbose");
    Statement stmt1 = con1.createStatement();
    assertNotNull("Statement is null", stmt1);
    stmt1.execute("set hive.support.concurrency = false");
    DatabaseMetaData metadata = con1.getMetaData();
    // Drop databases created by other test cases
    ResultSet databaseRes = metadata.getSchemas();
    while (databaseRes.next()) {
        String db = databaseRes.getString(1);
        if (!db.equals("default")) {
            System.err.println("Dropping database " + db);
            stmt1.execute("DROP DATABASE " + db + " CASCADE");
        }
    }
    stmt1.close();
    con1.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) BeforeClass(org.junit.BeforeClass)

Example 39 with Connection

use of java.sql.Connection in project hive by apache.

the class TestXSRFFilter method getConnection.

private Connection getConnection(String jdbcURL, String user, String pwd) throws SQLException {
    Connection conn = DriverManager.getConnection(jdbcURL, user, pwd);
    conn.createStatement().execute("set hive.support.concurrency = false");
    return conn;
}
Also used : Connection(java.sql.Connection)

Example 40 with Connection

use of java.sql.Connection in project hive by apache.

the class TestHS2AuthzContext method verifyContextContents.

private void verifyContextContents(final String cmd, String ctxCmd) throws Exception, HiveAuthzPluginException, HiveAccessControlException {
    Connection hs2Conn = getConnection("user1");
    Statement stmt = hs2Conn.createStatement();
    stmt.execute(cmd);
    stmt.close();
    hs2Conn.close();
    ArgumentCaptor<HiveAuthzContext> contextCapturer = ArgumentCaptor.forClass(HiveAuthzContext.class);
    verify(mockedAuthorizer).checkPrivileges(any(HiveOperationType.class), Matchers.anyListOf(HivePrivilegeObject.class), Matchers.anyListOf(HivePrivilegeObject.class), contextCapturer.capture());
    HiveAuthzContext context = contextCapturer.getValue();
    assertEquals("Command ", ctxCmd, context.getCommandString());
    assertTrue("ip address pattern check", context.getIpAddress().matches("[.:a-fA-F0-9]+"));
    // ip address size check - check for something better than non zero
    assertTrue("ip address size check", context.getIpAddress().length() > 7);
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) HiveAuthzContext(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzContext) HiveOperationType(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType) HivePrivilegeObject(org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject)

Aggregations

Connection (java.sql.Connection)6326 PreparedStatement (java.sql.PreparedStatement)2793 ResultSet (java.sql.ResultSet)2657 Test (org.junit.Test)2455 SQLException (java.sql.SQLException)2267 Properties (java.util.Properties)1188 Statement (java.sql.Statement)1078 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)689 ArrayList (java.util.ArrayList)397 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)232 DataSource (javax.sql.DataSource)211 BaseTest (org.apache.phoenix.query.BaseTest)201 CallableStatement (java.sql.CallableStatement)192 IOException (java.io.IOException)158 Reader (java.io.Reader)144 DatabaseMetaData (java.sql.DatabaseMetaData)144 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)134 HashMap (java.util.HashMap)123 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)114 Timestamp (java.sql.Timestamp)113