Search icon
Script icon
If icon
Log icon
SlackIncomingWebhook icon
Fail icon
Pause icon
Delete icon
Schedule icon

Delete Orphaned LDAP Entries Whose Owner DN No Longer Resolves

Find LDAP entries whose owner DN no longer resolves, delete the aged orphans after approval, then re-search to prove the delete landed.

Categories
Infrastructure

Decommission a service or a host and its LDAP entry usually stays. The owning group is deleted, the entry that pointed at it is not, so a bindable account outlives the thing it was created for. Nothing errors, so nobody notices. This blueprint resolves every entry's own reference attribute against the directory it lives in, finds the ones pointing at a DN that is gone, then deletes the aged ones once a human approves.

Safety

A delete is irreversible and the entries here are authentication objects. Delete the wrong one and the failure is an outage nobody can diagnose, because the account did not break, it stopped existing: binds fail with "invalid credentials", the service looks misconfigured, the entry is not in the directory to inspect. So the flow reports by default, writes only when two inputs are flipped, pauses for a human first, then holds the write behind three guards. An age gate keeps an entry that is still being provisioned. An allowlist of DNs is never touched. A per run ceiling refuses the whole run when the count is higher than expected. In that case the flow writes no delete records at all, so even a hand-edited gate has nothing to act on. Anything the flow cannot read it spares: an entry with no readable createTimestamp is reported rather than deleted, the same for a reference pointing outside the search scope.

How it works

  1. scan_referencing (io.kestra.plugin.ldap.Search) reads every entry under the base DN that carries the reference attribute (owner by default), with its createTimestamp, into an LDIF file.
  2. scan_existing (io.kestra.plugin.ldap.Search) reads every DN that exists under the resolve base. That set is what a reference has to point into. One directory, no outside inventory.
  3. classify (io.kestra.plugin.scripts.python.Script) resolves each entry's references against that set. An entry counts as orphaned only when every one of its reference values fails to resolve, so an entry with a second live owner is left alone. The orphans are then split: deletable, spared by the age gate, spared by the allowlist, spared because the timestamp was unreadable. It writes one LDIF record per deletable DN plus the counts.
  4. findings_alert (io.kestra.plugin.core.flow.If) logs the result then posts it to Slack whenever anything is orphaned, so a read-only run still surfaces the problem.
  5. ceiling_gate fails the run before the approval when the deletable count is over max_deletions.
  6. remediate_gate runs only when auto_remediate is true, dry_run is false and there is something to delete. It pauses, then delete_orphans (io.kestra.plugin.ldap.Delete) deletes each DN in the LDIF.
  7. verify_referencing plus verify_existing re-read the directory and verify_classify re-runs the same resolution. assert_gone fails the run if any deletable orphan is still there. It fails the same way when the entry count dropped below the expected floor.

What you get

  • A list of entries whose owner, manager or other reference DN no longer resolves, which is where a decommissioned service leaves a live auth object behind.
  • A detection that stays inside the directory. There is no second system to keep in sync, so there is nothing to get stale.
  • Three guards on an irreversible write: an age gate, a DN allowlist, a per run ceiling. Each one reports what it spared.
  • A re-search that fails the run when a delete did not land, because the Delete task logs a refused DN and keeps going.
  • An entry count before and after, so a delete that took more than it was asked for fails the run too.

Who it's for

  • IT and identity teams running OpenLDAP or 389 Directory Server who decommission services faster than they clean up the directory.
  • Security teams that need dead auth objects gone rather than listed, with proof they went.
  • Platform teams who want a scheduled audit that is safe to leave on, because it reports by default.

Why orchestrate this with Kestra

Doing this by hand is two ldapsearch runs, a diff in a spreadsheet, an ldapdelete that nobody re-checks. The risky part is not the search, it is deciding what to delete then proving only that went. Kestra puts the detection, the age and allowlist logic, the ceiling, the human approval, the delete plus the re-search in one audited run, with the Slack trail attached. The read-only default makes it safe to schedule.

Tested end to end

Run against OpenLDAP (osixia/openldap 1.5.0) with 11 seeded entries: two owning groups, four entries carrying an owner DN, four OUs. Against the intact directory the scan resolved all four references and reported zero orphans. Deleting both owning groups made four entries dangle and detection found exactly those four with identical inputs. A dry_run pass reported them and left all nine entries in place, confirmed with ldapsearch. A run with min_age_hours at 24 spared the freshly created orphan. The allowlist spared the entry under ou=system. A ceiling of 1 against 2 deletable orphans refused the run before the pause. A declined approval deleted nothing. An approved run deleted both target DNs, the re-search returned zero and ldapsearch went from 9 entries to 7 with only those two gone. A non-leaf orphan proved the re-search gate: the Delete task reported success while the server refused it. The re-search failed the run.

Prerequisites

An LDAP directory reachable from Kestra, plus a bind account allowed to search the tree and delete entries. The bind account also needs to see createTimestamp, which the age gate reads.

Secrets

  • LDAP_PASSWORD: bind password for the account named in bind_dn.
  • SLACK_WEBHOOK_URL: Slack incoming webhook for the findings alert plus the failure alert.

Inputs

  • dry_run (default true) and auto_remediate (default false): both guard every delete.
  • ldap_host, ldap_port, bind_dn, base_dn: connection plus audit scope.
  • entry_filter (default (objectClass=*)) and reference_attribute (default owner): which entries to audit and which DN valued attribute to resolve.
  • resolve_base_dn and resolve_filter: the existence search. A reference pointing outside it is reported as out of scope, never as missing.
  • min_age_hours (default 24): an orphan is deletable only once it is this old.
  • protected_dns (semicolon separated): DNs that are never deleted, including everything under them.
  • max_deletions (default 5): the per run ceiling.

Quick start

  1. Add the LDAP_PASSWORD and SLACK_WEBHOOK_URL secrets to your namespace.
  2. Run with the defaults. The flow reports orphans then writes nothing.
  3. Read the spared lines in the log. Tune min_age_hours, protected_dns plus max_deletions until the deletable list is one you would sign off.
  4. Run with dry_run false and auto_remediate true, then approve the pause to delete.
  5. Enable the schedule once you trust the report. It stays read-only until you change its inputs.

How to extend

  • Set reference_attribute to manager to find people entries whose manager left. Use seeAlso where your schema records the owning object there.
  • Point base_dn at one OU to scope the audit, keeping resolve_base_dn at the directory root so references out of that OU still resolve.
  • Add a step before the delete that writes each target entry to object storage, giving yourself a restore path.
  • Replace the delete with a move into a quarantine OU if your policy wants the entries held before they go.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.