title: How to check SSL Kafka topic is authorized using keystore
body: <p>I have a simple <a href="https://kafka.apache.org/10/javadoc/org/apache/kafka/clients/producer/KafkaProducer.html" rel="nofollow noreferrer">KafkaProducer</a> to produced into SSL Kafka topic using <code>keystore.jks</code> file</p>
  <pre><code>Properties props = new Properties();
  props.put(&quot;bootstrap.servers&quot;, &quot;localhost:9092&quot;);
  props.put(&quot;acks&quot;, &quot;all&quot;);
  props.put(&quot;retries&quot;, 0);
  props.put(&quot;batch.size&quot;, 16384);
  props.put(&quot;linger.ms&quot;, 1);
  props.put(&quot;buffer.memory&quot;, 33554432);
  props.put(&quot;key.serializer&quot;, &quot;org.apache.kafka.common.serialization.StringSerializer&quot;);
  props.put(&quot;value.serializer&quot;, &quot;org.apache.kafka.common.serialization.StringSerializer&quot;);
  props.put(&quot;security.protocol&quot;, &quot;SSL&quot;);
  props.put(&quot;ssl.key.password&quot;, &quot;password&quot;);
  props.put(&quot;ssl.keystore.password&quot;, &quot;password&quot;);
  props.put(&quot;ssl.keystore.location&quot;, &quot;/keystore.jks&quot;);
  
  Producer&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props);
  
  producer.send(new ProducerRecord&lt;String, String&gt;(&quot;my-topic&quot;, key, body));
  </code></pre>
  <p>But since <code>KafkaProducer</code> is a lazy instance and which creates a connection with a Kafka cluster on the arrival of the first message causing failure at runtime if the topic is unauthorized</p>
  <p><strong>Error:</strong></p>
  <blockquote>
  <p>org.apache.kafka.common.errors.TopicAuthorizationException: Not authorized to access topics: [my-topic]</p>
  </blockquote>
  <p>Is there a way to check topic authorization either using <a href="https://kafka.apache.org/20/javadoc/org/apache/kafka/clients/admin/KafkaAdminClient.html" rel="nofollow noreferrer">KafkaAdminClient</a> or writing custom logic before starting KafkaProducer? I do have below code extracting certs &amp; key from <code>keystore</code> file but couldn't find any next steps</p>
  <pre><code>KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());  
  char[] pwdArray = &quot;password&quot;.toCharArray();
  keyStore.load(new FileInputStream(&quot;/keystore.jks&quot;), pwdArray);
  </code></pre>