Search in sources :

Example 51 with ConfiguredStatement

use of io.confluent.ksql.statement.ConfiguredStatement in project ksql by confluentinc.

the class DistributingExecutorTest method shouldThrowServerExceptionIfServerServiceContextIsDeniedAuthorization.

@Test
public void shouldThrowServerExceptionIfServerServiceContextIsDeniedAuthorization() {
    // Given:
    final KsqlSecurityContext userSecurityContext = new KsqlSecurityContext(Optional.empty(), SandboxedServiceContext.create(TestServiceContext.create()));
    final PreparedStatement<Statement> preparedStatement = PreparedStatement.of("", new ListProperties(Optional.empty()));
    final ConfiguredStatement<Statement> configured = ConfiguredStatement.of(preparedStatement, SessionConfig.of(KSQL_CONFIG, ImmutableMap.of()));
    doNothing().when(authorizationValidator).checkAuthorization(eq(userSecurityContext), any(), any());
    doThrow(KsqlTopicAuthorizationException.class).when(authorizationValidator).checkAuthorization(ArgumentMatchers.argThat(securityContext -> securityContext.getServiceContext() == serviceContext), any(), any());
    // When:
    final Exception e = assertThrows(KsqlServerException.class, () -> distributor.execute(configured, executionContext, userSecurityContext));
    // Then:
    assertThat(e.getCause(), (is(instanceOf(KsqlTopicAuthorizationException.class))));
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) Query(io.confluent.ksql.parser.tree.Query) ArgumentMatchers(org.mockito.ArgumentMatchers) Matchers.isA(org.hamcrest.Matchers.isA) ColumnName(io.confluent.ksql.name.ColumnName) SourceName(io.confluent.ksql.name.SourceName) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) StatementExecutorResponse(io.confluent.ksql.rest.server.execution.StatementExecutorResponse) ServiceContext(io.confluent.ksql.services.ServiceContext) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) CommandId(io.confluent.ksql.rest.entity.CommandId) InjectorChain(io.confluent.ksql.statement.InjectorChain) CommonCreateConfigs(io.confluent.ksql.properties.with.CommonCreateConfigs) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) KsqlErrorMessage(io.confluent.ksql.rest.entity.KsqlErrorMessage) KsqlAuthorizationValidator(io.confluent.ksql.security.KsqlAuthorizationValidator) Mockito.doThrow(org.mockito.Mockito.doThrow) Duration(java.time.Duration) Mockito.doReturn(org.mockito.Mockito.doReturn) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) ImmutableMap(com.google.common.collect.ImmutableMap) Errors(io.confluent.ksql.rest.Errors) Mockito.doNothing(org.mockito.Mockito.doNothing) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) KsqlRestConfig(io.confluent.ksql.rest.server.KsqlRestConfig) TestServiceContext(io.confluent.ksql.services.TestServiceContext) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Status(io.confluent.ksql.rest.entity.CommandStatus.Status) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Statement(io.confluent.ksql.parser.tree.Statement) WarningEntity(io.confluent.ksql.rest.entity.WarningEntity) Matchers.containsString(org.hamcrest.Matchers.containsString) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Action(io.confluent.ksql.rest.entity.CommandId.Action) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) Supplier(java.util.function.Supplier) ListProperties(io.confluent.ksql.parser.tree.ListProperties) SessionConfig(io.confluent.ksql.config.SessionConfig) CreateStream(io.confluent.ksql.parser.tree.CreateStream) ImmutableList(com.google.common.collect.ImmutableList) Injector(io.confluent.ksql.statement.Injector) MetaStore(io.confluent.ksql.metastore.MetaStore) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) InsertInto(io.confluent.ksql.parser.tree.InsertInto) Before(org.junit.Before) TimeoutException(org.apache.kafka.common.errors.TimeoutException) InOrder(org.mockito.InOrder) Producer(org.apache.kafka.clients.producer.Producer) Assert.assertTrue(org.junit.Assert.assertTrue) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) SandboxedServiceContext(io.confluent.ksql.services.SandboxedServiceContext) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Type(io.confluent.ksql.rest.entity.CommandId.Type) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) KsqlServerException(io.confluent.ksql.util.KsqlServerException) TableElements(io.confluent.ksql.parser.tree.TableElements) DefaultErrorMessages(io.confluent.ksql.rest.DefaultErrorMessages) Assert.assertEquals(org.junit.Assert.assertEquals) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) ListProperties(io.confluent.ksql.parser.tree.ListProperties) KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) KsqlException(io.confluent.ksql.util.KsqlException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) Test(org.junit.Test)

Example 52 with ConfiguredStatement

use of io.confluent.ksql.statement.ConfiguredStatement in project ksql by confluentinc.

the class DistributingExecutorTest method shouldThrowExceptionWhenInsertIntoUnknownStream.

@Test
public void shouldThrowExceptionWhenInsertIntoUnknownStream() {
    // Given
    final PreparedStatement<Statement> preparedStatement = PreparedStatement.of("", new InsertInto(SourceName.of("s1"), mock(Query.class)));
    final ConfiguredStatement<Statement> configured = ConfiguredStatement.of(preparedStatement, SessionConfig.of(KSQL_CONFIG, ImmutableMap.of()));
    doReturn(null).when(metaStore).getSource(SourceName.of("s1"));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> distributor.execute(configured, executionContext, mock(KsqlSecurityContext.class)));
    // Then:
    assertThat(e.getMessage(), containsString("Cannot insert into an unknown stream/table: `s1`"));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) InsertInto(io.confluent.ksql.parser.tree.InsertInto) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) KsqlException(io.confluent.ksql.util.KsqlException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) Test(org.junit.Test)

Example 53 with ConfiguredStatement

use of io.confluent.ksql.statement.ConfiguredStatement in project ksql by confluentinc.

the class DistributingExecutorTest method shouldThrowExceptionWhenInsertIntoSourceWithHeaders.

@Test
public void shouldThrowExceptionWhenInsertIntoSourceWithHeaders() {
    // Given
    final PreparedStatement<Statement> preparedStatement = PreparedStatement.of("", new InsertInto(SourceName.of("s1"), mock(Query.class)));
    final ConfiguredStatement<Statement> configured = ConfiguredStatement.of(preparedStatement, SessionConfig.of(KSQL_CONFIG, ImmutableMap.of()));
    final DataSource dataSource = mock(DataSource.class);
    final LogicalSchema schema = mock(LogicalSchema.class);
    doReturn(dataSource).when(metaStore).getSource(SourceName.of("s1"));
    doReturn(schema).when(dataSource).getSchema();
    doReturn(ImmutableList.of(ColumnName.of("a"))).when(schema).headers();
    when(dataSource.getKafkaTopicName()).thenReturn("topic");
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> distributor.execute(configured, executionContext, mock(KsqlSecurityContext.class)));
    // Then:
    assertThat(e.getMessage(), is("Cannot insert into s1 because it has header columns"));
}
Also used : ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) InsertInto(io.confluent.ksql.parser.tree.InsertInto) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KsqlTopicAuthorizationException(io.confluent.ksql.exception.KsqlTopicAuthorizationException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) KsqlException(io.confluent.ksql.util.KsqlException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) DataSource(io.confluent.ksql.metastore.model.DataSource) Test(org.junit.Test)

Example 54 with ConfiguredStatement

use of io.confluent.ksql.statement.ConfiguredStatement in project ksql by confluentinc.

the class DistributingExecutorTest method shouldThrowExceptionIfUserServiceContextIsDeniedAuthorization.

@Test
public void shouldThrowExceptionIfUserServiceContextIsDeniedAuthorization() {
    // Given:
    final KsqlSecurityContext userSecurityContext = new KsqlSecurityContext(Optional.empty(), mock(ServiceContext.class));
    final PreparedStatement<Statement> preparedStatement = PreparedStatement.of("", new ListProperties(Optional.empty()));
    final ConfiguredStatement<Statement> configured = ConfiguredStatement.of(preparedStatement, SessionConfig.of(KSQL_CONFIG, ImmutableMap.of()));
    doThrow(KsqlTopicAuthorizationException.class).when(authorizationValidator).checkAuthorization(eq(userSecurityContext), any(), eq(configured.getStatement()));
    // When:
    assertThrows(KsqlTopicAuthorizationException.class, () -> distributor.execute(configured, executionContext, userSecurityContext));
}
Also used : ListProperties(io.confluent.ksql.parser.tree.ListProperties) KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) ServiceContext(io.confluent.ksql.services.ServiceContext) TestServiceContext(io.confluent.ksql.services.TestServiceContext) SandboxedServiceContext(io.confluent.ksql.services.SandboxedServiceContext) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Test(org.junit.Test)

Example 55 with ConfiguredStatement

use of io.confluent.ksql.statement.ConfiguredStatement in project ksql by confluentinc.

the class DistributingExecutorTest method shouldNotEnqueueRedundantIfNotExists.

@Test
public void shouldNotEnqueueRedundantIfNotExists() {
    // Given:
    final PreparedStatement<Statement> preparedStatement = PreparedStatement.of("", new CreateStream(SourceName.of("TEST"), TableElements.of(), false, true, CreateSourceProperties.from(ImmutableMap.of(CommonCreateConfigs.KAFKA_TOPIC_NAME_PROPERTY, new StringLiteral("topic"), CommonCreateConfigs.VALUE_FORMAT_PROPERTY, new StringLiteral("json"))), false));
    final ConfiguredStatement<Statement> configured = ConfiguredStatement.of(preparedStatement, SessionConfig.of(KSQL_CONFIG, ImmutableMap.of()));
    final DataSource dataSource = mock(DataSource.class);
    doReturn(dataSource).when(metaStore).getSource(SourceName.of("TEST"));
    // When:
    final StatementExecutorResponse response = distributor.execute(configured, executionContext, securityContext);
    // Then:
    assertThat("Should be present", response.getEntity().isPresent());
    assertThat(((WarningEntity) response.getEntity().get()).getMessage(), containsString(""));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) CreateStream(io.confluent.ksql.parser.tree.CreateStream) StatementExecutorResponse(io.confluent.ksql.rest.server.execution.StatementExecutorResponse) DataSource(io.confluent.ksql.metastore.model.DataSource) Test(org.junit.Test)

Aggregations

ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)84 Test (org.junit.Test)57 KsqlException (io.confluent.ksql.util.KsqlException)23 Statement (io.confluent.ksql.parser.tree.Statement)22 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)19 Optional (java.util.Optional)19 ServiceContext (io.confluent.ksql.services.ServiceContext)18 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)18 KsqlEngine (io.confluent.ksql.engine.KsqlEngine)17 KsqlConfig (io.confluent.ksql.util.KsqlConfig)16 Map (java.util.Map)14 DataSource (io.confluent.ksql.metastore.model.DataSource)12 QueryId (io.confluent.ksql.query.QueryId)12 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)12 Collectors (java.util.stream.Collectors)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 ListQueries (io.confluent.ksql.parser.tree.ListQueries)11 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)11 List (java.util.List)11 Query (io.confluent.ksql.parser.tree.Query)10