Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BP-1232] Connectionpool blacklist #181

Merged
merged 6 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (identifier == null) {
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
11 changes: 11 additions & 0 deletions src/CommonLib/LdapConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
internal class LdapConnectionPool : IDisposable{
private readonly ConcurrentBag<LdapConnectionWrapper> _connections;
private readonly ConcurrentBag<LdapConnectionWrapper> _globalCatalogConnection;
private readonly SemaphoreSlim _semaphore;

Check warning on line 22 in src/CommonLib/LdapConnectionPool.cs

View workflow job for this annotation

GitHub Actions / build

Field 'LdapConnectionPool._semaphore' is never assigned to, and will always have its default value null

Check warning on line 22 in src/CommonLib/LdapConnectionPool.cs

View workflow job for this annotation

GitHub Actions / build

Field 'LdapConnectionPool._semaphore' is never assigned to, and will always have its default value null
private readonly string _identifier;
private readonly string _poolIdentifier;
private readonly LdapConfig _ldapConfig;
Expand All @@ -31,6 +31,7 @@
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();
definitelynotagoblin marked this conversation as resolved.
Show resolved Hide resolved

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 @@
}

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

if (!_connections.TryTake(out var connectionWrapper)) {
var (success, connection, message) = await CreateNewConnection();
if (!success) {
Expand All @@ -613,6 +618,10 @@
}

public async Task<(bool Success, LdapConnectionWrapper ConnectionWrapper, string Message)> GetGlobalCatalogConnectionAsync() {
if (_blacklistedDomains.Contains(_identifier)) {
return (false, null, $"Identifier {_identifier} blacklisted for connection attempt");
definitelynotagoblin marked this conversation as resolved.
Show resolved Hide resolved
}

if (!_globalCatalogConnection.TryTake(out var connectionWrapper)) {
var (success, connection, message) = await CreateNewConnection(true);
if (!success) {
Expand Down Expand Up @@ -691,6 +700,7 @@
_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 +735,7 @@
}
} 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
Loading