001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.security.access;
019
020import java.util.HashSet;
021import java.util.List;
022import java.util.Set;
023import java.util.stream.Collectors;
024import java.util.stream.Stream;
025import org.apache.commons.lang3.mutable.MutableObject;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.apache.yetus.audience.InterfaceAudience;
028
029@InterfaceAudience.Private
030public final class PermissionUtils {
031
032  private static Set<String> getUserGroups(MutableObject<Set<String>> userGroupsHolder,
033    String username) {
034    Set<String> userGroups = userGroupsHolder.get();
035    if (userGroups != null) {
036      return userGroups;
037    }
038    userGroups = new HashSet<String>(AccessChecker.getUserGroups(username));
039    userGroupsHolder.setValue(userGroups);
040    return userGroups;
041  }
042
043  /**
044   * Filter user permissions based on username. Just delegate call to
045   * {@link #filter(List, byte[], byte[], String)} with null {@code cf} and {@code cq}.
046   */
047  public static List<UserPermission> filter(List<UserPermission> userPerms, String username) {
048    return filter(userPerms, null, null, username);
049  }
050
051  /**
052   * Filter user permissions based on column family, column qualifier, and username.
053   * <p>
054   * Notice that, if {@code cf} or {@code cq} is not null, we will assume that the permission is a
055   * table permission.
056   * <p>
057   * When there is group permission, i.e, the user field for the {@link UserPermission} is start
058   * with {@literal @}, then we will check whether the given {@code username} belongs to this group,
059   * if the given {@code username} itself is not a group.
060   */
061  public static List<UserPermission> filter(List<UserPermission> userPerms, byte[] cf, byte[] cq,
062    String username) {
063    if (cf == null && cq == null && username == null) {
064      // nothing to filter
065      return userPerms;
066    }
067    Stream<UserPermission> stream = userPerms.stream();
068    if (cf != null) {
069      stream =
070        stream.filter(up -> Bytes.equals(((TablePermission) up.getPermission()).getFamily(), cf));
071    }
072    if (cq != null) {
073      stream = stream
074        .filter(up -> Bytes.equals(((TablePermission) up.getPermission()).getQualifier(), cq));
075    }
076    if (username != null) {
077      MutableObject<Set<String>> userGroupsHolder = new MutableObject<>();
078      if (username.startsWith("@")) {
079        // the given username itself is a group, just check equals
080        stream = stream.filter(up -> up.getUser().equals(username));
081      } else {
082        // for normal user, when meeting a group, check whether the username belongs to the group
083        stream = stream.filter(up -> {
084          String user = up.getUser();
085          if (user.startsWith("@")) {
086            return getUserGroups(userGroupsHolder, username).contains(user.substring(1));
087          } else {
088            return user.equals(username);
089          }
090        });
091      }
092    }
093    return stream.collect(Collectors.toList());
094  }
095}