forked from nccgroup/Scout2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListAll.py
executable file
·135 lines (112 loc) · 4.37 KB
/
ListAll.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# Import AWS Scout2 tools
from AWSScout2.utils import *
# Import third-party packages
import datetime
import dateutil.parser
import netaddr
import re
import sys
########################################
##### Config file
########################################
class Bunch(object):
def __init__(self, adict):
self.__dict__.update(adict)
########################################
##### Main
########################################
def main(cmd_args):
# Configure the debug level
configPrintException(cmd_args.debug)
# Get the environment name
environment_names = get_environment_name(cmd_args)
# Support multiple environments
for environment_name in environment_names:
# Load arguments from config if specified
if len(cmd_args.config):
rule_metadata = {'filename': cmd_args.config[0], 'enabled': True, 'args': cmd_args.config_args}
config = load_config_from_json(rule_metadata, environment_name, cmd_args.ip_ranges)
if config:
args = Bunch(config)
else:
return 42
else:
args = cmd_args
config = {}
config['conditions'] = args.conditions if hasattr(args, 'conditions') else []
config['mapping'] = args.mapping if hasattr(args, 'mapping') else []
config['keys'] = args.keys
# Load the data
aws_config = {}
aws_config['services'] = {}
for service in supported_services:
try:
aws_config['services'][service] = load_info_from_json(service, environment_name)
except Exception as e:
printException(e)
# Recursion
if type(args.path) == list:
config['path'] = args.path[0]
else:
config['path'] = args.path
target_path = config['path'].split('.')
current_path = []
resources = recurse(aws_config['services'], aws_config['services'], target_path, current_path, config)
# Do a print here ...
if 'keys' in config:
for resource in resources:
output = ''
current_path = resource.split('.')
for key in config['keys']:
if not output:
output = get_value_at(aws_config['services'], current_path, key, True)
else:
output = output + ', ' + get_value_at(aws_config['services'], current_path, key, True)
printInfo(output)
else:
printInfo(json.dumps(resources, indent=4))
# service = entity.pop(0)
# if output_format != 'csv':
# printInfo(output_format['header'])
# list_all(aws_config, aws_config[service], entity, [ service ], args.keys, conditions, output_format, mapping)
# if output_format != 'csv':
# printInfo(output_format['footer'])
########################################
##### Argument parser
########################################
default_args = read_profile_default_args(parser.prog)
add_scout2_argument(parser, default_args, 'env')
parser.add_argument('--config',
dest='config',
default=[],
nargs='+',
help='Config file that sets the path and keys to be listed.')
parser.add_argument('--format',
dest='format',
default=['csv'],
nargs='+',
help='Bleh.')
parser.add_argument('--path',
dest='path',
default=[],
nargs='+',
help='Path of the resources to list (e.g. iam.users.id or ec2.regions.id.vpcs.id)')
parser.add_argument('--keys',
dest='keys',
default=[ 'this' ],
nargs='+',
help='Keys to be printed for the given object.')
parser.add_argument('--ip-ranges',
dest='ip_ranges',
default=[],
nargs='+',
help='Config file(s) that contain your own IP ranges.')
parser.add_argument('--config-args',
dest='config_args',
default=[],
nargs='+',
help='Arguments to be passed to the config file.')
args = parser.parse_args()
if __name__ == '__main__':
sys.exit(main(args))