Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tls certs and keys settings #87

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions ldapauthenticator/ldapauthenticator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re

import ssl
from jupyterhub.auth import Authenticator
import ldap3
from ldap3.utils.conv import escape_filter_chars
Expand Down Expand Up @@ -41,6 +41,33 @@ def _server_port_default(self):
"""
)

server_ca_file = Unicode(
config=True,
help="""
Path of the CA certificate file for the *secure* LDAP server

If you're receiving `CERTIFICATE_VERIFY_FAILED` errors, you have to use this setting.
"""
)

client_certificate_file = Unicode(
config=True,
help="""
Path of the certificate file for the LDAP client

If you're receiving `SSLV3_ALERT_HANDSHAKE_FAILURE` errors, you have to use this setting.
"""
)

client_key_file = Unicode(
config=True,
help="""
Path of the key file for the LDAP client

If you're receiving `SSLV3_ALERT_HANDSHAKE_FAILURE` errors, you have to use this setting.
"""
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not satisfied with the documentation that i wrote, but i think this could be left for another moment.

bind_dn_template = Union(
[List(),Unicode()],
config=True,
Expand Down Expand Up @@ -268,11 +295,22 @@ def authenticate(self, handler, data):
password = data['password']
# Get LDAP Connection
def getConnection(userdn, username, password):
server = ldap3.Server(
self.server_address,
port=self.server_port,
use_ssl=self.use_ssl
)
if self.use_ssl:
ca_cert = None
client_cert = None
client_key = None
if self.server_ca_file != '':
ca_cert = self.server_ca_file
if self.client_certificate_file != '':
client_cert = self.client_certificate_file
if self.client_key_file != '':
client_key = self.client_key_file
Comment on lines +298 to +307
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming we go for a merge of this PR, we should also simplify this logic to by letting the new traitlets based config have allow_none=True and letting the default value be None.

tlsSettings = ldap3.Tls(local_private_key_file=client_key,
local_certificate_file=client_cert,
ca_certs_file=ca_cert, validate=ssl.CERT_REQUIRED)
server = ldap3.Server(self.server_address, port=self.server_port, use_ssl=True, tls=tlsSettings)
else:
server = ldap3.Server(self.server_address, port=self.server_port, use_ssl=False)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the last changes it should be now possible to only supply certain options, before that, it was needed to use all the three new settings to work with tls endpoints.

self.log.debug('Attempting to bind {username} with {userdn}'.format(
username=username,
userdn=userdn
Expand Down