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.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  
25  /**
26   * A simple validator that validates the labels passed
27   */
28  @InterfaceAudience.Private
29  public class VisibilityLabelsValidator {
30    // We follow Accumulo parity for valid visibility labels.
31    private static final boolean[] validAuthChars = new boolean[256];
32  
33    public static final String regex = "[A-Za-z_\\-\\:\\/\\.0-9]+";
34    public static final Pattern pattern = Pattern.compile(regex);
35  
36    static {
37      for (int i = 0; i < 256; i++) {
38        validAuthChars[i] = false;
39      }
40  
41      for (int i = 'a'; i <= 'z'; i++) {
42        validAuthChars[i] = true;
43      }
44  
45      for (int i = 'A'; i <= 'Z'; i++) {
46        validAuthChars[i] = true;
47      }
48  
49      for (int i = '0'; i <= '9'; i++) {
50        validAuthChars[i] = true;
51      }
52  
53      validAuthChars['_'] = true;
54      validAuthChars['-'] = true;
55      validAuthChars[':'] = true;
56      validAuthChars['.'] = true;
57      validAuthChars['/'] = true;
58    }
59    
60    static final boolean isValidAuthChar(byte b) {
61      return validAuthChars[0xff & b];
62    }
63  
64    static final boolean isValidLabel(byte[] label) {
65      for (int i = 0; i < label.length; i++) {
66        if (!isValidAuthChar(label[i])) {
67          return false;
68        }
69      }
70      return true;
71    }
72  
73    public static final boolean isValidLabel(String label) {
74      Matcher matcher = pattern.matcher(label);
75      return matcher.matches();
76    }
77  }