def handler_wrapper(event, context, **kwargs): config_bucket = os.environ['CONFIG'] encrypt_key_arn = os.environ.get('ENCRYPT_KEY_ARN') conn = conf.conn(encrypt_key_arn) try: settings = conf.load_or_create( conn, config_bucket, config_file) if create else conf.load( conn, config_bucket, config_file) except Exception as err: logger.exception(err) logger.error('Failed to load or create configuration.') return { 'statusCode': 503, 'body': 'Failed to load configuration.' } configuration = { 'load': lambda: settings or {}, 'save': functools.partial(conf.save, conn, config_bucket, config_file), } return handler(event, context, configuration=configuration, **kwargs)
def test_read_only(): encrypt_key_arn = 'arn:aws:kms:region:account_id:key/guid' bucket_name = 'bucket' file_name = 'conf.json' settings = {'setting_1': 'foo'} s3 = botocore.session.get_session().create_client('s3') conn = conf.conn(encrypt_key_arn, client=s3) expected_put_response = { 'Expiration': 'string', 'ETag': 'string', 'ServerSideEncryption': 'AES256', 'VersionId': 'string', 'SSECustomerAlgorithm': 'string', 'SSECustomerKeyMD5': 'string', 'SSEKMSKeyId': 'string', 'RequestCharged': 'requester' } put_parameters = { 'Body': json.dumps(settings), 'Bucket': bucket_name, 'Key': file_name, 'SSEKMSKeyId': 'arn:aws:kms:region:account_id:key/guid', 'ServerSideEncryption': 'aws:kms' } data = BytesIO(json.dumps(settings).encode('utf-8')) # data.seek(0) expected_get_response = { 'Body': StreamingBody(raw_stream=data, content_length=20) } get_parameters = {'Bucket': bucket_name, 'Key': file_name} with Stubber(s3) as stubber: # verify we can put data in stubber.add_response('put_object', expected_put_response, put_parameters) saved_settings = conf.save(conn, bucket_name, file_name, settings) assert saved_settings == settings # verify we can get data out stubber.add_response('get_object', expected_get_response, get_parameters) loaded_settings = conf.read_only(conn, bucket_name, file_name) assert loaded_settings == settings # verify cache has data assert conf.read_only_cache.currsize == 1 # verify cache has the right data assert conf.read_only_cache[('bucket', 'conf.json')] == settings
def test_configuration_no_encrypt_key(): bucket_name = 'bucket' file_name = 'conf.json' settings = {'setting_1': 'foo'} s3 = botocore.session.get_session().create_client('s3') conn = conf.conn(client=s3) expected_put_response = { 'Expiration': 'string', 'ETag': 'string', 'ServerSideEncryption': 'AES256', 'VersionId': 'string', 'SSECustomerAlgorithm': 'string', 'SSECustomerKeyMD5': 'string', 'SSEKMSKeyId': 'string', 'RequestCharged': 'requester' } put_parameters = { 'Body': json.dumps(settings), 'Bucket': bucket_name, 'Key': file_name, 'ServerSideEncryption': 'AES256' } data = BytesIO(json.dumps(settings).encode('utf-8')) # data.seek(0) expected_get_response = { 'Body': StreamingBody(raw_stream=data, content_length=20) } get_parameters = {'Bucket': bucket_name, 'Key': file_name} with Stubber(s3) as stubber: stubber.add_response('put_object', expected_put_response, put_parameters) saved_settings = conf.save(conn, bucket_name, file_name, settings) stubber.add_response('get_object', expected_get_response, get_parameters) loaded_settings = conf.load(conn, bucket_name, file_name) assert saved_settings == settings assert loaded_settings == settings