nazgul33

sentry.hasaccess

Mar 23rd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1.   private class GroupCache {
  2.     List<org.apache.sentry.policy.common.Privilege> privileges;
  3.     public GroupCache(String group) {
  4.       ImmutableSet<String> pSet =
  5.           policy_.getAllPrivileges(Sets.newHashSet(group), ActiveRoleSet.ALL);
  6.  
  7.       privileges = new ArrayList<>(pSet.size());
  8.       for (String p: pSet) {
  9.         privileges.add(privilegeFactory_.createPrivilege(p));
  10.       }
  11.     }
  12.   }
  13.   HashMap<String, GroupCache> groupCacheMap = new HashMap<>();
  14.   private boolean hasCachedAccessInternal(Subject user, List<? extends Authorizable> authorizables, Set<? extends Action> actions) {
  15.     Set<String> groups = provider_.getGroupMapping().getGroups(user.getName());
  16.     List<String> hierarchy = new ArrayList<>();
  17.     for (Authorizable authorizable : authorizables) {
  18.       hierarchy.add(KV_JOINER.join(authorizable.getTypeName(), authorizable.getName()));
  19.     }
  20.     List<String> requestPrivileges = new ArrayList<String>();
  21.     for (Action action : actions) {
  22.       String requestPermission = AUTHORIZABLE_JOINER.join(hierarchy);
  23.       requestPermission = AUTHORIZABLE_JOINER.join(requestPermission,
  24.           KV_JOINER.join(PRIVILEGE_NAME, action.getValue()));
  25.       requestPrivileges.add(requestPermission);
  26.     }
  27.  
  28.     for (String g: groups) {
  29.       GroupCache gc = groupCacheMap.get(g);
  30.       if ( gc == null ) {
  31.         gc = this.new GroupCache(g);
  32.         groupCacheMap.put(g, gc);
  33.       }
  34.  
  35.       for (String requestPrivilege : requestPrivileges) {
  36.         org.apache.sentry.policy.common.Privilege reqPriv =
  37.             privilegeFactory_.createPrivilege(requestPrivilege);
  38.         for (org.apache.sentry.policy.common.Privilege permission : gc.privileges) {
  39.         /*
  40.          * Does the permission granted in the policy file imply the requested action?
  41.          */
  42.           boolean result = permission.implies(reqPriv);
  43.           if(LOG.isDebugEnabled()) {
  44.             LOG.debug("ProviderPrivilege {}, RequestPrivilege {}, RoleSet, {}, Result {}",
  45.                 permission, requestPrivilege, ActiveRoleSet.ALL, result);
  46.           }
  47.           if (result) {
  48.             return true;
  49.           }
  50.         }
  51.       }
  52.     }
  53.  
  54.     return false;
  55.   }
  56.  
  57.   /*
  58.    * Returns true if the given user has permission to execute the given
  59.    * request, false otherwise. Always returns true if authorization is disabled.
  60.    */
  61.   public boolean hasCachedAccess(String user, PrivilegeRequest request) {
  62.     Preconditions.checkNotNull(user);
  63.     Preconditions.checkNotNull(request);
  64.  
  65.     // If authorization is not enabled the user will always have access. If this is
  66.     // an internal request, the user will always have permission.
  67.     if (!config_.isEnabled()) {
  68.       return true;
  69.     }
  70.  
  71.     EnumSet<DBModelAction> actions = request.getPrivilege().getHiveActions();
  72.     List<DBModelAuthorizable> authorizeables = Lists.newArrayList(
  73.         server_.getHiveAuthorizeableHierarchy());
  74.     // If request.getAuthorizeable() is null, the request is for server-level permission.
  75.     if (request.getAuthorizeable() != null) {
  76.       authorizeables.addAll(request.getAuthorizeable().getHiveAuthorizeableHierarchy());
  77.     }
  78.  
  79.     // The Hive Access API does not currently provide a way to check if the user
  80.     // has any privileges on a given resource.
  81.     if (request.getPrivilege().getAnyOf()) {
  82.       for (DBModelAction action: anyActions) {
  83.         if (hasCachedAccessInternal(new Subject(user), authorizeables,
  84.             EnumSet.of(action))) {
  85.           return true;
  86.         }
  87.       }
  88.       return false;
  89.     } else if (request.getPrivilege() == Privilege.CREATE && authorizeables.size() > 1) {
  90.       // CREATE on an object requires CREATE on the parent,
  91.       // so don't check access on the object we're creating.
  92.       authorizeables.remove(authorizeables.size() - 1);
  93.     }
  94.     return hasCachedAccessInternal(new Subject(user), authorizeables, actions);
  95.   }
Advertisement
Add Comment
Please, Sign In to add comment