Repository: aws/aws-cli

Issue:
Reading/writing EMR key_pair_file configuration options behaves oddly
Version:

```
$ aws --version
aws-cli/1.11.75 Python/2.7.10 Darwin/15.6.0 botocore/1.5.38
```

[It's suggested that one can set a default key_pair_file argument here](https://github.com/aws/aws-cli/blob/master/awscli/customizations/emr/ssh.py#L25) by running `aws configure set emr.key_pair_file <value>`

By that token, I would expect `aws configure get emr.key_pair_file` to retrieve this item and to exit with a exit code of 0.

```
$ aws configure set emr.key_pair_file /tmp/foo

$ cat config
[default]
emr =
    key_pair_file = /tmp/foo

$ aws configure get emr.key_pair_file

$ echo $?
1
```

As you can see, setting this and trying to retrieve it exits with a non-zero exit code which makes it a pain to check for this config item being set as part of shell scripts prior to do other EMR-based commands (such as create-cluster).

As an aside, trying to get the top level `emr` config item fails too;

```
$ aws configure get emr

expected a character buffer object
```

Additionally this item doesn't show up when `aws configure list` is run either;

```
$ aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     REDACTED                    shared-credentials-file
secret_key      REDACTED                    shared-credentials-file
    region                <not set>             None    None

```


Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/aws/aws-cli/pull/2537?src=pr&el=h1) Report
> Merging [#2537](https://codecov.io/gh/aws/aws-cli/pull/2537?src=pr&el=desc) into [develop](https://codecov.io/gh/aws/aws-cli/commit/da8f5be25693108c68c63567e4cdc5436c715aa3?src=pr&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/aws/aws-cli/pull/2537/graphs/tree.svg?src=pr&token=aKPbq1EbIm&width=650&height=150)](https://codecov.io/gh/aws/aws-cli/pull/2537?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##           develop    #2537      +/-   ##
===========================================
+ Coverage    95.54%   95.57%   +0.02%     
===========================================
  Files          150      150              
  Lines        11732    11741       +9     
===========================================
+ Hits         11209    11221      +12     
+ Misses         523      520       -3
```


| [Impacted Files](https://codecov.io/gh/aws/aws-cli/pull/2537?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [awscli/customizations/configure/get.py](https://codecov.io/gh/aws/aws-cli/pull/2537?src=pr&el=tree#diff-YXdzY2xpL2N1c3RvbWl6YXRpb25zL2NvbmZpZ3VyZS9nZXQucHk=) | `98.36% <100%> (+6.05%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/aws/aws-cli/pull/2537?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/aws/aws-cli/pull/2537?src=pr&el=footer). Last update [da8f5be...16c88fb](https://codecov.io/gh/aws/aws-cli/pull/2537?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Patch:
diff --git a/.changes/next-release/bugfix-configure-87594.json b/.changes/next-release/bugfix-configure-87594.json
new file mode 100644
index 000000000..28b624d02
--- /dev/null
+++ b/.changes/next-release/bugfix-configure-87594.json
@@ -0,0 +1,5 @@
+{
+  "type": "bugfix",
+  "category": "configure",
+  "description": "Properly use the default profile in ``configure get``"
+}
diff --git a/awscli/customizations/configure/get.py b/awscli/customizations/configure/get.py
index 99f1bee08..a98d95585 100644
--- a/awscli/customizations/configure/get.py
+++ b/awscli/customizations/configure/get.py
@@ -11,17 +11,21 @@
 # ANY KIND, either express or implied. See the License for the specific
 # language governing permissions and limitations under the License.
 import sys
+import logging
 
 from awscli.customizations.commands import BasicCommand
+from awscli.compat import six
 
 from . import PREDEFINED_SECTION_NAMES
 
+LOG = logging.getLogger(__name__)
+
 
 class ConfigureGetCommand(BasicCommand):
     NAME = 'get'
     DESCRIPTION = BasicCommand.FROM_FILE('configure', 'get',
                                          '_description.rst')
-    SYNOPSIS = ('aws configure get varname [--profile profile-name]')
+    SYNOPSIS = 'aws configure get varname [--profile profile-name]'
     EXAMPLES = BasicCommand.FROM_FILE('configure', 'get', '_examples.rst')
     ARG_TABLE = [
         {'name': 'varname',
@@ -30,13 +34,14 @@ class ConfigureGetCommand(BasicCommand):
          'cli_type_name': 'string', 'positional_arg': True},
     ]
 
-    def __init__(self, session, stream=sys.stdout):
+    def __init__(self, session, stream=sys.stdout, error_stream=sys.stderr):
         super(ConfigureGetCommand, self).__init__(session)
         self._stream = stream
+        self._error_stream = error_stream
 
     def _run_main(self, args, parsed_globals):
         varname = args.varname
-        value = None
+
         if '.' not in varname:
             # get_scoped_config() returns the config variables in the config
             # file (not the logical_var names), which is what we want.
@@ -44,17 +49,30 @@ class ConfigureGetCommand(BasicCommand):
             value = config.get(varname)
         else:
             value = self._get_dotted_config_value(varname)
-        if value is not None:
+
+        LOG.debug(u'Config value retrieved: %s' % value)
+
+        if isinstance(value, six.string_types):
             self._stream.write(value)
             self._stream.write('\n')
             return 0
+        elif isinstance(value, dict):
+            # TODO: add support for this. We would need to print it off in
+            # the same format as the config file.
+            self._error_stream.write(
+                'varname (%s) must reference a value, not a section or '
+                'sub-section.' % varname
+            )
+            return 1
         else:
             return 1
 
     def _get_dotted_config_value(self, varname):
         parts = varname.split('.')
         num_dots = varname.count('.')
-        # Logic to deal with predefined sections like [preview], [plugin] and etc.
+
+        # Logic to deal with predefined sections like [preview], [plugin] and
+        # etc.
         if num_dots == 1 and parts[0] in PREDEFINED_SECTION_NAMES:
             full_config = self._session.full_config
             section, config_name = varname.split('.')
@@ -64,18 +82,23 @@ class ConfigureGetCommand(BasicCommand):
                 value = full_config['profiles'].get(
                     section, {}).get(config_name)
             return value
+
         if parts[0] == 'profile':
             profile_name = parts[1]
             config_name = parts[2]
             remaining = parts[3:]
-        # Check if varname starts with 'default' profile (e.g. default.emr-dev.emr.instance_profile)
-        # If not, go further to check if varname starts with a known profile name
-        elif parts[0] == 'default' or (parts[0] in self._session.full_config['profiles']):
+        # Check if varname starts with 'default' profile (e.g.
+        # default.emr-dev.emr.instance_profile) If not, go further to check
+        # if varname starts with a known profile name
+        elif parts[0] == 'default' or (
+                parts[0] in self._session.full_config['profiles']):
             profile_name = parts[0]
             config_name = parts[1]
             remaining = parts[2:]
         else:
             profile_name = self._session.get_config_variable('profile')
+            if profile_name is None:
+                profile_name = 'default'
             config_name = parts[0]
             remaining = parts[1:]
 


Test patch:
diff --git a/tests/unit/customizations/configure/test_get.py b/tests/unit/customizations/configure/test_get.py
index c2db9815e..cf34fbb4a 100644
--- a/tests/unit/customizations/configure/test_get.py
+++ b/tests/unit/customizations/configure/test_get.py
@@ -20,11 +20,16 @@ from . import FakeSession
 
 class TestConfigureGetCommand(unittest.TestCase):
 
+    def create_command(self, session):
+        stdout = six.StringIO()
+        stderr = six.StringIO()
+        command = ConfigureGetCommand(session, stdout, stderr)
+        return stdout, stderr, command
+
     def test_configure_get_command(self):
         session = FakeSession({})
         session.config['region'] = 'us-west-2'
-        stream = six.StringIO()
-        config_get = ConfigureGetCommand(session, stream)
+        stream, error_stream, config_get = self.create_command(session)
         config_get(args=['region'], parsed_globals=None)
         rendered = stream.getvalue()
         self.assertEqual(rendered.strip(), 'us-west-2')
@@ -32,8 +37,7 @@ class TestConfigureGetCommand(unittest.TestCase):
     def test_configure_get_command_no_exist(self):
         no_vars_defined = {}
         session = FakeSession(no_vars_defined)
-        stream = six.StringIO()
-        config_get = ConfigureGetCommand(session, stream)
+        stream, error_stream, config_get = self.create_command(session)
         rc = config_get(args=['region'], parsed_globals=None)
         rendered = stream.getvalue()
         # If a config value does not exist, we don't print any output.
@@ -44,8 +48,7 @@ class TestConfigureGetCommand(unittest.TestCase):
     def test_dotted_get(self):
         session = FakeSession({})
         session.full_config = {'preview': {'emr': 'true'}}
-        stream = six.StringIO()
-        config_get = ConfigureGetCommand(session, stream)
+        stream, error_stream, config_get = self.create_command(session)
         config_get(args=['preview.emr'], parsed_globals=None)
         rendered = stream.getvalue()
         self.assertEqual(rendered.strip(), 'true')
@@ -55,16 +58,16 @@ class TestConfigureGetCommand(unittest.TestCase):
         session.full_config = {'profiles': {'emr-dev': {
             'emr': {'instance_profile': 'my_ip'}}}}
         session.config = {'emr': {'instance_profile': 'my_ip'}}
-        stream = six.StringIO()
-        config_get = ConfigureGetCommand(session, stream)
+        stream, error_stream, config_get = self.create_command(session)
         config_get(args=['emr-dev.emr.instance_profile'], parsed_globals=None)
         rendered = stream.getvalue()
         self.assertEqual(rendered.strip(), 'my_ip')
 
     def test_get_from_profile(self):
         session = FakeSession({})
-        session.full_config = {'profiles': {'testing': {'aws_access_key_id': 'access_key'}}}
-        stream = six.StringIO()
+        session.full_config = {
+            'profiles': {'testing': {'aws_access_key_id': 'access_key'}}}
+        stream, error_stream, config_get = self.create_command(session)
         config_get = ConfigureGetCommand(session, stream)
         config_get(args=['profile.testing.aws_access_key_id'],
                    parsed_globals=None)
@@ -75,8 +78,7 @@ class TestConfigureGetCommand(unittest.TestCase):
         session = FakeSession({})
         session.full_config = {
             'profiles': {'testing': {'s3': {'signature_version': 's3v4'}}}}
-        stream = six.StringIO()
-        config_get = ConfigureGetCommand(session, stream)
+        stream, error_stream, config_get = self.create_command(session)
         config_get(args=['profile.testing.s3.signature_version'],
                    parsed_globals=None)
         rendered = stream.getvalue()
@@ -86,8 +88,7 @@ class TestConfigureGetCommand(unittest.TestCase):
         session = FakeSession({})
         session.full_config = {
             'profiles': {'default': {'s3': {'signature_version': 's3v4'}}}}
-        stream = six.StringIO()
-        config_get = ConfigureGetCommand(session, stream)
+        stream, error_stream, config_get = self.create_command(session)
         config_get(args=['default.s3.signature_version'],
                    parsed_globals=None)
         rendered = stream.getvalue()
@@ -96,9 +97,46 @@ class TestConfigureGetCommand(unittest.TestCase):
     def test_get_nested_attribute_from_default_does_not_exist(self):
         session = FakeSession({})
         session.full_config = {'profiles': {}}
-        stream = six.StringIO()
-        config_get = ConfigureGetCommand(session, stream)
+        stream, error_stream, config_get = self.create_command(session)
         config_get(args=['default.s3.signature_version'],
                    parsed_globals=None)
         rendered = stream.getvalue()
-        self.assertEqual(rendered.strip(), '')
\ No newline at end of file
+        self.assertEqual(rendered.strip(), '')
+
+    def test_get_nested_attribute_from_implicit_default(self):
+        session = FakeSession({})
+        session.full_config = {
+            'profiles': {'default': {'s3': {'signature_version': 's3v4'}}}}
+        stream, error_stream, config_get = self.create_command(session)
+        config_get(args=['s3.signature_version'],
+                   parsed_globals=None)
+        rendered = stream.getvalue()
+        self.assertEqual(rendered.strip(), 's3v4')
+
+    def test_get_section_returns_error(self):
+        session = FakeSession({})
+        session.full_config = {
+            'profiles': {'default': {'s3': {'signature_version': 's3v4'}}}}
+        session.config = {'s3': {'signature_version': 's3v4'}}
+        stream, error_stream, config_get = self.create_command(session)
+        rc = config_get(args=['s3'], parsed_globals=None)
+        self.assertEqual(rc, 1)
+
+        error_message = error_stream.getvalue()
+        expected_message = (
+            'varname (s3) must reference a value, not a section or '
+            'sub-section.')
+        self.assertEqual(error_message, expected_message)
+        self.assertEqual(stream.getvalue(), '')
+
+    def test_get_non_string_returns_error(self):
+        # This should never happen, but we handle this case so we should
+        # test it.
+        session = FakeSession({})
+        session.full_config = {
+            'profiles': {'default': {'foo': object()}}}
+        stream, error_stream, config_get = self.create_command(session)
+        rc = config_get(args=['foo'], parsed_globals=None)
+        self.assertEqual(rc, 1)
+        self.assertEqual(stream.getvalue(), '')
+        self.assertEqual(error_stream.getvalue(), '')