Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions awscli/customizations/configure/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions awscli/customizations/configure/exporter.py
Original file line number Diff line number Diff line change
@@ -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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a new command, without backward compat concerns, may I suggest using the same shape as is emitted by aws sts assume-role? it'd be one less special case for those wishing to consume the output

{
    "Credentials": {
        "AccessKeyId": "ASIA...",
        "SecretAccessKey": "sekrit...",
        "SessionToken": "bAsE64=",
        "Expiration": "2022-04-13T17:24:10.189000+00:00"
    }
}

where obviously SessionToken and Expiration would be elided just like they are in your current output

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mdaniel, I'll try to make the changes as suggested

'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.')