-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
180 changed files
with
21,011 additions
and
7,613 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
requests>=2.22.0 | ||
requests>=2.25.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ pytest>=4.2.1 | |
pytest-cov | ||
pytest-env | ||
bandit | ||
boto3==1.23.9 | ||
requests==2.27.1 |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: MIT-0 | ||
|
||
from __future__ import print_function | ||
import urllib3 | ||
import json | ||
|
||
SUCCESS = "SUCCESS" | ||
FAILED = "FAILED" | ||
|
||
http = urllib3.PoolManager() | ||
|
||
def send(event, context, responseStatus, responseData, physicalResourceId=None, noEcho=False, reason=None): | ||
responseUrl = event['ResponseURL'] | ||
|
||
print(responseUrl) | ||
|
||
responseBody = { | ||
'Status' : responseStatus, | ||
'Reason' : reason or "See the details in CloudWatch Log Stream: {}".format(context.log_stream_name), | ||
'PhysicalResourceId' : physicalResourceId or context.log_stream_name, | ||
'StackId' : event['StackId'], | ||
'RequestId' : event['RequestId'], | ||
'LogicalResourceId' : event['LogicalResourceId'], | ||
'NoEcho' : noEcho, | ||
'Data' : responseData | ||
} | ||
|
||
json_responseBody = json.dumps(responseBody) | ||
|
||
print("Response body:") | ||
print(json_responseBody) | ||
|
||
headers = { | ||
'content-type' : '', | ||
'content-length' : str(len(json_responseBody)) | ||
} | ||
|
||
try: | ||
response = http.request('PUT', responseUrl, headers=headers, body=json_responseBody) | ||
print("Status code:", response.status) | ||
|
||
except Exception as e: | ||
print("send(..) failed executing http.request(..):", e) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/python | ||
############################################################################### | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # | ||
# # | ||
# Licensed under the Apache License Version 2.0 (the "License"). You may not # | ||
# use this file except in compliance with the License. A copy of the License # | ||
# is located at # | ||
# # | ||
# http://www.apache.org/licenses/LICENSE-2.0/ # | ||
# # | ||
# or in the "license" file accompanying this file. This file is distributed # | ||
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express # | ||
# or implied. See the License for the specific language governing permis- # | ||
# sions and limitations under the License. # | ||
############################################################################### | ||
|
||
import cfnresponse | ||
from pytest_mock import mocker | ||
from unittest.mock import ANY | ||
import pytest | ||
import json | ||
import os | ||
|
||
os.environ['AWS_REGION'] = 'us-east-1' | ||
os.environ['AWS_PARTITION'] = 'aws' | ||
|
||
@pytest.fixture() | ||
def urllib_mock(mocker): | ||
yield mocker.patch('cfnresponse.http') | ||
|
||
@pytest.fixture() | ||
def event(): | ||
yield { | ||
'ResponseURL': 'response_url', | ||
'StackId': 'stack_id', | ||
'RequestId': 'request_id', | ||
'LogicalResourceId': 'logical_resource_id' | ||
} | ||
|
||
class Context: | ||
def __init__(self, log_stream_name): | ||
self.log_stream_name = log_stream_name | ||
|
||
@pytest.fixture() | ||
def context(): | ||
yield Context('log_stream_name') | ||
|
||
def body_correct(body, event, context, status, response_data, physical_resource_id = None, no_echo = False, reason = None): | ||
assert body['Status'] == status | ||
assert body['StackId'] == event['StackId'] | ||
assert body['RequestId'] == event['RequestId'] | ||
assert body['LogicalResourceId'] == event['LogicalResourceId'] | ||
if physical_resource_id is not None: | ||
assert body['PhysicalResourceId'] == physical_resource_id | ||
else: | ||
assert body['PhysicalResourceId'] == context.log_stream_name | ||
assert body['NoEcho'] == no_echo | ||
if reason is not None: | ||
assert body['Reason'] == reason | ||
else: | ||
assert context.log_stream_name in body['Reason'] | ||
assert body['Data'] == response_data | ||
return True | ||
|
||
def test_send(urllib_mock, event, context): | ||
status = cfnresponse.SUCCESS | ||
response_data = {} | ||
cfnresponse.send(event, context, status, response_data) | ||
urllib_mock.request.assert_called_once_with('PUT', event['ResponseURL'], body = ANY, headers = ANY) | ||
_, _, call_kwargs = urllib_mock.request.mock_calls[0] | ||
assert body_correct(json.loads(call_kwargs['body']), event, context, status, response_data) | ||
assert call_kwargs['headers']['content-length'] == str(len(call_kwargs['body'])) | ||
|
||
def test_send_with_reason(urllib_mock, event, context): | ||
status = cfnresponse.FAILED | ||
response_data = {'some': 'data', 'key': 'value'} | ||
physical_resource_id = 'some_id' | ||
no_echo = True | ||
reason = 'some_reason' | ||
cfnresponse.send(event, context, status, response_data, physical_resource_id, no_echo, reason) | ||
urllib_mock.request.assert_called_once_with('PUT', event['ResponseURL'], body = ANY, headers = ANY) | ||
_, _, call_kwargs = urllib_mock.request.mock_calls[0] | ||
assert body_correct(json.loads(call_kwargs['body']), event, context, status, response_data, physical_resource_id, no_echo, reason) | ||
assert call_kwargs['headers']['content-length'] == str(len(call_kwargs['body'])) | ||
|
||
def test_send_exception(urllib_mock, event, context): | ||
urllib_mock.request.side_effect = Exception() | ||
cfnresponse.send(event, context, cfnresponse.FAILED, {}) |
Binary file not shown.
Oops, something went wrong.