def test_get_master_account_id(): role_name = 'myrole' sts_client = boto3.client('sts') account_id = sts_client.get_caller_identity()['Account'] org_client = boto3.client('organizations') with pytest.raises(SystemExit): master_account_id = utils.get_master_account_id(role_name=role_name) org_client.create_organization(FeatureSet='ALL') master_account_id = utils.get_master_account_id(role_name=role_name) assert re.compile(r'[0-9]{12}').match(master_account_id)
def setup_crawler(org_access_role, account_access_role=None, accounts=None, regions=None): """ Returns a fully loaded orgcrawler.crawlers.Crawler object """ master_account_id = get_master_account_id(org_access_role) my_org = orgs.Org(master_account_id, org_access_role) my_org.load() my_crawler = crawlers.Crawler( my_org, access_role=account_access_role, accounts=accounts, regions=regions, ) my_crawler.load_account_credentials() return my_crawler
def main(command, argument, role, format): """ Arguments: \b COMMAND An organization query command to run ARGUMENT A command argument to supply if needed Available Query Commands: \b dump dump_accounts dump_org_units list_accounts_by_name list_accounts_by_id list_org_units_by_name list_org_units_by_id get_account ACCOUNT_IDENTIFIER get_account_id_by_name ACCOUNT_NAME get_account_name_by_id ACCOUNT_ID get_org_unit_id OU_IDENTIFIER list_accounts_in_ou OU_IDENTIFIER list_accounts_in_ou_recursive OU_IDENTIFIER list_org_units_in_ou OU_IDENTIFIER list_org_units_in_ou_recursive OU_IDENTIFIER Examples: \b orgquery -r OrgMasterRole list_accounts_by_name orgquery -r OrgMasterRole -f json get_account_id_by_name webapps """ if format == 'json': formatter = jsonfmt elif format == 'yaml': formatter = utils.yamlfmt master_account_id = utils.get_master_account_id(role) org = orgs.Org(master_account_id, role) org.load() cmd = eval('org.' + command) if argument: print(formatter(cmd(argument))) else: print(formatter(cmd()))
def main(command, argument, role, debug, format): """ Arguments: \b COMMAND An organization query command to run ARGUMENT A command argument to supply if needed Available Query Commands: \b dump dump_accounts dump_org_units dump_policies list_accounts_by_name list_accounts_by_id list_org_units_by_name list_org_units_by_id list_policies_by_name list_policies_by_id get_account ACCOUNT_IDENTIFIER get_account_id_by_name ACCOUNT_NAME get_account_name_by_id ACCOUNT_ID get_org_unit OU_IDENTIFIER get_org_unit_id OU_IDENTIFIER list_accounts_in_ou OU_IDENTIFIER list_accounts_in_ou_recursive OU_IDENTIFIER list_org_units_in_ou OU_IDENTIFIER list_org_units_in_ou_recursive OU_IDENTIFIER get_policy POLICY_IDENTIFIER get_policy_id_by_name POLICY_NAME get_policy_name_by_id POLICY_ID get_targets_for_policy POLICY_IDENTIFIER get_policies_for_target POLICY_IDENTIFIER get_accounts_for_policy_recursive POLICY_IDENTIFIER Examples: \b orgquery -r OrgMasterRole list_accounts_by_name orgquery -r OrgMasterRole -f yaml get_account_id_by_name webapps """ if format == 'json': formatter = jsonfmt elif format == 'yaml': formatter = utils.yamlfmt if debug == 0: log_level = 'warning' elif debug == 1: log_level = 'info' elif debug >= 2: log_level = 'debug' master_account_id = utils.get_master_account_id(role) org = orgs.Org(master_account_id, role, log_level) org.load() cmd = eval('org.' + command) if argument: print(formatter(cmd(argument))) else: print(formatter(cmd()))
#!/usr/bin/env python import sys from botocore.exceptions import ClientError from orgcrawler import orgs, crawlers, utils from orgcrawler.cli.utils import setup_crawler #cycles = 5 cycles = 100 errors = 0 role = sys.argv[1] timer = crawlers.CrawlerTimer() cycle_timer = crawlers.CrawlerTimer() master_account_id = utils.get_master_account_id(role) longest_time = 0 shortest_time = None timer.start() for i in range(cycles): cycle_timer.start() try: org = orgs.Org(master_account_id, role) org.load() except ClientError as e: errors += 1 print(e) org.clear_cache() org = None cycle_timer.stop()