Search in sources :

Example 1 with Environment

use of org.bf2.test.Environment in project kafka-admin-api by bf2fc6cc711aee1a0c2a.

the class AdminClientFactory method createAdminClient.

/**
 * Route handler common to all Kafka resource routes. Responsible for creating
 * the map of properties used to configure the Kafka Admin Client. When OAuth
 * has been enabled via the environment, the access token will be retrieved from
 * the authenticated user principal present in the context (created by Vert.x
 * handler when a valid JWT was presented by the client). The configuration property
 * map will be placed in the context under the key identified by the
 * {@link #ADMIN_CLIENT_CONFIG} constant.
 */
public AdminClient createAdminClient() {
    Map<String, Object> acConfig = config.getAcConfig();
    if (config.isOauthEnabled()) {
        if (token.isResolvable()) {
            final String accessToken = token.get().getRawToken();
            if (accessToken == null) {
                throw new NotAuthorizedException(Response.status(Status.UNAUTHORIZED));
            }
            acConfig.put(SaslConfigs.SASL_JAAS_CONFIG, String.format(SASL_OAUTH_CONFIG_TEMPLATE, accessToken));
        } else {
            log.warn("OAuth is enabled, but there is no JWT principal");
        }
    } else if (config.isBasicEnabled()) {
        extractCredentials(Optional.ofNullable(headers.get().getHeaderString(HttpHeaders.AUTHORIZATION))).ifPresentOrElse(credentials -> acConfig.put(SaslConfigs.SASL_JAAS_CONFIG, credentials), () -> {
            throw new NotAuthorizedException("Invalid or missing credentials", Response.status(Status.UNAUTHORIZED).build());
        });
    } else {
        log.debug("OAuth is disabled - no attempt to set access token in Admin Client config");
    }
    return AdminClient.create(acConfig);
}
Also used : Consumer(org.apache.kafka.clients.consumer.Consumer) Producer(org.apache.kafka.clients.producer.Producer) KafkaAdminConfigRetriever(org.bf2.admin.kafka.admin.KafkaAdminConfigRetriever) Logger(org.jboss.logging.Logger) Vertx(io.vertx.core.Vertx) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) AdminClient(org.apache.kafka.clients.admin.AdminClient) Inject(javax.inject.Inject) Objects(java.util.Objects) Base64(java.util.Base64) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) HttpHeaders(javax.ws.rs.core.HttpHeaders) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Response(javax.ws.rs.core.Response) RequestScoped(javax.enterprise.context.RequestScoped) JsonWebToken(org.eclipse.microprofile.jwt.JsonWebToken) Map(java.util.Map) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) Optional(java.util.Optional) SaslConfigs(org.apache.kafka.common.config.SaslConfigs) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) Status(javax.ws.rs.core.Response.Status) Instance(javax.enterprise.inject.Instance) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) NotAuthorizedException(javax.ws.rs.NotAuthorizedException)

Aggregations

Vertx (io.vertx.core.Vertx)1 Base64 (java.util.Base64)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 RequestScoped (javax.enterprise.context.RequestScoped)1 Instance (javax.enterprise.inject.Instance)1 Inject (javax.inject.Inject)1 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 Response (javax.ws.rs.core.Response)1 Status (javax.ws.rs.core.Response.Status)1 AdminClient (org.apache.kafka.clients.admin.AdminClient)1 Consumer (org.apache.kafka.clients.consumer.Consumer)1 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)1 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 Producer (org.apache.kafka.clients.producer.Producer)1 ProducerConfig (org.apache.kafka.clients.producer.ProducerConfig)1 SaslConfigs (org.apache.kafka.common.config.SaslConfigs)1