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  
19  package org.apache.hadoop.hbase.security.access;
20  
21  import java.io.DataInput;
22  import java.io.DataOutput;
23  import java.io.IOException;
24  import java.util.Arrays;
25  import java.util.Map;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.classification.InterfaceStability;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.io.VersionedWritable;
33  
34  import com.google.common.collect.Maps;
35  
36  /**
37   * Base permissions instance representing the ability to perform a given set
38   * of actions.
39   *
40   * @see TablePermission
41   */
42  @InterfaceAudience.Public
43  @InterfaceStability.Evolving
44  public class Permission extends VersionedWritable {
45    protected static final byte VERSION = 0;
46    public enum Action {
47      READ('R'), WRITE('W'), EXEC('X'), CREATE('C'), ADMIN('A');
48  
49      private byte code;
50      Action(char code) {
51        this.code = (byte)code;
52      }
53  
54      public byte code() { return code; }
55    }
56  
57    private static final Log LOG = LogFactory.getLog(Permission.class);
58    protected static final Map<Byte,Action> ACTION_BY_CODE = Maps.newHashMap();
59  
60    protected Action[] actions;
61  
62    static {
63      for (Action a : Action.values()) {
64        ACTION_BY_CODE.put(a.code(), a);
65      }
66    }
67  
68    /** Empty constructor for Writable implementation.  <b>Do not use.</b> */
69    public Permission() {
70      super();
71    }
72  
73    public Permission(Action... assigned) {
74      if (assigned != null && assigned.length > 0) {
75        actions = Arrays.copyOf(assigned, assigned.length);
76      }
77    }
78  
79    public Permission(byte[] actionCodes) {
80      if (actionCodes != null) {
81        Action acts[] = new Action[actionCodes.length];
82        int j = 0;
83        for (int i=0; i<actionCodes.length; i++) {
84          byte b = actionCodes[i];
85          Action a = ACTION_BY_CODE.get(b);
86          if (a == null) {
87            LOG.error("Ignoring unknown action code '"+
88                Bytes.toStringBinary(new byte[]{b})+"'");
89            continue;
90          }
91          acts[j++] = a;
92        }
93        this.actions = Arrays.copyOf(acts, j);
94      }
95    }
96  
97    public Action[] getActions() {
98      return actions;
99    }
100 
101   public boolean implies(Action action) {
102     if (this.actions != null) {
103       for (Action a : this.actions) {
104         if (a == action) {
105           return true;
106         }
107       }
108     }
109 
110     return false;
111   }
112 
113   @Override
114   public boolean equals(Object obj) {
115     if (!(obj instanceof Permission)) {
116       return false;
117     }
118     Permission other = (Permission)obj;
119     // check actions
120     if (actions == null && other.getActions() == null) {
121       return true;
122     } else if (actions != null && other.getActions() != null) {
123       Action[] otherActions = other.getActions();
124       if (actions.length != otherActions.length) {
125         return false;
126       }
127 
128       outer:
129       for (Action a : actions) {
130         for (Action oa : otherActions) {
131           if (a == oa) continue outer;
132         }
133         return false;
134       }
135       return true;
136     }
137 
138     return false;
139   }
140 
141   @Override
142   public int hashCode() {
143     final int prime = 37;
144     int result = 23;
145     for (Action a : actions) {
146       result = prime * result + a.code();
147     }
148     return result;
149   }
150 
151   public String toString() {
152     StringBuilder str = new StringBuilder("[Permission: ")
153         .append("actions=");
154     if (actions != null) {
155       for (int i=0; i<actions.length; i++) {
156         if (i > 0)
157           str.append(",");
158         if (actions[i] != null)
159           str.append(actions[i].toString());
160         else
161           str.append("NULL");
162       }
163     }
164     str.append("]");
165 
166     return str.toString();
167   }
168 
169   /** @return the object version number */
170   public byte getVersion() {
171     return VERSION;
172   }
173 
174   @Override
175   public void readFields(DataInput in) throws IOException {
176     super.readFields(in);
177     int length = (int)in.readByte();
178     if (length > 0) {
179       actions = new Action[length];
180       for (int i = 0; i < length; i++) {
181         byte b = in.readByte();
182         Action a = ACTION_BY_CODE.get(b);
183         if (a == null) {
184           throw new IOException("Unknown action code '"+
185               Bytes.toStringBinary(new byte[]{b})+"' in input");
186         }
187         this.actions[i] = a;
188       }
189     } else {
190       actions = new Action[0];
191     }
192   }
193 
194   @Override
195   public void write(DataOutput out) throws IOException {
196     super.write(out);
197     out.writeByte(actions != null ? actions.length : 0);
198     if (actions != null) {
199       for (Action a: actions) {
200         out.writeByte(a.code());
201       }
202     }
203   }
204 }