# Path: kingpin/config_utils/s3config.py
# class S3Config(object):
#     """ This class takes a boto config file with aws keys and an S3 bucket name as arguments.
#         This is used as the wrapper we read and write data to S3.
#     """
# 
#     def __init__(self, aws_key_file, s3_bucket, s3_endpoint="s3.amazonaws.com"):
#         self.aws_access_key_id, self.aws_secret_access_key = self._get_aws_credentials_from_file(aws_key_file)
#         self.s3_bucket = s3_bucket
#         self.s3_endpoint = s3_endpoint
#         self._set_aws_config(aws_key_file)
# 
#     def _get_aws_credentials_from_file(self, aws_key_file):
#         try:
#             config = ConfigParser.ConfigParser()
#             config.read(aws_key_file)
#             aws_access_key = config.get(AWS_KEYFILE_CREDENTIALS_SECTION,
#                                         AWS_KEYFILE_ACCESS_KEY_FIELD)
#             aws_secret_key = config.get(AWS_KEYFILE_CREDENTIALS_SECTION,
#                                         AWS_KEYFILE_SECRET_KEY_FIELD)
#             return aws_access_key, aws_secret_key
#         except Exception as e:
#             return None, None
# 
#     def _set_aws_config(self, config_file):
#         """ Parse an AWS credential file in boto (yaml) format:
#             [Credentials]
#             aws_access_key_id = [aws access key id]
#             aws_secret_access_key = [aws secret key id]
#         """
#         config = boto.config
#         if os.path.isfile(config_file):
#             config.load_credential_file(config_file)
# 
#     def _get_bucket_conn(self):
#         # Use named endpoint to achieve data consistency
#         s3_conn = boto.connect_s3(host=self.s3_endpoint,
#                                   aws_access_key_id=self.aws_access_key_id,
#                                   aws_secret_access_key=self.aws_secret_access_key)
#         s3_bucket = s3_conn.get_bucket(self.s3_bucket, validate=False)
#         return s3_bucket
# 
#     def put_config_string(self, keyname, data):
#         """ Put the config data into a keyname
#             will replace . with / in the keyname so that this will happen:
#             discovery.service.prod -> discovery/service/prod
#         """
#         keyname = keyname.replace('.', '/')
#         s3_bucket = self._get_bucket_conn()
#         s3_key = s3_bucket.get_key(keyname)
#         if s3_key is None:
#             s3_key = Key(s3_bucket, keyname)
#         try:
#             s3_key.set_contents_from_string(data)
#         except boto.exception.S3ResponseError, err:
#             return err
# 
#     def get_config_string(self, keyname):
#         """ Fetch the data from an S3 config file named keyname
#         """
#         keyname = keyname.replace('.', '/')
#         s3_bucket = self._get_bucket_conn()
#         s3_key = s3_bucket.get_key(keyname)
#         if s3_key is not None:
#             s3_data = s3_key.get_contents_as_string()
#             return s3_data
#         else:
#             raise ValueError("404 keyname not found")


from kingpin.config_utils.s3config import S3Config
from unittest import TestCase
#!/usr/bin/python
#
# Copyright (c) 2016, Pinterest Inc.  All rights reserved.



class ReadAwsKeyFileTestCase(TestCase):

    def test_read_aws_credentials(self):
        s3config = S3Config('kingpin/tests/config_utils/test_boto_configs.conf', "s3_bucket")
        aws_access_key_id = s3config.aws_access_key_id
        aws_secret_access_key = s3config.aws_secret_access_key
        self.assertEqual(aws_access_key_id, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
        self.assertEqual(aws_secret_access_key, 'abcdefghijklmnopqrstuvwxyz1234567890')

    def test_read_nonexisting_aws_key_file(self):

# Path: kingpin/config_utils/s3config.py
# class S3Config(object):
#     """ This class takes a boto config file with aws keys and an S3 bucket name as arguments.
#         This is used as the wrapper we read and write data to S3.
#     """
# 
#     def __init__(self, aws_key_file, s3_bucket, s3_endpoint="s3.amazonaws.com"):
#         self.aws_access_key_id, self.aws_secret_access_key = self._get_aws_credentials_from_file(aws_key_file)
#         self.s3_bucket = s3_bucket
#         self.s3_endpoint = s3_endpoint
#         self._set_aws_config(aws_key_file)
# 
#     def _get_aws_credentials_from_file(self, aws_key_file):
#         try:
#             config = ConfigParser.ConfigParser()
#             config.read(aws_key_file)
#             aws_access_key = config.get(AWS_KEYFILE_CREDENTIALS_SECTION,
#                                         AWS_KEYFILE_ACCESS_KEY_FIELD)
#             aws_secret_key = config.get(AWS_KEYFILE_CREDENTIALS_SECTION,
#                                         AWS_KEYFILE_SECRET_KEY_FIELD)
#             return aws_access_key, aws_secret_key
#         except Exception as e:
#             return None, None
# 
#     def _set_aws_config(self, config_file):
#         """ Parse an AWS credential file in boto (yaml) format:
#             [Credentials]
#             aws_access_key_id = [aws access key id]
#             aws_secret_access_key = [aws secret key id]
#         """
#         config = boto.config
#         if os.path.isfile(config_file):
#             config.load_credential_file(config_file)
# 
#     def _get_bucket_conn(self):
#         # Use named endpoint to achieve data consistency
#         s3_conn = boto.connect_s3(host=self.s3_endpoint,
#                                   aws_access_key_id=self.aws_access_key_id,
#                                   aws_secret_access_key=self.aws_secret_access_key)
#         s3_bucket = s3_conn.get_bucket(self.s3_bucket, validate=False)
#         return s3_bucket
# 
#     def put_config_string(self, keyname, data):
#         """ Put the config data into a keyname
#             will replace . with / in the keyname so that this will happen:
#             discovery.service.prod -> discovery/service/prod
#         """
#         keyname = keyname.replace('.', '/')
#         s3_bucket = self._get_bucket_conn()
#         s3_key = s3_bucket.get_key(keyname)
#         if s3_key is None:
#             s3_key = Key(s3_bucket, keyname)
#         try:
#             s3_key.set_contents_from_string(data)
#         except boto.exception.S3ResponseError, err:
#             return err
# 
#     def get_config_string(self, keyname):
#         """ Fetch the data from an S3 config file named keyname
#         """
#         keyname = keyname.replace('.', '/')
#         s3_bucket = self._get_bucket_conn()
#         s3_key = s3_bucket.get_key(keyname)
#         if s3_key is not None:
#             s3_data = s3_key.get_contents_as_string()
#             return s3_data
#         else:
#             raise ValueError("404 keyname not found")


# Path: kingpin/tests/config_utils/test_read_aws_key_file.py
from kingpin.config_utils.s3config import S3Config
from unittest import TestCase

#!/usr/bin/python
#
# Copyright (c) 2016, Pinterest Inc.  All rights reserved.



class ReadAwsKeyFileTestCase(TestCase):

    def test_read_aws_credentials(self):
        s3config = S3Config('kingpin/tests/config_utils/test_boto_configs.conf', "s3_bucket")
        aws_access_key_id = s3config.aws_access_key_id
        aws_secret_access_key = s3config.aws_secret_access_key
        self.assertEqual(aws_access_key_id, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
        self.assertEqual(aws_secret_access_key, 'abcdefghijklmnopqrstuvwxyz1234567890')

    def test_read_nonexisting_aws_key_file(self):

        s3config = S3Config('kingpin/tests/config_utils/non_exist_test_boto_configs.conf', "s3_bucket")