use of io.confluent.ksql.api.server.MetricsCallbackHolder in project ksql by confluentinc.
the class StreamedQueryResourceTest method shouldReturnForbiddenKafkaAccessIfPrintTopicKsqlTopicAuthorizationException.
@Test
public void shouldReturnForbiddenKafkaAccessIfPrintTopicKsqlTopicAuthorizationException() {
// Given:
print = PreparedStatement.of("print", mock(PrintTopic.class));
when(mockStatementParser.<PrintTopic>parseSingleStatement(PRINT_TOPIC)).thenReturn(print);
doThrow(new KsqlTopicAuthorizationException(AclOperation.READ, Collections.singleton(TOPIC_NAME))).when(authorizationValidator).checkAuthorization(any(), any(), any());
// When:
final EndpointResponse response = testResource.streamQuery(securityContext, new KsqlRequest(PRINT_TOPIC, Collections.emptyMap(), Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context);
assertEquals(response.getStatus(), AUTHORIZATION_ERROR_RESPONSE.getStatus());
assertEquals(response.getEntity(), AUTHORIZATION_ERROR_RESPONSE.getEntity());
}
use of io.confluent.ksql.api.server.MetricsCallbackHolder in project ksql by confluentinc.
the class StreamedQueryResourceTest method shouldThrowOnHandleStatementIfNotConfigured.
@Test
public void shouldThrowOnHandleStatementIfNotConfigured() {
// Given:
testResource = new StreamedQueryResource(mockKsqlEngine, ksqlRestConfig, mockStatementParser, commandQueue, DISCONNECT_CHECK_INTERVAL, COMMAND_QUEUE_CATCHUP_TIMOEUT, activenessRegistrar, Optional.of(authorizationValidator), errorsHandler, denyListPropertyValidator, queryExecutor);
// When:
final KsqlRestException e = assertThrows(KsqlRestException.class, () -> testResource.streamQuery(securityContext, new KsqlRequest("query", Collections.emptyMap(), Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context));
// Then:
assertThat(e, exceptionStatusCode(is(SERVICE_UNAVAILABLE.code())));
assertThat(e, exceptionErrorMessage(errorMessage(Matchers.is("Server initializing"))));
}
use of io.confluent.ksql.api.server.MetricsCallbackHolder in project ksql by confluentinc.
the class WSQueryEndpoint method handleQuery.
private void handleQuery(final RequestContext info, final Query query, final long startTimeNanos, final Context context) {
final WebSocketSubscriber<StreamedRow> streamSubscriber = new WebSocketSubscriber<>(info.websocket);
attachCloseHandler(info.websocket, streamSubscriber);
final PreparedStatement<Query> statement = PreparedStatement.of(info.request.getKsql(), query);
final MetricsCallbackHolder metricsCallbackHolder = new MetricsCallbackHolder();
final QueryMetadataHolder queryMetadataHolder = queryExecutor.handleStatement(info.securityContext.getServiceContext(), info.request.getConfigOverrides(), info.request.getRequestProperties(), statement, Optional.empty(), metricsCallbackHolder, context, true);
if (queryMetadataHolder.getPullQueryResult().isPresent()) {
new PullQueryPublisher(exec, queryMetadataHolder.getPullQueryResult().get(), metricsCallbackHolder, startTimeNanos).subscribe(streamSubscriber);
} else if (queryMetadataHolder.getPushQueryMetadata().isPresent()) {
new PushQueryPublisher(exec, queryMetadataHolder.getPushQueryMetadata().get(), metricsCallbackHolder, startTimeNanos).subscribe(streamSubscriber);
} else {
throw new KsqlStatementException("Unknown query type", statement.getStatementText());
}
}
use of io.confluent.ksql.api.server.MetricsCallbackHolder in project ksql by confluentinc.
the class StreamedQueryResourceTest method shouldReturnForbiddenKafkaAccessForPullQueryAuthorizationDenied.
@Test
public void shouldReturnForbiddenKafkaAccessForPullQueryAuthorizationDenied() {
// Given:
when(mockStatementParser.<Query>parseSingleStatement(PULL_QUERY_STRING)).thenReturn(query);
doThrow(new KsqlTopicAuthorizationException(AclOperation.READ, Collections.singleton(TOPIC_NAME))).when(authorizationValidator).checkAuthorization(any(), any(), any());
// When:
final EndpointResponse response = testResource.streamQuery(securityContext, new KsqlRequest(PULL_QUERY_STRING, Collections.emptyMap(), Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context);
final KsqlErrorMessage responseEntity = (KsqlErrorMessage) response.getEntity();
final KsqlErrorMessage expectedEntity = (KsqlErrorMessage) AUTHORIZATION_ERROR_RESPONSE.getEntity();
assertEquals(response.getStatus(), AUTHORIZATION_ERROR_RESPONSE.getStatus());
assertEquals(responseEntity.getMessage(), expectedEntity.getMessage());
}
use of io.confluent.ksql.api.server.MetricsCallbackHolder in project ksql by confluentinc.
the class StreamedQueryResourceTest method shouldThrowOnDenyListedStreamProperty.
@Test
public void shouldThrowOnDenyListedStreamProperty() {
// Given:
when(mockStatementParser.<Query>parseSingleStatement(PULL_QUERY_STRING)).thenReturn(query);
testResource = new StreamedQueryResource(mockKsqlEngine, ksqlRestConfig, mockStatementParser, commandQueue, DISCONNECT_CHECK_INTERVAL, COMMAND_QUEUE_CATCHUP_TIMOEUT, activenessRegistrar, Optional.of(authorizationValidator), errorsHandler, denyListPropertyValidator, queryExecutor);
final Map<String, Object> props = new HashMap<>(ImmutableMap.of(StreamsConfig.APPLICATION_SERVER_CONFIG, "something:1"));
props.put(KsqlConfig.KSQL_PROPERTIES_OVERRIDES_DENYLIST, StreamsConfig.NUM_STREAM_THREADS_CONFIG);
testResource.configure(new KsqlConfig(props));
final Map<String, Object> overrides = ImmutableMap.of(StreamsConfig.NUM_STREAM_THREADS_CONFIG, 1);
doThrow(new KsqlException("deny override")).when(denyListPropertyValidator).validateAll(overrides);
when(errorsHandler.generateResponse(any(), any())).thenReturn(badRequest("A property override was set locally for a property that the " + "server prohibits overrides for: 'num.stream.threads'"));
// When:
final EndpointResponse response = testResource.streamQuery(securityContext, new KsqlRequest(PULL_QUERY_STRING, // stream properties
overrides, Collections.emptyMap(), null), new CompletableFuture<>(), Optional.empty(), new MetricsCallbackHolder(), context);
// Then:
verify(denyListPropertyValidator).validateAll(overrides);
assertThat(response.getStatus(), CoreMatchers.is(BAD_REQUEST.code()));
assertThat(((KsqlErrorMessage) response.getEntity()).getMessage(), is("A property override was set locally for a property that the server prohibits " + "overrides for: '" + StreamsConfig.NUM_STREAM_THREADS_CONFIG + "'"));
}
Aggregations