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 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.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength()) != 0)) {
254 return false;
255 }
256
257 if (qualifier != null &&
258 (Bytes.compareTo(qualifier, 0, qualifier.length,
259 kv.getQualifierArray(), 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 @Override
358 public String toString() {
359 StringBuilder str = new StringBuilder("[TablePermission: ");
360 if(namespace != null) {
361 str.append("namespace=").append(namespace)
362 .append(", ");
363 }
364 else if(table != null) {
365 str.append("table=").append(table)
366 .append(", family=")
367 .append(family == null ? null : Bytes.toString(family))
368 .append(", qualifier=")
369 .append(qualifier == null ? null : Bytes.toString(qualifier))
370 .append(", ");
371 } else {
372 str.append("actions=");
373 }
374 if (actions != null) {
375 for (int i=0; i<actions.length; i++) {
376 if (i > 0)
377 str.append(",");
378 if (actions[i] != null)
379 str.append(actions[i].toString());
380 else
381 str.append("NULL");
382 }
383 }
384 str.append("]");
385
386 return str.toString();
387 }
388
389 @Override
390 public void readFields(DataInput in) throws IOException {
391 super.readFields(in);
392 byte[] tableBytes = Bytes.readByteArray(in);
393 table = TableName.valueOf(tableBytes);
394 if (in.readBoolean()) {
395 family = Bytes.readByteArray(in);
396 }
397 if (in.readBoolean()) {
398 qualifier = Bytes.readByteArray(in);
399 }
400 if(in.readBoolean()) {
401 namespace = Bytes.toString(Bytes.readByteArray(in));
402 }
403 }
404
405 @Override
406 public void write(DataOutput out) throws IOException {
407 super.write(out);
408 Bytes.writeByteArray(out, table.getName());
409 out.writeBoolean(family != null);
410 if (family != null) {
411 Bytes.writeByteArray(out, family);
412 }
413 out.writeBoolean(qualifier != null);
414 if (qualifier != null) {
415 Bytes.writeByteArray(out, qualifier);
416 }
417 out.writeBoolean(namespace != null);
418 if(namespace != null) {
419 Bytes.writeByteArray(out, Bytes.toBytes(namespace));
420 }
421 }
422 }