Skip to content

Instantly share code, notes, and snippets.

@manics
Last active October 4, 2024 16:09
Show Gist options
  • Save manics/df1a798d561d66ad5814d4fd1225d1eb to your computer and use it in GitHub Desktop.
Save manics/df1a798d561d66ad5814d4fd1225d1eb to your computer and use it in GitHub Desktop.
Test JupyterHub Authenticator configuration from the command line

JupyterHub Authenticator command line tester

It's often difficult to find the correct configuration for JupyterHub Authenticators, especially LDAP.

This command line utility loads your jupyterhub_config.py file, and attempts to login using a configured username and password with debug logging enabled. This should allow you to quickly see whether your Authenticator configuration is correct.

#!/usr/bin/env python
from asyncio import get_event_loop
from jupyterhub.auth import Authenticator
from jupyterhub.traitlets import EntryPointType
from traitlets import Dict, Instance, List, Unicode, default
from traitlets.config import Application, Configurable
class AuthTester(Application):
name = Unicode("AuthTester")
log_level = Unicode("DEBUG").tag(config=True)
classes = List()
@default("classes")
def _load_classes(self):
classes = {Authenticator}
for entry_point in (
self.traits(config=True)["authenticator_class"].load_entry_points().values()
):
cls = entry_point.load()
if issubclass(cls, Configurable):
classes.add(cls)
return list(classes)
config_file = Unicode("jupyterhub_config.py", help="Load this config file").tag(
config=True
)
authenticator_class = EntryPointType(
default_value=None,
klass=Authenticator,
entry_point_group="jupyterhub.authenticators",
help="Class for authenticating users",
).tag(config=True)
@default("authenticator_class")
def _authenticator_class_default(self):
return self.config.JupyterHub.authenticator_class
authenticator = Instance(Authenticator)
@default("authenticator")
def _authenticator_default(self):
return self.authenticator_class(parent=self)
username = Unicode(help="Test login with this username").tag(config=True)
password = Unicode(help="Test login with this password").tag(config=True)
aliases = Dict(
dict(
c="AuthTester.config_file",
p="AuthTester.password",
u="AuthTester.username",
)
)
def initialize(self, argv=None):
self.parse_command_line(argv)
if self.config_file:
self.load_config_file(self.config_file)
self.load_config_environ()
def start(self):
print("app.config:")
print(self.config)
print("try running with --help-all to see all available flags")
def auth(self):
self.log.info(
"Calling %s.authenticate()", self.authenticator.__class__.__name__
)
loop = get_event_loop()
r = loop.run_until_complete(
self.authenticator.authenticate(
handler=None, data=dict(username=self.username, password=self.password)
),
)
self.log.info(r)
def main():
app = AuthTester()
app.initialize()
# app.start()
app.auth()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment