Search in sources :

Example 1 with DescribeUserScramCredentialsResponseData

use of org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData in project kafka by apache.

the class DescribeUserScramCredentialsRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    ApiError apiError = ApiError.fromThrowable(e);
    DescribeUserScramCredentialsResponseData response = new DescribeUserScramCredentialsResponseData().setThrottleTimeMs(throttleTimeMs).setErrorCode(apiError.error().code()).setErrorMessage(apiError.message());
    for (DescribeUserScramCredentialsRequestData.UserName user : data.users()) {
        response.results().add(new DescribeUserScramCredentialsResponseData.DescribeUserScramCredentialsResult().setErrorCode(apiError.error().code()).setErrorMessage(apiError.message()));
    }
    return new DescribeUserScramCredentialsResponse(response);
}
Also used : DescribeUserScramCredentialsResponseData(org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData) DescribeUserScramCredentialsRequestData(org.apache.kafka.common.message.DescribeUserScramCredentialsRequestData)

Example 2 with DescribeUserScramCredentialsResponseData

use of org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData in project kafka by apache.

the class KafkaAdminClient method describeUserScramCredentials.

@Override
public DescribeUserScramCredentialsResult describeUserScramCredentials(List<String> users, DescribeUserScramCredentialsOptions options) {
    final KafkaFutureImpl<DescribeUserScramCredentialsResponseData> dataFuture = new KafkaFutureImpl<>();
    final long now = time.milliseconds();
    Call call = new Call("describeUserScramCredentials", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        @Override
        public DescribeUserScramCredentialsRequest.Builder createRequest(final int timeoutMs) {
            final DescribeUserScramCredentialsRequestData requestData = new DescribeUserScramCredentialsRequestData();
            if (users != null && !users.isEmpty()) {
                final List<UserName> userNames = new ArrayList<>(users.size());
                for (final String user : users) {
                    if (user != null) {
                        userNames.add(new UserName().setName(user));
                    }
                }
                requestData.setUsers(userNames);
            }
            return new DescribeUserScramCredentialsRequest.Builder(requestData);
        }

        @Override
        public void handleResponse(AbstractResponse abstractResponse) {
            DescribeUserScramCredentialsResponse response = (DescribeUserScramCredentialsResponse) abstractResponse;
            DescribeUserScramCredentialsResponseData data = response.data();
            short messageLevelErrorCode = data.errorCode();
            if (messageLevelErrorCode != Errors.NONE.code()) {
                dataFuture.completeExceptionally(Errors.forCode(messageLevelErrorCode).exception(data.errorMessage()));
            } else {
                dataFuture.complete(data);
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            dataFuture.completeExceptionally(throwable);
        }
    };
    runnable.call(call, now);
    return new DescribeUserScramCredentialsResult(dataFuture);
}
Also used : UserName(org.apache.kafka.common.message.DescribeUserScramCredentialsRequestData.UserName) DescribeUserScramCredentialsRequestData(org.apache.kafka.common.message.DescribeUserScramCredentialsRequestData) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) DescribeUserScramCredentialsResponseData(org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData) DescribeUserScramCredentialsRequest(org.apache.kafka.common.requests.DescribeUserScramCredentialsRequest) DescribeUserScramCredentialsResponse(org.apache.kafka.common.requests.DescribeUserScramCredentialsResponse)

Example 3 with DescribeUserScramCredentialsResponseData

use of org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData in project kafka by apache.

the class KafkaAdminClientTest method testDescribeUserScramCredentials.

@Test
public void testDescribeUserScramCredentials() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        final String user0Name = "user0";
        final ScramMechanism user0ScramMechanism0 = ScramMechanism.SCRAM_SHA_256;
        final int user0Iterations0 = 4096;
        final ScramMechanism user0ScramMechanism1 = ScramMechanism.SCRAM_SHA_512;
        final int user0Iterations1 = 8192;
        final CredentialInfo user0CredentialInfo0 = new CredentialInfo();
        user0CredentialInfo0.setMechanism(user0ScramMechanism0.type());
        user0CredentialInfo0.setIterations(user0Iterations0);
        final CredentialInfo user0CredentialInfo1 = new CredentialInfo();
        user0CredentialInfo1.setMechanism(user0ScramMechanism1.type());
        user0CredentialInfo1.setIterations(user0Iterations1);
        final String user1Name = "user1";
        final ScramMechanism user1ScramMechanism = ScramMechanism.SCRAM_SHA_256;
        final int user1Iterations = 4096;
        final CredentialInfo user1CredentialInfo = new CredentialInfo();
        user1CredentialInfo.setMechanism(user1ScramMechanism.type());
        user1CredentialInfo.setIterations(user1Iterations);
        final DescribeUserScramCredentialsResponseData responseData = new DescribeUserScramCredentialsResponseData();
        responseData.setResults(Arrays.asList(new DescribeUserScramCredentialsResponseData.DescribeUserScramCredentialsResult().setUser(user0Name).setCredentialInfos(Arrays.asList(user0CredentialInfo0, user0CredentialInfo1)), new DescribeUserScramCredentialsResponseData.DescribeUserScramCredentialsResult().setUser(user1Name).setCredentialInfos(singletonList(user1CredentialInfo))));
        final DescribeUserScramCredentialsResponse response = new DescribeUserScramCredentialsResponse(responseData);
        final Set<String> usersRequestedSet = new HashSet<>();
        usersRequestedSet.add(user0Name);
        usersRequestedSet.add(user1Name);
        for (final List<String> users : asList(null, new ArrayList<String>(), asList(user0Name, null, user1Name))) {
            env.kafkaClient().prepareResponse(response);
            final DescribeUserScramCredentialsResult result = env.adminClient().describeUserScramCredentials(users);
            final Map<String, UserScramCredentialsDescription> descriptionResults = result.all().get();
            final KafkaFuture<UserScramCredentialsDescription> user0DescriptionFuture = result.description(user0Name);
            final KafkaFuture<UserScramCredentialsDescription> user1DescriptionFuture = result.description(user1Name);
            final Set<String> usersDescribedFromUsersSet = new HashSet<>(result.users().get());
            assertEquals(usersRequestedSet, usersDescribedFromUsersSet);
            final Set<String> usersDescribedFromMapKeySet = descriptionResults.keySet();
            assertEquals(usersRequestedSet, usersDescribedFromMapKeySet);
            final UserScramCredentialsDescription userScramCredentialsDescription0 = descriptionResults.get(user0Name);
            assertEquals(user0Name, userScramCredentialsDescription0.name());
            assertEquals(2, userScramCredentialsDescription0.credentialInfos().size());
            assertEquals(user0ScramMechanism0, userScramCredentialsDescription0.credentialInfos().get(0).mechanism());
            assertEquals(user0Iterations0, userScramCredentialsDescription0.credentialInfos().get(0).iterations());
            assertEquals(user0ScramMechanism1, userScramCredentialsDescription0.credentialInfos().get(1).mechanism());
            assertEquals(user0Iterations1, userScramCredentialsDescription0.credentialInfos().get(1).iterations());
            assertEquals(userScramCredentialsDescription0, user0DescriptionFuture.get());
            final UserScramCredentialsDescription userScramCredentialsDescription1 = descriptionResults.get(user1Name);
            assertEquals(user1Name, userScramCredentialsDescription1.name());
            assertEquals(1, userScramCredentialsDescription1.credentialInfos().size());
            assertEquals(user1ScramMechanism, userScramCredentialsDescription1.credentialInfos().get(0).mechanism());
            assertEquals(user1Iterations, userScramCredentialsDescription1.credentialInfos().get(0).iterations());
            assertEquals(userScramCredentialsDescription1, user1DescriptionFuture.get());
        }
    }
}
Also used : CredentialInfo(org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData.CredentialInfo) DescribeUserScramCredentialsResponseData(org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData) DescribeUserScramCredentialsResponse(org.apache.kafka.common.requests.DescribeUserScramCredentialsResponse) HashSet(java.util.HashSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 4 with DescribeUserScramCredentialsResponseData

use of org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData in project kafka by apache.

the class DescribeUserScramCredentialsResultTest method testUserLevelErrors.

@Test
public void testUserLevelErrors() throws Exception {
    String goodUser = "goodUser";
    String unknownUser = "unknownUser";
    String failedUser = "failedUser";
    KafkaFutureImpl<DescribeUserScramCredentialsResponseData> dataFuture = new KafkaFutureImpl<>();
    ScramMechanism scramSha256 = ScramMechanism.SCRAM_SHA_256;
    int iterations = 4096;
    dataFuture.complete(new DescribeUserScramCredentialsResponseData().setErrorCode(Errors.NONE.code()).setResults(Arrays.asList(new DescribeUserScramCredentialsResponseData.DescribeUserScramCredentialsResult().setUser(goodUser).setCredentialInfos(Arrays.asList(new DescribeUserScramCredentialsResponseData.CredentialInfo().setMechanism(scramSha256.type()).setIterations(iterations))), new DescribeUserScramCredentialsResponseData.DescribeUserScramCredentialsResult().setUser(unknownUser).setErrorCode(Errors.RESOURCE_NOT_FOUND.code()), new DescribeUserScramCredentialsResponseData.DescribeUserScramCredentialsResult().setUser(failedUser).setErrorCode(Errors.DUPLICATE_RESOURCE.code()))));
    DescribeUserScramCredentialsResult results = new DescribeUserScramCredentialsResult(dataFuture);
    try {
        results.all().get();
        fail("expected all() to fail when there is a user-level error");
    } catch (Exception expected) {
    // ignore, expected
    }
    assertEquals(Arrays.asList(goodUser, failedUser), results.users().get(), "Expected 2 users with credentials");
    UserScramCredentialsDescription goodUserDescription = results.description(goodUser).get();
    assertEquals(new UserScramCredentialsDescription(goodUser, Arrays.asList(new ScramCredentialInfo(scramSha256, iterations))), goodUserDescription);
    try {
        results.description(failedUser).get();
        fail("expected description(failedUser) to fail when there is a user-level error");
    } catch (Exception expected) {
    // ignore, expected
    }
    try {
        results.description(unknownUser).get();
        fail("expected description(unknownUser) to fail when there is no such user");
    } catch (Exception expected) {
    // ignore, expected
    }
}
Also used : DescribeUserScramCredentialsResponseData(org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Test(org.junit.jupiter.api.Test)

Example 5 with DescribeUserScramCredentialsResponseData

use of org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData in project kafka by apache.

the class DescribeUserScramCredentialsResultTest method testSuccessfulDescription.

@Test
public void testSuccessfulDescription() throws Exception {
    String goodUser = "goodUser";
    String unknownUser = "unknownUser";
    KafkaFutureImpl<DescribeUserScramCredentialsResponseData> dataFuture = new KafkaFutureImpl<>();
    ScramMechanism scramSha256 = ScramMechanism.SCRAM_SHA_256;
    int iterations = 4096;
    dataFuture.complete(new DescribeUserScramCredentialsResponseData().setErrorCode(Errors.NONE.code()).setResults(Arrays.asList(new DescribeUserScramCredentialsResponseData.DescribeUserScramCredentialsResult().setUser(goodUser).setCredentialInfos(Arrays.asList(new DescribeUserScramCredentialsResponseData.CredentialInfo().setMechanism(scramSha256.type()).setIterations(iterations))))));
    DescribeUserScramCredentialsResult results = new DescribeUserScramCredentialsResult(dataFuture);
    assertEquals(Arrays.asList(goodUser), results.users().get(), "Expected 1 user with credentials");
    Map<String, UserScramCredentialsDescription> allResults = results.all().get();
    assertEquals(1, allResults.size());
    UserScramCredentialsDescription goodUserDescriptionViaAll = allResults.get(goodUser);
    assertEquals(new UserScramCredentialsDescription(goodUser, Arrays.asList(new ScramCredentialInfo(scramSha256, iterations))), goodUserDescriptionViaAll);
    assertEquals(goodUserDescriptionViaAll, results.description(goodUser).get(), "Expected same thing via all() and description()");
    try {
        results.description(unknownUser).get();
        fail("expected description(unknownUser) to fail when there is no such user even when all() succeeds");
    } catch (Exception expected) {
    // ignore, expected
    }
}
Also used : KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) DescribeUserScramCredentialsResponseData(org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData) Test(org.junit.jupiter.api.Test)

Aggregations

DescribeUserScramCredentialsResponseData (org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData)5 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)3 Test (org.junit.jupiter.api.Test)3 DescribeUserScramCredentialsRequestData (org.apache.kafka.common.message.DescribeUserScramCredentialsRequestData)2 DescribeUserScramCredentialsResponse (org.apache.kafka.common.requests.DescribeUserScramCredentialsResponse)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 UserName (org.apache.kafka.common.message.DescribeUserScramCredentialsRequestData.UserName)1 CredentialInfo (org.apache.kafka.common.message.DescribeUserScramCredentialsResponseData.CredentialInfo)1 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 DescribeUserScramCredentialsRequest (org.apache.kafka.common.requests.DescribeUserScramCredentialsRequest)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1