-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #781 from d-ylee/713-bug-adopt-rfc-certificate-gen…
…erator-from-631-into-user_to_site_mappingpy Use rfc2553dn generator in user_to_site_mapping.py
- Loading branch information
Showing
2 changed files
with
54 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Common utility functions between scripts | ||
""" | ||
|
||
RFC_ATTRIBUTE_ORDER = ["CN", "L", "ST", "O", "OU", "C", "STREET", "DC", "UID"] | ||
|
||
def get_attribute(attribute: str) -> str: | ||
"""Extracts attribute key""" | ||
try: | ||
return attribute[:attribute.index("=")] | ||
except ValueError: | ||
# print(f"element lacks attribute, {attribute}, {dn}") | ||
return "" | ||
|
||
|
||
def rfc2253dn(legacy_dn: str, verbose: bool = False) -> str: | ||
"""Converts legacy slash DB to comma separated DN""" | ||
if not legacy_dn.startswith('/'): | ||
if verbose: | ||
print(f'DN does not start with /: {legacy_dn}') | ||
return legacy_dn | ||
|
||
# replace commas with an escape character | ||
legacy_dn = legacy_dn.replace(',', r'\,') | ||
parts = legacy_dn.split('/')[1:] | ||
|
||
# parts are reversed. See https://datatracker.ietf.org/doc/html/rfc2253#section-2.3 | ||
elements = parts[::-1] | ||
attributes = [get_attribute(e) for e in elements] | ||
|
||
# skip any DNs who have an element without an attribute | ||
if "" in attributes: | ||
return "" | ||
|
||
if verbose: | ||
print(f"Attributes: {attributes}") | ||
print(f"Expected order: {RFC_ATTRIBUTE_ORDER}") | ||
|
||
indexes = [] | ||
for attr in attributes: | ||
try: | ||
indexes.append(RFC_ATTRIBUTE_ORDER.index(attr)) | ||
except ValueError: | ||
# skips any DNs that don't have a attribute in the RFC_ATTRIBUTE_ORDER | ||
pass | ||
|
||
# sort existing attributes based on expected attribute order | ||
result = [a[0] for a in sorted(zip(elements, attributes, indexes), key=lambda x: x[2])] | ||
|
||
if verbose: | ||
print(f"original: {legacy_dn}\nindexes: {indexes}\nconverted: {','.join(result)}") | ||
|
||
return ','.join(result) |