Search in sources :

Example 6 with ResourcePatternFilter

use of org.apache.kafka.common.resource.ResourcePatternFilter 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 7 with ResourcePatternFilter

use of org.apache.kafka.common.resource.ResourcePatternFilter in project kafka by apache.

the class AclControlManagerTest method testValidateFilter.

/**
 * Verify that validateFilter catches invalid filters.
 */
@Test
public void testValidateFilter() {
    AclControlManager.validateFilter(new AclBindingFilter(new ResourcePatternFilter(ResourceType.ANY, "*", LITERAL), new AccessControlEntryFilter("User:*", "*", AclOperation.ANY, AclPermissionType.ANY)));
    assertEquals("Unknown patternFilter.", assertThrows(InvalidRequestException.class, () -> AclControlManager.validateFilter(new AclBindingFilter(new ResourcePatternFilter(ResourceType.ANY, "*", PatternType.UNKNOWN), new AccessControlEntryFilter("User:*", "*", AclOperation.ANY, AclPermissionType.ANY)))).getMessage());
    assertEquals("Unknown entryFilter.", assertThrows(InvalidRequestException.class, () -> AclControlManager.validateFilter(new AclBindingFilter(new ResourcePatternFilter(ResourceType.ANY, "*", MATCH), new AccessControlEntryFilter("User:*", "*", AclOperation.ANY, AclPermissionType.UNKNOWN)))).getMessage());
}
Also used : AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) ResourcePatternFilter(org.apache.kafka.common.resource.ResourcePatternFilter) AccessControlEntryFilter(org.apache.kafka.common.acl.AccessControlEntryFilter) StandardAclWithIdTest(org.apache.kafka.metadata.authorizer.StandardAclWithIdTest) StandardAclTest(org.apache.kafka.metadata.authorizer.StandardAclTest) Test(org.junit.jupiter.api.Test)

Example 8 with ResourcePatternFilter

use of org.apache.kafka.common.resource.ResourcePatternFilter in project kafka by apache.

the class DescribeAclsRequest method filter.

public AclBindingFilter filter() {
    ResourcePatternFilter rpf = new ResourcePatternFilter(ResourceType.fromCode(data.resourceTypeFilter()), data.resourceNameFilter(), PatternType.fromCode(data.patternTypeFilter()));
    AccessControlEntryFilter acef = new AccessControlEntryFilter(data.principalFilter(), data.hostFilter(), AclOperation.fromCode(data.operation()), AclPermissionType.fromCode(data.permissionType()));
    return new AclBindingFilter(rpf, acef);
}
Also used : AclBindingFilter(org.apache.kafka.common.acl.AclBindingFilter) ResourcePatternFilter(org.apache.kafka.common.resource.ResourcePatternFilter) AccessControlEntryFilter(org.apache.kafka.common.acl.AccessControlEntryFilter)

Aggregations

AclBindingFilter (org.apache.kafka.common.acl.AclBindingFilter)8 ResourcePatternFilter (org.apache.kafka.common.resource.ResourcePatternFilter)8 Test (org.junit.jupiter.api.Test)5 AccessControlEntryFilter (org.apache.kafka.common.acl.AccessControlEntryFilter)3 HashSet (java.util.HashSet)2 AclBinding (org.apache.kafka.common.acl.AclBinding)2 ResourcePattern (org.apache.kafka.common.resource.ResourcePattern)2 StandardAclTest (org.apache.kafka.metadata.authorizer.StandardAclTest)2 StandardAclWithIdTest (org.apache.kafka.metadata.authorizer.StandardAclWithIdTest)2 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Endpoint (org.apache.kafka.common.Endpoint)1 AccessControlEntry (org.apache.kafka.common.acl.AccessControlEntry)1 InvalidRequestException (org.apache.kafka.common.errors.InvalidRequestException)1 PatternType (org.apache.kafka.common.resource.PatternType)1 KafkaPrincipal (org.apache.kafka.common.security.auth.KafkaPrincipal)1 LogContext (org.apache.kafka.common.utils.LogContext)1