更新时间:2022-07-19 GMT+08:00
分享

ACL安全配置

功能简介

访问权限控制,在关系型数据库中是一个已经很成熟的技术,HBase实现了一个较为简单的特性。这些特性归纳为读(R)、写(W)、创建(C)、执行(X)和管理(A)等。在普通模式下,该功能只有在开启HBase权限管理时才支持。

ACL的方法定义在工具类org.apache.hadoop.hbase.security.access.AccessControlClient中。

代码样例

以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseExample”类的grantACL方法中

public void grantACL() {
    LOG.info("Entering grantACL.");

    String user = "usertest";
    String permissions = "RW";

    String familyName = "info";
    String qualifierName = "name";

    Table mt = null;
    Admin hAdmin = null;
    try {
        // Create ACL Instance
        mt = conn.getTable(AccessControlLists.ACL_TABLE_NAME);

        Permission perm = new Permission(Bytes.toBytes(permissions));

        hAdmin = conn.getAdmin();
        HTableDescriptor ht = hAdmin.getTableDescriptor(tableName);

        // Judge whether the table exists
        if (hAdmin.tableExists(mt.getName())) {
            // Judge whether ColumnFamily exists
            if (ht.hasFamily(Bytes.toBytes(familyName))) {
                // grant permission
                AccessControlClient.grant(conn, tableName, user, Bytes.toBytes(familyName),
                        (qualifierName == null ? null : Bytes.toBytes(qualifierName)), perm.getActions());
            } else {
                // grant permission
                AccessControlClient.grant(conn, tableName, user, null, null, perm.getActions());
            }
        }
        LOG.info("Grant ACL successfully.");
    } catch (Throwable e) {
        LOG.error("Grant ACL failed ", e);
    } finally {
        if (mt != null) {
            try {
                // Close
                mt.close();
            } catch (IOException e) {
                LOG.error("Close table failed ", e);
            }
        }

        if (hAdmin != null) {
            try {
                // Close Admin Object
                hAdmin.close();
            } catch (IOException e) {
                LOG.error("Close admin failed ", e);
            }
        }
    }
    LOG.info("Exiting grantACL.");
}

Shell命令方式:

命令行
# 赋权 
grant <user> <permissions>[ <table>[ <column family>[ <column qualifier> ] ] ] 
           
# 撤销权限 
revoke <user> <permissions> [ <table> [ <column family> [ <column qualifier> ] ] ] 
           
# 设置表所有者 
alter <table> {owner => <user>} 
           
# 显示权限列表 
user_permission <table>  # displays existing permissions

例如:

grant 'user1', 'RWC' 
grant 'user2', 'RW', 'tableA'
user_permission 'tableA'
分享:

    相关文档

    相关产品