1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.security.access;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.hbase.TableName;
24 import org.apache.hadoop.hbase.KeyValue;
25 import org.apache.hadoop.hbase.util.Bytes;
26
27 import java.io.DataInput;
28 import java.io.DataOutput;
29 import java.io.IOException;
30
31
32
33
34
35
36
37 public class TablePermission extends Permission {
38 private static Log LOG = LogFactory.getLog(TablePermission.class);
39
40 private TableName table;
41 private byte[] family;
42 private byte[] qualifier;
43
44
45
46 private String namespace;
47
48
49 public TablePermission() {
50 super();
51 }
52
53
54
55
56
57
58
59
60 public TablePermission(TableName table, byte[] family, Action... assigned) {
61 this(table, family, null, assigned);
62 }
63
64
65
66
67
68
69
70
71 public TablePermission(TableName table, byte[] family, byte[] qualifier,
72 Action... assigned) {
73 super(assigned);
74 this.table = table;
75 this.family = family;
76 this.qualifier = qualifier;
77 }
78
79
80
81
82
83
84
85
86 public TablePermission(TableName table, byte[] family, byte[] qualifier,
87 byte[] actionCodes) {
88 super(actionCodes);
89 this.table = table;
90 this.family = family;
91 this.qualifier = qualifier;
92 }
93
94
95
96
97
98
99
100
101
102 public TablePermission(String namespace, TableName table, byte[] family, byte[] qualifier,
103 Action... assigned) {
104 super(assigned);
105 this.namespace = namespace;
106 this.table = table;
107 this.family = family;
108 this.qualifier = qualifier;
109 }
110
111
112
113
114
115
116
117
118
119 public TablePermission(String namespace, TableName table, byte[] family, byte[] qualifier,
120 byte[] actionCodes) {
121 super(actionCodes);
122 this.namespace = namespace;
123 this.table = table;
124 this.family = family;
125 this.qualifier = qualifier;
126 }
127
128
129
130
131
132
133
134 public TablePermission(String namespace, byte[] actionCodes) {
135 super(actionCodes);
136 this.namespace = namespace;
137 }
138
139
140
141
142
143
144
145 public TablePermission(String namespace, Action... assigned) {
146 super(assigned);
147 this.namespace = namespace;
148 }
149
150 public boolean hasTable() {
151 return table != null;
152 }
153
154 public TableName getTableName() {
155 return table;
156 }
157
158 public boolean hasFamily() {
159 return family != null;
160 }
161
162 public byte[] getFamily() {
163 return family;
164 }
165
166 public boolean hasQualifier() {
167 return qualifier != null;
168 }
169
170 public byte[] getQualifier() {
171 return qualifier;
172 }
173
174 public boolean hasNamespace() {
175 return namespace != null;
176 }
177
178 public String getNamespace() {
179 return namespace;
180 }
181
182
183
184
185
186
187
188
189
190
191 public boolean implies(String namespace, Action action) {
192 if (!this.namespace.equals(namespace)) {
193 return false;
194 }
195
196
197 return super.implies(action);
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public boolean implies(TableName table, byte[] family, byte[] qualifier,
214 Action action) {
215 if (!this.table.equals(table)) {
216 return false;
217 }
218
219 if (this.family != null &&
220 (family == null ||
221 !Bytes.equals(this.family, family))) {
222 return false;
223 }
224
225 if (this.qualifier != null &&
226 (qualifier == null ||
227 !Bytes.equals(this.qualifier, qualifier))) {
228 return false;
229 }
230
231
232 return super.implies(action);
233 }
234
235
236
237
238
239
240
241
242
243
244 public boolean implies(TableName table, KeyValue kv, Action action) {
245 if (!this.table.equals(table)) {
246 return false;
247 }
248
249 if (family != null &&
250 (Bytes.compareTo(family, 0, family.length,
251 kv.getBuffer(), kv.getFamilyOffset(), kv.getFamilyLength()) != 0)) {
252 return false;
253 }
254
255 if (qualifier != null &&
256 (Bytes.compareTo(qualifier, 0, qualifier.length,
257 kv.getBuffer(), kv.getQualifierOffset(), kv.getQualifierLength()) != 0)) {
258 return false;
259 }
260
261
262 return super.implies(action);
263 }
264
265
266
267
268
269
270
271
272
273 public boolean matchesFamily(TableName table, byte[] family, Action action) {
274 if (!this.table.equals(table)) {
275 return false;
276 }
277
278 if (this.family != null &&
279 (family == null ||
280 !Bytes.equals(this.family, family))) {
281 return false;
282 }
283
284
285
286 return super.implies(action);
287 }
288
289
290
291
292
293
294
295
296
297
298 public boolean matchesFamilyQualifier(TableName table, byte[] family, byte[] qualifier,
299 Action action) {
300 if (!matchesFamily(table, family, action)) {
301 return false;
302 } else {
303 if (this.qualifier != null &&
304 (qualifier == null ||
305 !Bytes.equals(this.qualifier, qualifier))) {
306 return false;
307 }
308 }
309 return super.implies(action);
310 }
311
312 @Override
313 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
314 justification="Passed on construction except on constructor not to be used")
315 public boolean equals(Object obj) {
316 if (!(obj instanceof TablePermission)) {
317 return false;
318 }
319 TablePermission other = (TablePermission)obj;
320
321 if (!(table.equals(other.getTableName()) &&
322 ((family == null && other.getFamily() == null) ||
323 Bytes.equals(family, other.getFamily())) &&
324 ((qualifier == null && other.getQualifier() == null) ||
325 Bytes.equals(qualifier, other.getQualifier())) &&
326 ((namespace == null && other.getNamespace() == null) ||
327 (namespace != null && namespace.equals(other.getNamespace())))
328 )) {
329 return false;
330 }
331
332
333 return super.equals(other);
334 }
335
336 @Override
337 public int hashCode() {
338 final int prime = 37;
339 int result = super.hashCode();
340 if (table != null) {
341 result = prime * result + table.hashCode();
342 }
343 if (family != null) {
344 result = prime * result + Bytes.hashCode(family);
345 }
346 if (qualifier != null) {
347 result = prime * result + Bytes.hashCode(qualifier);
348 }
349 if (namespace != null) {
350 result = prime * result + namespace.hashCode();
351 }
352 return result;
353 }
354
355 public String toString() {
356 StringBuilder str = new StringBuilder("[TablePermission: ");
357 if(namespace != null) {
358 str.append("namespace=").append(namespace)
359 .append(", ");
360 }
361 else if(table != null) {
362 str.append("table=").append(table)
363 .append(", family=")
364 .append(family == null ? null : Bytes.toString(family))
365 .append(", qualifier=")
366 .append(qualifier == null ? null : Bytes.toString(qualifier))
367 .append(", ");
368 } else {
369 str.append("actions=");
370 }
371 if (actions != null) {
372 for (int i=0; i<actions.length; i++) {
373 if (i > 0)
374 str.append(",");
375 if (actions[i] != null)
376 str.append(actions[i].toString());
377 else
378 str.append("NULL");
379 }
380 }
381 str.append("]");
382
383 return str.toString();
384 }
385
386 @Override
387 public void readFields(DataInput in) throws IOException {
388 super.readFields(in);
389 byte[] tableBytes = Bytes.readByteArray(in);
390 table = TableName.valueOf(tableBytes);
391 if (in.readBoolean()) {
392 family = Bytes.readByteArray(in);
393 }
394 if (in.readBoolean()) {
395 qualifier = Bytes.readByteArray(in);
396 }
397 if(in.readBoolean()) {
398 namespace = Bytes.toString(Bytes.readByteArray(in));
399 }
400 }
401
402 @Override
403 public void write(DataOutput out) throws IOException {
404 super.write(out);
405 Bytes.writeByteArray(out, table.getName());
406 out.writeBoolean(family != null);
407 if (family != null) {
408 Bytes.writeByteArray(out, family);
409 }
410 out.writeBoolean(qualifier != null);
411 if (qualifier != null) {
412 Bytes.writeByteArray(out, qualifier);
413 }
414 out.writeBoolean(namespace != null);
415 if(namespace != null) {
416 Bytes.writeByteArray(out, Bytes.toBytes(namespace));
417 }
418 }
419 }