Skip to content

Instantly share code, notes, and snippets.

@bbuechler
Created October 8, 2021 17:07
Show Gist options
  • Save bbuechler/680eb8facc8081fe5106fa243dce4939 to your computer and use it in GitHub Desktop.
Save bbuechler/680eb8facc8081fe5106fa243dce4939 to your computer and use it in GitHub Desktop.
Onion Peeler - Poke TEA from various levels

Onion Peeler

Setup

Create a lambda with these two files

Config

Enviroment

  • COOKIE - Active, valid JTW session cookie. Variable should be formatted <cookie-name>=<cookie-value>
  • aws_access_key_id & aws_secret_access_key - Creds for accessing the lambda

VPC

If you're running this in EDC, you'll need to copy the VPC networking (vpc, subnets, sg) from the local TEA.

import json
import boto3
import time
import uuid
import os
from urllib import request
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from statistics import mean
from statics import get_api_gateway_payload, NoRedirect
from statics import request_path, host, lambda_name, api_gateway_endpoint, cloudfront_endpoint
session = boto3.session.Session(aws_access_key_id=os.getenv("aws_access_key_id"),
aws_secret_access_key=os.getenv("aws_secret_access_key"),
region_name=os.getenv("region_name"))
def get_uuid():
return str(uuid.uuid1()).lower()
def call_lambda ( direct_origin_request = None):
# Generate a unique payload
origin_request = direct_origin_request or get_uuid()
print(f"Running request w/ x-origin-request-id = {origin_request}")
req_payload = get_api_gateway_payload(origin_request)
# Start your clocks!
timer = time.time()
lambda_res = session.client('lambda').invoke(FunctionName=lambda_name,
InvocationType='RequestResponse',
Payload=req_payload)
# How long did it take?
duration = "{0:.2f}".format((time.time() - timer)*1000)
# Some validation
resp = json.loads(lambda_res['Payload'].read())
redir = resp['headers']['Location']
request_id = resp['headers'].get('x-request-id')
origin_request_id = resp['headers'].get('x-origin-request-id')
print (f" - x-request-id = {request_id}")
return duration
def test_lambda_directly():
# warm up lambda from cold-start:
cold_start_uuid = get_uuid()
print(f"Warming things up with {cold_start_uuid}")
call_lambda(cold_start_uuid)
# Make 3 requests
direct_origin_request = get_uuid()
durations = []
for i in range(1,4):
print (f"Making Request {i} of 3 ({direct_origin_request})")
duration = call_lambda(direct_origin_request)
durations.append(float(duration))
avg = mean(durations)
print(f"Avg duration for direct lambda test was {avg:.2f}: {durations}")
return avg
def make_url(host):
return f"{host}/{request_path}"
def make_request (url, cookie, origin_request = None ):
origin_request = origin_request or get_uuid()
headers = {"cookie": cookie, "x-origin-request-id": origin_request }
timer = time.time()
opener = request.build_opener(NoRedirect)
request.install_opener(opener)
req = Request( url, headers=headers)
try:
resp = urlopen(req)
except HTTPError as e:
resp = e
except Exception as E:
print(f"Could not hit {url}: {E}")
return None
# Return our time a milliseconds
duration = "{0:.2f}".format((time.time() - timer)*1000)\
print (f"Response Headers: {resp.getheaders()}")
req_id = resp.getheader('x-request-id')
origin_req_id = resp.getheader('x-origin-request-id')
print (f"Executed {req_id}/{origin_req_id} in {duration} milliseconds")
return(duration)
def test_url(request_url):
cookie = os.getenv("COOKIE", "")
url = make_url(request_url)
# warm up lambda from cold-start:
cold_start_uuid = get_uuid()
print(f"Warming things up with {cold_start_uuid}")
make_request(url, cookie, cold_start_uuid)
# Make 3 requests
direct_origin_request = get_uuid()
durations = []
for i in range(1,4):
print (f"Making Request {i} of 3 ({direct_origin_request})")
duration = make_request(url, cookie, direct_origin_request)
durations.append(float(duration))
avg = mean(durations)
print(f"Avg duration for {request_url} test was {avg:.2f}: {durations}")
return avg
def lambda_handler(event, context):
test_lambda_directly()
test_url(api_gateway_endpoint)
test_url(cloudfront_endpoint)
lambda_handler(None, None)
import os
import json
import uuid
from urllib import request
request_path = 'METADATA_OCN/SB/S1B_WV_OCN__2SSH_20170317T183154_20170317T190001_004752_0084CF_5490.iso.xml'
host = 'sentinel1.asf.alaska.edu'
lambda_name = 'tea-dedployment-EgressLambda'
api_gateway_endpoint = 'https://xxxxxx.execute-api.us-west-2.amazonaws.com/API'
cloudfront_endpoint = 'https://xxxxxx.cloudfront.net'
cookie = os.getenv("COOKIE", "")
api_gateway_json = """
{
"body": "eyJ0ZXN0IjoiYm9keSJ9",
"headers": {
"Cookie": "asf-urs=<JWT-COOKIE>",
"Host": "cumulus-test.asf.alaska.edu",
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https",
"x-origin-request-id": "DE41270A-E8D0-49BA-A67A-E14510CB65E0"
},
"httpMethod": "GET",
"isBase64Encoded": true,
"multiValueHeaders": {},
"multiValueQueryStringParameters": {},
"path": "L2.2/A3/ALPSRP268471390-H2.2_UA.zip",
"pathParameters": {
"proxy": "L2.2/A3/ALPSRP268471390-H2.2_UA.zip"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "1234567890",
"httpMethod": "GET",
"identity": {},
"path": "L2.2/A3/ALPSRP268471390-H2.2_UA.zip",
"protocol": "HTTP/1.1",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"requestTime": "09/Apr/2015:12:34:56 +0000",
"requestTimeEpoch": 1428582896000,
"resourceId": "123456",
"resourcePath": "/{proxy+}",
"stage": "prod"
},
"resource": "/{proxy+}",
"stageVariables": {}
}
"""
def get_api_gateway_payload( origin_header_id=None, req_path=None):
payload = json.loads(api_gateway_json)
x_origin_req = origin_header_id or str(uuid.uuid1())
req_path = req_path or request_path
#print (f"Generating payload for {x_origin_req}/{req_path}")
# updates:
payload['headers']['Cookie'] = cookie
payload['headers']['Host'] = host
payload['headers']['x-origin-request-id'] = x_origin_req
payload['path'] = req_path
payload['pathParameters']['proxy'] = payload['path']
payload['requestContext']['path'] = payload['path']
return json.dumps(payload)
class NoRedirect(request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment