Skip to content

Commit

Permalink
🚨 Fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jemrobinson committed May 30, 2024
1 parent 6fb3ab9 commit 7c4f4b0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
27 changes: 20 additions & 7 deletions apricot/apricot_server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import inspect
import sys
from typing import Any, cast, Optional
from typing import Any, Optional, cast

from twisted.internet import reactor, task
from twisted.internet.endpoints import serverFromString, quoteStringArgument
from twisted.internet.endpoints import quoteStringArgument, serverFromString
from twisted.internet.interfaces import IReactorCore, IStreamServerEndpoint
from twisted.python import log

Expand Down Expand Up @@ -71,12 +71,18 @@ def __init__(
if self.debug:
log.msg("Creating an LDAPServerFactory.")
factory = OAuthLDAPServerFactory(
domain, oauth_client, background_refresh=background_refresh, enable_mirrored_groups=enable_mirrored_groups, refresh_interval=refresh_interval
domain,
oauth_client,
background_refresh=background_refresh,
enable_mirrored_groups=enable_mirrored_groups,
refresh_interval=refresh_interval,
)

if background_refresh:
if self.debug:
log.msg(f"Starting background refresh (interval={factory.adaptor.refresh_interval})")
log.msg(
f"Starting background refresh (interval={factory.adaptor.refresh_interval})"
)
loop = task.LoopingCall(factory.adaptor.refresh)
loop.start(factory.adaptor.refresh_interval)

Expand All @@ -88,11 +94,18 @@ def __init__(

# Attach a listening endpoint
if tls_port:
if not (tls_certificate or tls_private_key):
raise ValueError("No TLS certificate or private key provided. Make sure you provide these parameters or disable TLS by not providing the TLS port")
if not tls_certificate:
msg = "No TLS certificate provided. Please provide one with --tls-certificate or disable TLS by not providing the --tls-port argument."
raise ValueError(msg)
if not tls_private_key:
msg = "No TLS private key provided. Please provide one with --tls-private-key or disable TLS by not providing the --tls-port argument."
raise ValueError(msg)
if self.debug:
log.msg("Attaching a listening endpoint (TLS).")
ssl_endpoint: IStreamServerEndpoint = serverFromString(reactor, f"ssl:{tls_port}:privateKey={quoteStringArgument(tls_private_key)}:certKey={quoteStringArgument(tls_certificate)}")
ssl_endpoint: IStreamServerEndpoint = serverFromString(
reactor,
f"ssl:{tls_port}:privateKey={quoteStringArgument(tls_private_key)}:certKey={quoteStringArgument(tls_certificate)}",
)
ssl_endpoint.listen(factory)

# Load the Twisted reactor
Expand Down
14 changes: 12 additions & 2 deletions apricot/ldap/oauth_ldap_server_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

class OAuthLDAPServerFactory(ServerFactory):
def __init__(
self, domain: str, oauth_client: OAuthClient, *, background_refresh: bool, enable_mirrored_groups: bool, refresh_interval: int,
self,
domain: str,
oauth_client: OAuthClient,
*,
background_refresh: bool,
enable_mirrored_groups: bool,
refresh_interval: int,
):
"""
Initialise an OAuthLDAPServerFactory
Expand All @@ -22,7 +28,11 @@ def __init__(
"""
# Create an LDAP lookup tree
self.adaptor = OAuthLDAPTree(
domain, oauth_client, background_refresh=background_refresh, enable_mirrored_groups=enable_mirrored_groups, refresh_interval=refresh_interval
domain,
oauth_client,
background_refresh=background_refresh,
enable_mirrored_groups=enable_mirrored_groups,
refresh_interval=refresh_interval,
)

def __repr__(self) -> str:
Expand Down
13 changes: 9 additions & 4 deletions apricot/ldap/oauth_ldap_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(
*,
background_refresh: bool,
enable_mirrored_groups: bool,
refresh_interval,
refresh_interval: int,
) -> None:
"""
Initialise an OAuthLDAPTree
Expand Down Expand Up @@ -50,15 +50,20 @@ def root(self) -> OAuthLDAPEntry:
Lazy-load the LDAP tree on request
@return: An OAuthLDAPEntry for the tree
@raises: ValueError.
"""
if not self.background_refresh:
self.refresh()
if not self.root_:
msg = "LDAP tree could not be loaded"
raise ValueError(msg)
return self.root_

def refresh(self):
def refresh(self) -> None:
if (
not self.root_
or (time.monotonic() - self.last_update) > self.refresh_interval
not self.root_
or (time.monotonic() - self.last_update) > self.refresh_interval
):
# Update users and groups from the OAuth server
log.msg("Retrieving OAuth data.")
Expand Down

0 comments on commit 7c4f4b0

Please sign in to comment.