Search in sources :

Example 11 with InvalidRequestException

use of org.apache.kafka.common.errors.InvalidRequestException in project kafka by apache.

the class ClusterMetadataAuthorizerTest method testCreateAcls.

@Test
public void testCreateAcls() throws Exception {
    MockAclMutator mutator = new MockAclMutator();
    MockClusterMetadataAuthorizer authorizer = new MockClusterMetadataAuthorizer();
    authorizer.setAclMutator(mutator);
    CompletableFuture<List<AclCreateResult>> response = new CompletableFuture<>();
    response.complete(Arrays.asList(AclCreateResult.SUCCESS, new AclCreateResult(new InvalidRequestException("invalid"))));
    mutator.setCreateAclsResponse(response);
    List<? extends CompletionStage<AclCreateResult>> results = authorizer.createAcls(new MockAuthorizableRequestContext.Builder().build(), TEST_BINDINGS);
    assertEquals(2, results.size());
    assertEquals(Optional.empty(), results.get(0).toCompletableFuture().get().exception());
    assertEquals(InvalidRequestException.class, results.get(1).toCompletableFuture().get().exception().get().getClass());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) AclCreateResult(org.apache.kafka.server.authorizer.AclCreateResult) Test(org.junit.jupiter.api.Test)

Example 12 with InvalidRequestException

use of org.apache.kafka.common.errors.InvalidRequestException in project kafka by apache.

the class ClientQuotasImage method describe.

public DescribeClientQuotasResponseData describe(DescribeClientQuotasRequestData request) {
    DescribeClientQuotasResponseData response = new DescribeClientQuotasResponseData();
    Map<String, String> exactMatch = new HashMap<>();
    Set<String> typeMatch = new HashSet<>();
    for (DescribeClientQuotasRequestData.ComponentData component : request.components()) {
        if (component.entityType().isEmpty()) {
            throw new InvalidRequestException("Invalid empty entity type.");
        } else if (exactMatch.containsKey(component.entityType()) || typeMatch.contains(component.entityType())) {
            throw new InvalidRequestException("Entity type " + component.entityType() + " cannot appear more than once in the filter.");
        }
        if (!(component.entityType().equals(IP) || component.entityType().equals(USER) || component.entityType().equals(CLIENT_ID))) {
            throw new UnsupportedVersionException("Unsupported entity type " + component.entityType());
        }
        switch(component.matchType()) {
            case MATCH_TYPE_EXACT:
                if (component.match() == null) {
                    throw new InvalidRequestException("Request specified " + "MATCH_TYPE_EXACT, but set match string to null.");
                }
                exactMatch.put(component.entityType(), component.match());
                break;
            case MATCH_TYPE_DEFAULT:
                if (component.match() != null) {
                    throw new InvalidRequestException("Request specified " + "MATCH_TYPE_DEFAULT, but also specified a match string.");
                }
                exactMatch.put(component.entityType(), null);
                break;
            case MATCH_TYPE_SPECIFIED:
                if (component.match() != null) {
                    throw new InvalidRequestException("Request specified " + "MATCH_TYPE_SPECIFIED, but also specified a match string.");
                }
                typeMatch.add(component.entityType());
                break;
            default:
                throw new InvalidRequestException("Unknown match type " + component.matchType());
        }
    }
    if (exactMatch.containsKey(IP) || typeMatch.contains(IP)) {
        if ((exactMatch.containsKey(USER) || typeMatch.contains(USER)) || (exactMatch.containsKey(CLIENT_ID) || typeMatch.contains(CLIENT_ID))) {
            throw new InvalidRequestException("Invalid entity filter component " + "combination. IP filter component should not be used with " + "user or clientId filter component.");
        }
    }
    // TODO: this is O(N). We should add indexing here to speed it up. See KAFKA-13022.
    for (Entry<ClientQuotaEntity, ClientQuotaImage> entry : entities.entrySet()) {
        ClientQuotaEntity entity = entry.getKey();
        ClientQuotaImage quotaImage = entry.getValue();
        if (matches(entity, exactMatch, typeMatch, request.strict())) {
            response.entries().add(toDescribeEntry(entity, quotaImage));
        }
    }
    return response;
}
Also used : HashMap(java.util.HashMap) DescribeClientQuotasRequestData(org.apache.kafka.common.message.DescribeClientQuotasRequestData) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) DescribeClientQuotasResponseData(org.apache.kafka.common.message.DescribeClientQuotasResponseData) HashSet(java.util.HashSet) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 13 with InvalidRequestException

use of org.apache.kafka.common.errors.InvalidRequestException in project kafka by apache.

the class KafkaAdminClient method describeAcls.

@Override
public DescribeAclsResult describeAcls(final AclBindingFilter filter, DescribeAclsOptions options) {
    if (filter.isUnknown()) {
        KafkaFutureImpl<Collection<AclBinding>> future = new KafkaFutureImpl<>();
        future.completeExceptionally(new InvalidRequestException("The AclBindingFilter " + "must not contain UNKNOWN elements."));
        return new DescribeAclsResult(future);
    }
    final long now = time.milliseconds();
    final KafkaFutureImpl<Collection<AclBinding>> future = new KafkaFutureImpl<>();
    runnable.call(new Call("describeAcls", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        @Override
        DescribeAclsRequest.Builder createRequest(int timeoutMs) {
            return new DescribeAclsRequest.Builder(filter);
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            DescribeAclsResponse response = (DescribeAclsResponse) abstractResponse;
            if (response.error().isFailure()) {
                future.completeExceptionally(response.error().exception());
            } else {
                future.complete(DescribeAclsResponse.aclBindings(response.acls()));
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            future.completeExceptionally(throwable);
        }
    }, now);
    return new DescribeAclsResult(future);
}
Also used : DescribeAclsRequest(org.apache.kafka.common.requests.DescribeAclsRequest) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) DescribeAclsResponse(org.apache.kafka.common.requests.DescribeAclsResponse) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TopicNameCollection(org.apache.kafka.common.TopicCollection.TopicNameCollection) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) TopicIdCollection(org.apache.kafka.common.TopicCollection.TopicIdCollection) Collection(java.util.Collection) TopicCollection(org.apache.kafka.common.TopicCollection) CreatePartitionsTopicCollection(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopicCollection) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException)

Aggregations

InvalidRequestException (org.apache.kafka.common.errors.InvalidRequestException)13 List (java.util.List)5 Test (org.junit.jupiter.api.Test)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 AclBinding (org.apache.kafka.common.acl.AclBinding)3 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)3 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)3 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)3 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)3 ByteBuffer (java.nio.ByteBuffer)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 IllegalSaslStateException (org.apache.kafka.common.errors.IllegalSaslStateException)2 UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)2 UnsupportedSaslMechanismException (org.apache.kafka.common.errors.UnsupportedSaslMechanismException)2 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)2 ApiVersionsRequest (org.apache.kafka.common.requests.ApiVersionsRequest)2