Class KafkaSecurityConfiguration

java.lang.Object
com.reallifedeveloper.common.infrastructure.messaging.KafkaSecurityConfiguration

public final class KafkaSecurityConfiguration extends Object
Utility class used to set the security-related Kafka configuration properties when creating consumers and producers.

In particular, it allows reading the truststore from a classpath resource using a classpath: prefix.

Author:
RealLifeDeveloper
  • Field Details

    • SECURITY_PROTOCOL_CONFIGURATION_KEY

      public static final String SECURITY_PROTOCOL_CONFIGURATION_KEY
      The configuration property to use to set security protocol, e.g., SASL_SSL.
      See Also:
    • SSL_TRUSTSTORE_LOCATION_CONFIGURATION_KEY

      public static final String SSL_TRUSTSTORE_LOCATION_CONFIGURATION_KEY
      The configuration property to use to set the location of the truststore, supports classpath prefixes.
      See Also:
    • SSL_TRUSTSTORE_PASSWORD_CONFIGURATION_KEY

      public static final String SSL_TRUSTSTORE_PASSWORD_CONFIGURATION_KEY
      The configuration property to use to set the password of the truststore.
      See Also:
    • SASL_MECHNISM_CONFIGURATION_KEY

      public static final String SASL_MECHNISM_CONFIGURATION_KEY
      The configuration property to use to set the SASL mechanism, e.g., PLAIN.
      See Also:
    • SASL_JAAS_CONFIG_CONFIGURATION_KEY

      public static final String SASL_JAAS_CONFIG_CONFIGURATION_KEY
      The configuration property to use to set the JAAS config.
      See Also:
  • Constructor Details

    • KafkaSecurityConfiguration

      public KafkaSecurityConfiguration()
  • Method Details

    • applySecurityConfiguration

      public static void applySecurityConfiguration(String configurationKey, String configurationValue, Map<String,Object> configurationProperties) throws IOException
      Sets the value of a configuration property.

      A value can be set only once for a particular property; trying to reset a value causes an exception to be thrown.

      There is special handling of the "ssl.truststore.location" configuration property, with support for reading a trust store from the classpath if the configuration value starts with classpath:.

      In the case that the "ssl.truststore.location" value starts with classpath:, the trust store is read as a resource using the current thread's context classloader. The store is copied to a temporary file, and the path of the file is used as the value to set.

      Parameters:
      configurationKey - the configuration key to apply
      configurationValue - the configuration value
      configurationProperties - the currently set configuration properties
      Throws:
      IllegalStateException - if trying to reset a value
      IOException - if reading the trust store from classpath fails