diff --git a/awscli/customizations/configure/configure.py b/awscli/customizations/configure/configure.py index b2031bb7c168..621cdc582a81 100644 --- a/awscli/customizations/configure/configure.py +++ b/awscli/customizations/configure/configure.py @@ -25,6 +25,7 @@ from awscli.customizations.configure.importer import ConfigureImportCommand from awscli.customizations.configure.listprofiles import ListProfilesCommand from awscli.customizations.configure.sso import ConfigureSSOCommand +from awscli.customizations.configure.exporter import ConfigureExportCommand from . import mask_value, profile_to_section @@ -80,6 +81,7 @@ class ConfigureCommand(BasicCommand): {'name': 'import', 'command_class': ConfigureImportCommand}, {'name': 'list-profiles', 'command_class': ListProfilesCommand}, {'name': 'sso', 'command_class': ConfigureSSOCommand}, + {'name': 'export-credentials', 'command_class': ConfigureExportCommand}, ] # If you want to add new values to prompt, update this list here. diff --git a/awscli/customizations/configure/exporter.py b/awscli/customizations/configure/exporter.py new file mode 100644 index 000000000000..b2bef88eb79e --- /dev/null +++ b/awscli/customizations/configure/exporter.py @@ -0,0 +1,36 @@ +from awscli.customizations.configure.writer import ConfigFileWriter +from awscli.customizations.commands import BasicCommand +from awscli.customizations.utils import uni_print +import json + + +class ConfigureExportCommand(BasicCommand): + NAME = 'export-credentials' + DESCRIPTION = 'Exports the AWS credentials used with the command "aws configure" to a the config file.' + EXAMPLES = 'aws configure export-credentials' + + def __init__(self, session): + super(ConfigureExportCommand, self).__init__(session) + + def _run_main(self, parsed_args, parsed_globals): + self.export_credentials(self._session) + return 0 + + def export_credentials(self, session): + """ + Exports the AWS credentials used with the command 'aws configure' to a file. + """ + credentials = session.get_credentials() + if credentials is None: + raise RuntimeError( + 'No credentials available. Try running "aws configure" first.') + try: + credentials_data = credentials.get_frozen_credentials() + dump = {'aws_access_key_id': credentials_data.access_key, + 'aws_secret_access_key': credentials_data.secret_key} + if credentials_data.token is not None: + dump['aws_session_token'] = credentials_data.token + uni_print(json.dumps(dump, indent=4)) + except: + raise RuntimeError( + 'Failed while trying to export credentials. Check your permissions and try again.')