-
Notifications
You must be signed in to change notification settings - Fork 13
Improve LDAP user info retrieval #240
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
Open
AditiChikkali
wants to merge
3
commits into
NSLS2:main
Choose a base branch
from
AditiChikkali:feature/whoami-test-page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,51 +7,101 @@ | |
|
|
||
|
|
||
| def to_hex(val): | ||
|
|
||
| if isinstance(val, bytes): | ||
| return binascii.hexlify(val).decode() | ||
| return None | ||
|
|
||
| OPERATIONAL_ATTRIBUTES = [ | ||
| 'manager', | ||
| 'objectGUID', | ||
| 'objectSid', | ||
| 'whenCreated', | ||
| 'whenChanged', | ||
| 'logonCount', | ||
| 'lastLogon', | ||
| 'sAMAccountType', | ||
| 'street', | ||
| 'badPasswordTime', | ||
| 'memberOf' | ||
| ] | ||
|
|
||
|
|
||
| def get_user_info(upn, ldap_server, ldap_base_dn, ldap_bind_user, bind_password): | ||
| conn = None | ||
| conn = None | ||
| try: | ||
| server = Server(ldap_server) | ||
| conn = Connection(server, user=ldap_bind_user, password=bind_password, auto_bind=True) | ||
| search_filter = f"(&(objectclass=person)(userPrincipalName={upn}))" | ||
| conn.search(ldap_base_dn, search_filter, attributes=['sAMAccountName']) | ||
|
|
||
| username = upn.split("@")[0] | ||
| posix_filter = f"(&(objectclass=posixaccount)(sAMAccountName={username}))" | ||
|
|
||
| # SEARCH 1: Get all regular attributes with ['*'] | ||
| conn.search(ldap_base_dn, posix_filter, attributes=['*']) | ||
|
|
||
| if not conn.entries: | ||
| logger.warning("No entries found for the given UPN.") | ||
| logger.warning(f"No posixaccount entries found for username: {username}") | ||
| return None | ||
|
|
||
| entry = conn.entries[0] | ||
| username = entry.sAMAccountName.value if 'sAMAccountName' in entry else None | ||
| if username is None: | ||
| return None | ||
| user = _extract_attributes(entry) | ||
|
|
||
| search_filter = f"(&(objectclass=posixaccount)(sAMAccountName={username}))" | ||
| conn.search(ldap_base_dn, search_filter, attributes=['*']) | ||
| # SEARCH 2: Always fetch operational attributes explicitly | ||
| logger.info(f"Search 2 - fetching operational attributes for: {username}") | ||
| conn.search(ldap_base_dn, posix_filter, attributes=OPERATIONAL_ATTRIBUTES) | ||
|
|
||
| if not conn.entries: | ||
| logger.warning("no posix entries found for the given username.") | ||
| return None | ||
| if conn.entries: | ||
| operational = _extract_attributes(conn.entries[0]) | ||
|
|
||
| for attr, value in operational.items(): | ||
| if attr not in user or user[attr] is None or user[attr] == "": | ||
| user[attr] = value | ||
|
|
||
| # SEARCH 3: Check for still-missing operational attributes | ||
| missing_ops = [attr for attr in OPERATIONAL_ATTRIBUTES if not user.get(attr)] | ||
|
|
||
| if missing_ops: | ||
| logger.warning(f"Still missing after search 2 for {username}: {missing_ops}") | ||
|
|
||
| # Fetching each missing attribute individually hoping to bypass proxy cache since each is a unique query | ||
| for attr in missing_ops: | ||
| logger.info(f"Search 3 - fetching individual attribute '{attr}' for {username}") | ||
| conn.search(ldap_base_dn, posix_filter, attributes=[attr]) | ||
| if conn.entries: | ||
| val = _extract_attributes(conn.entries[0]) | ||
| if val.get(attr): | ||
| user[attr] = val[attr] | ||
|
|
||
| # FINAL VALIDATION: Log what we have and what's still missing | ||
| final_missing = [attr for attr in OPERATIONAL_ATTRIBUTES if not user.get(attr)] | ||
| if final_missing: | ||
| logger.warning(f"Final missing attributes for {username}: {final_missing} — user may not have these set in LDAP") | ||
| else: | ||
| logger.info(f"All operational attributes present for {username}") | ||
|
|
||
| logger.info(f"Final user dict has {len(user)} attributes for {username}") | ||
|
|
||
| entry = conn.entries[0] | ||
| user = dict() | ||
| for attribute in entry.entry_attributes: | ||
| value = entry[attribute].value | ||
| if attribute in ("objectGUID", "objectSid"): | ||
| user[attribute] = value # keep as bytes | ||
| else: | ||
| user[attribute] = str(value) | ||
| return user | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"LDAP Error: {e}") | ||
| logger.error(f"LDAP Error for UPN {upn}: {e}", exc_info=True) | ||
| return None | ||
| finally: | ||
| if conn is not None: | ||
| conn.unbind() | ||
|
|
||
|
|
||
| def _extract_attributes(entry): | ||
| data = {} | ||
| for attribute in entry.entry_attributes: | ||
| value = entry[attribute].value | ||
| if isinstance(value, bytes): | ||
| data[attribute] = value | ||
| elif isinstance(value, list): | ||
| data[attribute] = value | ||
| else: | ||
| data[attribute] = str(value) if value is not None else None | ||
| return data | ||
|
|
||
| def filetime_to_str(filetime): | ||
| try: | ||
| if filetime is None or int(filetime) == 0 or int(filetime) == 9223372036854775807: | ||
|
|
@@ -63,11 +113,15 @@ def filetime_to_str(filetime): | |
|
|
||
| def generalized_time_to_str(gt): | ||
| try: | ||
| if not gt: return "" | ||
| dt = datetime.strptime(gt.split(".")[0], "%Y%m%d%H%M%S") | ||
| if not gt: | ||
| return "" | ||
| if isinstance(gt, datetime): | ||
| return gt.strftime("%Y-%m-%d %H:%M:%S UTC") | ||
| gt_str = str(gt) | ||
| dt = datetime.strptime(gt_str.split(".")[0], "%Y%m%d%H%M%S") | ||
| return dt.strftime("%Y-%m-%d %H:%M:%S UTC") | ||
| except Exception: | ||
| return str(gt) | ||
| return str(gt) if gt else "" | ||
|
|
||
| def decode_uac(uac): | ||
| flags = [] | ||
|
|
@@ -87,11 +141,20 @@ def clean_groups(groups_val): | |
| if not groups_val: | ||
| return [] | ||
| if isinstance(groups_val, list): | ||
| return groups_val | ||
| return [str(g) for g in groups_val] | ||
| elif isinstance(groups_val, str): | ||
| return [g.strip() for g in groups_val.replace("\n", ",").split(",") if g.strip()] | ||
| return [] | ||
|
|
||
| def clean_object_class(obj_class_val): | ||
| if not obj_class_val: | ||
| return [] | ||
| if isinstance(obj_class_val, list): | ||
| return [str(s).strip() for s in obj_class_val] | ||
| elif isinstance(obj_class_val, str): | ||
| return [s.strip() for s in obj_class_val.replace(",", " ").split() if s.strip()] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might not work as intended if the value includes spaces or escaped commas It would be fine to merge this and then update later iso needed though. |
||
| return [] | ||
|
|
||
| return { | ||
| "dn": dn or user_info.get("distinguishedName"), | ||
| "status": status, | ||
|
|
@@ -142,6 +205,6 @@ def clean_groups(groups_val): | |
| "codePage": user_info.get("codePage"), | ||
| "countryCode": user_info.get("countryCode"), | ||
| "instanceType": user_info.get("instanceType"), | ||
| "objectClass": [s.strip() for s in user_info.get("objectClass", "").split() if s.strip()] | ||
| "objectClass": clean_object_class(user_info.get("objectClass")) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably still get logged as f"No LDAP posix entries found for {username}" or something similar.
Is it intentional to exit here in that case, or is it supposed to try the "recovery step" with OPERATIONAL_ATTRIBUTES before exiting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous code required additional updates, I have also discovered new observations with inconsistencies, which led to further adjustments. I hope the latest changes address this comment.