/hbase-server/src/main/ruby/hbase/security.rb

https://github.com/jmhsieh/hbase · Ruby · 172 lines · 125 code · 20 blank · 27 comment · 14 complexity · cea6a7827a18bb7bbb698c792fd9890d MD5 · raw file

  1. #
  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
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  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.
  17. #
  18. include Java
  19. # Wrapper for org.apache.hadoop.hbase.client.HBaseAdmin
  20. module Hbase
  21. class SecurityAdmin
  22. include HBaseConstants
  23. def initialize(configuration, formatter)
  24. @config = configuration
  25. @admin = org.apache.hadoop.hbase.client.HBaseAdmin.new(configuration)
  26. @formatter = formatter
  27. end
  28. #----------------------------------------------------------------------------------------------
  29. def grant(user, permissions, table_name=nil, family=nil, qualifier=nil)
  30. security_available?
  31. # TODO: need to validate user name
  32. if (table_name != nil)
  33. # Table should exist
  34. raise(ArgumentError, "Can't find a table: #{table_name}") unless exists?(table_name)
  35. tablebytes=table_name.to_java_bytes
  36. htd = @admin.getTableDescriptor(tablebytes)
  37. if (family != nil)
  38. raise(ArgumentError, "Can't find a family: #{family}") unless htd.hasFamily(family.to_java_bytes)
  39. end
  40. fambytes = family.to_java_bytes if (family != nil)
  41. qualbytes = qualifier.to_java_bytes if (qualifier != nil)
  42. end
  43. begin
  44. meta_table = org.apache.hadoop.hbase.client.HTable.new(@config,
  45. org.apache.hadoop.hbase.security.access.AccessControlLists::ACL_TABLE_NAME)
  46. service = meta_table.coprocessorService(
  47. org.apache.hadoop.hbase.HConstants::EMPTY_START_ROW)
  48. protocol = org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos::
  49. AccessControlService.newBlockingStub(service)
  50. perm = org.apache.hadoop.hbase.security.access.Permission.new(
  51. permissions.to_java_bytes)
  52. # invoke cp endpoint to perform access controlse
  53. org.apache.hadoop.hbase.protobuf.ProtobufUtil.grant(
  54. protocol, user, tablebytes, fambytes,
  55. qualbytes, perm.getActions())
  56. ensure
  57. meta_table.close()
  58. end
  59. end
  60. #----------------------------------------------------------------------------------------------
  61. def revoke(user, table_name=nil, family=nil, qualifier=nil)
  62. security_available?
  63. # TODO: need to validate user name
  64. if (table_name != nil)
  65. # Table should exist
  66. raise(ArgumentError, "Can't find a table: #{table_name}") unless exists?(table_name)
  67. tablebytes=table_name.to_java_bytes
  68. htd = @admin.getTableDescriptor(tablebytes)
  69. if (family != nil)
  70. raise(ArgumentError, "Can't find family: #{family}") unless htd.hasFamily(family.to_java_bytes)
  71. end
  72. fambytes = family.to_java_bytes if (family != nil)
  73. qualbytes = qualifier.to_java_bytes if (qualifier != nil)
  74. end
  75. begin
  76. meta_table = org.apache.hadoop.hbase.client.HTable.new(@config,
  77. org.apache.hadoop.hbase.security.access.AccessControlLists::ACL_TABLE_NAME)
  78. service = meta_table.coprocessorService(
  79. org.apache.hadoop.hbase.HConstants::EMPTY_START_ROW)
  80. protocol = org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos::
  81. AccessControlService.newBlockingStub(service)
  82. # invoke cp endpoint to perform access controlse
  83. org.apache.hadoop.hbase.protobuf.ProtobufUtil.revoke(
  84. protocol, user, tablebytes, fambytes, qualbytes)
  85. ensure
  86. meta_table.close()
  87. end
  88. end
  89. #----------------------------------------------------------------------------------------------
  90. def user_permission(table_name=nil)
  91. security_available?
  92. if (table_name != nil)
  93. raise(ArgumentError, "Can't find table: #{table_name}") unless exists?(table_name)
  94. tablebytes=table_name.to_java_bytes
  95. end
  96. begin
  97. meta_table = org.apache.hadoop.hbase.client.HTable.new(@config,
  98. org.apache.hadoop.hbase.security.access.AccessControlLists::ACL_TABLE_NAME)
  99. service = meta_table.coprocessorService(
  100. org.apache.hadoop.hbase.HConstants::EMPTY_START_ROW)
  101. protocol = org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos::
  102. AccessControlService.newBlockingStub(service)
  103. # invoke cp endpoint to perform access controlse
  104. perms = org.apache.hadoop.hbase.protobuf.ProtobufUtil.getUserPermissions(
  105. protocol, tablebytes)
  106. ensure
  107. meta_table.close()
  108. end
  109. res = {}
  110. count = 0
  111. perms.each do |value|
  112. user_name = String.from_java_bytes(value.getUser)
  113. table = (value.getTable != nil) ? org.apache.hadoop.hbase.util.Bytes::toStringBinary(value.getTable) : ''
  114. family = (value.getFamily != nil) ? org.apache.hadoop.hbase.util.Bytes::toStringBinary(value.getFamily) : ''
  115. qualifier = (value.getQualifier != nil) ? org.apache.hadoop.hbase.util.Bytes::toStringBinary(value.getQualifier) : ''
  116. action = org.apache.hadoop.hbase.security.access.Permission.new value.getActions
  117. if block_given?
  118. yield(user_name, "#{table},#{family},#{qualifier}: #{action.to_s}")
  119. else
  120. res[user_name] ||= {}
  121. res[user_name][family + ":" +qualifier] = action
  122. end
  123. count += 1
  124. end
  125. return ((block_given?) ? count : res)
  126. end
  127. # Does table exist?
  128. def exists?(table_name)
  129. @admin.tableExists(table_name)
  130. end
  131. # Make sure that security tables are available
  132. def security_available?()
  133. raise(ArgumentError, "DISABLED: Security features are not available") \
  134. unless exists?(org.apache.hadoop.hbase.security.access.AccessControlLists::ACL_TABLE_NAME)
  135. end
  136. end
  137. end