|
| 1 | +from graphviz import Digraph |
| 2 | +from .util import create_session, paginate |
| 3 | + |
| 4 | + |
| 5 | +def add_child_nodes(ou_id, org_client, graph): |
| 6 | + """Adds child nodes to the graph. |
| 7 | +
|
| 8 | + Args: |
| 9 | + ou_id (str): The ID of the organizational unit. |
| 10 | + org_client (boto3.client): The AWS Organizations client. |
| 11 | + graph (Digraph): The Graphviz Digraph object. |
| 12 | +
|
| 13 | + """ |
| 14 | + accounts = list_children(org_client, ou_id, 'ACCOUNT') |
| 15 | + ous = list_children(org_client, ou_id, 'ORGANIZATIONAL_UNIT') |
| 16 | + |
| 17 | + children = accounts + ous |
| 18 | + |
| 19 | + if children: |
| 20 | + for child in children: |
| 21 | + child_id = child['Id'] |
| 22 | + child_type = child['Type'] |
| 23 | + |
| 24 | + if child_type == 'ACCOUNT': |
| 25 | + account = org_client.describe_account(AccountId=child_id).get('Account') |
| 26 | + account_name = account.get('Name') |
| 27 | + account_id = account.get('Id') |
| 28 | + |
| 29 | + graph.node(child_id, label=account_name, shape='ellipse') |
| 30 | + graph.edge(ou_id, child_id) |
| 31 | + |
| 32 | + policies = get_policies_for_entity(account_id, org_client) |
| 33 | + |
| 34 | + add_policies_to_graph(graph, child_id, policies=policies) |
| 35 | + |
| 36 | + # Get the name of the child (OU or Account) |
| 37 | + if child_type == 'ORGANIZATIONAL_UNIT': |
| 38 | + current_ou = org_client.describe_organizational_unit(OrganizationalUnitId=child_id).get('OrganizationalUnit') |
| 39 | + ou_name = current_ou.get('Name') |
| 40 | + current_ou_id = current_ou.get('Id') |
| 41 | + |
| 42 | + graph.node(child_id, label=ou_name, shape='box') |
| 43 | + graph.edge(ou_id, child_id) |
| 44 | + |
| 45 | + policies = get_policies_for_entity(current_ou_id, org_client) |
| 46 | + add_policies_to_graph(graph, child_id, policies=policies) |
| 47 | + |
| 48 | + add_child_nodes(child_id, org_client, graph) |
| 49 | + |
| 50 | + |
| 51 | +def list_children(org_client, parent_id, child_type): |
| 52 | + """Lists the children of a parent entity. |
| 53 | +
|
| 54 | + Args: |
| 55 | + org_client (boto3.client): The AWS Organizations client. |
| 56 | + parent_id (str): The ID of the parent entity. |
| 57 | + child_type (str): The type of the child entities to list. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + list: A list of child entities. |
| 61 | + """ |
| 62 | + all_children = paginate(org_client, 'list_children', ParentId=parent_id, ChildType=child_type) |
| 63 | + children = [ child for page in all_children for child in page.get('Children')] |
| 64 | + return children |
| 65 | + |
| 66 | + |
| 67 | +def get_policies_for_entity(entity_id, org_client, filter='SERVICE_CONTROL_POLICY'): |
| 68 | + """Gets the policies associated with an entity. |
| 69 | +
|
| 70 | + Args: |
| 71 | + entity_id (str): The ID of the entity. |
| 72 | + org_client (boto3.client): The AWS Organizations client. |
| 73 | + filter (str): The filter to apply when retrieving policies. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + list: A list of policies associated with the entity. |
| 77 | + """ |
| 78 | + policies = org_client.list_policies_for_target( |
| 79 | + TargetId=entity_id, |
| 80 | + Filter=filter |
| 81 | + ) |
| 82 | + return policies.get('Policies') |
| 83 | + |
| 84 | + |
| 85 | +def add_policies_to_graph(graph, entity_id, policies=None): |
| 86 | + """Adds policies to the graph. |
| 87 | +
|
| 88 | + Args: |
| 89 | + graph (Digraph): The Graphviz Digraph object. |
| 90 | + entity_id (str): The ID of the entity. |
| 91 | + policies (list): A list of policies to add to the graph. |
| 92 | + """ |
| 93 | + if policies: |
| 94 | + for policy in policies: |
| 95 | + policy_name = policy.get('Name') |
| 96 | + graph.node(policy_name, label=policy_name, shape='trapezium') |
| 97 | + graph.edge(entity_id, policy_name) |
| 98 | + |
| 99 | + |
| 100 | +def visualize_policies(profile, outdir): |
| 101 | + session = create_session(profile) |
| 102 | + org_client = session.client('organizations') |
| 103 | + |
| 104 | + # Initialize a Graphviz Digraph object |
| 105 | + graph = Digraph('AWS_Organizations', graph_attr={'rankdir':'LR'}) |
| 106 | + |
| 107 | + # Get the root information |
| 108 | + root_id = org_client.list_roots()['Roots'][0]['Id'] |
| 109 | + graph.node(root_id, label="Root", shape='box') |
| 110 | + get_policies_for_entity(root_id, org_client) |
| 111 | + add_policies_to_graph(graph, root_id) |
| 112 | + |
| 113 | + # Start building the tree |
| 114 | + add_child_nodes(root_id, org_client, graph) |
| 115 | + |
| 116 | + # Output the graphical tree hierarchy to a file |
| 117 | + graph.render(directory=outdir, filename='aws_org_tree', view=True) |
| 118 | + |
0 commit comments