View Javadoc

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.hbase.security.visibility;
19  
20  import java.util.ArrayList;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.security.User;
30  
31  /**
32   * If the passed in authorization is null, then this ScanLabelGenerator
33   * feeds the set of predefined authorization labels for the given user. That is
34   * the set defined by the admin using the VisibilityClient admin interface
35   * or the set_auths shell command.
36   * Otherwise the passed in authorization labels are returned with no change.
37   *
38   * Note: This SLG should not be used alone because it does not check
39   * the passed in authorization labels against what the user is authorized for.
40   */
41  @InterfaceAudience.Private
42  public class FeedUserAuthScanLabelGenerator implements ScanLabelGenerator {
43  
44    private static final Log LOG = LogFactory.getLog(FeedUserAuthScanLabelGenerator.class);
45  
46    private Configuration conf;
47    private VisibilityLabelsCache labelsCache;
48  
49    public FeedUserAuthScanLabelGenerator() {
50      this.labelsCache = VisibilityLabelsCache.get();
51    }
52  
53    @Override
54    public void setConf(Configuration conf) {
55      this.conf = conf;
56    }
57  
58    @Override
59    public Configuration getConf() {
60      return this.conf;
61    }
62  
63    @Override
64    public List<String> getLabels(User user, Authorizations authorizations) {
65      if (authorizations == null || authorizations.getLabels() == null
66          || authorizations.getLabels().isEmpty()) {
67        String userName = user.getShortName();
68        Set<String> auths = new HashSet<String>();
69        auths.addAll(this.labelsCache.getUserAuths(userName));
70        auths.addAll(this.labelsCache.getGroupAuths(user.getGroupNames()));
71        return new ArrayList<String>(auths);
72      }
73      return authorizations.getLabels();
74    }
75  
76  }