PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.2.0-rc0/src/java/org/apache/hcatalog/common/AuthUtils.java

#
Java | 108 lines | 65 code | 14 blank | 29 comment | 7 complexity | 8c5286f9feac5fc893a6f447aa8210a9 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.hcatalog.common;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import javax.security.auth.login.LoginException;
  22. import org.apache.commons.lang.ArrayUtils;
  23. import org.apache.hadoop.conf.Configuration;
  24. import org.apache.hadoop.fs.FileStatus;
  25. import org.apache.hadoop.fs.Path;
  26. import org.apache.hadoop.fs.permission.FsAction;
  27. import org.apache.hadoop.fs.permission.FsPermission;
  28. import org.apache.hadoop.hive.conf.HiveConf;
  29. import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
  30. import org.apache.hadoop.hive.ql.parse.SemanticException;
  31. import org.apache.hadoop.hive.shims.ShimLoader;
  32. import org.apache.hadoop.security.AccessControlException;
  33. import org.apache.hadoop.security.UserGroupInformation;
  34. public class AuthUtils {
  35. /**
  36. * @param path non-null
  37. * @param action non-null
  38. * @param conf
  39. * @throws SemanticException
  40. * @throws HCatException
  41. *
  42. * This method validates only for existing path. If path doesn't exist
  43. * there is nothing to validate. So, make sure that path passed in is non-null.
  44. */
  45. @SuppressWarnings("deprecation")
  46. public static void authorize(final Path path, final FsAction action, final Configuration conf) throws SemanticException, HCatException{
  47. if(path == null) {
  48. throw new HCatException(ErrorType.ERROR_INTERNAL_EXCEPTION);
  49. }
  50. final FileStatus stat;
  51. try {
  52. stat = path.getFileSystem(conf).getFileStatus(path);
  53. } catch (FileNotFoundException fnfe){
  54. // File named by path doesn't exist; nothing to validate.
  55. return;
  56. }
  57. catch (AccessControlException ace) {
  58. throw new HCatException(ErrorType.ERROR_ACCESS_CONTROL, ace);
  59. } catch (org.apache.hadoop.fs.permission.AccessControlException ace){
  60. // Older hadoop version will throw this @deprecated Exception.
  61. throw new HCatException(ErrorType.ERROR_ACCESS_CONTROL, ace);
  62. } catch (IOException ioe){
  63. throw new SemanticException(ioe);
  64. }
  65. final UserGroupInformation ugi;
  66. try {
  67. ugi = ShimLoader.getHadoopShims().getUGIForConf(conf);
  68. } catch (LoginException le) {
  69. throw new HCatException(ErrorType.ERROR_ACCESS_CONTROL,le);
  70. } catch (IOException ioe) {
  71. throw new SemanticException(ioe);
  72. }
  73. final FsPermission dirPerms = stat.getPermission();
  74. final String user = HiveConf.getBoolVar(conf, ConfVars.METASTORE_USE_THRIFT_SASL) ?
  75. ugi.getShortUserName() : ugi.getUserName();
  76. final String grp = stat.getGroup();
  77. if(user.equals(stat.getOwner())){
  78. if(dirPerms.getUserAction().implies(action)){
  79. return;
  80. }
  81. throw new HCatException(ErrorType.ERROR_ACCESS_CONTROL);
  82. }
  83. if(ArrayUtils.contains(ugi.getGroupNames(), grp)){
  84. if(dirPerms.getGroupAction().implies(action)){
  85. return;
  86. }
  87. throw new HCatException(ErrorType.ERROR_ACCESS_CONTROL);
  88. }
  89. if(dirPerms.getOtherAction().implies(action)){
  90. return;
  91. }
  92. throw new HCatException(ErrorType.ERROR_ACCESS_CONTROL);
  93. }
  94. }