Skip to content

Commit

Permalink
Ignore escaped commas when parsing active directory DNs (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
boavenn authored Oct 22, 2024
1 parent 0339204 commit 3822216
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public static String getRelativeDN(String distinguishedName,
}

public static String getParentDN(String distinguishedName) {
var dnParts = Arrays.stream(distinguishedName.split(",")).toList();
var commasIgnoringEscapedRegex = "(?<!\\\\),";
var dnParts = Arrays.stream(distinguishedName.split(commasIgnoringEscapedRegex)).toList();
if (dnParts.size() == 1) {
return "";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ private static String getRDN(SearchResult object) {
}

private static String getRDN(String distinguishedName) {
return distinguishedName.split(",", 2)[0];
var commasIgnoringEscapedRegex = "(?<!\\\\),";
return distinguishedName.split(commasIgnoringEscapedRegex, 2)[0];
}

public List<SearchResult> getDirectChildrenObjects() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ private Optional<ActiveDirectoryNode> searchNode(String distinguishedName) {
if (relativeDn.isBlank()) {
return Optional.of(root);
}
var dnParts = Arrays.stream(relativeDn.split(",")).toList();
var commasIgnoringEscapedRegex = "(?<!\\\\),";
var dnParts = Arrays.stream(relativeDn.split(commasIgnoringEscapedRegex)).toList();
return searchNode(root, dnParts);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ private User getUserLeaderUnsafe(User user) throws NamingException {
private List<String> extractOrganizationalUnitsDNs(String distinguishedName) {
var result = new ArrayList<String>();

var components = distinguishedName.split(",");
var commasIgnoringEscapedRegex = "(?<!\\\\),";
var components = distinguishedName.split(commasIgnoringEscapedRegex);

for (var i = 0; i < components.length; i++) {
var component = components[i].trim();
if (component.startsWith("OU=")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class ActiveDirectoryUtilsTest extends Specification {
result == parentDN

where:
actualDN | parentDN
"OU=SomeNestedTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl"
"OU=Teams" | ""
"" | ""
actualDN | parentDN
"OU=Some name with\\, comma,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
"OU=SomeNestedTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl"
"OU=Teams" | ""
"" | ""
}

def "getRelativeDN WHEN base matches, ends with or differs SHOULD return correct relative DN"() {
Expand All @@ -28,14 +29,14 @@ class ActiveDirectoryUtilsTest extends Specification {
result == expectedRelativeDN

where:
distinguishedName | base | expectedRelativeDN
"OU=SomeNestedTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"| "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"| "OU=SomeNestedTeam"
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam"
"OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl" | ""
"OU=RandomTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"| "OU=RandomTeam"
"OU=Teams,DC=fingo,DC=pl" | "OU=DifferentBase,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl"
"" | "" | ""
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
distinguishedName | base | expectedRelativeDN
"OU=SomeNestedTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeNestedTeam"
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam"
"OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl" | ""
"OU=RandomTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=RandomTeam"
"OU=Teams,DC=fingo,DC=pl" | "OU=DifferentBase,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl"
"" | "" | ""
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
}


Expand Down

0 comments on commit 3822216

Please sign in to comment.