Search in sources :

Example 1 with KsqlSecurityContext

use of io.confluent.ksql.security.KsqlSecurityContext in project ksql by confluentinc.

the class DistributingExecutor method checkAuthorization.

private void checkAuthorization(final ConfiguredStatement<?> configured, final KsqlSecurityContext userSecurityContext, final KsqlExecutionContext serverExecutionContext) {
    final Statement statement = configured.getStatement();
    final MetaStore metaStore = serverExecutionContext.getMetaStore();
    // Check the User will be permitted to execute this statement
    authorizationValidator.ifPresent(validator -> validator.checkAuthorization(userSecurityContext, metaStore, statement));
    try {
        // Check the KSQL service principal will be permitted too
        authorizationValidator.ifPresent(validator -> validator.checkAuthorization(new KsqlSecurityContext(Optional.empty(), serverExecutionContext.getServiceContext()), metaStore, statement));
    } catch (final Exception e) {
        throw new KsqlServerException("The KSQL server is not permitted to execute the command", e);
    }
}
Also used : MetaStore(io.confluent.ksql.metastore.MetaStore) KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) TimeoutException(org.apache.kafka.common.errors.TimeoutException) AuthorizationException(org.apache.kafka.common.errors.AuthorizationException) OutOfOrderSequenceException(org.apache.kafka.common.errors.OutOfOrderSequenceException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) KsqlRestException(io.confluent.ksql.rest.server.resources.KsqlRestException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlServerException(io.confluent.ksql.util.KsqlServerException)

Example 2 with KsqlSecurityContext

use of io.confluent.ksql.security.KsqlSecurityContext in project ksql by confluentinc.

the class KsqlRestApplication method maybeCreateProcessingLogStream.

private static void maybeCreateProcessingLogStream(final ProcessingLogConfig processingLogConfig, final KsqlConfig ksqlConfig, final KsqlRestConfig restConfig, final KsqlResource ksqlResource, final ServiceContext serviceContext) {
    if (!processingLogConfig.getBoolean(ProcessingLogConfig.STREAM_AUTO_CREATE)) {
        return;
    }
    try {
        final SimpleKsqlClient internalClient = new ServerInternalKsqlClient(ksqlResource, new KsqlSecurityContext(Optional.empty(), serviceContext));
        final URI serverEndpoint = ServerUtil.getServerAddress(restConfig);
        final String processingLogStreamName = processingLogConfig.getString(ProcessingLogConfig.STREAM_NAME);
        if (!processingLogStreamExists(internalClient, serverEndpoint, processingLogStreamName)) {
            final RestResponse<KsqlEntityList> response = internalClient.makeKsqlRequest(serverEndpoint, ProcessingLogServerUtils.processingLogStreamCreateStatement(processingLogConfig, ksqlConfig), ImmutableMap.of());
            if (response.isSuccessful()) {
                log.info("Successfully created processing log stream.");
            }
        }
    } catch (final Exception e) {
        log.error("Error while sending processing log CreateStream request to KsqlResource: ", e);
    }
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) SimpleKsqlClient(io.confluent.ksql.services.SimpleKsqlClient) ServerInternalKsqlClient(io.confluent.ksql.rest.server.services.ServerInternalKsqlClient) URI(java.net.URI) KsqlException(io.confluent.ksql.util.KsqlException) ExecutionException(java.util.concurrent.ExecutionException) KsqlServerException(io.confluent.ksql.util.KsqlServerException) MalformedURLException(java.net.MalformedURLException)

Example 3 with KsqlSecurityContext

use of io.confluent.ksql.security.KsqlSecurityContext in project ksql by confluentinc.

the class KsqlRestApplicationTest method setUp.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Before
public void setUp() {
    when(processingLogConfig.getBoolean(ProcessingLogConfig.STREAM_AUTO_CREATE)).thenReturn(true);
    when(processingLogConfig.getString(ProcessingLogConfig.STREAM_NAME)).thenReturn(LOG_STREAM_NAME);
    when(processingLogConfig.getString(ProcessingLogConfig.TOPIC_NAME)).thenReturn(LOG_TOPIC_NAME);
    when(processingLogConfig.getBoolean(ProcessingLogConfig.TOPIC_AUTO_CREATE)).thenReturn(true);
    when(processingLogContext.getConfig()).thenReturn(processingLogConfig);
    when(ksqlEngine.parse(any())).thenReturn(ImmutableList.of(parsedStatement));
    when(ksqlEngine.prepare(any())).thenReturn((PreparedStatement) preparedStatement);
    when(commandQueue.getCommandTopicName()).thenReturn(CMD_TOPIC_NAME);
    when(serviceContext.getTopicClient()).thenReturn(topicClient);
    when(topicClient.isTopicExists(CMD_TOPIC_NAME)).thenReturn(false);
    when(ksqlConfig.getString(KsqlConfig.KSQL_SERVICE_ID_CONFIG)).thenReturn("ksql-id");
    when(ksqlConfig.getKsqlStreamConfigProps()).thenReturn(ImmutableMap.of("state.dir", "/tmp/cat"));
    when(precondition1.checkPrecondition(any(), any(), any())).thenReturn(Optional.empty());
    when(precondition2.checkPrecondition(any(), any(), any())).thenReturn(Optional.empty());
    when(response.getStatus()).thenReturn(200);
    when(response.getEntity()).thenReturn(new KsqlEntityList(Collections.singletonList(new StreamsList(LIST_STREAMS_SQL, Collections.emptyList()))));
    when(ksqlResource.handleKsqlStatements(any(), any())).thenReturn(response);
    securityContext = new KsqlSecurityContext(Optional.empty(), serviceContext);
    logCreateStatement = ProcessingLogServerUtils.processingLogStreamCreateStatement(processingLogConfig, ksqlConfig);
    givenAppWithRestConfig(ImmutableMap.of(KsqlRestConfig.LISTENERS_CONFIG, "http://localhost:0"), new MetricCollectors());
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) StreamsList(io.confluent.ksql.rest.entity.StreamsList) KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) Before(org.junit.Before)

Example 4 with KsqlSecurityContext

use of io.confluent.ksql.security.KsqlSecurityContext in project ksql by confluentinc.

the class AuthTest method shouldAllowAccessWithPermissionCheck.

private void shouldAllowAccessWithPermissionCheck(final String expectedUser, final String expectedMethod, final String expectedPath, final ExceptionThrowingRunnable action) throws Exception {
    stopServer();
    stopClient();
    AtomicReference<Principal> principalAtomicReference = new AtomicReference<>();
    AtomicReference<String> methodAtomicReference = new AtomicReference<>();
    AtomicReference<String> pathAtomicReference = new AtomicReference<>();
    this.authorizationProvider = new KsqlAuthorizationProvider() {

        @Override
        public void checkEndpointAccess(final Principal user, final String method, final String path) {
            throwIfNullPrincipal(user);
            principalAtomicReference.set(user);
            methodAtomicReference.set(method);
            pathAtomicReference.set(path);
        }

        @Override
        public void checkPrivileges(final KsqlSecurityContext securityContext, final AuthObjectType objectType, final String objectName, final List<AclOperation> privileges) {
        // Not required for vert.x authX as it only authorizes endpoints
        }
    };
    createServer(createServerConfig());
    client = createClient();
    action.run();
    assertThat(principalAtomicReference.get().getName(), is(expectedUser));
    assertThat(methodAtomicReference.get(), is(expectedMethod));
    assertThat(pathAtomicReference.get(), is(expectedPath));
}
Also used : KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) AuthObjectType(io.confluent.ksql.security.AuthObjectType) AclOperation(org.apache.kafka.common.acl.AclOperation) AtomicReference(java.util.concurrent.atomic.AtomicReference) KsqlAuthorizationProvider(io.confluent.ksql.security.KsqlAuthorizationProvider) Principal(java.security.Principal)

Example 5 with KsqlSecurityContext

use of io.confluent.ksql.security.KsqlSecurityContext in project ksql by confluentinc.

the class AuthTest method shouldAllowAccessWithoutAuthentication.

private void shouldAllowAccessWithoutAuthentication(final ExceptionThrowingRunnable action) throws Exception {
    stopServer();
    stopClient();
    AtomicReference<Boolean> authorizationCallReference = new AtomicReference<>(false);
    this.authorizationProvider = new KsqlAuthorizationProvider() {

        @Override
        public void checkEndpointAccess(final Principal user, final String method, final String path) {
            authorizationCallReference.set(true);
        }

        @Override
        public void checkPrivileges(final KsqlSecurityContext securityContext, final AuthObjectType objectType, final String objectName, final List<AclOperation> privileges) {
        // Not required for vert.x authX as it only authorizes endpoints
        }
    };
    createServer(createServerConfig());
    client = createClient();
    action.run();
    assertThat("Should not call authorization", authorizationCallReference.get(), is(false));
}
Also used : KsqlSecurityContext(io.confluent.ksql.security.KsqlSecurityContext) AuthObjectType(io.confluent.ksql.security.AuthObjectType) AclOperation(org.apache.kafka.common.acl.AclOperation) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KsqlAuthorizationProvider(io.confluent.ksql.security.KsqlAuthorizationProvider) Principal(java.security.Principal)

Aggregations

KsqlSecurityContext (io.confluent.ksql.security.KsqlSecurityContext)16 Statement (io.confluent.ksql.parser.tree.Statement)7 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)7 KsqlException (io.confluent.ksql.util.KsqlException)7 Test (org.junit.Test)7 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)6 Before (org.junit.Before)6 SessionConfig (io.confluent.ksql.config.SessionConfig)4 KsqlTopicAuthorizationException (io.confluent.ksql.exception.KsqlTopicAuthorizationException)4 KsqlRestException (io.confluent.ksql.rest.server.resources.KsqlRestException)4 SandboxedServiceContext (io.confluent.ksql.services.SandboxedServiceContext)4 ServiceContext (io.confluent.ksql.services.ServiceContext)4 TestServiceContext (io.confluent.ksql.services.TestServiceContext)4 KsqlConfig (io.confluent.ksql.util.KsqlConfig)4 Optional (java.util.Optional)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)3 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)3 DataSource (io.confluent.ksql.metastore.model.DataSource)3