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.mapreduce;
19  
20  import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_FAMILY;
21  import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_NAME;
22  import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABEL_QUALIFIER;
23  
24  import java.io.IOException;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  import org.apache.hadoop.conf.Configuration;
33  import org.apache.hadoop.hbase.TableNotFoundException;
34  import org.apache.hadoop.hbase.Tag;
35  import org.apache.hadoop.hbase.client.HTable;
36  import org.apache.hadoop.hbase.client.Result;
37  import org.apache.hadoop.hbase.client.ResultScanner;
38  import org.apache.hadoop.hbase.client.Scan;
39  import org.apache.hadoop.hbase.security.visibility.Authorizations;
40  import org.apache.hadoop.hbase.security.visibility.VisibilityLabelOrdinalProvider;
41  import org.apache.hadoop.hbase.security.visibility.VisibilityUtils;
42  import org.apache.hadoop.hbase.util.Bytes;
43  
44  /**
45   * This implementation creates tags by expanding expression using label ordinal. Labels will be
46   * serialized in sorted order of it's ordinal.
47   */
48  @InterfaceAudience.Private
49  public class DefaultVisibilityExpressionResolver implements VisibilityExpressionResolver {
50    private static final Log LOG = LogFactory.getLog(DefaultVisibilityExpressionResolver.class);
51  
52    private Configuration conf;
53    private final Map<String, Integer> labels = new HashMap<String, Integer>();
54  
55    @Override
56    public Configuration getConf() {
57      return this.conf;
58    }
59  
60    @Override
61    public void setConf(Configuration conf) {
62      this.conf = conf;
63    }
64  
65    @Override
66    public void init() {
67      // Reading all the labels and ordinal.
68      // This scan should be done by user with global_admin previliges.. Ensure that it works
69      HTable labelsTable = null;
70      try {
71        labelsTable = new HTable(conf, LABELS_TABLE_NAME);
72      } catch (TableNotFoundException e) {
73        // Just return with out doing any thing. When the VC is not used we wont be having 'labels'
74        // table in the cluster.
75        return;
76      } catch (IOException e) {
77        LOG.error("Error opening 'labels' table", e);
78        return;
79      }
80      Scan scan = new Scan();
81      scan.setAuthorizations(new Authorizations(VisibilityUtils.SYSTEM_LABEL));
82      scan.addColumn(LABELS_TABLE_FAMILY, LABEL_QUALIFIER);
83      ResultScanner scanner = null;
84      try {
85        scanner = labelsTable.getScanner(scan);
86        Result next = null;
87        while ((next = scanner.next()) != null) {
88          byte[] row = next.getRow();
89          byte[] value = next.getValue(LABELS_TABLE_FAMILY, LABEL_QUALIFIER);
90          labels.put(Bytes.toString(value), Bytes.toInt(row));
91        }
92      } catch (IOException e) {
93        LOG.error("Error reading 'labels' table", e);
94      } finally {
95        try {
96          if (scanner != null) {
97            scanner.close();
98          }
99        } finally {
100         try {
101           labelsTable.close();
102         } catch (IOException e) {
103           LOG.warn("Error on closing 'labels' table", e);
104         }
105       }
106     }
107   }
108 
109   @Override
110   public List<Tag> createVisibilityExpTags(String visExpression) throws IOException {
111     VisibilityLabelOrdinalProvider provider = new VisibilityLabelOrdinalProvider() {
112       @Override
113       public int getLabelOrdinal(String label) {
114         return labels.get(label);
115       }
116 
117       @Override
118       public String getLabel(int ordinal) {
119         // Unused
120         throw new UnsupportedOperationException(
121             "getLabel should not be used in VisibilityExpressionResolver");
122       }
123     };
124     return VisibilityUtils.createVisibilityExpTags(visExpression, true, false, null, provider);
125   }
126 }