Skip to content

Commit 1a2c80f

Browse files
author
Erik Steringer
committed
fixed up AWS Organizations code for non-aws partitions
1 parent 94a14b9 commit 1a2c80f

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

principalmapper/graphing/graph_cli.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from principalmapper.graphing.gathering import get_organizations_data
2929
from principalmapper.graphing.edge_identification import checker_map
3030
from principalmapper.querying import query_orgs
31-
from principalmapper.util import botocore_tools
31+
from principalmapper.util import botocore_tools, arns
3232
from principalmapper.util.storage import get_storage_root, get_default_graph_path
3333

3434

@@ -149,9 +149,13 @@ def process_arguments(parsed_args: Namespace):
149149
stsclient = session.create_client('sts')
150150
caller_identity = stsclient.get_caller_identity()
151151
caller_account = caller_identity['Account']
152+
partition = arns.get_partition(caller_identity['Arn'])
152153
logger.debug("Caller Identity: {}".format(caller_identity))
153154

154-
org_tree_search_dir = Path(get_storage_root())
155+
if partition == 'aws':
156+
org_tree_search_dir = Path(get_storage_root())
157+
else:
158+
org_tree_search_dir = Path(os.path.join(get_storage_root(), partition))
155159
org_id_pattern = re.compile(r'/o-\w+')
156160
for subdir in org_tree_search_dir.iterdir():
157161
if org_id_pattern.search(str(subdir)) is not None:

principalmapper/graphing/orgs_cli.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from principalmapper.graphing.gathering import get_organizations_data
3030
from principalmapper.querying.query_orgs import produce_scp_list
3131
from principalmapper.util import botocore_tools
32-
from principalmapper.util.storage import get_storage_root
32+
from principalmapper.util.storage import get_storage_root, get_default_graph_path
3333

3434

3535
logger = logging.getLogger(__name__)
@@ -106,15 +106,20 @@ def process_arguments(parsed_args: Namespace):
106106
# create the account -> OU path map and apply to all accounts (same as orgs update operation)
107107
account_ou_map = _map_account_ou_paths(org_tree)
108108
logger.debug('account_ou_map: {}'.format(account_ou_map))
109-
_update_accounts_with_ou_path_map(org_tree.org_id, account_ou_map, get_storage_root())
109+
root_path = get_storage_root() if org_tree.partition == 'aws' else os.path.join(get_storage_root(), org_tree.partition)
110+
_update_accounts_with_ou_path_map(org_tree.org_id, account_ou_map, root_path)
110111
logger.info('Updated currently stored Graphs with applicable AWS Organizations data')
111112

112113
# create and cache a list of edges between all the accounts we have data for
113114
edge_list = []
114115
graph_objs = []
115116
for account in org_tree.accounts:
116117
try:
117-
potential_path = os.path.join(get_storage_root(), account)
118+
if org_tree.partition != 'aws':
119+
potential_path = get_default_graph_path(f'{org_tree.partition}:{account}')
120+
else:
121+
potential_path = get_default_graph_path(account)
122+
118123
logger.debug('Trying to load a Graph from {}'.format(potential_path))
119124
graph_obj = Graph.create_graph_from_local_disk(potential_path)
120125
graph_objs.append(graph_obj)
@@ -135,26 +140,34 @@ def process_arguments(parsed_args: Namespace):
135140
org_tree.edge_list = edge_list
136141
logger.info('Compiled cross-account edges')
137142

138-
org_tree.save_organization_to_disk(os.path.join(get_storage_root(), org_tree.org_id))
143+
if org_tree.partition != 'aws':
144+
org_storage_path = get_default_graph_path(f'{org_tree.partition}:{org_tree.org_id}')
145+
else:
146+
org_storage_path = get_default_graph_path(org_tree.org_id)
147+
org_tree.save_organization_to_disk(org_storage_path)
139148
logger.info('Stored organization data to disk')
140149

141150
elif parsed_args.picked_orgs_cmd == 'update':
142151
# pull the existing data from disk
143-
org_filepath = os.path.join(get_storage_root(), parsed_args.org)
152+
org_filepath = get_default_graph_path(parsed_args.org)
144153
org_tree = OrganizationTree.create_from_dir(org_filepath)
145154

146155
# create the account -> OU path map and apply to all accounts
147156
account_ou_map = _map_account_ou_paths(org_tree)
148157
logger.debug('account_ou_map: {}'.format(account_ou_map))
149-
_update_accounts_with_ou_path_map(org_tree.org_id, account_ou_map, get_storage_root())
158+
root_path = get_storage_root() if org_tree.partition == 'aws' else os.path.join(get_storage_root(), org_tree.partition)
159+
_update_accounts_with_ou_path_map(org_tree.org_id, account_ou_map, root_path)
150160
logger.info('Updated currently stored Graphs with applicable AWS Organizations data')
151161

152162
# create and cache a list of edges between all the accounts we have data for
153163
edge_list = []
154164
graph_objs = []
155165
for account in org_tree.accounts:
156166
try:
157-
potential_path = os.path.join(get_storage_root(), account)
167+
if org_tree.partition != 'aws':
168+
potential_path = get_default_graph_path(f'{org_tree.partition}:{account}')
169+
else:
170+
potential_path = get_default_graph_path(account)
158171
logger.debug('Trying to load a Graph from {}'.format(potential_path))
159172
graph_obj = Graph.create_graph_from_local_disk(potential_path)
160173
graph_objs.append(graph_obj)
@@ -175,12 +188,16 @@ def process_arguments(parsed_args: Namespace):
175188
org_tree.edge_list = edge_list
176189
logger.info('Compiled cross-account edges')
177190

178-
org_tree.save_organization_to_disk(os.path.join(get_storage_root(), org_tree.org_id))
191+
if org_tree.partition != 'aws':
192+
org_storage_path = get_default_graph_path(f'{org_tree.partition}:{org_tree.org_id}')
193+
else:
194+
org_storage_path = get_default_graph_path(org_tree.org_id)
195+
org_tree.save_organization_to_disk(org_storage_path)
179196
logger.info('Stored organization data to disk')
180197

181198
elif parsed_args.picked_orgs_cmd == 'display':
182199
# pull the existing data from disk
183-
org_filepath = os.path.join(get_storage_root(), parsed_args.org)
200+
org_filepath = get_default_graph_path(parsed_args.org)
184201
org_tree = OrganizationTree.create_from_dir(org_filepath)
185202

186203
def _print_account(org_account: OrganizationAccount, indent_level: int, inherited_scps: List[Policy]):

0 commit comments

Comments
 (0)