1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.mapreduce;
19
20 import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_FAMILY;
21 import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_NAME;
22 import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABEL_QUALIFIER;
23
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.TableNotFoundException;
33 import org.apache.hadoop.hbase.Tag;
34 import org.apache.hadoop.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.hbase.client.Connection;
36 import org.apache.hadoop.hbase.client.ConnectionFactory;
37 import org.apache.hadoop.hbase.client.Result;
38 import org.apache.hadoop.hbase.client.ResultScanner;
39 import org.apache.hadoop.hbase.client.Scan;
40 import org.apache.hadoop.hbase.client.Table;
41 import org.apache.hadoop.hbase.security.visibility.Authorizations;
42 import org.apache.hadoop.hbase.security.visibility.VisibilityLabelOrdinalProvider;
43 import org.apache.hadoop.hbase.security.visibility.VisibilityUtils;
44 import org.apache.hadoop.hbase.util.Bytes;
45
46
47
48
49
50 @InterfaceAudience.Private
51 public class DefaultVisibilityExpressionResolver implements VisibilityExpressionResolver {
52 private static final Log LOG = LogFactory.getLog(DefaultVisibilityExpressionResolver.class);
53
54 private Configuration conf;
55 private final Map<String, Integer> labels = new HashMap<String, Integer>();
56
57 @Override
58 public Configuration getConf() {
59 return this.conf;
60 }
61
62 @Override
63 public void setConf(Configuration conf) {
64 this.conf = conf;
65 }
66
67 @Override
68 public void init() {
69
70
71 Table labelsTable = null;
72 Connection connection = null;
73 try {
74 connection = ConnectionFactory.createConnection(conf);
75 try {
76 labelsTable = connection.getTable(LABELS_TABLE_NAME);
77 } catch (TableNotFoundException e) {
78
79
80 return;
81 } catch (IOException e) {
82 LOG.error("Error opening 'labels' table", e);
83 return;
84 }
85 Scan scan = new Scan();
86 scan.setAuthorizations(new Authorizations(VisibilityUtils.SYSTEM_LABEL));
87 scan.addColumn(LABELS_TABLE_FAMILY, LABEL_QUALIFIER);
88 ResultScanner scanner = null;
89 try {
90 scanner = labelsTable.getScanner(scan);
91 Result next = null;
92 while ((next = scanner.next()) != null) {
93 byte[] row = next.getRow();
94 byte[] value = next.getValue(LABELS_TABLE_FAMILY, LABEL_QUALIFIER);
95 labels.put(Bytes.toString(value), Bytes.toInt(row));
96 }
97 } catch (IOException e) {
98 LOG.error("Error scanning 'labels' table", e);
99 } finally {
100 if (scanner != null) scanner.close();
101 }
102 } catch (IOException ioe) {
103 LOG.error("Failed reading 'labels' tags", ioe);
104 return;
105 } finally {
106 if (labelsTable != null) {
107 try {
108 labelsTable.close();
109 } catch (IOException ioe) {
110 LOG.warn("Error closing 'labels' table", ioe);
111 }
112 }
113 if (connection != null)
114 try {
115 connection.close();
116 } catch (IOException ioe) {
117 LOG.warn("Failed close of temporary connection", ioe);
118 }
119 }
120 }
121
122 @Override
123 public List<Tag> createVisibilityExpTags(String visExpression) throws IOException {
124 VisibilityLabelOrdinalProvider provider = new VisibilityLabelOrdinalProvider() {
125 @Override
126 public int getLabelOrdinal(String label) {
127 return labels.get(label);
128 }
129
130 @Override
131 public String getLabel(int ordinal) {
132
133 throw new UnsupportedOperationException(
134 "getLabel should not be used in VisibilityExpressionResolver");
135 }
136 };
137 return VisibilityUtils.createVisibilityExpTags(visExpression, true, false, null, provider);
138 }
139 }