diff --git a/.gitignore b/.gitignore index 0217c33..1bb9b25 100644 --- a/.gitignore +++ b/.gitignore @@ -21,5 +21,5 @@ public/js/ *.iml .idea/ -myauth.edn -myconfig.edn +valid-auth.edn +valid-config.edn diff --git a/src/main/cljc/dda/k8s_keycloak/core.cljc b/src/main/cljc/dda/k8s_keycloak/core.cljc index ba85307..3d6af19 100644 --- a/src/main/cljc/dda/k8s_keycloak/core.cljc +++ b/src/main/cljc/dda/k8s_keycloak/core.cljc @@ -30,9 +30,16 @@ (def auth? (s/keys :req-un [::keycloak-admin-user ::keycloak-admin-password ::postgres-db-user ::postgres-db-password])) +(defn replace-named-value + [coll name value] + (clojure.walk/postwalk #(if (and (map? %) + (= name (:name %))) + {:name name :value value} + %) coll)) + (defn replace-all-matching-values-by-new-value [coll value-to-match value-to-replace] - (clojure.walk/postwalk #(if (and (= (type value-to-match) (type %)) + (clojure.walk/postwalk #(if (and (= (type value-to-match) (type %)) (= value-to-match %)) value-to-replace %) coll)) @@ -51,17 +58,17 @@ keycloak-admin-user keycloak-admin-password]} my-auth] (-> (yaml/from-string (yaml/load-resource "deployment.yaml")) - (assoc-in [:spec :template :spec :containers 0 :env 3 :value] postgres-db-user) - (assoc-in [:spec :template :spec :containers 0 :env 5 :value] postgres-db-password) - (assoc-in [:spec :template :spec :containers 0 :env 6 :value] keycloak-admin-user) - (assoc-in [:spec :template :spec :containers 0 :env 7 :value] keycloak-admin-password)))) + (replace-named-value "KEYCLOAK_USER" keycloak-admin-user) + (replace-named-value "DB_USER" postgres-db-user) + (replace-named-value "DB_PASSWORD" postgres-db-password) + (replace-named-value "KEYCLOAK_PASSWORD" keycloak-admin-password)))) (defn generate-postgres-deployment [my-auth] (let [{:keys [postgres-db-user postgres-db-password]} my-auth] (-> (yaml/from-string (yaml/load-resource "postgres/postgres-deployment.yaml")) - (assoc-in [:spec :template :spec :containers 0 :env 0 :value] postgres-db-user) - (assoc-in [:spec :template :spec :containers 0 :env 2 :value] postgres-db-password)))) + (replace-named-value "POSTGRES_USER" postgres-db-user) + (replace-named-value "POSTGRES_PASSWORD" postgres-db-password)))) (defn generate-certificate [config] (let [{:keys [fqdn issuer] diff --git a/src/test/cljc/dda/k8s_keycloak/core_test.cljc b/src/test/cljc/dda/k8s_keycloak/core_test.cljc index 3297db7..1ccd9ff 100644 --- a/src/test/cljc/dda/k8s_keycloak/core_test.cljc +++ b/src/test/cljc/dda/k8s_keycloak/core_test.cljc @@ -77,12 +77,19 @@ [{:name "keycloak" :image "quay.io/keycloak/keycloak:13.0.0" :env - [{:name "KEYCLOAK_USER", :value "testuser"} + [{:name "DB_VENDOR", :value "POSTGRES"} + {:name "DB_ADDR", :value "postgresql-service"} + {:name "DB_DATABASE", :value "keycloak"} + {:name "DB_USER", :value "db-user"} + {:name "DB_SCHEMA", :value "public"} + {:name "DB_PASSWORD", :value "db-password"} + {:name "KEYCLOAK_USER", :value "testuser"} {:name "KEYCLOAK_PASSWORD", :value "test1234"} {:name "PROXY_ADDRESS_FORWARDING", :value "true"}] :ports [{:name "http", :containerPort 8080}] :readinessProbe {:httpGet {:path "/auth/realms/master", :port 8080}}}]}}}} - (cut/generate-deployment {:user-name "testuser" :user-password "test1234"})))) + (cut/generate-deployment {:keycloak-admin-user "testuser" :keycloak-admin-password "test1234" + :postgres-db-user "db-user" :postgres-db-password "db-password"})))) (deftest should-generate-postgres-deployment (is (= {:apiVersion "apps/v1" @@ -109,4 +116,4 @@ :subPath "postgresql.conf" :readOnly true}]}] :volumes [{:name "postgres-config-volume", :configMap {:name "postgres-config"}}]}}}} - (cut/generate-postgres-deployment {:postgres-user "psqluser" :postgres-db "keycloak" :postgres-password "test1234"})))) + (cut/generate-postgres-deployment {:postgres-db-user "psqluser" :postgres-db-password "test1234"}))))