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