2020-10-20 14:00:53 +01:00
|
|
|
import json
|
|
|
|
|
|
2020-10-20 13:33:51 +01:00
|
|
|
import boto3
|
|
|
|
|
|
2020-10-29 11:12:28 +00:00
|
|
|
from app.models import BroadcastProviders
|
|
|
|
|
|
2020-10-22 12:19:25 +01:00
|
|
|
# The variable names in this file have specific meaning in a CAP message
|
|
|
|
|
#
|
|
|
|
|
# identifier is a unique field for each CAP message
|
|
|
|
|
#
|
|
|
|
|
# headline is a field which we are not sure if we will use
|
|
|
|
|
#
|
|
|
|
|
# description is the body of the message
|
2020-10-23 16:44:11 +01:00
|
|
|
|
|
|
|
|
# areas is a list of dicts, with the following items
|
|
|
|
|
# * description is a string which populates the areaDesc field
|
|
|
|
|
# * polygon is a list of lat/long pairs
|
2020-10-22 12:19:25 +01:00
|
|
|
#
|
|
|
|
|
# references is a whitespace separated list of message identifiers
|
|
|
|
|
# where each identifier is a previous sent message
|
|
|
|
|
# ie a Cancel message would have a unique identifier but have the identifier of
|
|
|
|
|
# the preceeding Alert message in the references field
|
|
|
|
|
|
2020-10-20 15:18:24 +01:00
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
class CBCProxyException(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2020-10-20 11:18:46 +01:00
|
|
|
# Noop = no operation
|
|
|
|
|
class CBCProxyNoopClient:
|
|
|
|
|
|
|
|
|
|
def init_app(self, app):
|
|
|
|
|
pass
|
|
|
|
|
|
2020-10-26 17:14:08 +00:00
|
|
|
def send_canary(
|
|
|
|
|
self,
|
|
|
|
|
identifier,
|
|
|
|
|
):
|
|
|
|
|
pass
|
|
|
|
|
|
2020-10-27 14:44:04 +00:00
|
|
|
def send_link_test(
|
|
|
|
|
self,
|
|
|
|
|
identifier,
|
|
|
|
|
):
|
|
|
|
|
pass
|
|
|
|
|
|
2020-10-20 11:18:46 +01:00
|
|
|
def create_and_send_broadcast(
|
|
|
|
|
self,
|
2020-10-28 11:26:38 +00:00
|
|
|
identifier, headline, description, areas,
|
|
|
|
|
sent, expires,
|
2020-10-20 11:18:46 +01:00
|
|
|
):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# We have not implementated updating a broadcast
|
|
|
|
|
def update_and_send_broadcast(
|
|
|
|
|
self,
|
2020-10-28 11:26:38 +00:00
|
|
|
identifier, references, headline, description, areas,
|
|
|
|
|
sent, expires,
|
2020-10-20 11:18:46 +01:00
|
|
|
):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# We have not implemented cancelling a broadcast
|
|
|
|
|
def cancel_broadcast(
|
|
|
|
|
self,
|
2020-10-28 11:26:38 +00:00
|
|
|
identifier, references, headline, description, areas,
|
|
|
|
|
sent, expires,
|
2020-10-20 11:18:46 +01:00
|
|
|
):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CBCProxyClient:
|
2020-10-29 11:12:28 +00:00
|
|
|
provider_function_name_map = {
|
|
|
|
|
BroadcastProviders.EE: 'bt-ee-1-proxy',
|
|
|
|
|
}
|
2020-10-20 11:18:46 +01:00
|
|
|
|
|
|
|
|
def init_app(self, app):
|
2020-10-22 12:22:11 +01:00
|
|
|
self._lambda_client = boto3.client(
|
2020-10-20 13:33:51 +01:00
|
|
|
'lambda',
|
|
|
|
|
region_name='eu-west-2',
|
|
|
|
|
aws_access_key_id=app.config['CBC_PROXY_AWS_ACCESS_KEY_ID'],
|
|
|
|
|
aws_secret_access_key=app.config['CBC_PROXY_AWS_SECRET_ACCESS_KEY'],
|
|
|
|
|
)
|
2020-10-20 11:18:46 +01:00
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
def _invoke_lambda(self, function_name, payload):
|
|
|
|
|
payload_bytes = bytes(json.dumps(payload), encoding='utf8')
|
2020-10-26 17:14:08 +00:00
|
|
|
|
|
|
|
|
result = self._lambda_client.invoke(
|
2020-10-29 10:22:50 +00:00
|
|
|
FunctionName=function_name,
|
2020-10-26 17:14:08 +00:00
|
|
|
InvocationType='RequestResponse',
|
|
|
|
|
Payload=payload_bytes,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if result['StatusCode'] > 299:
|
2020-10-29 10:22:50 +00:00
|
|
|
raise CBCProxyException('Could not invoke lambda')
|
2020-10-26 17:14:08 +00:00
|
|
|
|
|
|
|
|
if 'FunctionError' in result:
|
2020-10-29 10:22:50 +00:00
|
|
|
raise CBCProxyException('Function exited with unhandled exception')
|
2020-10-26 17:14:08 +00:00
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def send_canary(
|
2020-10-27 14:44:04 +00:00
|
|
|
self,
|
|
|
|
|
identifier,
|
|
|
|
|
):
|
2020-10-29 11:12:28 +00:00
|
|
|
"""
|
|
|
|
|
canary - a specific lambda that does not connect to a provider, but just confirms the connectivity between
|
|
|
|
|
Notify and the CBC proxy AWS account
|
|
|
|
|
"""
|
2020-10-29 10:22:50 +00:00
|
|
|
self._invoke_lambda(function_name='canary', payload={'identifier': identifier})
|
2020-10-27 14:44:04 +00:00
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
def send_link_test(
|
|
|
|
|
self,
|
|
|
|
|
identifier,
|
2020-10-29 11:12:28 +00:00
|
|
|
provider,
|
2020-10-29 10:22:50 +00:00
|
|
|
):
|
2020-10-29 11:12:28 +00:00
|
|
|
"""
|
|
|
|
|
link test - open up a connection to a specific provider, and send them an xml payload with a <msgType> of
|
|
|
|
|
test.
|
|
|
|
|
"""
|
2020-10-29 10:22:50 +00:00
|
|
|
payload = {'message_type': 'test', 'identifier': identifier}
|
2020-10-27 14:44:04 +00:00
|
|
|
|
2020-10-29 10:22:50 +00:00
|
|
|
self._invoke_lambda(function_name='bt-ee-1-proxy', payload=payload)
|
2020-10-27 14:44:04 +00:00
|
|
|
|
2020-10-20 11:18:46 +01:00
|
|
|
def create_and_send_broadcast(
|
|
|
|
|
self,
|
2020-10-23 16:44:11 +01:00
|
|
|
identifier, headline, description, areas,
|
2020-10-28 11:26:38 +00:00
|
|
|
sent, expires,
|
2020-10-20 11:18:46 +01:00
|
|
|
):
|
2020-10-29 10:22:50 +00:00
|
|
|
payload = {
|
2020-10-27 14:44:04 +00:00
|
|
|
'message_type': 'alert',
|
2020-10-20 14:00:53 +01:00
|
|
|
'identifier': identifier,
|
|
|
|
|
'headline': headline,
|
|
|
|
|
'description': description,
|
2020-10-23 16:44:11 +01:00
|
|
|
'areas': areas,
|
2020-10-29 10:22:50 +00:00
|
|
|
'sent': sent,
|
|
|
|
|
'expires': expires,
|
|
|
|
|
}
|
|
|
|
|
self._invoke_lambda(function_name='bt-ee-1-proxy', payload=payload)
|
2020-10-20 11:18:46 +01:00
|
|
|
|
|
|
|
|
# We have not implementated updating a broadcast
|
|
|
|
|
def update_and_send_broadcast(
|
|
|
|
|
self,
|
2020-10-23 16:44:11 +01:00
|
|
|
identifier, references, headline, description, areas,
|
2020-10-28 11:26:38 +00:00
|
|
|
sent, expires,
|
2020-10-20 11:18:46 +01:00
|
|
|
):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# We have not implemented cancelling a broadcast
|
|
|
|
|
def cancel_broadcast(
|
|
|
|
|
self,
|
2020-10-23 16:44:11 +01:00
|
|
|
identifier, references, headline, description, areas,
|
2020-10-28 11:26:38 +00:00
|
|
|
sent, expires,
|
2020-10-20 11:18:46 +01:00
|
|
|
):
|
|
|
|
|
pass
|