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