Add type annotations to 30 files across jw.util, resolving all 191 'no-untyped-def' mypy errors in strict mode. The changes cover Signals, CppState, Bunch, RedirectStdIO, StopWatch, Process, Options, ArgsContainer, Object, multi_key_dict, ldap, Session, rows, ShuntingYard, TableIoHandler, DataBase, Queries, Query, QueryResult, and all db/schema files (Column, Table, ColumnSet, DataType, Schema, SingleForeignKey, CompositeForeignKey), plus asyncio variants and ev/base/Object and auth/dummy/Auth. Additionally, unreachable code and type annotation conflicts were resolved in StopWatch.py, Options.py, rows.py, and Table.py. Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0 Signed-off-by: Jan Lindemann <jan@janware.com>
416 lines
14 KiB
Python
416 lines
14 KiB
Python
from __future__ import annotations
|
|
|
|
import copy
|
|
import getpass
|
|
import pathlib
|
|
|
|
from enum import Flag, auto
|
|
from typing import TYPE_CHECKING, Any, Self
|
|
|
|
import ldap # type: ignore[import-untyped]
|
|
import networkx as nx # type: ignore[import-untyped]
|
|
|
|
from ldap.schema.models import ObjectClass # type: ignore[import-untyped]
|
|
|
|
from .log import ERR, INFO, WARNING, slog
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Callable
|
|
|
|
from .Config import Config as BaseConfig
|
|
|
|
class Config:
|
|
__ldap_uri: str | None
|
|
__bind_dn: str | None
|
|
__bind_pw: str | None
|
|
__base_dn: str | None
|
|
|
|
def __init__(self, external: BaseConfig | None = None) -> None:
|
|
self.__external = external
|
|
self.__ldap_uri = None
|
|
self.__bind_dn = None
|
|
self.__bind_pw = None
|
|
self.__base_dn = None
|
|
|
|
def __get(self, key: str, default: str | None) -> str | None:
|
|
if not self.__external:
|
|
return default
|
|
return self.__external.value(key, default = default)
|
|
|
|
@property
|
|
def ldap_uri(self) -> str | None:
|
|
if self.__ldap_uri is None:
|
|
for key in ['ldap_uri', 'uri']:
|
|
self.__ldap_uri = self.__get(key, default = None)
|
|
if self.__ldap_uri is not None:
|
|
break
|
|
else:
|
|
self.__ldap_uri = 'ldap://ldap.janware.com'
|
|
return self.__ldap_uri
|
|
|
|
@ldap_uri.setter
|
|
def ldap_uri(self, rhs: str | None) -> None:
|
|
self.__ldap_uri = rhs
|
|
|
|
@property
|
|
def bind_dn(self) -> str | None:
|
|
if self.__bind_dn is None:
|
|
self.__bind_dn = self.__get(
|
|
'bind_dn',
|
|
default = f'uid={getpass.getuser()},ou=users,dc=jannet,dc=de'
|
|
)
|
|
return self.__bind_dn
|
|
|
|
@bind_dn.setter
|
|
def bind_dn(self, rhs: str | None) -> None:
|
|
self.__bind_dn = rhs
|
|
|
|
@property
|
|
def bind_pw(self) -> str | None:
|
|
if self.__bind_pw is None:
|
|
ret: str | None = None
|
|
for key in ['bind_pw', 'password']:
|
|
val = self.__get(key, default = None)
|
|
if val is not None:
|
|
ret = val
|
|
break
|
|
if ret is None:
|
|
ldap_secret_file = self.__get(
|
|
'secret_file', f'{pathlib.Path.home()}/.ldap.secret'
|
|
)
|
|
assert ldap_secret_file is not None, 'ldap_secret_file'
|
|
with open(ldap_secret_file, 'r') as file:
|
|
ret = file.read()
|
|
file.closed
|
|
ret = ret.strip()
|
|
self.__bind_pw = ret
|
|
return self.__bind_pw
|
|
|
|
@bind_pw.setter
|
|
def bind_pw(self, rhs: str | None) -> None:
|
|
self.__bind_pw = rhs
|
|
|
|
@property
|
|
def base_dn(self) -> str | None:
|
|
if self.__base_dn is None:
|
|
self.__base_dn = self.__get('base_dn', default = 'dc=jannet,dc=de')
|
|
return self.__base_dn
|
|
|
|
@base_dn.setter
|
|
def base_dn(self, rhs: str | None) -> None:
|
|
self.__base_dn = rhs
|
|
|
|
class Connection: # export
|
|
|
|
class AttrType(Flag):
|
|
Must = auto()
|
|
May = auto()
|
|
|
|
def __init__(
|
|
self, conf: Config | BaseConfig | None = None, backtrace: bool = False
|
|
):
|
|
uri: str | None = None
|
|
c = conf if isinstance(conf, Config) else Config(conf)
|
|
try:
|
|
uri = c.ldap_uri
|
|
except Exception:
|
|
# mypy says: E: "Config" has no attribute "uri" [attr-defined]
|
|
# FIXME: Who adds .uri?
|
|
uri = c.uri # type: ignore[attr-defined]
|
|
try:
|
|
ret = ldap.initialize(uri)
|
|
ret.start_tls_s()
|
|
except Exception as e:
|
|
slog(ERR, f'Failed to initialize LDAP connection to "{uri}" ({str(e)})')
|
|
raise
|
|
try:
|
|
ret.bind_s(c.bind_dn, c.bind_pw) # method)
|
|
except Exception as e:
|
|
slog(ERR, f'Failed to bind to "{uri}" with dn "{c.bind_dn}" ({str(e)})')
|
|
raise
|
|
self.__ldap = ret
|
|
self.__backtrace = backtrace
|
|
self.__object_classes_by_oid: dict[str, ObjectClass] | None = None
|
|
self.__object_class_tree: nx.Graph | None = None
|
|
self.__object_classes_by_name: dict[str, ObjectClass] | None = None
|
|
|
|
@property
|
|
def ldap(self) -> Any:
|
|
return self.__ldap
|
|
|
|
def add(self, attrs: dict[str, bytes], dn: str | None = None) -> None:
|
|
if dn is None:
|
|
if 'dn' not in attrs:
|
|
raise Exception('No DN to add an LDAP entry to')
|
|
attrs = copy.deepcopy(attrs)
|
|
del attrs['dn']
|
|
try:
|
|
slog(INFO, f'LDAP: Add [{dn}] -> {attrs}')
|
|
ml =ldap.modlist.addModlist( # pyright: ignore[reportAttributeAccessIssue]
|
|
attrs
|
|
)
|
|
self.__ldap.add_s(dn, ml)
|
|
except Exception as e:
|
|
slog(ERR, f'{dn}: Failed to add entry {attrs} ({e})')
|
|
raise
|
|
|
|
def delete(
|
|
self, dn: str, recursive: bool = False, force_existence: bool = False
|
|
) -> None:
|
|
|
|
def __walk_cb_delete(conn: Connection, entry: Any, context: Any) -> None:
|
|
self.walk(
|
|
__walk_cb_delete,
|
|
base = entry[0],
|
|
scope = ldap.
|
|
SCOPE_ONELEVEL, # pyright: ignore[reportAttributeAccessIssue]
|
|
context = context
|
|
)
|
|
self.__ldap.delete_s(entry[0])
|
|
|
|
try:
|
|
if recursive:
|
|
self.walk(
|
|
__walk_cb_delete,
|
|
dn,
|
|
scope = ldap.
|
|
SCOPE_ONELEVEL # pyright: ignore[reportAttributeAccessIssue]
|
|
)
|
|
self.__ldap.delete_s(dn)
|
|
else:
|
|
self.__ldap.delete_s(dn)
|
|
except ldap.NO_SUCH_OBJECT: # pyright: ignore[reportAttributeAccessIssue]
|
|
if force_existence:
|
|
raise
|
|
except Exception as e:
|
|
slog(ERR, f'Failed to delete {dn} ({e})')
|
|
raise
|
|
|
|
def walk(
|
|
self,
|
|
callback: Callable[[Self, Any, Any], None],
|
|
base: str,
|
|
scope: Any,
|
|
context: Any = None,
|
|
filterstr: Any = None,
|
|
attrlist: Any = None,
|
|
attrsonly: int = 0,
|
|
serverctrls: Any = None,
|
|
clientctrls: Any = None,
|
|
timeout: int = -1,
|
|
sizelimit: int = 0,
|
|
decode: bool = False,
|
|
unroll: bool = False
|
|
) -> None:
|
|
|
|
# TODO: Support ignored arguments
|
|
search_return = self.__ldap.search(
|
|
base = base,
|
|
scope = scope,
|
|
filterstr = filterstr,
|
|
attrlist = attrlist,
|
|
attrsonly = attrsonly
|
|
)
|
|
while True:
|
|
result_type, result_data = self.__ldap.result(search_return, 0)
|
|
if (not result_data):
|
|
break
|
|
if result_type != ldap.RES_SEARCH_ENTRY: # pyright: ignore[reportAttributeAccessIssue]
|
|
continue
|
|
for entry in result_data:
|
|
if decode:
|
|
entry = entry[0], {
|
|
key: [val.decode() for val in vals]
|
|
for key, vals in entry[1].items()
|
|
}
|
|
try:
|
|
callback(self, entry, context)
|
|
except Exception as e:
|
|
msg = f'Exception {e}'
|
|
if self.__backtrace:
|
|
slog(ERR, msg)
|
|
raise
|
|
slog(WARNING, msg)
|
|
continue
|
|
|
|
def find(
|
|
self,
|
|
base: str,
|
|
scope: Any,
|
|
filterstr: Any = None,
|
|
attrlist: Any = None,
|
|
attrsonly: int = 0,
|
|
serverctrls: Any = None,
|
|
clientctrls: Any = None,
|
|
timeout: int = -1,
|
|
sizelimit: int = 0,
|
|
assert_unique: bool = False,
|
|
assert_not_empty: bool = False,
|
|
) -> list[Any]:
|
|
|
|
def __walk_cb_find(conn: Connection, entry: Any, context: Any) -> None:
|
|
result.append(entry)
|
|
|
|
def __search() -> str:
|
|
return f'{base} -> "{filterstr}"'
|
|
|
|
try:
|
|
result: list[Any] = []
|
|
self.walk(
|
|
__walk_cb_find,
|
|
base,
|
|
scope = scope,
|
|
filterstr = filterstr,
|
|
attrlist = attrlist
|
|
)
|
|
except Exception as e:
|
|
slog(ERR, f'Failed search {__search()} ({e})')
|
|
raise
|
|
if assert_not_empty and not result:
|
|
raise Exception(f'Empty result for search {__search()}')
|
|
if assert_unique and len(result) > 1:
|
|
raise Exception(f'Found {len(result)} results for search {__search()}')
|
|
return result
|
|
|
|
@property
|
|
def object_classes(self) -> dict[str, ObjectClass]:
|
|
#def object_classes(self):
|
|
if self.__object_classes_by_oid is None:
|
|
res = self.find(
|
|
base = '',
|
|
scope = ldap.SCOPE_BASE, # pyright: ignore[reportAttributeAccessIssue]
|
|
filterstr = '(objectClass=*)',
|
|
attrlist = ['subschemaSubentry']
|
|
)
|
|
dn = res[0][1]['subschemaSubentry'][0].decode(
|
|
'utf-8'
|
|
) # Usually yields cn=Subschema
|
|
res = self.find(
|
|
base = dn,
|
|
scope = ldap.SCOPE_BASE, # pyright: ignore[reportAttributeAccessIssue]
|
|
filterstr = '(objectClass=*)',
|
|
attrlist = ['*', '+']
|
|
)
|
|
subschema_entry = res[0]
|
|
subschema_subentry = ldap.cidict.cidict( # pyright: ignore[reportAttributeAccessIssue]
|
|
subschema_entry[1]
|
|
)
|
|
subschema = ldap.schema.SubSchema( # pyright: ignore[reportAttributeAccessIssue]
|
|
subschema_subentry
|
|
)
|
|
object_class_oids = subschema.listall(ObjectClass)
|
|
self.__object_classes_by_oid = {
|
|
oid: subschema.get_obj(ObjectClass, oid)
|
|
for oid in object_class_oids
|
|
}
|
|
return self.__object_classes_by_oid
|
|
|
|
@property
|
|
def object_class_by_name(self) -> dict[str, ObjectClass]:
|
|
if self.__object_classes_by_name is None:
|
|
ret: dict[str, ObjectClass] = {}
|
|
self.__object_classes_by_name = ret
|
|
for oid, oc in self.object_classes.items():
|
|
ret[oid] = oc
|
|
for name in oc.names:
|
|
ret[name] = oc
|
|
ret[name.lower()] = oc
|
|
return self.__object_classes_by_name
|
|
|
|
def __oc_recurse_to_top(
|
|
self, cur: str | ObjectClass, cb: Any, context: Any
|
|
) -> None:
|
|
cur_oc = cur if isinstance(cur, ObjectClass) else self.object_class_by_name[
|
|
cur.lower()]
|
|
for s in cur_oc.sup:
|
|
self.__oc_recurse_to_top(s, cb, context)
|
|
cb(cur_oc, context)
|
|
|
|
def object_class_path(self, leaf: str | ObjectClass) -> Any:
|
|
|
|
def cb(oc: Any, context: Any) -> None:
|
|
ret.append(oc)
|
|
|
|
ret: list[str] = []
|
|
self.__oc_recurse_to_top(leaf, cb, None)
|
|
return reversed(ret)
|
|
|
|
@property
|
|
def object_class_tree(self) -> nx.Graph:
|
|
|
|
if self.__object_class_tree is None:
|
|
|
|
def collect(root: ObjectClass, attr: str) -> set[str]:
|
|
ret: set[str] = set()
|
|
|
|
def cb(oc: Any, attr: Any) -> None:
|
|
vals = getattr(oc, attr)
|
|
if vals is None:
|
|
return
|
|
for val in vals:
|
|
ret.add(val)
|
|
|
|
self.__oc_recurse_to_top(root, cb, attr)
|
|
return ret
|
|
|
|
kind = {0: 'STRUCTURAL', 1: 'ABSTRACT', 2: 'AUXILIARY'}
|
|
ret = nx.DiGraph()
|
|
for oid, oc in self.object_classes.items():
|
|
ret.add_node(
|
|
oid,
|
|
oid = oid,
|
|
name = oc.names[0],
|
|
kind = kind[oc.kind],
|
|
must = ', '.join(collect(oc, 'must')),
|
|
may = ', '.join(collect(oc, 'may'))
|
|
)
|
|
for base_class in oc.sup:
|
|
try:
|
|
ret.add_edge(
|
|
oid, self.object_class_by_name[base_class.lower()].oid
|
|
)
|
|
except Exception as e:
|
|
slog(
|
|
WARNING,
|
|
f'Failed to add edge {oid}:{oc.names} -> {base_class} ({e})'
|
|
)
|
|
self.__object_class_tree = ret
|
|
return self.__object_class_tree
|
|
|
|
def object_class_attrs(
|
|
self,
|
|
oc: str | ObjectClass,
|
|
required: AttrType = AttrType.Must,
|
|
origins: bool = False
|
|
) -> dict[str, set[str]] | set[str]:
|
|
all_attrs: set[str] = set()
|
|
attrs_by_origin: dict[str, set[str]] = {}
|
|
for oc in self.object_class_path(oc):
|
|
cur = set()
|
|
if required & self.AttrType.Must:
|
|
cur |= set(oc.must) # pyright: ignore[reportAttributeAccessIssue]
|
|
if required & self.AttrType.May:
|
|
cur |= set(oc.may) # pyright: ignore[reportAttributeAccessIssue]
|
|
if cur:
|
|
all_attrs |= cur
|
|
attrs_by_origin[str(oc)] = cur
|
|
return attrs_by_origin if origins else all_attrs
|
|
|
|
def is_derived_class(self, name: str, base_candidate: str) -> bool:
|
|
#oid = self.object_class_by_name[name].oid
|
|
#base_oid = self.object_class_by_name[base_candidate].oid
|
|
#if base_oid in [oc.oid for oc in self.object_class_path(name)]:
|
|
# return True
|
|
return bool(
|
|
nx.has_path(
|
|
self.object_class_tree,
|
|
self.object_class_by_name[name.lower()].oid,
|
|
self.object_class_by_name[base_candidate.lower()].oid
|
|
)
|
|
)
|
|
|
|
def default_config() -> Config: # export
|
|
return Config()
|
|
|
|
def bind(conf: Config | BaseConfig | None = None) -> Connection:
|
|
return Connection(conf)
|