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 boolean hasFamily() {
161 return family != null;
162 }
163
164 public byte[] getFamily() {
165 return family;
166 }
167
168 public boolean hasQualifier() {
169 return qualifier != null;
170 }
171
172 public byte[] getQualifier() {
173 return qualifier;
174 }
175
176 public boolean hasNamespace() {
177 return namespace != null;
178 }
179
180 public String getNamespace() {
181 return namespace;
182 }
183
184
185
186
187
188
189
190
191
192
193 public boolean implies(String namespace, Action action) {
194 if (!this.namespace.equals(namespace)) {
195 return false;
196 }
197
198
199 return super.implies(action);
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public boolean implies(TableName table, byte[] family, byte[] qualifier,
216 Action action) {
217 if (!this.table.equals(table)) {
218 return false;
219 }
220
221 if (this.family != null &&
222 (family == null ||
223 !Bytes.equals(this.family, family))) {
224 return false;
225 }
226
227 if (this.qualifier != null &&
228 (qualifier == null ||
229 !Bytes.equals(this.qualifier, qualifier))) {
230 return false;
231 }
232
233
234 return super.implies(action);
235 }
236
237
238
239
240
241
242
243
244
245
246 public boolean implies(TableName table, KeyValue kv, Action action) {
247 if (!this.table.equals(table)) {
248 return false;
249 }
250
251 if (family != null &&
252 (Bytes.compareTo(family, 0, family.length,
253 kv.getBuffer(), kv.getFamilyOffset(), kv.getFamilyLength()) != 0)) {
254 return false;
255 }
256
257 if (qualifier != null &&
258 (Bytes.compareTo(qualifier, 0, qualifier.length,
259 kv.getBuffer(), kv.getQualifierOffset(), kv.getQualifierLength()) != 0)) {
260 return false;
261 }
262
263
264 return super.implies(action);
265 }
266
267
268
269
270
271
272
273
274
275 public boolean matchesFamily(TableName table, byte[] family, Action action) {
276 if (!this.table.equals(table)) {
277 return false;
278 }
279
280 if (this.family != null &&
281 (family == null ||
282 !Bytes.equals(this.family, family))) {
283 return false;
284 }
285
286
287
288 return super.implies(action);
289 }
290
291
292
293
294
295
296
297
298
299
300 public boolean matchesFamilyQualifier(TableName table, byte[] family, byte[] qualifier,
301 Action action) {
302 if (!matchesFamily(table, family, action)) {
303 return false;
304 } else {
305 if (this.qualifier != null &&
306 (qualifier == null ||
307 !Bytes.equals(this.qualifier, qualifier))) {
308 return false;
309 }
310 }
311 return super.implies(action);
312 }
313
314 @Override
315 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
316 justification="Passed on construction except on constructor not to be used")
317 public boolean equals(Object obj) {
318 if (!(obj instanceof TablePermission)) {
319 return false;
320 }
321 TablePermission other = (TablePermission)obj;
322
323 if (!(table.equals(other.getTableName()) &&
324 ((family == null && other.getFamily() == null) ||
325 Bytes.equals(family, other.getFamily())) &&
326 ((qualifier == null && other.getQualifier() == null) ||
327 Bytes.equals(qualifier, other.getQualifier())) &&
328 ((namespace == null && other.getNamespace() == null) ||
329 (namespace != null && namespace.equals(other.getNamespace())))
330 )) {
331 return false;
332 }
333
334
335 return super.equals(other);
336 }
337
338 @Override
339 public int hashCode() {
340 final int prime = 37;
341 int result = super.hashCode();
342 if (table != null) {
343 result = prime * result + table.hashCode();
344 }
345 if (family != null) {
346 result = prime * result + Bytes.hashCode(family);
347 }
348 if (qualifier != null) {
349 result = prime * result + Bytes.hashCode(qualifier);
350 }
351 if (namespace != null) {
352 result = prime * result + namespace.hashCode();
353 }
354 return result;
355 }
356
357 public String toString() {
358 StringBuilder str = new StringBuilder("[TablePermission: ");
359 if(namespace != null) {
360 str.append("namespace=").append(namespace)
361 .append(", ");
362 }
363 else if(table != null) {
364 str.append("table=").append(table)
365 .append(", family=")
366 .append(family == null ? null : Bytes.toString(family))
367 .append(", qualifier=")
368 .append(qualifier == null ? null : Bytes.toString(qualifier))
369 .append(", ");
370 } else {
371 str.append("actions=");
372 }
373 if (actions != null) {
374 for (int i=0; i<actions.length; i++) {
375 if (i > 0)
376 str.append(",");
377 if (actions[i] != null)
378 str.append(actions[i].toString());
379 else
380 str.append("NULL");
381 }
382 }
383 str.append("]");
384
385 return str.toString();
386 }
387
388 @Override
389 public void readFields(DataInput in) throws IOException {
390 super.readFields(in);
391 byte[] tableBytes = Bytes.readByteArray(in);
392 table = TableName.valueOf(tableBytes);
393 if (in.readBoolean()) {
394 family = Bytes.readByteArray(in);
395 }
396 if (in.readBoolean()) {
397 qualifier = Bytes.readByteArray(in);
398 }
399 if(in.readBoolean()) {
400 namespace = Bytes.toString(Bytes.readByteArray(in));
401 }
402 }
403
404 @Override
405 public void write(DataOutput out) throws IOException {
406 super.write(out);
407 Bytes.writeByteArray(out, table.getName());
408 out.writeBoolean(family != null);
409 if (family != null) {
410 Bytes.writeByteArray(out, family);
411 }
412 out.writeBoolean(qualifier != null);
413 if (qualifier != null) {
414 Bytes.writeByteArray(out, qualifier);
415 }
416 out.writeBoolean(namespace != null);
417 if(namespace != null) {
418 Bytes.writeByteArray(out, Bytes.toBytes(namespace));
419 }
420 }
421 }