Search in sources :

Example 1 with AclBindingFilter

use of org.apache.kafka.common.acl.AclBindingFilter in project apache-kafka-on-k8s by banzaicloud.

the class RequestResponseTest method createDeleteAclsRequest.

private DeleteAclsRequest createDeleteAclsRequest() {
    List<AclBindingFilter> filters = new ArrayList<>();
    filters.add(new AclBindingFilter(new ResourceFilter(ResourceType.ANY, null), new AccessControlEntryFilter("User:ANONYMOUS", null, AclOperation.ANY, AclPermissionType.ANY)));
    filters.add(new AclBindingFilter(new ResourceFilter(ResourceType.ANY, null), new AccessControlEntryFilter("User:bob", null, AclOperation.ANY, AclPermissionType.ANY)));
    return new DeleteAclsRequest.Builder(filters).build();
}
Also used : ResourceFilter(org.apache.kafka.common.resource.ResourceFilter) AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) ArrayList(java.util.ArrayList) AccessControlEntryFilter(org.apache.kafka.common.acl.AccessControlEntryFilter)

Example 2 with AclBindingFilter

use of org.apache.kafka.common.acl.AclBindingFilter in project apache-kafka-on-k8s by banzaicloud.

the class KafkaAdminClient method deleteAcls.

@Override
public DeleteAclsResult deleteAcls(Collection<AclBindingFilter> filters, DeleteAclsOptions options) {
    final long now = time.milliseconds();
    final Map<AclBindingFilter, KafkaFutureImpl<FilterResults>> futures = new HashMap<>();
    final List<AclBindingFilter> filterList = new ArrayList<>();
    for (AclBindingFilter filter : filters) {
        if (futures.get(filter) == null) {
            filterList.add(filter);
            futures.put(filter, new KafkaFutureImpl<FilterResults>());
        }
    }
    runnable.call(new Call("deleteAcls", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        @Override
        AbstractRequest.Builder createRequest(int timeoutMs) {
            return new DeleteAclsRequest.Builder(filterList);
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            DeleteAclsResponse response = (DeleteAclsResponse) abstractResponse;
            List<AclFilterResponse> responses = response.responses();
            Iterator<AclFilterResponse> iter = responses.iterator();
            for (AclBindingFilter filter : filterList) {
                KafkaFutureImpl<FilterResults> future = futures.get(filter);
                if (!iter.hasNext()) {
                    future.completeExceptionally(new UnknownServerException("The broker reported no deletion result for the given filter."));
                } else {
                    AclFilterResponse deletion = iter.next();
                    if (deletion.error().isFailure()) {
                        future.completeExceptionally(deletion.error().exception());
                    } else {
                        List<FilterResult> filterResults = new ArrayList<>();
                        for (AclDeletionResult deletionResult : deletion.deletions()) {
                            filterResults.add(new FilterResult(deletionResult.acl(), deletionResult.error().exception()));
                        }
                        future.complete(new FilterResults(filterResults));
                    }
                }
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(futures.values(), throwable);
        }
    }, now);
    return new DeleteAclsResult(new HashMap<AclBindingFilter, KafkaFuture<FilterResults>>(futures));
}
Also used : HashMap(java.util.HashMap) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) DeleteAclsResponse(org.apache.kafka.common.requests.DeleteAclsResponse) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) FilterResults(org.apache.kafka.clients.admin.DeleteAclsResult.FilterResults) AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) KafkaFuture(org.apache.kafka.common.KafkaFuture) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) AclDeletionResult(org.apache.kafka.common.requests.DeleteAclsResponse.AclDeletionResult) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) DeleteAclsRequest(org.apache.kafka.common.requests.DeleteAclsRequest) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) AclFilterResponse(org.apache.kafka.common.requests.DeleteAclsResponse.AclFilterResponse) FilterResult(org.apache.kafka.clients.admin.DeleteAclsResult.FilterResult)

Example 3 with AclBindingFilter

use of org.apache.kafka.common.acl.AclBindingFilter in project apache-kafka-on-k8s by banzaicloud.

the class KafkaAdminClientTest method testDeleteAcls.

@Test
public void testDeleteAcls() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareMetadataUpdate(env.cluster(), Collections.<String>emptySet());
        env.kafkaClient().setNode(env.cluster().controller());
        // Test a call where one filter has an error.
        env.kafkaClient().prepareResponse(new DeleteAclsResponse(0, asList(new AclFilterResponse(asList(new AclDeletionResult(ACL1), new AclDeletionResult(ACL2))), new AclFilterResponse(new ApiError(Errors.SECURITY_DISABLED, "No security"), Collections.<AclDeletionResult>emptySet()))));
        DeleteAclsResult results = env.adminClient().deleteAcls(asList(FILTER1, FILTER2));
        Map<AclBindingFilter, KafkaFuture<FilterResults>> filterResults = results.values();
        FilterResults filter1Results = filterResults.get(FILTER1).get();
        assertEquals(null, filter1Results.values().get(0).exception());
        assertEquals(ACL1, filter1Results.values().get(0).binding());
        assertEquals(null, filter1Results.values().get(1).exception());
        assertEquals(ACL2, filter1Results.values().get(1).binding());
        assertFutureError(filterResults.get(FILTER2), SecurityDisabledException.class);
        assertFutureError(results.all(), SecurityDisabledException.class);
        // Test a call where one deletion result has an error.
        env.kafkaClient().prepareResponse(new DeleteAclsResponse(0, asList(new AclFilterResponse(asList(new AclDeletionResult(ACL1), new AclDeletionResult(new ApiError(Errors.SECURITY_DISABLED, "No security"), ACL2))), new AclFilterResponse(Collections.<AclDeletionResult>emptySet()))));
        results = env.adminClient().deleteAcls(asList(FILTER1, FILTER2));
        assertTrue(results.values().get(FILTER2).get().values().isEmpty());
        assertFutureError(results.all(), SecurityDisabledException.class);
        // Test a call where there are no errors.
        env.kafkaClient().prepareResponse(new DeleteAclsResponse(0, asList(new AclFilterResponse(asList(new AclDeletionResult(ACL1))), new AclFilterResponse(asList(new AclDeletionResult(ACL2))))));
        results = env.adminClient().deleteAcls(asList(FILTER1, FILTER2));
        Collection<AclBinding> deleted = results.all().get();
        assertCollectionIs(deleted, ACL1, ACL2);
    }
}
Also used : DeleteAclsResponse(org.apache.kafka.common.requests.DeleteAclsResponse) FilterResults(org.apache.kafka.clients.admin.DeleteAclsResult.FilterResults) AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) KafkaFuture(org.apache.kafka.common.KafkaFuture) AclFilterResponse(org.apache.kafka.common.requests.DeleteAclsResponse.AclFilterResponse) AclDeletionResult(org.apache.kafka.common.requests.DeleteAclsResponse.AclDeletionResult) ApiError(org.apache.kafka.common.requests.ApiError) AclBinding(org.apache.kafka.common.acl.AclBinding) Test(org.junit.Test)

Example 4 with AclBindingFilter

use of org.apache.kafka.common.acl.AclBindingFilter in project apache-kafka-on-k8s by banzaicloud.

the class DeleteAclsRequest method toStruct.

@Override
protected Struct toStruct() {
    Struct struct = new Struct(DELETE_ACLS.requestSchema(version()));
    List<Struct> filterStructs = new ArrayList<>();
    for (AclBindingFilter filter : filters) {
        Struct filterStruct = struct.instance(FILTERS);
        RequestUtils.resourceFilterSetStructFields(filter.resourceFilter(), filterStruct);
        RequestUtils.aceFilterSetStructFields(filter.entryFilter(), filterStruct);
        filterStructs.add(filterStruct);
    }
    struct.set(FILTERS, filterStructs.toArray());
    return struct;
}
Also used : AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) ArrayList(java.util.ArrayList) Struct(org.apache.kafka.common.protocol.types.Struct)

Aggregations

AclBindingFilter (org.apache.kafka.common.acl.AclBindingFilter)4 ArrayList (java.util.ArrayList)3 FilterResults (org.apache.kafka.clients.admin.DeleteAclsResult.FilterResults)2 KafkaFuture (org.apache.kafka.common.KafkaFuture)2 DeleteAclsResponse (org.apache.kafka.common.requests.DeleteAclsResponse)2 AclDeletionResult (org.apache.kafka.common.requests.DeleteAclsResponse.AclDeletionResult)2 AclFilterResponse (org.apache.kafka.common.requests.DeleteAclsResponse.AclFilterResponse)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 FilterResult (org.apache.kafka.clients.admin.DeleteAclsResult.FilterResult)1 AccessControlEntryFilter (org.apache.kafka.common.acl.AccessControlEntryFilter)1 AclBinding (org.apache.kafka.common.acl.AclBinding)1 UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)1 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)1 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)1 Struct (org.apache.kafka.common.protocol.types.Struct)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 ApiError (org.apache.kafka.common.requests.ApiError)1