Skip to content

Commit

Permalink
Add domain blacklist to LdapConnectionPool to try to alleviate pain f…
Browse files Browse the repository at this point in the history
…rom repeated query attempts to a domain we can't reach
  • Loading branch information
definitelynotagoblin committed Jan 8, 2025
1 parent ebf78f6 commit bda2bc4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/CommonLib/ConnectionPoolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public void ReleaseConnection(LdapConnectionWrapper connectionWrapper, bool conn
}

private bool GetPool(string identifier, out LdapConnectionPool pool) {
if (string.IsNullOrWhiteSpace(identifier) || identifier == ".") {
if (string.IsNullOrWhiteSpace(identifier)) {
pool = default;
return false;
}

var resolved = ResolveIdentifier(identifier);
if (!_pools.TryGetValue(resolved, out pool)) {
pool = new LdapConnectionPool(identifier, resolved, _ldapConfig,scanner: _portScanner);
pool = new LdapConnectionPool(identifier, resolved, _ldapConfig, scanner: _portScanner);
_pools.TryAdd(resolved, pool);
}

Expand All @@ -96,6 +96,7 @@ private bool GetPool(string identifier, out LdapConnectionPool pool) {
if (globalCatalog) {
return await pool.GetGlobalCatalogConnectionAsync();
}

return await pool.GetConnectionAsync();
}

Expand Down
7 changes: 7 additions & 0 deletions src/CommonLib/LdapConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal class LdapConnectionPool : IDisposable{
private const int BackoffDelayMultiplier = 2;
private const int MaxRetries = 3;
private static readonly ConcurrentDictionary<string, NetAPIStructs.DomainControllerInfo?> DCInfoCache = new();
private static readonly ConcurrentHashSet _blacklistedDomains = new();

public LdapConnectionPool(string identifier, string poolIdentifier, LdapConfig config, PortScanner scanner = null, NativeMethods nativeMethods = null, ILogger log = null) {
_connections = new ConcurrentBag<LdapConnectionWrapper>();
Expand Down Expand Up @@ -595,6 +596,10 @@ private bool CallDsGetDcName(string domainName, out NetAPIStructs.DomainControll
}

public async Task<(bool Success, LdapConnectionWrapper ConnectionWrapper, string Message)> GetConnectionAsync() {
if (_blacklistedDomains.Contains(_identifier)) {
return (false, null, $"Identifier {_identifier} blacklisted for connection attempt");
}

if (!_connections.TryTake(out var connectionWrapper)) {
var (success, connection, message) = await CreateNewConnection();
if (!success) {
Expand Down Expand Up @@ -691,6 +696,7 @@ public void Dispose() {
_log.LogDebug(
"Could not get domain object from GetDomain, unable to create ldap connection for domain {Domain}",
_identifier);
_blacklistedDomains.Add(_identifier);
return (false, null, "Unable to get domain object for further strategies");
}
tempDomainName = domainObject.Name.ToUpper().Trim();
Expand Down Expand Up @@ -725,6 +731,7 @@ public void Dispose() {
}
} catch (Exception e) {
_log.LogInformation(e, "We will not be able to connect to domain {Domain} by any strategy, leaving it.", _identifier);
_blacklistedDomains.Add(_identifier);
}

return (false, null, "All attempted connections failed");
Expand Down

0 comments on commit bda2bc4

Please sign in to comment.