-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsynchronise.py
executable file
·147 lines (133 loc) · 4.93 KB
/
synchronise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /usr/bin/env python3
import logging
import os
import time
from guacamole_user_sync.ldap import LDAPClient
from guacamole_user_sync.models import LDAPError, LDAPQuery, PostgreSQLError
from guacamole_user_sync.postgresql import PostgreSQLClient, SchemaVersion
def main( # noqa: PLR0913
ldap_bind_dn: str | None,
ldap_bind_password: str | None,
ldap_group_base_dn: str,
ldap_group_filter: str,
ldap_group_name_attr: str,
ldap_host: str,
ldap_port: int,
ldap_user_base_dn: str,
ldap_user_filter: str,
ldap_user_name_attr: str,
postgresql_database_name: str,
postgresql_host_name: str,
postgresql_password: str,
postgresql_port: int,
postgresql_user_name: str,
repeat_interval: int,
) -> None:
# Initialise LDAP resources
ldap_client = LDAPClient(
f"{ldap_host}:{ldap_port}",
bind_dn=ldap_bind_dn,
bind_password=ldap_bind_password,
)
ldap_group_query = LDAPQuery(
base_dn=ldap_group_base_dn,
filter=ldap_group_filter,
id_attr=ldap_group_name_attr,
)
ldap_user_query = LDAPQuery(
base_dn=ldap_user_base_dn,
filter=ldap_user_filter,
id_attr=ldap_user_name_attr,
)
postgresql_client = PostgreSQLClient(
database_name=postgresql_database_name,
host_name=postgresql_host_name,
port=postgresql_port,
user_name=postgresql_user_name,
user_password=postgresql_password,
)
# Loop until terminated
while True:
# Run synchronisation step
synchronise(
ldap_client=ldap_client,
ldap_group_query=ldap_group_query,
ldap_user_query=ldap_user_query,
postgresql_client=postgresql_client,
)
# Wait before repeating
logger.info("Waiting %s seconds.", repeat_interval)
time.sleep(repeat_interval)
def synchronise(
*,
ldap_client: LDAPClient,
ldap_group_query: LDAPQuery,
ldap_user_query: LDAPQuery,
postgresql_client: PostgreSQLClient,
) -> None:
logger.info("Starting synchronisation.")
try:
ldap_groups = ldap_client.search_groups(ldap_group_query)
ldap_users = ldap_client.search_users(ldap_user_query)
except LDAPError:
logger.warning("LDAP server query failed")
return
try:
postgresql_client.ensure_schema(SchemaVersion.v1_5_5)
postgresql_client.update(groups=ldap_groups, users=ldap_users)
except PostgreSQLError:
logger.warning("PostgreSQL update failed")
return
if __name__ == "__main__":
if not (ldap_host := os.getenv("LDAP_HOST", None)):
msg = "LDAP_HOST is not defined"
raise ValueError(msg)
if not (ldap_group_base_dn := os.getenv("LDAP_GROUP_BASE_DN", None)):
msg = "LDAP_GROUP_BASE_DN is not defined"
raise ValueError(msg)
if not (ldap_group_filter := os.getenv("LDAP_GROUP_FILTER", None)):
msg = "LDAP_GROUP_FILTER is not defined"
raise ValueError(msg)
if not (ldap_user_base_dn := os.getenv("LDAP_USER_BASE_DN", None)):
msg = "LDAP_USER_BASE_DN is not defined"
raise ValueError(msg)
if not (ldap_user_filter := os.getenv("LDAP_USER_FILTER", None)):
msg = "LDAP_USER_FILTER is not defined"
raise ValueError(msg)
if not (postgresql_host_name := os.getenv("POSTGRESQL_HOST", None)):
msg = "POSTGRESQL_HOST is not defined"
raise ValueError(msg)
if not (postgresql_password := os.getenv("POSTGRESQL_PASSWORD", None)):
msg = "POSTGRESQL_PASSWORD is not defined"
raise ValueError(msg)
if not (postgresql_user_name := os.getenv("POSTGRESQL_USERNAME", None)):
msg = "POSTGRESQL_USERNAME is not defined"
raise ValueError(msg)
logging.basicConfig(
level=(
logging.DEBUG
if os.getenv("DEBUG", "False").lower() == "true"
else logging.INFO
),
format=r"%(asctime)s [%(levelname)-8s] %(message)s",
datefmt=r"%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger("guacamole_user_sync")
main(
ldap_bind_dn=os.getenv("LDAP_BIND_DN", None),
ldap_bind_password=os.getenv("LDAP_BIND_PASSWORD", None),
ldap_group_base_dn=ldap_group_base_dn,
ldap_group_filter=ldap_group_filter,
ldap_group_name_attr=os.getenv("LDAP_GROUP_NAME_ATTR", "cn"),
ldap_host=ldap_host,
ldap_port=int(os.getenv("LDAP_PORT", "389")),
ldap_user_base_dn=ldap_user_base_dn,
ldap_user_filter=ldap_user_filter,
ldap_user_name_attr=os.getenv("LDAP_USER_NAME_ATTR", "userPrincipalName"),
postgresql_database_name=os.getenv("POSTGRESQL_DB_NAME", "guacamole"),
postgresql_host_name=postgresql_host_name,
postgresql_password=postgresql_password,
postgresql_port=int(os.getenv("POSTGRESQL_PORT", "5432")),
postgresql_user_name=postgresql_user_name,
repeat_interval=int(os.getenv("REPEAT_INTERVAL", "300")),
)