-
Notifications
You must be signed in to change notification settings - Fork 73
/
aws_delete_user.py
executable file
·284 lines (244 loc) · 9.19 KB
/
aws_delete_user.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
"""
aws_delete_user.py
==================
Script using boto3 to delete an IAM User, including all of the associated
resources and attachments that need to be deleted first. Includes dry-run option.
If you have ideas for improvements, or want the latest version, it's at:
<https://github.com/jantman/misc-scripts/blob/master/aws_delete_user.py>
License
-------
Copyright 2019 Jason Antman <[email protected]> <http://www.jasonantman.com>
Free for any use provided that patches are submitted back to me.
CHANGELOG
---------
2019-01-04 Jason Antman <[email protected]>:
- initial version of script
"""
import sys
import argparse
import logging
try:
import boto3
from botocore.exceptions import ClientError
except ImportError:
sys.stderr.write('ERROR: You must "pip install boto3"\n')
raise SystemExit(1)
FORMAT = "[%(asctime)s %(levelname)s] %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger()
# suppress boto3 internal logging below WARNING level
boto3_log = logging.getLogger("boto3")
boto3_log.setLevel(logging.WARNING)
boto3_log.propagate = True
# suppress botocore internal logging below WARNING level
botocore_log = logging.getLogger("botocore")
botocore_log.setLevel(logging.WARNING)
botocore_log.propagate = True
class IamUserDeleter(object):
def __init__(self, dry_run=False):
self.dry_run = dry_run
logger.debug('Connecting to IAM as service resource...')
self._iam = boto3.resource('iam')
def run(self, username):
logger.debug('Getting IAM User: %s', username)
user = self._iam.User(username)
try:
# see if the user exists
if self.dry_run:
logger.info(
'DRY RUN for deletion of IAM User %s (%s)',
username, user.user_id
)
else:
logger.info(
'Preparing to delete IAM User %s (%s)',
username, user.user_id
)
except ClientError as ex:
if ex.response.get('Error', {}).get('Code', '') == 'NoSuchEntity':
logger.info(
'User %s does not exist or was already deleted', username
)
raise SystemExit(0)
raise
self._access_keys(user)
self._certificates(user)
self._login_profile(user)
self._mfa(user)
self._attached_user_policies(user)
self._inline_policies(user)
self._group_memberships(user)
self._ssh_public_keys(user)
self._service_specific_creds(user)
if self.dry_run:
logger.info(
'DRY RUN - Would Delete IAM User %s (%s)', username,
user.user_id
)
return
logger.info('Deleting IAM User "%s" (%s)', username, user.user_id)
user.delete()
def _format_call_log(self, clsname, del_meth, del_kwargs):
if del_meth == 'delete' and del_kwargs == {}:
return ''
s = ' by calling %s.%s(' % (clsname, del_meth)
if del_kwargs == {}:
return '%s)' % s
return '%s**%s)' % (s, del_kwargs)
def _loop_and_delete(self, user, collection_attr, title,
id_attr='id', del_meth='delete', del_kwargs={}):
collection = getattr(user, collection_attr)
items = list(collection.all())
if len(items) == 0:
logger.info('User has zero %ss.', title)
return
for i in items:
_id = getattr(i, id_attr)
if self.dry_run:
logger.info(
'DRY RUN - Would Delete %s %s%s', title, _id,
self._format_call_log(
i.__class__.__name__, del_meth, del_kwargs
)
)
continue
logger.info('Deleting %s %s', title, _id)
deleter = getattr(i, del_meth)
deleter(**del_kwargs)
def _access_keys(self, user):
self._loop_and_delete(user, 'access_keys', 'Access Key')
def _certificates(self, user):
self._loop_and_delete(
user, 'signing_certificates', 'Signing Certificate'
)
def _login_profile(self, user):
lp = user.LoginProfile()
try:
logger.debug('Found LoginProfile created on %s', lp.create_date)
except ClientError as ex:
if ex.response.get('Error', {}).get('Code', '') == 'NoSuchEntity':
logger.info('User has no Login Profile.')
return
if self.dry_run:
logger.info(
'DRY RUN - Would Delete Login Profile created on %s',
lp.create_date
)
return
logger.info('Deleting Login Profile created on %s', lp.create_date)
lp.delete()
def _mfa(self, user):
self._loop_and_delete(
user, 'mfa_devices', 'MFA Device', id_attr='serial_number',
del_meth='disassociate'
)
def _attached_user_policies(self, user):
self._loop_and_delete(
user, 'attached_policies', 'Attached Policy', id_attr='arn',
del_meth='detach_user', del_kwargs={'UserName': user.name}
)
def _inline_policies(self, user):
self._loop_and_delete(
user, 'policies', 'Inline Policy', id_attr='name'
)
def _group_memberships(self, user):
self._loop_and_delete(
user, 'groups', 'Group Membership', id_attr='name',
del_meth='remove_user', del_kwargs={'UserName': user.name}
)
def _ssh_public_keys(self, user):
client = self._iam.meta.client
resp = client.list_ssh_public_keys(UserName=user.name)['SSHPublicKeys']
if len(resp) == 0:
logger.info('User has zero SSH Public Keys')
return
for k in resp:
if self.dry_run:
logger.info(
'DRY RUN - Would Delete SSH Public Key %s',
k['SSHPublicKeyId']
)
continue
logger.info(
'Deleting SSH Public Key %s for user %s',
k['SSHPublicKeyId'], user.name
)
client.delete_ssh_public_key(
UserName=user.name, SSHPublicKeyId=k['SSHPublicKeyId']
)
def _service_specific_creds(self, user):
client = self._iam.meta.client
resp = client.list_service_specific_credentials(
UserName=user.name
)['ServiceSpecificCredentials']
if len(resp) == 0:
logger.info('User has zero Service Specific Credentials')
return
for k in resp:
if self.dry_run:
logger.info(
'DRY RUN - Would Delete Service Specific Credential %s'
'(Service=%s ServiceUserName=%s)',
k['ServiceSpecificCredentialId'],
k['ServiceName'], k['ServiceUserName']
)
continue
logger.info(
'Deleting Service Specific Credential %s'
'(Service=%s ServiceUserName=%s) for user %s',
k['ServiceSpecificCredentialId'],
k['ServiceName'], k['ServiceUserName'], user.name
)
client.delete_service_specific_credential(
UserName=user.name,
ServiceSpecificCredentialId=k['ServiceSpecificCredentialId']
)
def parse_args(argv):
"""
parse arguments/options
this uses the new argparse module instead of optparse
see: <https://docs.python.org/2/library/argparse.html>
"""
p = argparse.ArgumentParser(
description='Delete an IAM user and all its dependencies'
)
p.add_argument('-d', '--dry-run', dest='dry_run', action='store_true',
default=False,
help="dry-run - don't actually make any changes")
p.add_argument('-v', '--verbose', dest='verbose', action='store_true',
default=False, help='verbose output')
p.add_argument('USER_NAME', action='store', type=str,
help='Name of IAM User to delete')
args = p.parse_args(argv)
return args
def set_log_info():
"""set logger level to INFO"""
set_log_level_format(logging.INFO,
'%(asctime)s %(levelname)s:%(name)s:%(message)s')
def set_log_debug():
"""set logger level to DEBUG, and debug-level output format"""
set_log_level_format(
logging.DEBUG,
"%(asctime)s [%(levelname)s %(filename)s:%(lineno)s - "
"%(name)s.%(funcName)s() ] %(message)s"
)
def set_log_level_format(level, format):
"""
Set logger level and format.
:param level: logging level; see the :py:mod:`logging` constants.
:type level: int
:param format: logging formatter format string
:type format: str
"""
formatter = logging.Formatter(fmt=format)
logger.handlers[0].setFormatter(formatter)
logger.setLevel(level)
if __name__ == "__main__":
args = parse_args(sys.argv[1:])
# set logging level
if args.verbose:
set_log_debug()
else:
set_log_info()
IamUserDeleter(dry_run=args.dry_run).run(args.USER_NAME)