Search in sources :

Example 6 with MetricsCallbackHolder

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());
}
Also used : KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) EndpointResponse(io.confluent.ksql.rest.EndpointResponse) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) PrintTopic(io.confluent.ksql.parser.tree.PrintTopic) Test(org.junit.Test)

Example 7 with MetricsCallbackHolder

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"))));
}
Also used : KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) CompletableFuture(java.util.concurrent.CompletableFuture) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) Test(org.junit.Test)

Example 8 with MetricsCallbackHolder

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());
    }
}
Also used : StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) QueryMetadataHolder(io.confluent.ksql.rest.server.query.QueryMetadataHolder) Query(io.confluent.ksql.parser.tree.Query) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Example 9 with MetricsCallbackHolder

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());
}
Also used : KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) EndpointResponse(io.confluent.ksql.rest.EndpointResponse) Query(io.confluent.ksql.parser.tree.Query) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) KsqlErrorMessage(io.confluent.ksql.rest.entity.KsqlErrorMessage) Test(org.junit.Test)

Example 10 with MetricsCallbackHolder

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 + "'"));
}
Also used : EndpointResponse(io.confluent.ksql.rest.EndpointResponse) Query(io.confluent.ksql.parser.tree.Query) MetricsCallbackHolder(io.confluent.ksql.api.server.MetricsCallbackHolder) HashMap(java.util.HashMap) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Matchers.containsString(org.hamcrest.Matchers.containsString) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Aggregations

MetricsCallbackHolder (io.confluent.ksql.api.server.MetricsCallbackHolder)14 KsqlRequest (io.confluent.ksql.rest.entity.KsqlRequest)13 Test (org.junit.Test)13 Query (io.confluent.ksql.parser.tree.Query)5 EndpointResponse (io.confluent.ksql.rest.EndpointResponse)5 KsqlRestException (io.confluent.ksql.rest.server.resources.KsqlRestException)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)4 PrintTopic (io.confluent.ksql.parser.tree.PrintTopic)3 KsqlErrorMessage (io.confluent.ksql.rest.entity.KsqlErrorMessage)3 KsqlConfig (io.confluent.ksql.util.KsqlConfig)3 StreamedRow (io.confluent.ksql.rest.entity.StreamedRow)2 QueryMetadataHolder (io.confluent.ksql.rest.server.query.QueryMetadataHolder)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 GenericRow (io.confluent.ksql.GenericRow)1 GenericRow.genericRow (io.confluent.ksql.GenericRow.genericRow)1 StreamingOutput (io.confluent.ksql.api.server.StreamingOutput)1 SessionConfig (io.confluent.ksql.config.SessionConfig)1