wip(lldap): Consultant

This commit is contained in:
Madeleine Sydney
2025-02-18 23:17:14 -07:00
parent 98a71dc13b
commit c7b5479388
38 changed files with 2944 additions and 47 deletions

View File

@@ -0,0 +1,83 @@
(ns ldap-nginx-plumber.main
(:require [clojure.spec.alpha :as spec]
[org.httpkit.server :as http]
[clj-ldap.client :as ldap]
[babashka.cli :as cli])
(:gen-class))
(defn- port? [x]
(and (nat-int? %)
(<= 0 % 65535)))
(def cli-spec
{:spec
{:port {:coerce :int
:desc "Port to listen on"
:alias :p
:validate port?
:require true}
:base-dn {:coerce :string
:desc "Base DN for LDAP searches"
:require true}
:ldap-host {:coerce :string
:require true}
:ldap-port {:coerce :int
:validate port?
:default 389}}})
(defonce ldap-connection-pool
(atom nil))
(defn- response [status & {:as more}]
(apply merge
{:status status
:headers {"Content-Type" "text/plain"}}
more))
(defn- consultant-app [opts]
(fn [req]
(prn req)
(response 200 :body "hi")
#_
(let [user-dn (format "uid=%s,%s"
)]
(if (ldap/bind? @ldap-connection-pool)
(response 200
:body "Dong")
(response 401)))))
(defonce consultant-server (atom nil))
(defn- stop-consultant! []
(when @consultant-server
;; Graceful shutdown: wait 100ms for existing requests to be finished.
;; :timeout is optional, when no timeout, stop immediately.
(http/server-stop! @consultant-server {:timeout 100})
(reset! consultant-server nil)))
(defn- start-consultant [& {:keys [port] :as opts}]
(if @consultant-server
(throw (ex-info "Refusing to start the server whilst a previous lingers" {}))
(reset! consultant-server
(http/run-server (#'consultant-app opts)
{:port port
:legacy-return-value? false}))))
(defn- connect-to-ldap [& {:keys [base-dn ldap-host ldap-port]}]
(reset! ldap-connection-pool
(or @ldap-connection-pool
(ldap/connect {:host {:address ldap-host
:port ldap-port}
:max-connections 8}))))
(defn- main* [& opts]
(and (apply connect-to-ldap opts)
(apply start-consultant opts)))
#_ ; Start on :8080
(main* :port 8080)
#_ ; Shutdown
(stop-consultant!)
(defn -main [& args]
(main* (cli/parse-opts args cli-spec)))