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.access; 019 020import java.io.DataInput; 021import java.io.DataOutput; 022import java.io.IOException; 023import java.util.Objects; 024import org.apache.hadoop.hbase.NamespaceDescriptor; 025import org.apache.hadoop.hbase.util.Bytes; 026import org.apache.yetus.audience.InterfaceAudience; 027 028/** 029 * Represents an authorization for access for the given namespace. 030 */ 031@InterfaceAudience.Public 032public class NamespacePermission extends Permission { 033 034 private String namespace = NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR; 035 036 /** 037 * Construct a namespace permission. 038 * @param namespace namespace's name 039 * @param assigned assigned actions 040 */ 041 NamespacePermission(String namespace, Action... assigned) { 042 super(assigned); 043 this.namespace = namespace; 044 this.scope = Scope.NAMESPACE; 045 } 046 047 public String getNamespace() { 048 return namespace; 049 } 050 051 /** 052 * check if given action is granted in given namespace. 053 * @param namespace namespace's name 054 * @param action action to be checked 055 * @return true if granted, false otherwise 056 */ 057 public boolean implies(String namespace, Action action) { 058 return namespace.equals(this.namespace) && implies(action); 059 } 060 061 @Override 062 public boolean equalsExceptActions(Object obj) { 063 if (!(obj instanceof NamespacePermission)) { 064 return false; 065 } 066 NamespacePermission gp = (NamespacePermission) obj; 067 return namespace.equals(gp.namespace); 068 } 069 070 @Override 071 public int hashCode() { 072 return Objects.hash(namespace) + super.hashCode(); 073 } 074 075 @Override 076 public boolean equals(Object obj) { 077 return equalsExceptActions(obj) && super.equals(obj); 078 } 079 080 @Override 081 public String toString() { 082 return "[NamespacePermission: " + rawExpression() + "]"; 083 } 084 085 @Override 086 protected String rawExpression() { 087 StringBuilder raw = new StringBuilder("namespace=").append(namespace).append(", "); 088 return raw.toString() + super.rawExpression(); 089 } 090 091 @Override 092 public void readFields(DataInput in) throws IOException { 093 super.readFields(in); 094 namespace = Bytes.toString(Bytes.readByteArray(in)); 095 } 096 097 @Override 098 public void write(DataOutput out) throws IOException { 099 super.write(out); 100 Bytes.writeByteArray(out, Bytes.toBytes(namespace)); 101 } 102}