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;
021
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * A simple validator that validates the labels passed
026 */
027@InterfaceAudience.Private
028public class VisibilityLabelsValidator {
029  private static final boolean[] validAuthChars = new boolean[256];
030
031  public static final String regex = "[A-Za-z_\\-\\:\\/\\.0-9]+";
032  public static final Pattern pattern = Pattern.compile(regex);
033
034  static {
035    for (int i = 0; i < 256; i++) {
036      validAuthChars[i] = false;
037    }
038
039    for (int i = 'a'; i <= 'z'; i++) {
040      validAuthChars[i] = true;
041    }
042
043    for (int i = 'A'; i <= 'Z'; i++) {
044      validAuthChars[i] = true;
045    }
046
047    for (int i = '0'; i <= '9'; i++) {
048      validAuthChars[i] = true;
049    }
050
051    validAuthChars['_'] = true;
052    validAuthChars['-'] = true;
053    validAuthChars[':'] = true;
054    validAuthChars['.'] = true;
055    validAuthChars['/'] = true;
056  }
057  
058  static final boolean isValidAuthChar(byte b) {
059    return validAuthChars[0xff & b];
060  }
061
062  public static final boolean isValidLabel(byte[] label) {
063    for (int i = 0; i < label.length; i++) {
064      if (!isValidAuthChar(label[i])) {
065        return false;
066      }
067    }
068    return true;
069  }
070}