001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.security.visibility;
019
020import java.util.regex.Pattern;
021import org.apache.yetus.audience.InterfaceAudience;
022
023/**
024 * A simple validator that validates the labels passed
025 */
026@InterfaceAudience.Private
027public class VisibilityLabelsValidator {
028  private static final boolean[] validAuthChars = new boolean[256];
029
030  public static final String regex = "[A-Za-z_\\-\\:\\/\\.0-9]+";
031  public static final Pattern pattern = Pattern.compile(regex);
032
033  static {
034    for (int i = 0; i < 256; i++) {
035      validAuthChars[i] = false;
036    }
037
038    for (int i = 'a'; i <= 'z'; i++) {
039      validAuthChars[i] = true;
040    }
041
042    for (int i = 'A'; i <= 'Z'; i++) {
043      validAuthChars[i] = true;
044    }
045
046    for (int i = '0'; i <= '9'; i++) {
047      validAuthChars[i] = true;
048    }
049
050    validAuthChars['_'] = true;
051    validAuthChars['-'] = true;
052    validAuthChars[':'] = true;
053    validAuthChars['.'] = true;
054    validAuthChars['/'] = true;
055  }
056
057  static final boolean isValidAuthChar(byte b) {
058    return validAuthChars[0xff & b];
059  }
060
061  public static final boolean isValidLabel(byte[] label) {
062    for (int i = 0; i < label.length; i++) {
063      if (!isValidAuthChar(label[i])) {
064        return false;
065      }
066    }
067    return true;
068  }
069}