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;
}
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");
}
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);
}
}
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);
}
}
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));
}
Aggregations