-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Normalize auth header to query string
- Loading branch information
Showing
8 changed files
with
155 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Because us-east-1 is really the only place that AWS works properly. | ||
This shouldn't be a thing. But, it is a thing. | ||
Our main stack is in `eu-west-2` and the CloudFront | ||
distribution is "global", meaning a very specific part of the US. | ||
However, when we want to attach a lambda@edge function | ||
to the "global" resource for our `eu-west-2` deployment, the lambda | ||
function itself needs to be in...you guessed it, a very specific part | ||
of the US. | ||
Fine, but we also need a way to share the version ARN of that function | ||
with the CloudFront distribution. The right way to do this is via the | ||
CloudFormation Exports system, however they don't work across regions. | ||
This means that either Lambda@Edge only really works if you deploy to | ||
a very specific part of the US, or if you hack some script together like | ||
the below. | ||
""" | ||
|
||
import sys | ||
|
||
import boto3 | ||
|
||
|
||
def get_export_value(export_name): | ||
cf_client = boto3.client("cloudformation", region_name="us-east-1") | ||
for export in cf_client.list_exports()["Exports"]: | ||
if export["Name"] == export_name: | ||
return export["Value"] | ||
raise ValueError(f"Export {export_name} not found") | ||
|
||
|
||
if __name__ == "__main__": | ||
export_name = sys.argv[1] | ||
print(get_export_value(export_name)) |
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,42 @@ | ||
from urllib.parse import parse_qs, urlencode | ||
|
||
|
||
def lambda_handler(event, context): | ||
request = event["Records"][0]["cf"]["request"] | ||
|
||
""" | ||
This code is run between a request from the web and that request | ||
being passed on to CloudFront: | ||
Request -> function -> CloudFront -> Origin | ||
We use it to convert the `Authorization` header into a | ||
query string parameter. | ||
This is due to AWS API Gateway not being able to support | ||
more than one authentication method using OR. | ||
That is, you can add header and query string authentication methods, but | ||
they are ANDed together meaning you need to supply both. | ||
This function allows us to accept either by converting one method to using | ||
the other method. | ||
""" | ||
|
||
# Check if the header exists | ||
token_header = request["headers"].get("authorization") | ||
if not token_header: | ||
# If not, return early. Nothing to be done here | ||
return request | ||
|
||
# Parse request querystring to get dictionary/json | ||
params = {k: v[0] for k, v in parse_qs(request["querystring"]).items()} | ||
# The header value is in the format of: | ||
# {lower_case_header_name: [{"key": "Title_Case_Header_Name": "value": "value"}]} | ||
# And the authorization header value is "Token api_key" | ||
token = token_header[0]["value"].split(" ")[-1] | ||
params["token"] = token | ||
request["querystring"] = urlencode(params) | ||
|
||
return request |
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,20 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: "EC API public access: TLS, CDN, DNS" | ||
|
||
Resources: | ||
AuthHeaderToQueryStringFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Role: !Sub "arn:aws:iam::${AWS::AccountId}:role/ECApiLambdaExecutionRole" | ||
CodeUri: ./api_endpoints/lambda_edge/auth_header_to_query | ||
Handler: handler.lambda_handler | ||
Runtime: python3.8 | ||
AutoPublishAlias: live | ||
|
||
Outputs: | ||
AuthHeaderToQueryStringFunctionVersion: | ||
Description: The version ARN of the ID of the AuthHeaderToQueryStringFunction | ||
Value: !Ref AuthHeaderToQueryStringFunction.Version | ||
Export: | ||
Name: "AuthHeaderToQueryString" |
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