-
Notifications
You must be signed in to change notification settings - Fork 30
Feature/vpcleng/collectors #111
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
vpcleng
wants to merge
2
commits into
main
Choose a base branch
from
feature/vpcleng/collectors
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.
+108
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """Admin license footprint collector. | ||
|
|
||
| CIS Microsoft 365 Foundations Benchmark Controls: | ||
| v6.0.0: 1.1.4 | ||
|
|
||
| Connection Method: Microsoft Graph API | ||
| Required Scopes: Directory.Read.All, User.Read.All | ||
| Graph Endpoints: /directoryRoles, /directoryRoles/{id}/members, /users/{id}, /subscribedSkus | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from collectors.base import BaseDataCollector | ||
| from collectors.graph_client import GraphClient | ||
|
|
||
|
|
||
| class AdminLicenseFootprintDataCollector(BaseDataCollector): | ||
| """Collects license assignments for administrative accounts. | ||
|
|
||
| This collector identifies users with directory roles (admin accounts), | ||
| fetches their assigned licenses, and maps SKU IDs to SKU part numbers. | ||
| """ | ||
|
|
||
| async def collect(self, client: GraphClient) -> dict[str, Any]: | ||
| # Fetch directory roles and members | ||
| roles = await client.get_directory_roles() | ||
|
|
||
| admin_users: dict[str, dict[str, Any]] = {} | ||
| for role in roles: | ||
| role_id = role.get("id") | ||
| role_name = role.get("displayName") | ||
| if not role_id: | ||
| continue | ||
|
|
||
| members = await client.get_role_members(role_id) | ||
| for member in members: | ||
| if member.get("@odata.type") != "#microsoft.graph.user": | ||
| continue | ||
| user_id = member.get("id") | ||
| if not user_id: | ||
| continue | ||
|
|
||
| entry = admin_users.setdefault( | ||
| user_id, | ||
| { | ||
| "id": user_id, | ||
| "displayName": member.get("displayName"), | ||
| "userPrincipalName": member.get("userPrincipalName"), | ||
| "roles": [], | ||
| }, | ||
| ) | ||
| entry["roles"].append(role_name) | ||
|
|
||
| # Map SKU IDs to readable names | ||
| sku_response = await client.get("/subscribedSkus") | ||
| sku_map = { | ||
| sku.get("skuId"): { | ||
| "skuPartNumber": sku.get("skuPartNumber"), | ||
| "prepaidUnits": sku.get("prepaidUnits"), | ||
| } | ||
| for sku in sku_response.get("value", []) | ||
| if sku.get("skuId") | ||
| } | ||
|
|
||
| admin_license_details: list[dict[str, Any]] = [] | ||
| for user_id, info in admin_users.items(): | ||
| user_detail = await client.get( | ||
| f"/users/{user_id}", | ||
| params={ | ||
| "$select": "id,displayName,userPrincipalName,assignedLicenses,accountEnabled", | ||
| }, | ||
| ) | ||
| assigned = user_detail.get("assignedLicenses", []) | ||
| license_skus = [lic.get("skuId") for lic in assigned if lic.get("skuId")] | ||
|
|
||
| admin_license_details.append( | ||
| { | ||
| "id": user_id, | ||
| "displayName": info.get("displayName") or user_detail.get("displayName"), | ||
| "userPrincipalName": info.get("userPrincipalName") | ||
| or user_detail.get("userPrincipalName"), | ||
| "accountEnabled": user_detail.get("accountEnabled"), | ||
| "roles": info.get("roles", []), | ||
| "assignedLicenses": assigned, | ||
| "assignedSkuIds": license_skus, | ||
| } | ||
| ) | ||
|
|
||
| return { | ||
| "admin_users_count": len(admin_users), | ||
| "admin_users": admin_license_details, | ||
| "sku_map": sku_map, | ||
| } | ||
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
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
Oops, something went wrong.
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 collector only processes members whose
@odata.typeis#microsoft.graph.user, so if a directory role is assigned to a group, all users in that group are skipped. In tenants that use group-based role assignments (common for admin roles), the output undercounts admin accounts and therefore misses their license assignments. Consider expanding group membership (e.g., via/transitiveMembers) or resolving group members before buildingadmin_users.Useful? React with 👍 / 👎.