Search in sources :

Example 1 with ListLabelsResponse

use of org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse in project hbase by apache.

the class TestVisibilityLabelsWithDefaultVisLabelService method testListLabelsWithRegEx.

@Test
public void testListLabelsWithRegEx() throws Throwable {
    PrivilegedExceptionAction<ListLabelsResponse> action = new PrivilegedExceptionAction<ListLabelsResponse>() {

        @Override
        public ListLabelsResponse run() throws Exception {
            ListLabelsResponse response = null;
            try (Connection conn = ConnectionFactory.createConnection(conf)) {
                response = VisibilityClient.listLabels(conn, ".*secret");
            } catch (Throwable e) {
                throw new IOException(e);
            }
            // Only return the labels that end with 'secret'
            List<ByteString> labels = response.getLabelList();
            assertEquals(2, labels.size());
            assertTrue(labels.contains(ByteString.copyFrom(Bytes.toBytes(SECRET))));
            assertTrue(labels.contains(ByteString.copyFrom(Bytes.toBytes(TOPSECRET))));
            return null;
        }
    };
    SUPERUSER.runAs(action);
}
Also used : ByteString(org.apache.hbase.thirdparty.com.google.protobuf.ByteString) Connection(org.apache.hadoop.hbase.client.Connection) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) ListLabelsResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse) Test(org.junit.Test)

Example 2 with ListLabelsResponse

use of org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse in project hbase by apache.

the class VisibilityController method listLabels.

@Override
public synchronized void listLabels(RpcController controller, ListLabelsRequest request, RpcCallback<ListLabelsResponse> done) {
    ListLabelsResponse.Builder response = ListLabelsResponse.newBuilder();
    if (!initialized) {
        controller.setFailed("VisibilityController not yet initialized");
    } else {
        List<String> labels = null;
        String regex = request.hasRegex() ? request.getRegex() : null;
        try {
            // AccessController CP methods.
            if (authorizationEnabled && accessControllerAvailable && !isSystemOrSuperUser()) {
                User requestingUser = VisibilityUtils.getActiveUser();
                throw new AccessDeniedException("User '" + (requestingUser != null ? requestingUser.getShortName() : "null") + "' is not authorized to perform this action.");
            }
            labels = this.visibilityLabelService.listLabels(regex);
            logResult(true, "listLabels", "Listing labels allowed", null, null, regex);
        } catch (AccessDeniedException e) {
            logResult(false, "listLabels", e.getMessage(), null, null, regex);
            CoprocessorRpcUtils.setControllerException(controller, e);
        } catch (IOException e) {
            CoprocessorRpcUtils.setControllerException(controller, e);
        }
        if (labels != null && !labels.isEmpty()) {
            for (String label : labels) {
                response.addLabel(ByteString.copyFrom(Bytes.toBytes(label)));
            }
        }
    }
    done.run(response.build());
}
Also used : AccessDeniedException(org.apache.hadoop.hbase.security.AccessDeniedException) User(org.apache.hadoop.hbase.security.User) ByteString(org.apache.hbase.thirdparty.com.google.protobuf.ByteString) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) IOException(java.io.IOException) ListLabelsResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse)

Example 3 with ListLabelsResponse

use of org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse in project hbase by apache.

the class VisibilityClient method listLabels.

/**
 * Retrieve the list of visibility labels defined in the system.
 * @param connection The Connection instance to use.
 * @param regex  The regular expression to filter which labels are returned.
 * @return labels The list of visibility labels defined in the system.
 * @throws Throwable
 */
public static ListLabelsResponse listLabels(Connection connection, final String regex) throws Throwable {
    try (Table table = connection.getTable(LABELS_TABLE_NAME)) {
        Batch.Call<VisibilityLabelsService, ListLabelsResponse> callable = new Batch.Call<VisibilityLabelsService, ListLabelsResponse>() {

            ServerRpcController controller = new ServerRpcController();

            CoprocessorRpcUtils.BlockingRpcCallback<ListLabelsResponse> rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();

            @Override
            public ListLabelsResponse call(VisibilityLabelsService service) throws IOException {
                ListLabelsRequest.Builder listAuthLabelsReqBuilder = ListLabelsRequest.newBuilder();
                if (regex != null) {
                    // Compile the regex here to catch any regex exception earlier.
                    Pattern pattern = Pattern.compile(regex);
                    listAuthLabelsReqBuilder.setRegex(pattern.toString());
                }
                service.listLabels(controller, listAuthLabelsReqBuilder.build(), rpcCallback);
                ListLabelsResponse response = rpcCallback.get();
                if (controller.failedOnException()) {
                    throw controller.getFailedOn();
                }
                return response;
            }
        };
        Map<byte[], ListLabelsResponse> result = table.coprocessorService(VisibilityLabelsService.class, HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, callable);
        // There will be exactly one region for labels
        return result.values().iterator().next();
    // table and so one entry in result Map.
    }
}
Also used : VisibilityLabelsService(org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsService) Pattern(java.util.regex.Pattern) Table(org.apache.hadoop.hbase.client.Table) ListLabelsRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsRequest) ListLabelsResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse) ServerRpcController(org.apache.hadoop.hbase.ipc.ServerRpcController) CoprocessorRpcUtils(org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils) Batch(org.apache.hadoop.hbase.client.coprocessor.Batch)

Example 4 with ListLabelsResponse

use of org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse in project hbase by apache.

the class TestVisibilityLabelsWithDefaultVisLabelService method testListLabels.

@Test
public void testListLabels() throws Throwable {
    PrivilegedExceptionAction<ListLabelsResponse> action = new PrivilegedExceptionAction<ListLabelsResponse>() {

        @Override
        public ListLabelsResponse run() throws Exception {
            ListLabelsResponse response = null;
            try (Connection conn = ConnectionFactory.createConnection(conf)) {
                response = VisibilityClient.listLabels(conn, null);
            } catch (Throwable e) {
                throw new IOException(e);
            }
            // The addLabels() in setup added:
            // { SECRET, TOPSECRET, CONFIDENTIAL, PUBLIC, PRIVATE, COPYRIGHT, ACCENT,
            // UNICODE_VIS_TAG, UC1, UC2 };
            // The previous tests added 2 more labels: ABC, XYZ
            // The 'system' label is excluded.
            List<ByteString> labels = response.getLabelList();
            assertEquals(12, labels.size());
            assertTrue(labels.contains(ByteString.copyFrom(Bytes.toBytes(SECRET))));
            assertTrue(labels.contains(ByteString.copyFrom(Bytes.toBytes(TOPSECRET))));
            assertTrue(labels.contains(ByteString.copyFrom(Bytes.toBytes(CONFIDENTIAL))));
            assertTrue(labels.contains(ByteString.copyFrom(Bytes.toBytes("ABC"))));
            assertTrue(labels.contains(ByteString.copyFrom(Bytes.toBytes("XYZ"))));
            assertFalse(labels.contains(ByteString.copyFrom(Bytes.toBytes(SYSTEM_LABEL))));
            return null;
        }
    };
    SUPERUSER.runAs(action);
}
Also used : ByteString(org.apache.hbase.thirdparty.com.google.protobuf.ByteString) Connection(org.apache.hadoop.hbase.client.Connection) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) ListLabelsResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse) Test(org.junit.Test)

Aggregations

ListLabelsResponse (org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse)4 IOException (java.io.IOException)3 ByteString (org.apache.hbase.thirdparty.com.google.protobuf.ByteString)3 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)2 Connection (org.apache.hadoop.hbase.client.Connection)2 Test (org.junit.Test)2 Pattern (java.util.regex.Pattern)1 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)1 Table (org.apache.hadoop.hbase.client.Table)1 Batch (org.apache.hadoop.hbase.client.coprocessor.Batch)1 CoprocessorRpcUtils (org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils)1 ServerRpcController (org.apache.hadoop.hbase.ipc.ServerRpcController)1 AccessDeniedException (org.apache.hadoop.hbase.security.AccessDeniedException)1 User (org.apache.hadoop.hbase.security.User)1 ListLabelsRequest (org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.ListLabelsRequest)1 VisibilityLabelsService (org.apache.hadoop.hbase.shaded.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsService)1