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

Fix for exception thrown by GPOLocalGroupProcessor #142

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions src/CommonLib/ConnectionPoolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public void ReleaseConnection(LdapConnectionWrapper connectionWrapper, bool conn

public async Task<(bool Success, LdapConnectionWrapper ConnectionWrapper, string Message)> GetLdapConnection(
string identifier, bool globalCatalog) {
if (identifier == null) {
return (false, default, "Provided a null identifier for the connection");
}
var resolved = ResolveIdentifier(identifier);

if (!_pools.TryGetValue(resolved, out var pool)) {
Expand Down Expand Up @@ -72,8 +75,7 @@ private string ResolveIdentifier(string identifier) {
if (_resolvedIdentifiers.TryGetValue(identifier, out var resolved)) {
return resolved;
}



if (GetDomainSidFromDomainName(identifier, out var sid)) {
_log.LogDebug("Resolved identifier {Identifier} to {Resolved}", identifier, sid);
_resolvedIdentifiers.TryAdd(identifier, sid);
Expand Down
20 changes: 17 additions & 3 deletions src/CommonLib/Processors/GPOLocalGroupProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Task<ResultingGPOChanges> ReadGPOLocalGroups(IDirectoryObject entry) {
return ReadGPOLocalGroups(links, dn);
}

return default;
return Task.FromResult(new ResultingGPOChanges());
}

public async Task<ResultingGPOChanges> ReadGPOLocalGroups(string gpLink, string distinguishedName) {
Expand All @@ -63,13 +63,26 @@ public async Task<ResultingGPOChanges> ReadGPOLocalGroups(string gpLink, string
if (gpLink == null)
return ret;

string domain;
//If our dn is null, use our default domain
if (string.IsNullOrEmpty(distinguishedName)) {
if (!_utils.GetDomain(out var d)) {
return ret;
}

domain = d.Name;
} else {
domain = Helpers.DistinguishedNameToDomain(distinguishedName);
}

// First lets check if this OU actually has computers that it contains. If not, then we'll ignore it.
// Its cheaper to fetch the affected computers from LDAP first and then process the GPLinks
var affectedComputers = new List<TypedPrincipal>();
await foreach (var result in _utils.Query(new LdapQueryParameters() {
LDAPFilter = new LdapFilter().AddComputersNoMSAs().GetFilter(),
Attributes = CommonProperties.ObjectSID,
SearchBase = distinguishedName
SearchBase = distinguishedName,
DomainName = domain
})) {
if (!result.IsSuccess) {
break;
Expand Down Expand Up @@ -119,7 +132,8 @@ public async Task<ResultingGPOChanges> ReadGPOLocalGroups(string gpLink, string
LDAPFilter = new LdapFilter().AddAllObjects().GetFilter(),
SearchScope = SearchScope.Base,
Attributes = CommonProperties.GPCFileSysPath,
SearchBase = linkDn
SearchBase = linkDn,
DomainName = gpoDomain
}).DefaultIfEmpty(LdapResult<IDirectoryObject>.Fail()).FirstOrDefaultAsync();

if (!result.IsSuccess) {
Expand Down
13 changes: 7 additions & 6 deletions test/unit/GPOLocalGroupProcessorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,15 @@ public async Task GPOLocalGroupProcessor_ReadGPOLocalGroups_Null_Gpcfilesyspath(
var processor = new GPOLocalGroupProcessor(mockLDAPUtils.Object);
var testGPLinkProperty =
"[LDAP:/o=foo/ou=foo Group (ABC123)/cn=foouser (blah)123/dc=somedomain;0;][LDAP:/o=foo/ou=foo Group (ABC123)/cn=foouser (blah)123/dc=someotherdomain;2;]";
var result = await processor.ReadGPOLocalGroups(testGPLinkProperty, null);
var result = await processor.ReadGPOLocalGroups(testGPLinkProperty, "DC=Testlab,DC=Local");

Assert.NotNull(result);
Assert.Single(result.AffectedComputers);
var actual = result.AffectedComputers.First();
Assert.Equal(Label.Computer, actual.ObjectType);
Assert.Equal("teapot", actual.ObjectIdentifier);
}

[Fact]
[WindowsOnlyFact]
public async Task GPOLocalGroupProcessor_ReadGPOLocalGroups() {
var mockLDAPUtils = new Mock<ILdapUtils>(MockBehavior.Loose);
var gpcFileSysPath = Path.GetTempPath();
Expand All @@ -182,15 +181,17 @@ public async Task GPOLocalGroupProcessor_ReadGPOLocalGroups() {
.Returns(mockComputerResults.ToAsyncEnumerable)
.Returns(mockGCPFileSysPathResults.ToAsyncEnumerable)
.Returns(Array.Empty<LdapResult<IDirectoryObject>>().ToAsyncEnumerable);
var domain = MockableDomain.Construct("TESTLAB.LOCAL");
mockLDAPUtils.Setup(x => x.GetDomain(out domain)).Returns(true);

var processor = new GPOLocalGroupProcessor(mockLDAPUtils.Object);


var testGPLinkProperty =
"[LDAP:/o=foo/ou=foo Group (ABC123)/cn=foouser (blah)123/dc=somedomain;0;][LDAP:/o=foo/ou=foo Group (ABC123)/cn=foouser (blah)123/dc=someotherdomain;2;]";
var result = await processor.ReadGPOLocalGroups(testGPLinkProperty, null);

mockLDAPUtils.VerifyAll();
Assert.NotNull(result);

//mockLDAPUtils.VerifyAll();
Assert.Single(result.AffectedComputers);
var actual = result.AffectedComputers.First();
Assert.Equal(Label.Computer, actual.ObjectType);
Expand Down
Loading