Path: redb-identity/src/redb.Identity.Ldap/LdapSyncRouteBuilder.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/src/redb.Identity.Ldap/LdapSyncRouteBuilder.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Core;
using redb.Route.Ldap;
using static redb.Route.Ldap.LdapDsl;
namespace redb.Identity.Ldap;
///
/// Route definition that watches an LDAP directory for changes and syncs
/// user profiles (and optionally group memberships) to the Identity system.
///
public sealed class LdapSyncRouteBuilder : RouteBuilder
{
private readonly LdapSyncOptions _options;
private readonly ILogger? _logger;
public LdapSyncRouteBuilder(LdapSyncOptions options, ILogger? logger = null)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
_options.Validate();
_logger = logger;
}
protected override void Configure()
{
From(BuildWatchUri())
.RouteId(_options.RouteId)
.Log($"LDAP sync: ${{header.{LdapHeaders.ChangeType}}} ${{header.{LdapHeaders.Dn}}}")
.Bean(async (handler, exchange, ct) =>
{
if (exchange.In.Body is LdapEntry entry)
{
try
{
await handler.ProcessEntryAsync(entry, ct);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
_logger?.LogError(ex, "Failed to process LDAP entry: {Dn}", entry.Dn);
}
}
else
{
_logger?.LogDebug("Unexpected body type in sync route: {Type}",
exchange.In.Body?.GetType());
}
});
}
private string BuildWatchUri()
{
var builder = Watch(_options.UserBaseDn)
.Server(_options.Server)
.Port(_options.Port)
.Filter(_options.UserFilter)
.PollInterval(_options.PollIntervalMs)
.ChangeTracking(ParseChangeTrackingMode());
if (_options.InitialLoad)
builder.InitialLoad();
if (_options.DetectDeletions)
builder.DetectDeletions();
if (_options.FullSyncInterval > 0)
builder.FullSyncInterval(_options.FullSyncInterval);
// Connection settings
if (!string.IsNullOrWhiteSpace(_options.BindDn))
builder.BindDn(_options.BindDn);
if (!string.IsNullOrWhiteSpace(_options.BindPassword))
builder.BindPassword(_options.BindPassword);
if (_options.UseSsl)
builder.Ssl();
if (_options.UseStartTls)
builder.StartTls();
if (_options.SkipCertificateValidation)
builder.TrustAllCertificates();
if (_options.Attributes.Length > 0)
builder.Attributes(_options.Attributes);
return builder.Build();
}
private LdapChangeTrackingMode ParseChangeTrackingMode() =>
_options.ChangeTrackingMode?.ToLowerInvariant() switch
{
"modifytimestamp" => LdapChangeTrackingMode.ModifyTimestamp,
"usn" => LdapChangeTrackingMode.Usn,
"persistent" => LdapChangeTrackingMode.Persistent,
_ => throw new InvalidOperationException(
$"Unknown ChangeTrackingMode: '{_options.ChangeTrackingMode}'.")
};
}