Search code examples
micronautmicronaut-clientmicronaut-kafkamicronaut-kubernetes

In micronaut-kafka, How can I use JAAS config to two different consumers from one application?


I have tried the approach mentioned on official documentations to override the bootstrap servers in kafka client config.

https://micronaut-projects.github.io/micronaut-kafka/latest/guide/#kafkaClient

However, in my case, I am getting one JaaS config from other bean dependency and other one is available in secret path. Having a custom configuration just load one JaaS config and other consumer get disconnected.

For example,

kafka:
  sasl:
    mechanism: PLAIN
    jaas:
      config: >-
        org.apache.kafka.common.security.plain.PlainLoginModule required
        username="$ConnectionString"
        password="%s";
  security:
    protocol: SASL_SSL
  consumers:
    abc-consumer-client:
      sasl:
        mechanism: PLAIN
        jaas:
          config: >-
            org.apache.kafka.common.security.plain.PlainLoginModule required
            username="$ConnectionString"
            password="%s";
      security:
        protocol: SASL_SSL
    xyz-client:
      sasl:
        mechanism: PLAIN
        jaas:
          config: >-
            org.apache.kafka.common.security.plain.PlainLoginModule required
            username="$ConnectionString"
            password="%s";
      security:
        protocol: SASL_SSL

I related the micronaut-kafka with spring kafka implementation. It seems like I will have to override the beans to achieve this but getting stuck on which order I should override and in which sequence to achieve this.

Similar Spring Kafka reference- spring-kafka application.properties configuration for JAAS/SASL not working

I have tried the options listed on micronaut documentation but in my case, other jaas config is coming from other bean depedency which makes a GRPC call to fetch bootstrap URL and JaaS config.

What am I looking for here is the order in which I should override the micronaut-kafka beans to achieve two consumer connecting to two different bootstrap servers with each having its own JaasConfig and other JaasConfig is dependent on other service call.


Solution

  • Finally, I was able to figure out this problem myself.

    In order to solve this problem, I had to override the KafkaDefaultConfiguration bean and include the properties in the environment variable based on groupID.

    You can actually override properties at runtime during the bean instantiation phase. For instance,

    environment.addPropertySource(PropertySource.of("overrides",
                    Map.of("kafka.consumers." + GroupId + ".bootstrap.servers", baseUrl,
                            "kafka.consumers." + GroupId + ".sasl.jaas.config", newJaasConfig )));