Search in sources :

Example 16 with AclBinding

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

the class DescribeAclsResponse method toStruct.

@Override
protected Struct toStruct(short version) {
    Struct struct = new Struct(ApiKeys.DESCRIBE_ACLS.responseSchema(version));
    struct.set(THROTTLE_TIME_MS, throttleTimeMs);
    error.write(struct);
    Map<Resource, List<AccessControlEntry>> resourceToData = new HashMap<>();
    for (AclBinding acl : acls) {
        List<AccessControlEntry> entry = resourceToData.get(acl.resource());
        if (entry == null) {
            entry = new ArrayList<>();
            resourceToData.put(acl.resource(), entry);
        }
        entry.add(acl.entry());
    }
    List<Struct> resourceStructs = new ArrayList<>();
    for (Map.Entry<Resource, List<AccessControlEntry>> tuple : resourceToData.entrySet()) {
        Resource resource = tuple.getKey();
        Struct resourceStruct = struct.instance(RESOURCES_KEY_NAME);
        RequestUtils.resourceSetStructFields(resource, resourceStruct);
        List<Struct> dataStructs = new ArrayList<>();
        for (AccessControlEntry entry : tuple.getValue()) {
            Struct dataStruct = resourceStruct.instance(ACLS_KEY_NAME);
            RequestUtils.aceSetStructFields(entry, dataStruct);
            dataStructs.add(dataStruct);
        }
        resourceStruct.set(ACLS_KEY_NAME, dataStructs.toArray());
        resourceStructs.add(resourceStruct);
    }
    struct.set(RESOURCES_KEY_NAME, resourceStructs.toArray());
    return struct;
}
Also used : HashMap(java.util.HashMap) Resource(org.apache.kafka.common.resource.Resource) ArrayList(java.util.ArrayList) AccessControlEntry(org.apache.kafka.common.acl.AccessControlEntry) Struct(org.apache.kafka.common.protocol.types.Struct) ArrayList(java.util.ArrayList) List(java.util.List) AclBinding(org.apache.kafka.common.acl.AclBinding) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with AclBinding

use of org.apache.kafka.common.acl.AclBinding in project kafka by apache.

the class MirrorSourceConnectorTest method testAclFiltering.

@Test
public void testAclFiltering() {
    MirrorSourceConnector connector = new MirrorSourceConnector(new SourceAndTarget("source", "target"), new DefaultReplicationPolicy(), x -> true, x -> true);
    assertFalse(connector.shouldReplicateAcl(new AclBinding(new ResourcePattern(ResourceType.TOPIC, "test_topic", PatternType.LITERAL), new AccessControlEntry("kafka", "", AclOperation.WRITE, AclPermissionType.ALLOW))), "should not replicate ALLOW WRITE");
    assertTrue(connector.shouldReplicateAcl(new AclBinding(new ResourcePattern(ResourceType.TOPIC, "test_topic", PatternType.LITERAL), new AccessControlEntry("kafka", "", AclOperation.ALL, AclPermissionType.ALLOW))), "should replicate ALLOW ALL");
}
Also used : ResourcePattern(org.apache.kafka.common.resource.ResourcePattern) AccessControlEntry(org.apache.kafka.common.acl.AccessControlEntry) AclBinding(org.apache.kafka.common.acl.AclBinding) Test(org.junit.jupiter.api.Test)

Example 18 with AclBinding

use of org.apache.kafka.common.acl.AclBinding in project kafka by apache.

the class KafkaAdminClientTest method testDeleteAcls.

@Test
public void testDeleteAcls() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        // Test a call where one filter has an error.
        env.kafkaClient().prepareResponse(new DeleteAclsResponse(new DeleteAclsResponseData().setThrottleTimeMs(0).setFilterResults(asList(new DeleteAclsResponseData.DeleteAclsFilterResult().setMatchingAcls(asList(DeleteAclsResponse.matchingAcl(ACL1, ApiError.NONE), DeleteAclsResponse.matchingAcl(ACL2, ApiError.NONE))), new DeleteAclsResponseData.DeleteAclsFilterResult().setErrorCode(Errors.SECURITY_DISABLED.code()).setErrorMessage("No security"))), ApiKeys.DELETE_ACLS.latestVersion()));
        DeleteAclsResult results = env.adminClient().deleteAcls(asList(FILTER1, FILTER2));
        Map<AclBindingFilter, KafkaFuture<FilterResults>> filterResults = results.values();
        FilterResults filter1Results = filterResults.get(FILTER1).get();
        assertNull(filter1Results.values().get(0).exception());
        assertEquals(ACL1, filter1Results.values().get(0).binding());
        assertNull(filter1Results.values().get(1).exception());
        assertEquals(ACL2, filter1Results.values().get(1).binding());
        TestUtils.assertFutureError(filterResults.get(FILTER2), SecurityDisabledException.class);
        TestUtils.assertFutureError(results.all(), SecurityDisabledException.class);
        // Test a call where one deletion result has an error.
        env.kafkaClient().prepareResponse(new DeleteAclsResponse(new DeleteAclsResponseData().setThrottleTimeMs(0).setFilterResults(asList(new DeleteAclsResponseData.DeleteAclsFilterResult().setMatchingAcls(asList(DeleteAclsResponse.matchingAcl(ACL1, ApiError.NONE), new DeleteAclsResponseData.DeleteAclsMatchingAcl().setErrorCode(Errors.SECURITY_DISABLED.code()).setErrorMessage("No security").setPermissionType(AclPermissionType.ALLOW.code()).setOperation(AclOperation.ALTER.code()).setResourceType(ResourceType.CLUSTER.code()).setPatternType(FILTER2.patternFilter().patternType().code()))), new DeleteAclsResponseData.DeleteAclsFilterResult())), ApiKeys.DELETE_ACLS.latestVersion()));
        results = env.adminClient().deleteAcls(asList(FILTER1, FILTER2));
        assertTrue(results.values().get(FILTER2).get().values().isEmpty());
        TestUtils.assertFutureError(results.all(), SecurityDisabledException.class);
        // Test a call where there are no errors.
        env.kafkaClient().prepareResponse(new DeleteAclsResponse(new DeleteAclsResponseData().setThrottleTimeMs(0).setFilterResults(asList(new DeleteAclsResponseData.DeleteAclsFilterResult().setMatchingAcls(asList(DeleteAclsResponse.matchingAcl(ACL1, ApiError.NONE))), new DeleteAclsResponseData.DeleteAclsFilterResult().setMatchingAcls(asList(DeleteAclsResponse.matchingAcl(ACL2, ApiError.NONE))))), ApiKeys.DELETE_ACLS.latestVersion()));
        results = env.adminClient().deleteAcls(asList(FILTER1, FILTER2));
        Collection<AclBinding> deleted = results.all().get();
        assertCollectionIs(deleted, ACL1, ACL2);
    }
}
Also used : FilterResults(org.apache.kafka.clients.admin.DeleteAclsResult.FilterResults) AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) KafkaFuture(org.apache.kafka.common.KafkaFuture) DeleteAclsResponseData(org.apache.kafka.common.message.DeleteAclsResponseData) DeleteAclsResponse(org.apache.kafka.common.requests.DeleteAclsResponse) AclBinding(org.apache.kafka.common.acl.AclBinding) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 19 with AclBinding

use of org.apache.kafka.common.acl.AclBinding in project kafka by apache.

the class CreateAclsRequestTest method assertRequestEquals.

private static void assertRequestEquals(final CreateAclsRequest original, final CreateAclsRequest actual) {
    assertEquals(original.aclCreations().size(), actual.aclCreations().size(), "Number of Acls wrong");
    for (int idx = 0; idx != original.aclCreations().size(); ++idx) {
        final AclBinding originalBinding = CreateAclsRequest.aclBinding(original.aclCreations().get(idx));
        final AclBinding actualBinding = CreateAclsRequest.aclBinding(actual.aclCreations().get(idx));
        assertEquals(originalBinding, actualBinding);
    }
}
Also used : AclBinding(org.apache.kafka.common.acl.AclBinding)

Example 20 with AclBinding

use of org.apache.kafka.common.acl.AclBinding in project kafka by apache.

the class DescribeAclsResponseTest method testAclBindings.

@Test
public void testAclBindings() {
    final AclBinding original = new AclBinding(new ResourcePattern(ResourceType.TOPIC, "foo", PatternType.LITERAL), new AccessControlEntry("User:ANONYMOUS", "127.0.0.1", AclOperation.CREATE, AclPermissionType.ALLOW));
    final List<AclBinding> result = DescribeAclsResponse.aclBindings(Collections.singletonList(LITERAL_ACL1));
    assertEquals(1, result.size());
    assertEquals(original, result.get(0));
}
Also used : ResourcePattern(org.apache.kafka.common.resource.ResourcePattern) AccessControlEntry(org.apache.kafka.common.acl.AccessControlEntry) AclBinding(org.apache.kafka.common.acl.AclBinding) Test(org.junit.jupiter.api.Test)

Aggregations

AclBinding (org.apache.kafka.common.acl.AclBinding)28 AccessControlEntry (org.apache.kafka.common.acl.AccessControlEntry)16 ResourcePattern (org.apache.kafka.common.resource.ResourcePattern)14 ArrayList (java.util.ArrayList)11 List (java.util.List)7 Test (org.junit.jupiter.api.Test)6 HashMap (java.util.HashMap)5 AclBindingFilter (org.apache.kafka.common.acl.AclBindingFilter)5 Map (java.util.Map)4 UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)4 ApiError (org.apache.kafka.common.requests.ApiError)4 Iterator (java.util.Iterator)3 LinkedList (java.util.LinkedList)3 InvalidRequestException (org.apache.kafka.common.errors.InvalidRequestException)3 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)3 DeleteAclsResponseData (org.apache.kafka.common.message.DeleteAclsResponseData)3 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)3 Errors (org.apache.kafka.common.protocol.Errors)3 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)3 PatternType (org.apache.kafka.common.resource.PatternType)3