Skip to content

Commit

Permalink
Make log level configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
lkiesow committed Feb 2, 2024
1 parent d43aaa7 commit 8e66034
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ services:
LDAP_SEARCH_FILTER: '(uid={username})'
LISTEN_ADDR: '0.0.0.0'
LISTEN_PORT: 5555
LOGLEVEL: info
```
### NGINX
Expand Down
13 changes: 7 additions & 6 deletions nginx-ldap-connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ldap3 import Server, Connection, ALL, AUTO_BIND_NO_TLS

# Logger
logger = logging.getLogger(__name__)
logging.root.setLevel(os.environ.get('LOGLEVEL', 'INFO').upper())

ldap_server = os.environ['LDAP_SERVER']
ldap_port = int(os.environ['LDAP_PORT'])
Expand Down Expand Up @@ -39,20 +39,20 @@ def ldap_login(username: str, password: str) -> dict[str, list]:
'''
user_dn = ldap_user_dn.format(username=username)

logger.debug('Trying to log into LDAP with user_dn `%s`', user_dn)
logging.debug('Trying to log into LDAP with user_dn `%s`', user_dn)
conn = connect(user_dn, password)
logger.debug('Login successful with user_dn `%s`', user_dn)
logging.debug('Login successful with user_dn `%s`', user_dn)

attributes = []

logger.debug('Searching for user data')
logging.debug('Searching for user data')
conn.search(
ldap_base_dn,
ldap_search_filter.format(username=username),
attributes=attributes)
if len(conn.entries) != 1:
raise ValueError('Search must return exactly one result', conn.entries)
logger.debug('Found user data')
logging.debug('Found user data')
return conn.entries[0].entry_attributes_as_dict


Expand All @@ -61,7 +61,8 @@ def check_auth(auth):
return False
try:
ldap_login(auth.username, auth.password)
except Exception:
except Exception as e:
logging.debug('Error logging in: %s', e)
return False
return True

Expand Down

0 comments on commit 8e66034

Please sign in to comment.