Skip to content
This repository has been archived by the owner on May 5, 2024. It is now read-only.

Commit

Permalink
fix: Do not throw NPE if KAFKA_SASL_MECHANISM is null (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
joschi authored Oct 19, 2022
1 parent 8fd14ee commit 4a89b2c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ private static void handleAuthentication(AtomicReference<String> username, Atomi
if (username.get() != null && password.get() != null) {
// Do we need the Plain or SCRAM module?
String loginModule = null;
if (config.get(SaslConfigs.SASL_MECHANISM).equals("PLAIN")) {
Object saslMechanism = config.get(SaslConfigs.SASL_MECHANISM);

if ("PLAIN".equals(saslMechanism)) {
loginModule = "org.apache.kafka.common.security.plain.PlainLoginModule";
} else if (config.get(SaslConfigs.SASL_MECHANISM).equals("SCRAM-SHA-256") || config.get(SaslConfigs.SASL_MECHANISM).equals("SCRAM-SHA-512")) {
} else if ("SCRAM-SHA-256".equals(saslMechanism) || "SCRAM-SHA-512".equals(saslMechanism)) {
loginModule = "org.apache.kafka.common.security.scram.ScramLoginModule";
} else {
throw new MissingConfigurationException("KAFKA_SASL_MECHANISM");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devshawn.kafka.gitops.config

import com.devshawn.kafka.gitops.exception.MissingConfigurationException
import org.apache.kafka.clients.CommonClientConfigs
import org.apache.kafka.common.config.SaslConfigs
import org.junit.ClassRule
Expand Down Expand Up @@ -59,4 +60,16 @@ class KafkaGitopsConfigLoaderSpec extends Specification {
config.config.get(SaslConfigs.SASL_MECHANISM) == "PLAIN"
}

void 'load() fails with MissingConfigurationException if SASL mechanism is missing'() {
setup:
environmentVariables.set("KAFKA_SASL_JAAS_USERNAME", "test")
environmentVariables.set("KAFKA_SASL_JAAS_PASSWORD", "test-secret")
environmentVariables.clear("KAFKA_SASL_MECHANISM")

when:
KafkaGitopsConfigLoader.load()

then:
thrown(MissingConfigurationException)
}
}

0 comments on commit 4a89b2c

Please sign in to comment.