From: Douglas Bagnall Date: Sun, 17 Aug 2025 08:34:57 +0000 (+0000) Subject: py:key_credential_link: filter_kcl_list helper for samba-tool X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ca754d8f25c0753d02859a97d7f2664a8b46462;p=thirdparty%2Fsamba.git py:key_credential_link: filter_kcl_list helper for samba-tool This will be used in `samba-tool user keytrust delete` and `samba-tool computer keytrust delete` and is mainly to deduplicate that code. Potentially it could also be used in `keytrust view`. Signed-off-by: Douglas Bagnall Reviewed-by: Gary Lockyer --- diff --git a/python/samba/key_credential_link.py b/python/samba/key_credential_link.py index 7193e0ddda2..f8c82e1a82b 100644 --- a/python/samba/key_credential_link.py +++ b/python/samba/key_credential_link.py @@ -352,3 +352,30 @@ def kcl_in_list(kcl: KeyCredentialLinkDn, others: Iterable[KeyCredentialLinkDn]) if km == other.key_material() and kcl.dn == other.dn: return True return False + + +def filter_kcl_list(samdb: SamDB, + keycredlinks: Iterable[KeyCredentialLinkDn], + link_target: Optional[str] = None, + fingerprint: Optional[str] = None) -> list: + """Select only the input links that match at least one of the + criteria. + """ + # used in samba-tool X keytrust delete + selected = [] + filters = [] + if link_target is not None: + target_dn = Dn(samdb, link_target) + filters.append(lambda x: x.dn == target_dn) + + if fingerprint is not None: + fingerprint = fingerprint.upper() + filters.append(lambda x: x.fingerprint() == fingerprint) + + for x in keycredlinks: + for fn in filters: + if fn(x): + selected.append(x) + break + + return selected