Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check for nested UnspportedVersionException during auth op check #6467

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.Config;
import org.apache.kafka.clients.admin.DescribeClusterOptions;
Expand Down Expand Up @@ -51,9 +52,11 @@ public static boolean isAuthorizedOperationsSupported(final Admin adminClient) {
);

return authorizedOperations.authorizedOperations().get() != null;
} catch (final UnsupportedVersionException e) {
return false;
} catch (final Exception e) {
if (ExceptionUtils.indexOfType(e, UnsupportedVersionException.class) != -1) {
LOG.info("Received nested unsupported version error testing authorized operations api", e);
return false;
}
throw new KsqlServerException("Could not get Kafka authorized operations!", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.Config;
import org.apache.kafka.clients.admin.ConfigEntry;
Expand Down Expand Up @@ -181,19 +182,32 @@ public void shouldReturnEmptyValidatorIfAuthorizedOperationsReturnNull() {
@Test
public void shouldReturnEmptyValidatorIfKafkaBrokerVersionTooLowButAuthorizerClassConfigIsSet() {
// Given:
final Collection<Node> nodes = Collections.singletonList(node);
final DescribeClusterResult describeClusterResult = mock(DescribeClusterResult.class);
when(describeClusterResult.nodes()).thenReturn(KafkaFuture.completedFuture(nodes));
when(adminClient.describeCluster()).thenReturn(describeClusterResult);
givenSingleNode();
givenAuthorizerClass("a-class");
when(adminClient.describeCluster(any())).thenThrow(new UnsupportedVersionException("too old"));

final DescribeConfigsResult describeConfigsResult = describeBrokerResult(
Collections.singletonList(
new ConfigEntry(KAFKA_AUTHORIZER_CLASS_NAME, "a-class")
)
// When:
final Optional<KsqlAuthorizationValidator> validator = KsqlAuthorizationValidatorFactory.create(
ksqlConfig,
serviceContext
);
when(adminClient.describeConfigs(describeBrokerRequest()))
.thenReturn(describeConfigsResult);
when(adminClient.describeCluster(any())).thenThrow(new UnsupportedVersionException("too old"));

// Then
assertThat(validator, is(Optional.empty()));
}

@Test
public void shouldReturnEmptyValidatorIfKafkaBrokerVersionTooLowAndExceptionWrapped()
throws InterruptedException, ExecutionException {
// Given:
givenSingleNode();
givenAuthorizerClass("a-class");
final KafkaFuture<Set<AclOperation>> authorized = mockAuthorizedOperationsFuture();
final DescribeClusterResult result = mock(DescribeClusterResult.class);
when(adminClient.describeCluster(any())).thenReturn(result);
when(result.authorizedOperations()).thenReturn(authorized);
when(authorized.get())
.thenThrow(new ExecutionException(new UnsupportedVersionException("too old")));

// When:
final Optional<KsqlAuthorizationValidator> validator = KsqlAuthorizationValidatorFactory.create(
Expand Down Expand Up @@ -242,4 +256,26 @@ private DescribeConfigsResult describeBrokerResult(final List<ConfigEntry> broke
when(describeConfigsResult.all()).thenReturn(KafkaFuture.completedFuture(config));
return describeConfigsResult;
}

private void givenSingleNode() {
final Collection<Node> nodes = Collections.singletonList(node);
final DescribeClusterResult describeClusterResult = mock(DescribeClusterResult.class);
when(describeClusterResult.nodes()).thenReturn(KafkaFuture.completedFuture(nodes));
when(adminClient.describeCluster()).thenReturn(describeClusterResult);
}

private void givenAuthorizerClass(final String name) {
final DescribeConfigsResult describeConfigsResult = describeBrokerResult(
Collections.singletonList(
new ConfigEntry(KAFKA_AUTHORIZER_CLASS_NAME, name)
)
);
when(adminClient.describeConfigs(describeBrokerRequest()))
.thenReturn(describeConfigsResult);
}

@SuppressWarnings("unchecked")
private KafkaFuture<Set<AclOperation>> mockAuthorizedOperationsFuture() {
return mock(KafkaFuture.class);
}
}