Search in sources :

Example 1 with ListLabelsResponse

use of org.apache.hadoop.hbase.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(false, "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(ByteStringer.wrap(Bytes.toBytes(label)));
            }
        }
    }
    done.run(response.build());
}
Also used : AccessDeniedException(org.apache.hadoop.hbase.security.AccessDeniedException) User(org.apache.hadoop.hbase.security.User) ByteString(com.google.protobuf.ByteString) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) IOException(java.io.IOException) ListLabelsResponse(org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse)

Example 2 with ListLabelsResponse

use of org.apache.hadoop.hbase.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>() {

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

Example 3 with ListLabelsResponse

use of org.apache.hadoop.hbase.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>() {

        public ListLabelsResponse run() throws Exception {
            ListLabelsResponse response = null;
            try (Connection conn = ConnectionFactory.createConnection(conf)) {
                response = VisibilityClient.listLabels(conn, null);
            } catch (Throwable e) {
                fail("Should not have thrown exception");
            }
            // 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(SECRET.getBytes())));
            assertTrue(labels.contains(ByteString.copyFrom(TOPSECRET.getBytes())));
            assertTrue(labels.contains(ByteString.copyFrom(CONFIDENTIAL.getBytes())));
            assertTrue(labels.contains(ByteString.copyFrom("ABC".getBytes())));
            assertTrue(labels.contains(ByteString.copyFrom("XYZ".getBytes())));
            assertFalse(labels.contains(ByteString.copyFrom(SYSTEM_LABEL.getBytes())));
            return null;
        }
    };
    SUPERUSER.runAs(action);
}
Also used : ByteString(com.google.protobuf.ByteString) Connection(org.apache.hadoop.hbase.client.Connection) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) ListLabelsResponse(org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse) Test(org.junit.Test)

Example 4 with ListLabelsResponse

use of org.apache.hadoop.hbase.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 {
    Table table = null;
    try {
        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<>();

            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.
    } finally {
        if (table != null) {
            table.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : VisibilityLabelsService(org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsService) Pattern(java.util.regex.Pattern) Table(org.apache.hadoop.hbase.client.Table) ListLabelsRequest(org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.ListLabelsRequest) ListLabelsResponse(org.apache.hadoop.hbase.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)

Aggregations

ListLabelsResponse (org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.ListLabelsResponse)4 ByteString (com.google.protobuf.ByteString)3 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)2 Connection (org.apache.hadoop.hbase.client.Connection)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 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 ListLabelsRequest (org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.ListLabelsRequest)1 VisibilityLabelsService (org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsService)1 AccessDeniedException (org.apache.hadoop.hbase.security.AccessDeniedException)1 User (org.apache.hadoop.hbase.security.User)1