mirror of
https://github.com/projecthorus/sondehub-infra.git
synced 2024-12-23 15:02:26 +00:00
add flight doc updates
This commit is contained in:
parent
73b46b88ba
commit
e4218a65e4
25
es.tf
25
es.tf
@ -112,6 +112,11 @@ resource "aws_cognito_identity_pool" "CognitoIdentityPool" {
|
||||
client_id = "u3ggvo1spp1e6cffbietq7fbm"
|
||||
provider_name = "cognito-idp.us-east-1.amazonaws.com/us-east-1_G4H7NMniM"
|
||||
}
|
||||
cognito_identity_providers { // for the website
|
||||
client_id = "21dpr4kth8lonk2rq803loh5oa"
|
||||
provider_name = "cognito-idp.us-east-1.amazonaws.com/us-east-1_G4H7NMniM"
|
||||
server_side_token_check = false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -392,6 +397,26 @@ resource "aws_iam_role_policy" "IAMPolicy3" {
|
||||
}
|
||||
]
|
||||
}
|
||||
EOF
|
||||
role = aws_iam_role.auth_role.name
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy" "IAMPolicy4" {
|
||||
policy = <<EOF
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": [
|
||||
"execute-api:*"
|
||||
],
|
||||
"Resource": [
|
||||
"arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.main.id}/*/*/amateur/flightdoc"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
EOF
|
||||
role = aws_iam_role.auth_role.name
|
||||
}
|
45
ham_flightdoc.tf
Normal file
45
ham_flightdoc.tf
Normal file
@ -0,0 +1,45 @@
|
||||
# cognito auth role
|
||||
|
||||
resource "aws_lambda_function" "ham_flight_doc" {
|
||||
function_name = "ham-put-flight-doc"
|
||||
handler = "ham_update_flight_doc.lambda_handler"
|
||||
s3_bucket = aws_s3_bucket_object.lambda.bucket
|
||||
s3_key = aws_s3_bucket_object.lambda.key
|
||||
source_code_hash = data.archive_file.lambda.output_base64sha256
|
||||
publish = true
|
||||
memory_size = 128
|
||||
role = aws_iam_role.basic_lambda_role.arn
|
||||
runtime = "python3.9"
|
||||
timeout = 30
|
||||
architectures = ["arm64"]
|
||||
environment {
|
||||
variables = {
|
||||
"ES" = "es.${local.domain_name}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_lambda_permission" "ham_flight_doc" {
|
||||
action = "lambda:InvokeFunction"
|
||||
function_name = aws_lambda_function.ham_flight_doc.arn
|
||||
principal = "apigateway.amazonaws.com"
|
||||
source_arn = "arn:aws:execute-api:us-east-1:${data.aws_caller_identity.current.account_id}:${aws_apigatewayv2_api.main.id}/*/*/amateur/flightdoc"
|
||||
}
|
||||
|
||||
resource "aws_apigatewayv2_route" "ham_flight_doc" {
|
||||
api_id = aws_apigatewayv2_api.main.id
|
||||
api_key_required = false
|
||||
authorization_type = "AWS_IAM"
|
||||
route_key = "PUT /amateur/flightdoc"
|
||||
target = "integrations/${aws_apigatewayv2_integration.ham_flight_doc.id}"
|
||||
}
|
||||
|
||||
resource "aws_apigatewayv2_integration" "ham_flight_doc" {
|
||||
api_id = aws_apigatewayv2_api.main.id
|
||||
connection_type = "INTERNET"
|
||||
integration_method = "POST"
|
||||
integration_type = "AWS_PROXY"
|
||||
integration_uri = aws_lambda_function.ham_flight_doc.arn
|
||||
timeout_milliseconds = 30000
|
||||
payload_format_version = "2.0"
|
||||
}
|
34
lambda/ham_update_flight_doc/__init__.py
Normal file
34
lambda/ham_update_flight_doc/__init__.py
Normal file
@ -0,0 +1,34 @@
|
||||
import json
|
||||
import zlib
|
||||
import base64
|
||||
import datetime
|
||||
from email.utils import parsedate
|
||||
|
||||
import es
|
||||
|
||||
def lambda_handler(event, context):
|
||||
if "isBase64Encoded" in event and event["isBase64Encoded"] == True:
|
||||
event["body"] = base64.b64decode(event["body"])
|
||||
if (
|
||||
"content-encoding" in event["headers"]
|
||||
and event["headers"]["content-encoding"] == "gzip"
|
||||
):
|
||||
event["body"] = zlib.decompress(event["body"], 16 + zlib.MAX_WBITS)
|
||||
try:
|
||||
payload = json.loads(event["body"])
|
||||
except:
|
||||
return {"statusCode": 400, "body": "JSON decode issue"}
|
||||
payload["datetime"] = datetime.datetime.utcnow().isoformat()
|
||||
if type(payload['ascent_rate']) not in [float, int]:
|
||||
return {"statusCode": 400, "body": "ascent_rate must be a number"}
|
||||
if type(payload['descent_rate']) not in [float, int]:
|
||||
return {"statusCode": 400, "body": "descent_rate must be a number"}
|
||||
if type(payload['peak_altitude']) not in [float, int]:
|
||||
return {"statusCode": 400, "body": "peak_altitude must be a number"}
|
||||
if type(payload['float_expected']) not in [bool]:
|
||||
return {"statusCode": 400, "body": "float_expected must be a bool"}
|
||||
payload['identity'] = event['requestContext']['authorizer']['iam']['cognitoIdentity']['amr'][2]
|
||||
|
||||
es.request(json.dumps(payload),f"flight-doc/_doc","POST")
|
||||
|
||||
return {"statusCode": 200, "body": "^v^ updated"}
|
67
lambda/ham_update_flight_doc/__main__.py
Normal file
67
lambda/ham_update_flight_doc/__main__.py
Normal file
@ -0,0 +1,67 @@
|
||||
from . import *
|
||||
payload = {
|
||||
"version": "2.0",
|
||||
"routeKey": "PUT /amateur/flightdoc",
|
||||
"rawPath": "/amateur/flightdoc",
|
||||
"rawQueryString": "",
|
||||
"headers": {
|
||||
"accept": "*/*",
|
||||
"accept-encoding": "gzip, deflate, br",
|
||||
"accept-language": "en-AU,en;q=0.9",
|
||||
"authorization": "AWS4-HMAC-SHA256 Credential=ASIASC7NF3EG2IN32V5U/20220510/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token;x-amz-user-agent, Signature=2c79b43997baee65869b98bbf8525116a297d57d90af1f311b5c1ff0e046cab0",
|
||||
"content-length": "109",
|
||||
"content-type": "application/json",
|
||||
"host": "api-raw.v2.sondehub.org",
|
||||
"origin": "http://localhost:8000",
|
||||
"referer": "http://localhost:8000/",
|
||||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15",
|
||||
"x-amz-content-sha256": "5cdaa82af662f6efdff22bc28a9ce534a4250025378f30f7b917506d31cbec90",
|
||||
"x-amz-date": "20220510T233932Z",
|
||||
"x-amz-security-token": "IQoJb3JpZ2luX2VjEEgaCXVzLWVhc3QtMSJIMEYCIQC1dWVTNpEB0saU5TPR9NHVmOdiBK1QfjMB1LrPZsea+AIhAIuhvNxDNF/gUaNrnmgK2de45dogn8+1C5HhEpqpKHqBKsQECCEQARoMMTQzODQxOTQxNzczIgzIMKmrFJy94ru7nrwqoQRnvyF9anCZpsqJbtc8X2aErXQvZwrKClPq26KpnHot4alk8k4WzOHjFYA1GOPDpbAPe8jpTKoNpzcU7twpKDk6znA+fo3bSnkhAqAyackmag7Tff4w5bABgysWmHafHrZBo4TIyihlRSmHSZNUo7NwFajOKg5wiHSrneF719Qxl1hcbqp5FuxUG1xWc8ySUOFarQ+gxtm1yGz33WMYqjkND1R5wANH3ii4Wz39Xd4FR5IJgm5nAdyaKVM536tW/KZOOtbyGPlMyyGjKNZYV0PDtWclHaBEPwSgCI4m+l5WRga2/WQuRtkTFAQXSn45aGcT7nUNflixbyD0tsHuHILFVOr64MVliumnTf7v60FJjUJqyTLYlG4hcSG+U+FWxFnBcWxPklILzx3QtSRj0gOCIV8rll5i6APq9vngHu3QBC9V96w06Fgz21jmyv2KF4/+lYTSR4d5DwF44+OttE9vtoibV8i7nZCZcNLwzb3F9qMyAmnJL2I2ZjcL8JeJyd9fz9IBiT1T2E/vmD1yfZt5gGN3/N+aSwj+w30CChUt8bhdpTVJqUtPhnLYdT3rOAez84HWvOmMIrZakCSy+ZRixLP6JCjmko2uQvKF3ohUeVGs97Z4d1cyJKaGYNxq7Jm48xy8oa5zsFN6b2up0fpRa7UqvFsiAMYq7C3SZKHiry67GO8ESa0zJkjke5g/SGNUf1O6IrmyuDScg/r2SjF9zzCM7uuTBjqEAjkBXGngIs6QyM2CUNEKAfGZ+MSM5D/da4EdnKIN+YJsu4KHYTepXDGtCpXuXmsI0fZkN7lxQPBYxtRnRzngph0SZaXq8cxLY/PBPievd4oQ8dnGX9Gy9lPLyq360igqlpxXAAke8C2CORr+y29pO6M8lFg7ds47Y5TXQ8wyfdUQAtXkBqklqHNEDMNJrZPFg/dJqSEGxxT/1GYNTdUKupGkNw0fHAv9XoBeewPsXoulOk4HlyiaKeEpZZWDsclpjNHtEYaASR0KzlWJ/KYPTkFaGpb8OzMg/KKL3Qvg8t8drEy5LZPhWNIOvOnKYaf+6V0nL62yS03YMIa07dADPwUTSHgA",
|
||||
"x-amz-user-agent": "aws-sdk-js/2.1130.0",
|
||||
"x-amzn-trace-id": "Root=1-627af7b4-5966222c69fc1dcb6a0d98e0",
|
||||
"x-forwarded-for": "14.203.163.173",
|
||||
"x-forwarded-port": "443",
|
||||
"x-forwarded-proto": "https"
|
||||
},
|
||||
"requestContext": {
|
||||
"accountId": "143841941773",
|
||||
"apiId": "r03szwwq41",
|
||||
"authorizer": {
|
||||
"iam": {
|
||||
"accessKey": "ASIASC7NF3EG2IN32V5U",
|
||||
"accountId": "143841941773",
|
||||
"callerId": "AROASC7NF3EGZURC7BJTC:CognitoIdentityCredentials",
|
||||
"cognitoIdentity": {
|
||||
"amr": [
|
||||
"authenticated",
|
||||
"cognito-idp.us-east-1.amazonaws.com/us-east-1_G4H7NMniM",
|
||||
"cognito-idp.us-east-1.amazonaws.com/us-east-1_G4H7NMniM:CognitoSignIn:61f3c171-39f9-474b-9900-9953b755b474"
|
||||
],
|
||||
"identityId": "us-east-1:54e08cdd-84df-42b3-ab16-e568ef118e8c",
|
||||
"identityPoolId": "us-east-1:55e43eac-9626-43e1-a7d2-bbc57f5f5aa9"
|
||||
},
|
||||
"principalOrgId": None,
|
||||
"userArn": "arn:aws:sts::143841941773:assumed-role/Cognito_sondesAuth_Role/CognitoIdentityCredentials",
|
||||
"userId": "AROASC7NF3EGZURC7BJTC:CognitoIdentityCredentials"
|
||||
}
|
||||
},
|
||||
"domainName": "api-raw.v2.sondehub.org",
|
||||
"domainPrefix": "api-raw",
|
||||
"http": {
|
||||
"method": "PUT",
|
||||
"path": "/amateur/flightdoc",
|
||||
"protocol": "HTTP/1.1",
|
||||
"sourceIp": "14.203.163.173",
|
||||
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15"
|
||||
},
|
||||
"requestId": "R7ukRiWYIAMEJ2g=",
|
||||
"routeKey": "PUT /amateur/flightdoc",
|
||||
"stage": "$default",
|
||||
"time": "10/May/2022:23:39:32 +0000",
|
||||
"timeEpoch": 1652225972793
|
||||
},
|
||||
"body": "{\"payload_callsign\":\"4FSKTEST\",\"float_expected\":false,\"peak_altitude\":30000,\"descent_rate\":5,\"ascent_rate\":5}",
|
||||
"isBase64Encoded": False
|
||||
}
|
||||
print(lambda_handler(payload, {}))
|
Loading…
Reference in New Issue
Block a user