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 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.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.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.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.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 @Override
319 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
320 justification="Passed on construction except on constructor not to be used")
321 public boolean equals(Object obj) {
322 if (!(obj instanceof TablePermission)) {
323 return false;
324 }
325 TablePermission other = (TablePermission)obj;
326
327 if (!(table.equals(other.getTableName()) &&
328 ((family == null && other.getFamily() == null) ||
329 Bytes.equals(family, other.getFamily())) &&
330 ((qualifier == null && other.getQualifier() == null) ||
331 Bytes.equals(qualifier, other.getQualifier())) &&
332 ((namespace == null && other.getNamespace() == null) ||
333 (namespace != null && namespace.equals(other.getNamespace())))
334 )) {
335 return false;
336 }
337
338
339 return super.equals(other);
340 }
341
342 @Override
343 public int hashCode() {
344 final int prime = 37;
345 int result = super.hashCode();
346 if (table != null) {
347 result = prime * result + table.hashCode();
348 }
349 if (family != null) {
350 result = prime * result + Bytes.hashCode(family);
351 }
352 if (qualifier != null) {
353 result = prime * result + Bytes.hashCode(qualifier);
354 }
355 if (namespace != null) {
356 result = prime * result + namespace.hashCode();
357 }
358 return result;
359 }
360
361 @Override
362 public String toString() {
363 StringBuilder str = new StringBuilder("[TablePermission: ");
364 if(namespace != null) {
365 str.append("namespace=").append(namespace)
366 .append(", ");
367 }
368 else if(table != null) {
369 str.append("table=").append(table)
370 .append(", family=")
371 .append(family == null ? null : Bytes.toString(family))
372 .append(", qualifier=")
373 .append(qualifier == null ? null : Bytes.toString(qualifier))
374 .append(", ");
375 } else {
376 str.append("actions=");
377 }
378 if (actions != null) {
379 for (int i=0; i<actions.length; i++) {
380 if (i > 0)
381 str.append(",");
382 if (actions[i] != null)
383 str.append(actions[i].toString());
384 else
385 str.append("NULL");
386 }
387 }
388 str.append("]");
389
390 return str.toString();
391 }
392
393 @Override
394 public void readFields(DataInput in) throws IOException {
395 super.readFields(in);
396 byte[] tableBytes = Bytes.readByteArray(in);
397 table = TableName.valueOf(tableBytes);
398 if (in.readBoolean()) {
399 family = Bytes.readByteArray(in);
400 }
401 if (in.readBoolean()) {
402 qualifier = Bytes.readByteArray(in);
403 }
404 if(in.readBoolean()) {
405 namespace = Bytes.toString(Bytes.readByteArray(in));
406 }
407 }
408
409 @Override
410 public void write(DataOutput out) throws IOException {
411 super.write(out);
412 Bytes.writeByteArray(out, table.getName());
413 out.writeBoolean(family != null);
414 if (family != null) {
415 Bytes.writeByteArray(out, family);
416 }
417 out.writeBoolean(qualifier != null);
418 if (qualifier != null) {
419 Bytes.writeByteArray(out, qualifier);
420 }
421 out.writeBoolean(namespace != null);
422 if(namespace != null) {
423 Bytes.writeByteArray(out, Bytes.toBytes(namespace));
424 }
425 }
426 }