Search in sources :

Example 91 with Connection

use of org.apache.hadoop.hbase.client.Connection in project hbase by apache.

the class TestCoprocessorWhitelistMasterObserver method negativeTestCase.

/**
   * Test a table modification adding a coprocessor path
   * which is whitelisted
   * @result The coprocessor should be added to the table
   *         descriptor successfully
   * @param whitelistedPaths A String array of paths to add in
   *         for the whitelisting configuration
   * @param coprocessorPath A String to use as the
   *         path for a mock coprocessor
   */
private static void negativeTestCase(String[] whitelistedPaths, String coprocessorPath) throws Exception {
    Configuration conf = UTIL.getConfiguration();
    conf.setInt("hbase.client.retries.number", 1);
    // load coprocessor under test
    conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, CoprocessorWhitelistMasterObserver.class.getName());
    // set retries low to raise exception quickly
    // set a coprocessor whitelist path for test
    conf.setStrings(CoprocessorWhitelistMasterObserver.CP_COPROCESSOR_WHITELIST_PATHS_KEY, whitelistedPaths);
    UTIL.startMiniCluster();
    UTIL.createTable(TEST_TABLE, new byte[][] { TEST_FAMILY });
    UTIL.waitUntilAllRegionsAssigned(TEST_TABLE);
    Connection connection = ConnectionFactory.createConnection(conf);
    Admin admin = connection.getAdmin();
    // disable table so we do not actually try loading non-existant
    // coprocessor file
    admin.disableTable(TEST_TABLE);
    Table t = connection.getTable(TEST_TABLE);
    HTableDescriptor htd = t.getTableDescriptor();
    htd.addCoprocessor("net.clayb.hbase.coprocessor.Whitelisted", new Path(coprocessorPath), Coprocessor.PRIORITY_USER, null);
    LOG.info("Modifying Table");
    admin.modifyTable(TEST_TABLE, htd);
    assertEquals(1, t.getTableDescriptor().getCoprocessors().size());
    LOG.info("Done Modifying Table");
}
Also used : Path(org.apache.hadoop.fs.Path) Table(org.apache.hadoop.hbase.client.Table) Configuration(org.apache.hadoop.conf.Configuration) Connection(org.apache.hadoop.hbase.client.Connection) Admin(org.apache.hadoop.hbase.client.Admin) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Example 92 with Connection

use of org.apache.hadoop.hbase.client.Connection in project hbase by apache.

the class TestNamespaceCommands method testListNamespaces.

@Test
public void testListNamespaces() throws Exception {
    AccessTestAction listAction = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Connection unmanagedConnection = ConnectionFactory.createConnection(UTIL.getConfiguration());
            Admin admin = unmanagedConnection.getAdmin();
            try {
                return Arrays.asList(admin.listNamespaceDescriptors());
            } finally {
                admin.close();
                unmanagedConnection.close();
            }
        }
    };
    // listNamespaces         : All access*
    // * Returned list will only show what you can call getNamespaceDescriptor()
    verifyAllowed(listAction, SUPERUSER, USER_GLOBAL_ADMIN, USER_NS_ADMIN, USER_GROUP_ADMIN);
    // we have 3 namespaces: [default, hbase, TEST_NAMESPACE, TEST_NAMESPACE2]
    assertEquals(4, ((List) SUPERUSER.runAs(listAction)).size());
    assertEquals(4, ((List) USER_GLOBAL_ADMIN.runAs(listAction)).size());
    assertEquals(4, ((List) USER_GROUP_ADMIN.runAs(listAction)).size());
    assertEquals(2, ((List) USER_NS_ADMIN.runAs(listAction)).size());
    assertEquals(0, ((List) USER_GLOBAL_CREATE.runAs(listAction)).size());
    assertEquals(0, ((List) USER_GLOBAL_WRITE.runAs(listAction)).size());
    assertEquals(0, ((List) USER_GLOBAL_READ.runAs(listAction)).size());
    assertEquals(0, ((List) USER_GLOBAL_EXEC.runAs(listAction)).size());
    assertEquals(0, ((List) USER_NS_CREATE.runAs(listAction)).size());
    assertEquals(0, ((List) USER_NS_WRITE.runAs(listAction)).size());
    assertEquals(0, ((List) USER_NS_READ.runAs(listAction)).size());
    assertEquals(0, ((List) USER_NS_EXEC.runAs(listAction)).size());
    assertEquals(0, ((List) USER_TABLE_CREATE.runAs(listAction)).size());
    assertEquals(0, ((List) USER_TABLE_WRITE.runAs(listAction)).size());
    assertEquals(0, ((List) USER_GROUP_CREATE.runAs(listAction)).size());
    assertEquals(0, ((List) USER_GROUP_READ.runAs(listAction)).size());
    assertEquals(0, ((List) USER_GROUP_WRITE.runAs(listAction)).size());
}
Also used : Connection(org.apache.hadoop.hbase.client.Connection) Admin(org.apache.hadoop.hbase.client.Admin) Test(org.junit.Test)

Example 93 with Connection

use of org.apache.hadoop.hbase.client.Connection in project hbase by apache.

the class TestVisibilityLabels method addLabels.

public static void addLabels() throws Exception {
    PrivilegedExceptionAction<VisibilityLabelsResponse> action = new PrivilegedExceptionAction<VisibilityLabelsResponse>() {

        public VisibilityLabelsResponse run() throws Exception {
            String[] labels = { SECRET, TOPSECRET, CONFIDENTIAL, PUBLIC, PRIVATE, COPYRIGHT, ACCENT, UNICODE_VIS_TAG, UC1, UC2 };
            try (Connection conn = ConnectionFactory.createConnection(conf)) {
                VisibilityClient.addLabels(conn, labels);
            } catch (Throwable t) {
                throw new IOException(t);
            }
            return null;
        }
    };
    SUPERUSER.runAs(action);
}
Also used : Connection(org.apache.hadoop.hbase.client.Connection) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) VisibilityLabelsResponse(org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse)

Example 94 with Connection

use of org.apache.hadoop.hbase.client.Connection in project hbase by apache.

the class TestTablePermissions method testGlobalPermission.

@Test
public void testGlobalPermission() throws Exception {
    Configuration conf = UTIL.getConfiguration();
    // add some permissions
    try (Connection connection = ConnectionFactory.createConnection(conf)) {
        addUserPermission(conf, new UserPermission(Bytes.toBytes("user1"), Permission.Action.READ, Permission.Action.WRITE), connection.getTable(AccessControlLists.ACL_TABLE_NAME));
        addUserPermission(conf, new UserPermission(Bytes.toBytes("user2"), Permission.Action.CREATE), connection.getTable(AccessControlLists.ACL_TABLE_NAME));
        addUserPermission(conf, new UserPermission(Bytes.toBytes("user3"), Permission.Action.ADMIN, Permission.Action.READ, Permission.Action.CREATE), connection.getTable(AccessControlLists.ACL_TABLE_NAME));
    }
    ListMultimap<String, TablePermission> perms = AccessControlLists.getTablePermissions(conf, null);
    List<TablePermission> user1Perms = perms.get("user1");
    assertEquals("Should have 1 permission for user1", 1, user1Perms.size());
    assertEquals("user1 should have WRITE permission", new Permission.Action[] { Permission.Action.READ, Permission.Action.WRITE }, user1Perms.get(0).getActions());
    List<TablePermission> user2Perms = perms.get("user2");
    assertEquals("Should have 1 permission for user2", 1, user2Perms.size());
    assertEquals("user2 should have CREATE permission", new Permission.Action[] { Permission.Action.CREATE }, user2Perms.get(0).getActions());
    List<TablePermission> user3Perms = perms.get("user3");
    assertEquals("Should have 1 permission for user3", 1, user3Perms.size());
    assertEquals("user3 should have ADMIN, READ, CREATE permission", new Permission.Action[] { Permission.Action.READ, Permission.Action.CREATE, Permission.Action.ADMIN }, user3Perms.get(0).getActions());
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) Connection(org.apache.hadoop.hbase.client.Connection) Test(org.junit.Test)

Example 95 with Connection

use of org.apache.hadoop.hbase.client.Connection in project hbase by apache.

the class TestTablePermissions method testPersistence.

@Test
public void testPersistence() throws Exception {
    Configuration conf = UTIL.getConfiguration();
    try (Connection connection = ConnectionFactory.createConnection(conf)) {
        addUserPermission(conf, new UserPermission(Bytes.toBytes("albert"), TEST_TABLE, null, (byte[]) null, TablePermission.Action.READ), connection.getTable(AccessControlLists.ACL_TABLE_NAME));
        addUserPermission(conf, new UserPermission(Bytes.toBytes("betty"), TEST_TABLE, null, (byte[]) null, TablePermission.Action.READ, TablePermission.Action.WRITE), connection.getTable(AccessControlLists.ACL_TABLE_NAME));
        addUserPermission(conf, new UserPermission(Bytes.toBytes("clark"), TEST_TABLE, TEST_FAMILY, TablePermission.Action.READ), connection.getTable(AccessControlLists.ACL_TABLE_NAME));
        addUserPermission(conf, new UserPermission(Bytes.toBytes("dwight"), TEST_TABLE, TEST_FAMILY, TEST_QUALIFIER, TablePermission.Action.WRITE), connection.getTable(AccessControlLists.ACL_TABLE_NAME));
    }
    // verify permissions survive changes in table metadata
    ListMultimap<String, TablePermission> preperms = AccessControlLists.getTablePermissions(conf, TEST_TABLE);
    Table table = UTIL.getConnection().getTable(TEST_TABLE);
    table.put(new Put(Bytes.toBytes("row1")).addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes("v1")));
    table.put(new Put(Bytes.toBytes("row2")).addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes("v2")));
    Admin admin = UTIL.getAdmin();
    admin.split(TEST_TABLE);
    // wait for split
    Thread.sleep(10000);
    ListMultimap<String, TablePermission> postperms = AccessControlLists.getTablePermissions(conf, TEST_TABLE);
    checkMultimapEqual(preperms, postperms);
}
Also used : Table(org.apache.hadoop.hbase.client.Table) Configuration(org.apache.hadoop.conf.Configuration) Connection(org.apache.hadoop.hbase.client.Connection) Admin(org.apache.hadoop.hbase.client.Admin) Put(org.apache.hadoop.hbase.client.Put) Test(org.junit.Test)

Aggregations

Connection (org.apache.hadoop.hbase.client.Connection)307 Table (org.apache.hadoop.hbase.client.Table)194 Test (org.junit.Test)174 IOException (java.io.IOException)117 TableName (org.apache.hadoop.hbase.TableName)103 Result (org.apache.hadoop.hbase.client.Result)102 Admin (org.apache.hadoop.hbase.client.Admin)90 Scan (org.apache.hadoop.hbase.client.Scan)81 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)77 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)71 Put (org.apache.hadoop.hbase.client.Put)68 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)58 Delete (org.apache.hadoop.hbase.client.Delete)55 Configuration (org.apache.hadoop.conf.Configuration)54 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)52 Get (org.apache.hadoop.hbase.client.Get)48 InterruptedIOException (java.io.InterruptedIOException)45 Cell (org.apache.hadoop.hbase.Cell)41 CellScanner (org.apache.hadoop.hbase.CellScanner)34 ArrayList (java.util.ArrayList)26