Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Vruttant1403 committed Apr 23, 2024
1 parent 095e02b commit 3edc85b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
50 changes: 44 additions & 6 deletions src/ziggurat/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
(defn ssl-config []
(get-in config [:ziggurat :ssl]))

(defn sasl-config []
(get-in config [:ziggurat :sasl]))

(defn rabbitmq-config []
(get (ziggurat-config) :rabbit-mq))

Expand Down Expand Up @@ -214,12 +217,17 @@
(let [username (get jaas-config :username)
password (get jaas-config :password)
mechanism (get jaas-config :mechanism)
protocol (get jaas-config :protocol)
jaas_props (create-jaas-properties username password mechanism)]
(doto properties
(.put SaslConfigs/SASL_JAAS_CONFIG jaas_props)
(.put SaslConfigs/SASL_MECHANISM mechanism)
(.put CommonClientConfigs/SECURITY_PROTOCOL_CONFIG protocol)))
(.put SaslConfigs/SASL_JAAS_CONFIG jaas_props)))
properties))

(defn- add-sasl-properties
[properties mechanism protocol]
(if (and (some? mechanism) (some? protocol))
(doto properties
(.put SaslConfigs/SASL_MECHANISM mechanism)
(.put CommonClientConfigs/SECURITY_PROTOCOL_CONFIG protocol))
properties))

(defn build-ssl-properties
Expand Down Expand Up @@ -250,6 +258,35 @@
(reduce-kv set-property-fn pr ssl-config-map))
properties)))

(defn build-sasl-properties
[properties set-property-fn sasl-config-map]
"Builds SASL properties from sasl-config-map which is a map where keys are
Clojure keywords in kebab case. These keys are converted to Kafka properties by set-property-fn.
SASL properties are only set if [:ziggurat :sasl :enabled] returns true.
Creates JAAS template if values are provided in the map provided agains this key sequence
[:ziggurat :ssl :jaas].
Example of sasl-config-map
{:enabled true
:protocol <>
{:jaas
{:username <>
:password <>
:mechanism}}}
"
(let [sasl-configs-enabled (:enabled sasl-config-map)
jaas-config (get sasl-config-map :jaas)
mechanism (get jaas-config :mechanism)
protocol (get sasl-config-map :protocol)]
(if (true? sasl-configs-enabled)
(as-> properties pr
(add-jaas-properties pr jaas-config)
(add-sasl-properties pr mechanism protocol)
(reduce-kv set-property-fn pr sasl-config-map))
properties)))

(defn build-properties
"Builds Properties object from the provided config-map which is a map where keys are
Clojure keywords in kebab case. These keys are converted to Kafka properties by set-property-fn.
Expand All @@ -270,8 +307,9 @@
"
[set-property-fn config-map]
(as-> (Properties.) pr
(build-ssl-properties pr set-property-fn (ssl-config))
(reduce-kv set-property-fn pr config-map)))
(build-ssl-properties pr set-property-fn (ssl-config))
(build-sasl-properties pr set-property-fn (sasl-config))
(reduce-kv set-property-fn pr config-map)))

(def build-consumer-config-properties
(partial build-properties (partial set-property consumer-config-mapping-table)))
Expand Down
21 changes: 18 additions & 3 deletions test/ziggurat/config_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
statsd-config
ziggurat-config
ssl-config
sasl-config
create-jaas-properties]]
[ziggurat.fixtures :as f])
(:import (java.util ArrayList Properties)))
Expand Down Expand Up @@ -323,8 +324,7 @@
:ssl-keystore-password "some-password"
:jaas {:username "myuser"
:password "mypassword"
:mechanism "SCRAM-SHA-512"
:protocol "PLAINTEXT"}})]
:mechanism "SCRAM-SHA-512"}})]
(let [streams-config-map {:auto-offset-reset :latest}
props (build-streams-config-properties streams-config-map)
auto-offset-reset (.getProperty props "auto.offset.reset")
Expand All @@ -348,7 +348,22 @@
(is (= auto-offset-reset "latest"))
(is (= ssl-ks-location "/some/location"))
(is (= ssl-ks-password "some-password"))
(is (nil? sasl-jaas-config)))))))
(is (nil? sasl-jaas-config)))))
(testing "sasl properties create jaas template from the map provided in [:ziggurat :sasl :jaas]"
(with-redefs [sasl-config (constantly {:enabled true
:protocol "SASL_PLAINTEXT"
:jaas {:username "myuser"
:password "mypassword"
:mechanism "SCRAM-SHA-256"}})]
(let [streams-config-map {:auto-offset-reset :latest}
props (build-streams-config-properties streams-config-map)
auto-offset-reset (.getProperty props "auto.offset.reset")
sasl-jaas-config (.getProperty props "sasl.jaas.config")
sasl-protocol (.getProperty props "security.protocol")
sasl-mechanism (.getProperty props "sasl.mechanism")]
(is (= auto-offset-reset "latest"))
(is (= sasl-protocol "SASL_PLAINTEXT"))
(is (= sasl-jaas-config (create-jaas-properties "myuser" "mypassword" "SCRAM-SHA-256"))))))))

(deftest test-set-property
(testing "set-property with empty (with spaces) value"
Expand Down

0 comments on commit 3edc85b

Please sign in to comment.