From 00573b5ad4ec6696c658a5caa1cdddc65d64b49c Mon Sep 17 00:00:00 2001 From: Luis Fontes Date: Tue, 31 Dec 2024 17:13:20 +0000 Subject: [PATCH 1/2] Add command to delete AWS keys --- pacu/main.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/pacu/main.py b/pacu/main.py index e79c4be6..26e99dad 100644 --- a/pacu/main.py +++ b/pacu/main.py @@ -184,7 +184,7 @@ def get_data_from_traceback(tb) -> Tuple[Optional[PacuSession], List[str], List[ class Main: COMMANDS = [ - 'assume_role', 'aws', 'console', 'data', 'delete_session', 'exec', 'exit', 'export_keys', 'help', + 'assume_role', 'aws', 'console', 'data', 'delete_keys', 'delete_session', 'exec', 'exit', 'export_keys', 'help', 'history', 'import_keys', 'list', 'list_sessions', 'load_commands_file', 'ls', 'open_console', 'quit', 'regions', 'run', 'search', 'services', 'sessions', 'set_keys', 'set_regions', 'set_ua_suffix', 'swap_keys', 'swap_session', 'unset_ua_suffix', 'update_regions', 'use', 'whoami', 'debug' @@ -600,6 +600,8 @@ def parse_command(self, command): self.check_sessions(command) elif command[0] == 'delete_session': self.delete_session() + elif command[0] == 'delete_keys': + self.delete_keys() elif command[0] == 'export_keys': self.export_keys(command) elif command[0] == 'help': @@ -1091,6 +1093,8 @@ def display_command_help(self, command_name: str) -> None: print('\n sessions/list_sessions\n List all sessions stored in the Pacu database\n') elif command_name == 'swap_session': print('\n swap_session\n Swap the active Pacu session for another one stored in the database or a brand new session\n') + elif command_name == 'delete_keys': + print('\n delete_keys\n Delete a set of AWS keys in the current session from the Pacu database\n') elif command_name == 'delete_session': print('\n delete_session\n Delete a session from the Pacu database. Note that this does not delete the output folder for that session\n') elif command_name == 'help': @@ -1358,6 +1362,35 @@ def swap_keys(self, key_name: str = None) -> None: self.database.commit() self.print('AWS key is now {}.'.format(session.key_alias)) + def delete_keys(self) -> None: + active_session = self.get_active_session() + all_keys = self.database.query(AWSKey).filter(AWSKey.session_id == active_session.id).all() + print('Delete which key set?') + + for index, key in enumerate(all_keys, 0): + if key.key_alias == active_session.key_alias: + print(' [{}] {} (ACTIVE)'.format(index, key.key_alias)) + else: + print(' [{}] {}'.format(index, key.key_alias)) + + choice = input('Choose an option: ') + + try: + key = all_keys[int(choice)] + if key.key_alias == active_session.key_alias: + print('Cannot delete the active keys! Switch keys and try again.') + return + except (ValueError, IndexError): + print('Please choose a number from 0 to {}.'.format(len(all_keys) - 1)) + return self.delete_keys() + + self.database.delete(key) + self.database.commit() + + print('Deleted {} from the database!'.format(key.key_alias)) + + return + def activate_session(self, session_name) -> None: sessions = self.database.query(PacuSession).all() found_session = False From 3ca57d9be927b5eb71409e17ee270f78a33a2c3e Mon Sep 17 00:00:00 2001 From: DaveYesland Date: Thu, 23 Jan 2025 13:57:52 -0800 Subject: [PATCH 2/2] Add delete_keys to main help --- pacu/main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pacu/main.py b/pacu/main.py index 26e99dad..64bc988d 100644 --- a/pacu/main.py +++ b/pacu/main.py @@ -123,6 +123,7 @@ def display_pacu_help(): at ~/.aws/credentials) to the current sessions database. Enter the name of a profile you would like to import or supply --all to import all the credentials in the file. + delete_keys Delete a set of AWS keys in the current session from the Pacu database assume_role Call AssumeRole on the specified role from the current credentials, add the resulting temporary keys to the Pacu key database and start using these new credentials. @@ -1093,8 +1094,6 @@ def display_command_help(self, command_name: str) -> None: print('\n sessions/list_sessions\n List all sessions stored in the Pacu database\n') elif command_name == 'swap_session': print('\n swap_session\n Swap the active Pacu session for another one stored in the database or a brand new session\n') - elif command_name == 'delete_keys': - print('\n delete_keys\n Delete a set of AWS keys in the current session from the Pacu database\n') elif command_name == 'delete_session': print('\n delete_session\n Delete a session from the Pacu database. Note that this does not delete the output folder for that session\n') elif command_name == 'help': @@ -1126,6 +1125,8 @@ def display_command_help(self, command_name: str) -> None: print('\n set_keys\n Add a set of AWS keys to the session and set them as the default\n') elif command_name == 'swap_keys': print('\n swap_keys\n Change the currently active AWS key to another key that has previously been set for this session\n') + elif command_name == 'delete_keys': + print('\n delete_keys\n Delete a set of AWS keys in the current session from the Pacu database\n') elif command_name == 'exit' or command_name == 'quit': print('\n exit/quit\n Exit Pacu\n') elif command_name == 'load_commands_file':