Search in sources :

Example 21 with AclBinding

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

the class AclControlManagerTest method testCreateAclDeleteAcl.

@Test
public void testCreateAclDeleteAcl() {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    AclControlManager manager = new AclControlManager(snapshotRegistry, Optional.empty());
    MockClusterMetadataAuthorizer authorizer = new MockClusterMetadataAuthorizer();
    authorizer.loadSnapshot(manager.idToAcl());
    List<AclBinding> toCreate = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        toCreate.add(TEST_ACLS.get(i).toBinding());
    }
    toCreate.add(new AclBinding(new ResourcePattern(TOPIC, "*", PatternType.UNKNOWN), new AccessControlEntry("User:*", "*", ALTER, ALLOW)));
    ControllerResult<List<AclCreateResult>> createResult = manager.createAcls(toCreate);
    List<AclCreateResult> expectedResults = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        expectedResults.add(AclCreateResult.SUCCESS);
    }
    expectedResults.add(new AclCreateResult(new InvalidRequestException("Invalid patternType UNKNOWN")));
    for (int i = 0; i < expectedResults.size(); i++) {
        AclCreateResult expectedResult = expectedResults.get(i);
        if (expectedResult.exception().isPresent()) {
            assertEquals(expectedResult.exception().get().getMessage(), createResult.response().get(i).exception().get().getMessage());
        } else {
            assertFalse(createResult.response().get(i).exception().isPresent());
        }
    }
    RecordTestUtils.replayAll(manager, createResult.records());
    assertTrue(manager.iterator(Long.MAX_VALUE).hasNext());
    ControllerResult<List<AclDeleteResult>> deleteResult = manager.deleteAcls(Arrays.asList(new AclBindingFilter(new ResourcePatternFilter(ResourceType.ANY, null, LITERAL), AccessControlEntryFilter.ANY), new AclBindingFilter(new ResourcePatternFilter(ResourceType.UNKNOWN, null, LITERAL), AccessControlEntryFilter.ANY)));
    assertEquals(2, deleteResult.response().size());
    Set<AclBinding> deleted = new HashSet<>();
    for (AclDeleteResult.AclBindingDeleteResult result : deleteResult.response().get(0).aclBindingDeleteResults()) {
        assertEquals(Optional.empty(), result.exception());
        deleted.add(result.aclBinding());
    }
    assertEquals(new HashSet<>(Arrays.asList(TEST_ACLS.get(0).toBinding(), TEST_ACLS.get(2).toBinding())), deleted);
    assertEquals(InvalidRequestException.class, deleteResult.response().get(1).exception().get().getClass());
    RecordTestUtils.replayAll(manager, deleteResult.records());
    Iterator<List<ApiMessageAndVersion>> iterator = manager.iterator(Long.MAX_VALUE);
    assertTrue(iterator.hasNext());
    List<ApiMessageAndVersion> list = iterator.next();
    assertEquals(1, list.size());
    assertEquals(TEST_ACLS.get(1).toBinding(), StandardAcl.fromRecord((AccessControlEntryRecord) list.get(0).message()).toBinding());
    assertFalse(iterator.hasNext());
}
Also used : ResourcePatternFilter(org.apache.kafka.common.resource.ResourcePatternFilter) ResourcePattern(org.apache.kafka.common.resource.ResourcePattern) ArrayList(java.util.ArrayList) AclDeleteResult(org.apache.kafka.server.authorizer.AclDeleteResult) AclCreateResult(org.apache.kafka.server.authorizer.AclCreateResult) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) List(java.util.List) ArrayList(java.util.ArrayList) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) AclBinding(org.apache.kafka.common.acl.AclBinding) HashSet(java.util.HashSet) AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) LogContext(org.apache.kafka.common.utils.LogContext) AccessControlEntry(org.apache.kafka.common.acl.AccessControlEntry) Endpoint(org.apache.kafka.common.Endpoint) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) StandardAclWithIdTest(org.apache.kafka.metadata.authorizer.StandardAclWithIdTest) StandardAclTest(org.apache.kafka.metadata.authorizer.StandardAclTest) Test(org.junit.jupiter.api.Test)

Example 22 with AclBinding

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

the class AclControlManagerTest method testValidateNewAcl.

/**
 * Verify that validateNewAcl catches invalid ACLs.
 */
@Test
public void testValidateNewAcl() {
    AclControlManager.validateNewAcl(new AclBinding(new ResourcePattern(TOPIC, "*", LITERAL), new AccessControlEntry("User:*", "*", ALTER, ALLOW)));
    assertEquals("Invalid patternType UNKNOWN", assertThrows(InvalidRequestException.class, () -> AclControlManager.validateNewAcl(new AclBinding(new ResourcePattern(TOPIC, "*", PatternType.UNKNOWN), new AccessControlEntry("User:*", "*", ALTER, ALLOW)))).getMessage());
    assertEquals("Invalid resourceType UNKNOWN", assertThrows(InvalidRequestException.class, () -> AclControlManager.validateNewAcl(new AclBinding(new ResourcePattern(ResourceType.UNKNOWN, "*", LITERAL), new AccessControlEntry("User:*", "*", ALTER, ALLOW)))).getMessage());
    assertEquals("Invalid operation UNKNOWN", assertThrows(InvalidRequestException.class, () -> AclControlManager.validateNewAcl(new AclBinding(new ResourcePattern(TOPIC, "*", LITERAL), new AccessControlEntry("User:*", "*", AclOperation.UNKNOWN, ALLOW)))).getMessage());
    assertEquals("Invalid permissionType UNKNOWN", assertThrows(InvalidRequestException.class, () -> AclControlManager.validateNewAcl(new AclBinding(new ResourcePattern(TOPIC, "*", LITERAL), new AccessControlEntry("User:*", "*", ALTER, AclPermissionType.UNKNOWN)))).getMessage());
}
Also used : ResourcePattern(org.apache.kafka.common.resource.ResourcePattern) AccessControlEntry(org.apache.kafka.common.acl.AccessControlEntry) AclBinding(org.apache.kafka.common.acl.AclBinding) StandardAclWithIdTest(org.apache.kafka.metadata.authorizer.StandardAclWithIdTest) StandardAclTest(org.apache.kafka.metadata.authorizer.StandardAclTest) Test(org.junit.jupiter.api.Test)

Example 23 with AclBinding

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

the class StandardAclTest method testToBindingRoundTrips.

@Test
public void testToBindingRoundTrips() {
    for (StandardAcl acl : TEST_ACLS) {
        AclBinding binding = acl.toBinding();
        StandardAcl acl2 = StandardAcl.fromAclBinding(binding);
        assertEquals(acl2, acl);
    }
}
Also used : AclBinding(org.apache.kafka.common.acl.AclBinding) Test(org.junit.jupiter.api.Test)

Example 24 with AclBinding

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

the class StandardAuthorizerTest method assertContains.

private static void assertContains(Iterable<AclBinding> iterable, StandardAcl... acls) {
    Iterator<AclBinding> iterator = iterable.iterator();
    for (int i = 0; iterator.hasNext(); i++) {
        AclBinding acl = iterator.next();
        assertTrue(i < acls.length, "Only expected " + i + " element(s)");
        assertEquals(acls[i].toBinding(), acl, "Unexpected element " + i);
    }
    assertFalse(iterator.hasNext(), "Expected only " + acls.length + " element(s)");
}
Also used : AclBinding(org.apache.kafka.common.acl.AclBinding)

Example 25 with AclBinding

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

the class CreateAclsRequest method aclBinding.

public static AclBinding aclBinding(AclCreation acl) {
    ResourcePattern pattern = new ResourcePattern(ResourceType.fromCode(acl.resourceType()), acl.resourceName(), PatternType.fromCode(acl.resourcePatternType()));
    AccessControlEntry entry = new AccessControlEntry(acl.principal(), acl.host(), AclOperation.fromCode(acl.operation()), AclPermissionType.fromCode(acl.permissionType()));
    return new AclBinding(pattern, entry);
}
Also used : ResourcePattern(org.apache.kafka.common.resource.ResourcePattern) AccessControlEntry(org.apache.kafka.common.acl.AccessControlEntry) AclBinding(org.apache.kafka.common.acl.AclBinding)

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