PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/session/CreateTableAutomaticGrant.java

#
Java | 134 lines | 104 code | 12 blank | 18 comment | 29 complexity | c6a6ab7c734967efec5fee24fd5f47fd MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.ql.session;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.apache.hadoop.hive.conf.HiveConf;
  24. import org.apache.hadoop.hive.metastore.api.PrincipalType;
  25. import org.apache.hadoop.hive.metastore.api.PrivilegeGrantInfo;
  26. import org.apache.hadoop.hive.ql.metadata.HiveException;
  27. import org.apache.hadoop.hive.ql.security.authorization.Privilege;
  28. import org.apache.hadoop.hive.ql.security.authorization.PrivilegeRegistry;
  29. public class CreateTableAutomaticGrant {
  30. private Map<String, List<PrivilegeGrantInfo>> userGrants;
  31. private Map<String, List<PrivilegeGrantInfo>> groupGrants;
  32. private Map<String, List<PrivilegeGrantInfo>> roleGrants;
  33. public static CreateTableAutomaticGrant create(HiveConf conf)
  34. throws HiveException {
  35. CreateTableAutomaticGrant grants = new CreateTableAutomaticGrant();
  36. grants.userGrants = getGrantMap(HiveConf.getVar(conf,
  37. HiveConf.ConfVars.HIVE_AUTHORIZATION_TABLE_USER_GRANTS));
  38. grants.groupGrants = getGrantMap(HiveConf.getVar(conf,
  39. HiveConf.ConfVars.HIVE_AUTHORIZATION_TABLE_GROUP_GRANTS));
  40. grants.roleGrants = getGrantMap(HiveConf.getVar(conf,
  41. HiveConf.ConfVars.HIVE_AUTHORIZATION_TABLE_ROLE_GRANTS));
  42. String grantor = null;
  43. if (SessionState.get() != null
  44. && SessionState.get().getAuthenticator() != null) {
  45. grantor = SessionState.get().getAuthenticator().getUserName();
  46. List<PrivilegeGrantInfo> ownerGrant = getGrantorInfoList(HiveConf.getVar(conf,
  47. HiveConf.ConfVars.HIVE_AUTHORIZATION_TABLE_OWNER_GRANTS));
  48. if(ownerGrant != null) {
  49. if (grants.userGrants == null) {
  50. grants.userGrants = new HashMap<String, List<PrivilegeGrantInfo>>();
  51. }
  52. grants.userGrants.put(grantor, ownerGrant);
  53. }
  54. }
  55. return grants;
  56. }
  57. private static Map<String, List<PrivilegeGrantInfo>> getGrantMap(String grantMapStr)
  58. throws HiveException {
  59. if (grantMapStr != null && !grantMapStr.trim().equals("")) {
  60. String[] grantArrayStr = grantMapStr.split(";");
  61. Map<String, List<PrivilegeGrantInfo>> grantsMap = new HashMap<String, List<PrivilegeGrantInfo>>();
  62. for (String grantStr : grantArrayStr) {
  63. String[] principalListAndPrivList = grantStr.split(":");
  64. if (principalListAndPrivList.length != 2
  65. || principalListAndPrivList[0] == null
  66. || principalListAndPrivList[0].trim().equals("")) {
  67. throw new HiveException(
  68. "Can not understand the config privilege definition " + grantStr);
  69. }
  70. String userList = principalListAndPrivList[0];
  71. String privList = principalListAndPrivList[1];
  72. List<PrivilegeGrantInfo> grantInfoList = getGrantorInfoList(privList);
  73. if(grantInfoList != null) {
  74. String[] users = userList.split(",");
  75. for (String user : users) {
  76. grantsMap.put(user, grantInfoList);
  77. }
  78. }
  79. }
  80. return grantsMap;
  81. }
  82. return null;
  83. }
  84. private static List<PrivilegeGrantInfo> getGrantorInfoList(String privList)
  85. throws HiveException {
  86. if (privList == null || privList.trim().equals("")) {
  87. return null;
  88. }
  89. checkPrivilege(privList);
  90. String[] grantArray = privList.split(",");
  91. List<PrivilegeGrantInfo> grantInfoList = new ArrayList<PrivilegeGrantInfo>();
  92. String grantor = null;
  93. if (SessionState.get().getAuthenticator() != null) {
  94. grantor = SessionState.get().getAuthenticator().getUserName();
  95. }
  96. for (String grant : grantArray) {
  97. grantInfoList.add(new PrivilegeGrantInfo(grant, -1, grantor,
  98. PrincipalType.USER, true));
  99. }
  100. return grantInfoList;
  101. }
  102. private static void checkPrivilege(String ownerGrantsInConfig)
  103. throws HiveException {
  104. String[] ownerGrantArray = ownerGrantsInConfig.split(",");
  105. // verify the config
  106. for (String ownerGrant : ownerGrantArray) {
  107. Privilege prive = PrivilegeRegistry.getPrivilege(ownerGrant);
  108. if (prive == null) {
  109. throw new HiveException("Privilege " + ownerGrant + " is not found.");
  110. }
  111. }
  112. }
  113. public Map<String, List<PrivilegeGrantInfo>> getUserGrants() {
  114. return userGrants;
  115. }
  116. public Map<String, List<PrivilegeGrantInfo>> getGroupGrants() {
  117. return groupGrants;
  118. }
  119. public Map<String, List<PrivilegeGrantInfo>> getRoleGrants() {
  120. return roleGrants;
  121. }
  122. }