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.io.IOException;
21  import java.util.Map;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.filter.FilterBase;
26  import org.apache.hadoop.hbase.util.ByteRange;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.apache.hadoop.hbase.util.SimpleMutableByteRange;
29  
30  /**
31   * This Filter checks the visibility expression with each KV against visibility labels associated
32   * with the scan. Based on the check the KV is included in the scan result or gets filtered out.
33   */
34  @InterfaceAudience.Private
35  class VisibilityLabelFilter extends FilterBase {
36  
37    private final VisibilityExpEvaluator expEvaluator;
38    private final Map<ByteRange, Integer> cfVsMaxVersions;
39    private final ByteRange curFamily;
40    private final ByteRange curQualifier;
41    private int curFamilyMaxVersions;
42    private int curQualMetVersions;
43  
44    public VisibilityLabelFilter(VisibilityExpEvaluator expEvaluator,
45        Map<ByteRange, Integer> cfVsMaxVersions) {
46      this.expEvaluator = expEvaluator;
47      this.cfVsMaxVersions = cfVsMaxVersions;
48      this.curFamily = new SimpleMutableByteRange();
49      this.curQualifier = new SimpleMutableByteRange();
50    }
51  
52    @Override
53    public ReturnCode filterKeyValue(Cell cell) throws IOException {
54      if (curFamily.getBytes() == null
55          || (Bytes.compareTo(curFamily.getBytes(), curFamily.getOffset(), curFamily.getLength(),
56              cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()) != 0)) {
57        curFamily.set(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
58        // For this family, all the columns can have max of curFamilyMaxVersions versions. No need to
59        // consider the older versions for visibility label check.
60        // Ideally this should have been done at a lower layer by HBase (?)
61        curFamilyMaxVersions = cfVsMaxVersions.get(curFamily);
62        // Family is changed. Just unset curQualifier.
63        curQualifier.unset();
64      }
65      if (curQualifier.getBytes() == null
66          || (Bytes.compareTo(curQualifier.getBytes(), curQualifier.getOffset(),
67              curQualifier.getLength(), cell.getQualifierArray(), cell.getQualifierOffset(),
68              cell.getQualifierLength()) != 0)) {
69        curQualifier.set(cell.getQualifierArray(), cell.getQualifierOffset(),
70            cell.getQualifierLength());
71        curQualMetVersions = 0;
72      }
73      curQualMetVersions++;
74      if (curQualMetVersions > curFamilyMaxVersions) {
75        return ReturnCode.SKIP;
76      }
77  
78      return this.expEvaluator.evaluate(cell) ? ReturnCode.INCLUDE : ReturnCode.SKIP;
79    }
80  
81    // Override here explicitly as the method in super class FilterBase might do a KeyValue recreate.
82    // See HBASE-12068
83    @Override
84    public Cell transformCell(Cell v) {
85      return v;
86    }
87  
88    @Override
89    public void reset() throws IOException {
90      this.curFamily.unset();
91      this.curQualifier.unset();
92      this.curFamilyMaxVersions = 0;
93      this.curQualMetVersions = 0;
94    }
95  }