2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
21 # Wrapper for org.apache.hadoop.hbase.client.HBaseAdmin
27 @connection = @admin.getConnection
34 #----------------------------------------------------------------------------------------------
35 def grant(user, permissions, table_name = nil, family = nil, qualifier = nil)
38 # TODO: need to validate user name
41 # Verify that the specified permission is valid
42 if permissions.nil? || permissions.empty?
43 raise(ArgumentError, 'Invalid permission: no actions associated with user')
46 perm = org.apache.hadoop.hbase.security.access.Permission.new(
47 permissions.to_java_bytes
51 tablebytes = table_name.to_java_bytes
52 # check if the tablename passed is actually a namespace
53 if isNamespace?(table_name)
54 # Namespace should exist first.
55 namespace_name = table_name[1...table_name.length]
56 raise(ArgumentError, "Can't find a namespace: #{namespace_name}") unless
57 namespace_exists?(namespace_name)
59 org.apache.hadoop.hbase.security.access.AccessControlClient.grant(
60 @connection, namespace_name, user, perm.getActions
64 raise(ArgumentError, "Can't find a table: #{table_name}") unless exists?(table_name)
66 tableName = org.apache.hadoop.hbase.TableName.valueOf(table_name)
67 td = @admin.getDescriptor(tableName)
70 raise(ArgumentError, "Can't find a family: #{family}") unless td.hasColumnFamily(family.to_java_bytes)
73 fambytes = family.to_java_bytes unless family.nil?
74 qualbytes = qualifier.to_java_bytes unless qualifier.nil?
76 org.apache.hadoop.hbase.security.access.AccessControlClient.grant(
77 @connection, tableName, user, fambytes, qualbytes, perm.getActions
81 # invoke cp endpoint to perform access controls
82 org.apache.hadoop.hbase.security.access.AccessControlClient.grant(
83 @connection, user, perm.getActions
89 #----------------------------------------------------------------------------------------------
90 def revoke(user, table_name = nil, family = nil, qualifier = nil)
93 # TODO: need to validate user name
97 # check if the tablename passed is actually a namespace
98 if isNamespace?(table_name)
99 # Namespace should exist first.
100 namespace_name = table_name[1...table_name.length]
101 raise(ArgumentError, "Can't find a namespace: #{namespace_name}") unless namespace_exists?(namespace_name)
103 tablebytes = table_name.to_java_bytes
104 org.apache.hadoop.hbase.security.access.AccessControlClient.revoke(
105 @connection, namespace_name, user
109 raise(ArgumentError, "Can't find a table: #{table_name}") unless exists?(table_name)
111 tableName = org.apache.hadoop.hbase.TableName.valueOf(table_name)
112 td = @admin.getDescriptor(tableName)
115 raise(ArgumentError, "Can't find a family: #{family}") unless td.hasColumnFamily(family.to_java_bytes)
118 fambytes = family.to_java_bytes unless family.nil?
119 qualbytes = qualifier.to_java_bytes unless qualifier.nil?
121 org.apache.hadoop.hbase.security.access.AccessControlClient.revoke(
122 @connection, tableName, user, fambytes, qualbytes
126 perm = org.apache.hadoop.hbase.security.access.Permission.new(''.to_java_bytes)
127 org.apache.hadoop.hbase.security.access.AccessControlClient.revoke(
128 @connection, user, perm.getActions
134 #----------------------------------------------------------------------------------------------
135 def user_permission(table_regex = nil)
137 all_perms = org.apache.hadoop.hbase.security.access.AccessControlClient.getUserPermissions(
138 @connection, table_regex
142 all_perms.each do |value|
143 user_name = value.getUser
144 permission = value.getPermission
148 if !table_regex.nil? && isNamespace?(table_regex)
149 nsPerm = permission.to_java(org.apache.hadoop.hbase.security.access.NamespacePermission)
150 namespace = nsPerm.getNamespace
151 elsif !table_regex.nil? && isTablePermission?(permission)
152 tblPerm = permission.to_java(org.apache.hadoop.hbase.security.access.TablePermission)
153 namespace = tblPerm.getNamespace
154 table = !tblPerm.getTableName.nil? ? tblPerm.getTableName.getNameAsString : ''
155 family = !tblPerm.getFamily.nil? ?
156 org.apache.hadoop.hbase.util.Bytes.toStringBinary(tblPerm.getFamily) : ''
157 qualifier = !tblPerm.getQualifier.nil? ?
158 org.apache.hadoop.hbase.util.Bytes.toStringBinary(tblPerm.getQualifier) : ''
161 action = org.apache.hadoop.hbase.security.access.Permission.new permission.getActions
164 yield(user_name, "#{namespace},#{table},#{family},#{qualifier}: #{action}")
166 res[user_name] ||= {}
167 res[user_name]["#{family}:#{qualifier}"] = action
172 (block_given? ? count : res)
176 def exists?(table_name)
177 @admin.tableExists(TableName.valueOf(table_name))
180 def isNamespace?(table_name)
181 table_name.start_with?('@')
184 def isTablePermission?(permission)
185 permission.java_kind_of?(org.apache.hadoop.hbase.security.access.TablePermission)
188 # Does Namespace exist
189 def namespace_exists?(namespace_name)
190 return !@admin.getNamespaceDescriptor(namespace_name).nil?
191 rescue org.apache.hadoop.hbase.NamespaceNotFoundException => e
195 # Make sure that security features are available
196 def security_available?
199 # Try the getSecurityCapabilities API where supported.
200 # We only need to look at AUTHORIZATION, the AccessController doesn't support
201 # CELL_AUTHORIZATION without AUTHORIZATION also available.
202 caps = @admin.getSecurityCapabilities
204 # If we are unable to use getSecurityCapabilities, fall back with a check for
205 # deployment of the ACL table
206 raise(ArgumentError, 'DISABLED: Security features are not available') unless \
207 exists?(org.apache.hadoop.hbase.security.access.PermissionStorage::ACL_TABLE_NAME.getNameAsString)
210 raise(ArgumentError, 'DISABLED: Security features are not available') unless \
211 caps.include? org.apache.hadoop.hbase.client.security.SecurityCapability::AUTHORIZATION