Path: blob/develop/awscli/customizations/configure/configure.py
1567 views
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import os13import logging1415from botocore.exceptions import ProfileNotFound1617from awscli.compat import compat_input18from awscli.customizations.commands import BasicCommand19from awscli.customizations.configure.addmodel import AddModelCommand20from awscli.customizations.configure.set import ConfigureSetCommand21from awscli.customizations.configure.get import ConfigureGetCommand22from awscli.customizations.configure.list import ConfigureListCommand23from awscli.customizations.configure.writer import ConfigFileWriter2425from . import mask_value, profile_to_section262728logger = logging.getLogger(__name__)293031def register_configure_cmd(cli):32cli.register('building-command-table.main',33ConfigureCommand.add_command)343536class InteractivePrompter(object):3738def get_value(self, current_value, config_name, prompt_text=''):39if config_name in ('aws_access_key_id', 'aws_secret_access_key'):40current_value = mask_value(current_value)41response = compat_input("%s [%s]: " % (prompt_text, current_value))42if not response:43# If the user hits enter, we return a value of None44# instead of an empty string. That way we can determine45# whether or not a value has changed.46response = None47return response484950class ConfigureCommand(BasicCommand):51NAME = 'configure'52DESCRIPTION = BasicCommand.FROM_FILE()53SYNOPSIS = ('aws configure [--profile profile-name]')54EXAMPLES = (55'To create a new configuration::\n'56'\n'57' $ aws configure\n'58' AWS Access Key ID [None]: accesskey\n'59' AWS Secret Access Key [None]: secretkey\n'60' Default region name [None]: us-west-2\n'61' Default output format [None]:\n'62'\n'63'To update just the region name::\n'64'\n'65' $ aws configure\n'66' AWS Access Key ID [****]:\n'67' AWS Secret Access Key [****]:\n'68' Default region name [us-west-1]: us-west-2\n'69' Default output format [None]:\n'70)71SUBCOMMANDS = [72{'name': 'list', 'command_class': ConfigureListCommand},73{'name': 'get', 'command_class': ConfigureGetCommand},74{'name': 'set', 'command_class': ConfigureSetCommand},75{'name': 'add-model', 'command_class': AddModelCommand}76]7778# If you want to add new values to prompt, update this list here.79VALUES_TO_PROMPT = [80# (logical_name, config_name, prompt_text)81('aws_access_key_id', "AWS Access Key ID"),82('aws_secret_access_key', "AWS Secret Access Key"),83('region', "Default region name"),84('output', "Default output format"),85]8687def __init__(self, session, prompter=None, config_writer=None):88super(ConfigureCommand, self).__init__(session)89if prompter is None:90prompter = InteractivePrompter()91self._prompter = prompter92if config_writer is None:93config_writer = ConfigFileWriter()94self._config_writer = config_writer9596def _run_main(self, parsed_args, parsed_globals):97# Called when invoked with no args "aws configure"98new_values = {}99# This is the config from the config file scoped to a specific100# profile.101try:102config = self._session.get_scoped_config()103except ProfileNotFound:104config = {}105for config_name, prompt_text in self.VALUES_TO_PROMPT:106current_value = config.get(config_name)107new_value = self._prompter.get_value(current_value, config_name,108prompt_text)109if new_value is not None and new_value != current_value:110new_values[config_name] = new_value111config_filename = os.path.expanduser(112self._session.get_config_variable('config_file'))113if new_values:114profile = self._session.profile115self._write_out_creds_file_values(new_values, profile)116if profile is not None:117section = profile_to_section(profile)118new_values['__section__'] = section119self._config_writer.update_config(new_values, config_filename)120121def _write_out_creds_file_values(self, new_values, profile_name):122# The access_key/secret_key are now *always* written to the shared123# credentials file (~/.aws/credentials), see aws/aws-cli#847.124# post-conditions: ~/.aws/credentials will have the updated credential125# file values and new_values will have the cred vars removed.126credential_file_values = {}127if 'aws_access_key_id' in new_values:128credential_file_values['aws_access_key_id'] = new_values.pop(129'aws_access_key_id')130if 'aws_secret_access_key' in new_values:131credential_file_values['aws_secret_access_key'] = new_values.pop(132'aws_secret_access_key')133if credential_file_values:134if profile_name is not None:135credential_file_values['__section__'] = profile_name136shared_credentials_filename = os.path.expanduser(137self._session.get_config_variable('credentials_file'))138self._config_writer.update_config(139credential_file_values,140shared_credentials_filename)141142143