Skip to content

Commit 3bd0e6b

Browse files
authored
Merge pull request #460 from luisfontes19/delete-keys
Add command to delete AWS keys
2 parents c4f92a2 + efbd928 commit 3bd0e6b

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

pacu/main.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def display_pacu_help():
123123
at ~/.aws/credentials) to the current sessions database.
124124
Enter the name of a profile you would like to import or
125125
supply --all to import all the credentials in the file.
126+
delete_keys Delete a set of AWS keys in the current session from the Pacu database
126127
assume_role <role arn> Call AssumeRole on the specified role from the current
127128
credentials, add the resulting temporary keys to the Pacu
128129
key database and start using these new credentials.
@@ -184,7 +185,7 @@ def get_data_from_traceback(tb) -> Tuple[Optional[PacuSession], List[str], List[
184185

185186
class Main:
186187
COMMANDS = [
187-
'assume_role', 'aws', 'console', 'data', 'delete_session', 'exec', 'exit', 'export_keys', 'help',
188+
'assume_role', 'aws', 'console', 'data', 'delete_keys', 'delete_session', 'exec', 'exit', 'export_keys', 'help',
188189
'history', 'import_keys', 'list', 'list_sessions', 'load_commands_file', 'ls', 'open_console', 'quit',
189190
'regions', 'run', 'search', 'services', 'sessions', 'set_keys', 'set_regions', 'set_ua_suffix',
190191
'swap_keys', 'swap_session', 'unset_ua_suffix', 'update_regions', 'use', 'whoami', 'debug'
@@ -600,6 +601,8 @@ def parse_command(self, command):
600601
self.check_sessions(command)
601602
elif command[0] == 'delete_session':
602603
self.delete_session()
604+
elif command[0] == 'delete_keys':
605+
self.delete_keys()
603606
elif command[0] == 'export_keys':
604607
self.export_keys(command)
605608
elif command[0] == 'help':
@@ -1122,6 +1125,8 @@ def display_command_help(self, command_name: str) -> None:
11221125
print('\n set_keys\n Add a set of AWS keys to the session and set them as the default\n')
11231126
elif command_name == 'swap_keys':
11241127
print('\n swap_keys\n Change the currently active AWS key to another key that has previously been set for this session\n')
1128+
elif command_name == 'delete_keys':
1129+
print('\n delete_keys\n Delete a set of AWS keys in the current session from the Pacu database\n')
11251130
elif command_name == 'exit' or command_name == 'quit':
11261131
print('\n exit/quit\n Exit Pacu\n')
11271132
elif command_name == 'load_commands_file':
@@ -1358,6 +1363,35 @@ def swap_keys(self, key_name: str = None) -> None:
13581363
self.database.commit()
13591364
self.print('AWS key is now {}.'.format(session.key_alias))
13601365

1366+
def delete_keys(self) -> None:
1367+
active_session = self.get_active_session()
1368+
all_keys = self.database.query(AWSKey).filter(AWSKey.session_id == active_session.id).all()
1369+
print('Delete which key set?')
1370+
1371+
for index, key in enumerate(all_keys, 0):
1372+
if key.key_alias == active_session.key_alias:
1373+
print(' [{}] {} (ACTIVE)'.format(index, key.key_alias))
1374+
else:
1375+
print(' [{}] {}'.format(index, key.key_alias))
1376+
1377+
choice = input('Choose an option: ')
1378+
1379+
try:
1380+
key = all_keys[int(choice)]
1381+
if key.key_alias == active_session.key_alias:
1382+
print('Cannot delete the active keys! Switch keys and try again.')
1383+
return
1384+
except (ValueError, IndexError):
1385+
print('Please choose a number from 0 to {}.'.format(len(all_keys) - 1))
1386+
return self.delete_keys()
1387+
1388+
self.database.delete(key)
1389+
self.database.commit()
1390+
1391+
print('Deleted {} from the database!'.format(key.key_alias))
1392+
1393+
return
1394+
13611395
def activate_session(self, session_name) -> None:
13621396
sessions = self.database.query(PacuSession).all()
13631397
found_session = False

0 commit comments

Comments
 (0)