diff --git a/botocore/utils.py b/botocore/utils.py index bc916cbc67..e93644efb1 100644 --- a/botocore/utils.py +++ b/botocore/utils.py @@ -211,8 +211,13 @@ def parse_key_val_file(filename, _open=open): def parse_key_val_file_contents(contents): + # This was originally extracted from the EC2 credential provider, which was + # fairly lenient in its parsing. We only try to parse key/val pairs if + # there's a '=' in the line. final = {} for line in contents.splitlines(): + if '=' not in line: + continue key, val = line.split('=', 1) key = key.strip() val = val.strip() diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index d6eefde84c..c531e67dad 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -151,6 +151,26 @@ def test_parse_ec2_content_empty(self): contents = "" self.assertEqual(parse_key_val_file_contents(contents), {}) + def test_key_val_pair_with_blank_lines(self): + # The \n\n has an extra blank between the access/secret keys. + contents = "AWSAccessKeyId=a\n\nAWSSecretKey=b\n" + self.assertEqual(parse_key_val_file_contents(contents), + {'AWSAccessKeyId': 'a', + 'AWSSecretKey': 'b'}) + + def test_key_val_parser_lenient(self): + # Ignore any line that does not have a '=' char in it. + contents = "AWSAccessKeyId=a\nNOTKEYVALLINE\nAWSSecretKey=b\n" + self.assertEqual(parse_key_val_file_contents(contents), + {'AWSAccessKeyId': 'a', + 'AWSSecretKey': 'b'}) + + def test_multiple_equals_on_line(self): + contents = "AWSAccessKeyId=a\nAWSSecretKey=secret_key_with_equals=b\n" + self.assertEqual(parse_key_val_file_contents(contents), + {'AWSAccessKeyId': 'a', + 'AWSSecretKey': 'secret_key_with_equals=b'}) + def test_os_error_raises_config_not_found(self): mock_open = mock.Mock() mock_open.side_effect = OSError()