-
Notifications
You must be signed in to change notification settings - Fork 1
/
mugc.py
213 lines (176 loc) · 7.06 KB
/
mugc.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Copyright 2016-2018 Capital One Services, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import itertools
import json
import os
import logging
import sys
from c7n.credentials import SessionFactory
from c7n.config import Config
from c7n.policy import load as policy_load, PolicyCollection
from c7n import mu
# TODO: mugc has alot of aws assumptions
from c7n.resources.aws import AWS
import boto3
from botocore.exceptions import ClientError
log = logging.getLogger('mugc')
def load_policies(options, config):
policies = PolicyCollection([], config)
for f in options.config_files:
policies += policy_load(config, f).filter(options.policy_filter)
return policies
def region_gc(options, region, policy_config, policies):
session_factory = SessionFactory(
region=region,
assume_role=policy_config.assume_role,
profile=policy_config.profile,
external_id=policy_config.external_id)
manager = mu.LambdaManager(session_factory)
funcs = list(manager.list_functions(options.prefix))
client = session_factory().client('lambda')
remove = []
current_policies = [p.name for p in policies]
for f in funcs:
pn = f['FunctionName'].split('-', 1)[1]
if pn not in current_policies:
remove.append(f)
for n in remove:
events = []
try:
result = client.get_policy(FunctionName=n['FunctionName'])
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
log.warning(
"Region:%s Lambda Function or Access Policy Statement missing: %s",
region, n['FunctionName'])
else:
log.warning(
"Region:%s Unexpected error: %s for function %s",
region, e, n['FunctionName'])
# Continue on with next function instead of raising an exception
continue
if 'Policy' not in result:
pass
else:
p = json.loads(result['Policy'])
for s in p['Statement']:
principal = s.get('Principal')
if not isinstance(principal, dict):
log.info("Skipping function %s" % n['FunctionName'])
continue
if principal == {'Service': 'events.amazonaws.com'}:
events.append(
mu.CloudWatchEventSource({}, session_factory))
elif principal == {'Service': 'config.amazonaws.com'}:
events.append(
mu.ConfigRule({}, session_factory))
f = mu.LambdaFunction({
'name': n['FunctionName'],
'role': n['Role'],
'handler': n['Handler'],
'timeout': n['Timeout'],
'memory_size': n['MemorySize'],
'description': n['Description'],
'runtime': n['Runtime'],
'events': events}, None)
log.info("Region:%s Removing %s", region, n['FunctionName'])
if options.dryrun:
log.info("Dryrun skipping removal")
continue
manager.remove(f)
log.info("Region:%s Removed %s", region, n['FunctionName'])
def resources_gc_prefix(options, policy_config, policy_collection):
"""Garbage collect old custodian policies based on prefix.
We attempt to introspect to find the event sources for a policy
but without the old configuration this is implicit.
"""
# Classify policies by region
policy_regions = {}
for p in policy_collection:
if p.execution_mode == 'poll':
continue
policy_regions.setdefault(p.options.region, []).append(p)
regions = get_gc_regions(options.regions)
for r in regions:
region_gc(options, r, policy_config, policy_regions.get(r, []))
def get_gc_regions(regions):
if 'all' in regions:
session = boto3.Session(
region_name='us-east-1',
aws_access_key_id='never',
aws_secret_access_key='found')
return session.get_available_regions('s3')
return regions
def setup_parser():
parser = argparse.ArgumentParser()
parser.add_argument("configs", nargs='*', help="Policy configuration file(s)")
parser.add_argument(
'-c', '--config', dest="config_files", nargs="*", action='append',
help="Policy configuration files(s)", default=[])
parser.add_argument(
'-r', '--region', action='append', dest='regions', metavar='REGION',
help="AWS Region to target. Can be used multiple times, also supports `all`")
parser.add_argument('--dryrun', action="store_true", default=False)
parser.add_argument(
"--profile", default=os.environ.get('AWS_PROFILE'),
help="AWS Account Config File Profile to utilize")
parser.add_argument(
"--prefix", default="custodian-",
help="The Lambda name prefix to use for clean-up")
parser.add_argument("-p", "--policies", default=None, dest='policy_filter',
help="Only use named/matched policies")
parser.add_argument(
"--assume", default=None, dest="assume_role",
help="Role to assume")
parser.add_argument(
"-v", dest="verbose", action="store_true", default=False,
help='toggle verbose logging')
return parser
def main():
parser = setup_parser()
options = parser.parse_args()
log_level = logging.INFO
if options.verbose:
log_level = logging.DEBUG
logging.basicConfig(
level=log_level,
format="%(asctime)s: %(name)s:%(levelname)s %(message)s")
logging.getLogger('botocore').setLevel(logging.ERROR)
logging.getLogger('urllib3').setLevel(logging.ERROR)
logging.getLogger('c7n.cache').setLevel(logging.WARNING)
if not options.regions:
options.regions = [os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')]
files = []
files.extend(itertools.chain(*options.config_files))
files.extend(options.configs)
options.config_files = files
if not files:
parser.print_help()
sys.exit(1)
policy_config = Config.empty(
regions=options.regions,
profile=options.profile,
assume_role=options.assume_role)
# use cloud provider to initialize policies to get region expansion
policies = AWS().initialize_policies(
PolicyCollection([
p for p in load_policies(
options, policy_config)
if p.provider_name == 'aws'],
policy_config),
policy_config)
resources_gc_prefix(options, policy_config, policies)
if __name__ == '__main__':
main()