Skip to content

Commit

Permalink
Better logging (delete function and :eldap log callback) see #11
Browse files Browse the repository at this point in the history
  • Loading branch information
minijackson committed Nov 10, 2017
1 parent a971b81 commit 87b9dc3
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/paddle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ defmodule Paddle do

Logger.info("Connecting to ldap#{if ssl, do: "s"}://#{host}:#{port}")

{:ok, ldap_conn} = :eldap.open([host], ssl: ssl, port: port)
{:ok, ldap_conn} = :eldap.open([host], ssl: ssl, port: port, log: &eldap_log_callback/3)
:eldap.controlling_process(ldap_conn, self())
Logger.info("Connected to LDAP")
{:ok, ldap_conn}
Expand All @@ -169,7 +169,7 @@ defmodule Paddle do
end

def handle_call({:authenticate, dn, password}, _from, ldap_conn) do
Logger.debug "Checking credentials with dn: #{dn}"
Logger.debug "Authenticating with dn: #{dn}"
status = :eldap.simple_bind(ldap_conn, dn, password)

case status do
Expand All @@ -182,7 +182,7 @@ defmodule Paddle do
dn = Parsing.construct_dn(kwdn, config(base))
filter = Filters.construct_filter(filter)

Logger.debug("Getting entries with dn: #{dn} and filter: #{inspect filter}")
Logger.debug("Getting entries with dn: #{dn} and filter: #{inspect filter, pretty: true}")

{:reply,
:eldap.search(ldap_conn, base: dn, filter: filter)
Expand All @@ -194,7 +194,7 @@ defmodule Paddle do
dn = Parsing.construct_dn(kwdn, config(base))
filter = Filters.construct_filter(filter)

Logger.debug("Getting single entry with dn: #{dn} and filter: #{inspect filter}")
Logger.debug("Getting single entry with dn: #{dn} and filter: #{inspect filter, pretty: true}")

{:reply,
:eldap.search(ldap_conn,
Expand All @@ -221,6 +221,8 @@ defmodule Paddle do
def handle_call({:delete, kwdn, base}, _from, ldap_conn) do
dn = Parsing.construct_dn(kwdn, config(base))

Logger.info("Deleting entry with dn: #{dn}")

{:reply, :eldap.delete(ldap_conn, dn), ldap_conn}
end

Expand All @@ -230,15 +232,14 @@ defmodule Paddle do
Logger.info("Modifying entry: \"#{dn}\" with mods: #{inspect mods}")

mods = mods |> Enum.map(&Parsing.mod_convert/1)
Logger.debug("Mods translated in :eldap form: #{inspect mods}")

{:reply, :eldap.modify(ldap_conn, dn, mods), ldap_conn}
end

@type authenticate_ldap_error :: :operationsError | :protocolError |
:authMethodNotSupported | :strongAuthRequired | :referral |
:saslBindInProgress | :inappropriateAuthentication | :invalidCredentials |
:unavailable
:unavailable | :anonymous_auth
@spec authenticate(keyword | binary, binary) :: :ok | {:error, authenticate_ldap_error}

@doc ~S"""
Expand Down Expand Up @@ -640,4 +641,16 @@ defmodule Paddle do
defp ensure_single_result({:ok, []}), do: {:error, :noSuchObject}
defp ensure_single_result({:ok, [result]}), do: {:ok, result}

@spec eldap_log_callback(pos_integer(), charlist(), [term()]) :: :ok

@doc false
def eldap_log_callback(level, format_string, format_args) do
message = :io_lib.format(format_string, format_args)
case level do
# Level 1 seems unused by :eldap
1 -> Logger.info(message)
2 -> Logger.debug(message)
end
end

end

0 comments on commit 87b9dc3

Please sign in to comment.