1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.security.visibility;
19
20 import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_NAME;
21
22 import java.io.IOException;
23 import java.util.Map;
24
25 import org.apache.hadoop.hbase.util.ByteStringer;
26 import org.apache.hadoop.classification.InterfaceAudience;
27 import org.apache.hadoop.classification.InterfaceStability;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.client.HTable;
31 import org.apache.hadoop.hbase.client.coprocessor.Batch;
32 import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
33 import org.apache.hadoop.hbase.ipc.ServerRpcController;
34 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.GetAuthsRequest;
35 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.GetAuthsResponse;
36 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.SetAuthsRequest;
37 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabel;
38 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsRequest;
39 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse;
40 import org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsService;
41 import org.apache.hadoop.hbase.util.Bytes;
42
43 import com.google.protobuf.ServiceException;
44
45
46
47
48 @InterfaceAudience.Public
49 @InterfaceStability.Evolving
50 public class VisibilityClient {
51
52
53
54
55
56
57
58
59
60 public static VisibilityLabelsResponse addLabel(Configuration conf, final String label)
61 throws Throwable {
62 return addLabels(conf, new String[] { label });
63 }
64
65
66
67
68
69
70
71
72
73 public static VisibilityLabelsResponse addLabels(Configuration conf, final String[] labels)
74 throws Throwable {
75 HTable ht = null;
76 try {
77 ht = new HTable(conf, LABELS_TABLE_NAME.getName());
78 Batch.Call<VisibilityLabelsService, VisibilityLabelsResponse> callable =
79 new Batch.Call<VisibilityLabelsService, VisibilityLabelsResponse>() {
80 ServerRpcController controller = new ServerRpcController();
81 BlockingRpcCallback<VisibilityLabelsResponse> rpcCallback =
82 new BlockingRpcCallback<VisibilityLabelsResponse>();
83
84 public VisibilityLabelsResponse call(VisibilityLabelsService service) throws IOException {
85 VisibilityLabelsRequest.Builder builder = VisibilityLabelsRequest.newBuilder();
86 for (String label : labels) {
87 if (label.length() > 0) {
88 VisibilityLabel.Builder newBuilder = VisibilityLabel.newBuilder();
89 newBuilder.setLabel(ByteStringer.wrap(Bytes.toBytes(label)));
90 builder.addVisLabel(newBuilder.build());
91 }
92 }
93 service.addLabels(controller, builder.build(), rpcCallback);
94 return rpcCallback.get();
95 }
96 };
97 Map<byte[], VisibilityLabelsResponse> result = ht.coprocessorService(
98 VisibilityLabelsService.class, HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY,
99 callable);
100 return result.values().iterator().next();
101
102 } finally {
103 if (ht != null) {
104 ht.close();
105 }
106 }
107 }
108
109
110
111
112
113
114
115
116
117 public static VisibilityLabelsResponse setAuths(Configuration conf, final String[] auths,
118 final String user) throws Throwable {
119 return setOrClearAuths(conf, auths, user, true);
120 }
121
122
123
124
125
126
127
128 public static GetAuthsResponse getAuths(Configuration conf, final String user) throws Throwable {
129 HTable ht = null;
130 try {
131 ht = new HTable(conf, LABELS_TABLE_NAME.getName());
132 Batch.Call<VisibilityLabelsService, GetAuthsResponse> callable =
133 new Batch.Call<VisibilityLabelsService, GetAuthsResponse>() {
134 ServerRpcController controller = new ServerRpcController();
135 BlockingRpcCallback<GetAuthsResponse> rpcCallback =
136 new BlockingRpcCallback<GetAuthsResponse>();
137
138 public GetAuthsResponse call(VisibilityLabelsService service) throws IOException {
139 GetAuthsRequest.Builder getAuthReqBuilder = GetAuthsRequest.newBuilder();
140 getAuthReqBuilder.setUser(ByteStringer.wrap(Bytes.toBytes(user)));
141 service.getAuths(controller, getAuthReqBuilder.build(), rpcCallback);
142 return rpcCallback.get();
143 }
144 };
145 Map<byte[], GetAuthsResponse> result = ht.coprocessorService(VisibilityLabelsService.class,
146 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, callable);
147 return result.values().iterator().next();
148
149 } finally {
150 if (ht != null) {
151 ht.close();
152 }
153 }
154 }
155
156
157
158
159
160
161
162
163
164 public static VisibilityLabelsResponse clearAuths(Configuration conf, final String[] auths,
165 final String user) throws Throwable {
166 return setOrClearAuths(conf, auths, user, false);
167 }
168
169 private static VisibilityLabelsResponse setOrClearAuths(Configuration conf, final String[] auths,
170 final String user, final boolean setOrClear) throws IOException, ServiceException, Throwable {
171 HTable ht = null;
172 try {
173 ht = new HTable(conf, LABELS_TABLE_NAME.getName());
174 Batch.Call<VisibilityLabelsService, VisibilityLabelsResponse> callable =
175 new Batch.Call<VisibilityLabelsService, VisibilityLabelsResponse>() {
176 ServerRpcController controller = new ServerRpcController();
177 BlockingRpcCallback<VisibilityLabelsResponse> rpcCallback =
178 new BlockingRpcCallback<VisibilityLabelsResponse>();
179
180 public VisibilityLabelsResponse call(VisibilityLabelsService service) throws IOException {
181 SetAuthsRequest.Builder setAuthReqBuilder = SetAuthsRequest.newBuilder();
182 setAuthReqBuilder.setUser(ByteStringer.wrap(Bytes.toBytes(user)));
183 for (String auth : auths) {
184 if (auth.length() > 0) {
185 setAuthReqBuilder.addAuth(ByteStringer.wrap(Bytes.toBytes(auth)));
186 }
187 }
188 if (setOrClear) {
189 service.setAuths(controller, setAuthReqBuilder.build(), rpcCallback);
190 } else {
191 service.clearAuths(controller, setAuthReqBuilder.build(), rpcCallback);
192 }
193 return rpcCallback.get();
194 }
195 };
196 Map<byte[], VisibilityLabelsResponse> result = ht.coprocessorService(
197 VisibilityLabelsService.class, HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY,
198 callable);
199 return result.values().iterator().next();
200
201 } finally {
202 if (ht != null) {
203 ht.close();
204 }
205 }
206 }
207 }