-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/issue 186 Implement API keys (#188)
* API Gateway Lambda authorizer to facilitate API keys and usage plans * Unit tests to test Lambda authorizer * Fix terraform file formatting * API Gateway Lambda Authorizer - Lambda function - API Keys and Authorizer definition in OpenAPI spec - API gateway API keys - API gateway usage plans - SSM parameters for API keys * Fix trailing whitespace * Set default region environment variable * Fix SNYK vulnerabilities * Add issue to changelog * Implement custom trusted partner header x-hydrocron-key * Update cryptography for SNYK vulnerability * Update documentation to include API key usage * Update quota and throttle settings for API Gateway * Update API keys documentation to indicate to be implemented * Move API key lookup to Lambda INIT * Remove API key authentication and update API key to x-hydrocron-key
- Loading branch information
Showing
19 changed files
with
663 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
Lambda Authorizer to facilitate usage of API keys and usage plans. | ||
Taken from example: | ||
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html | ||
""" | ||
|
||
import json | ||
import logging | ||
|
||
from hydrocron.utils import connection | ||
|
||
|
||
logging.getLogger().setLevel(logging.INFO) | ||
|
||
|
||
ssm_client = connection.ssm_client | ||
STORED_API_KEY_TRUSTED = ssm_client.get_parameter(Name="/service/hydrocron/api-key-trusted", WithDecryption=True)["Parameter"]["Value"] | ||
STORED_API_KEY_DEFAULT = ssm_client.get_parameter(Name="/service/hydrocron/api-key-default", WithDecryption=True)["Parameter"]["Value"] | ||
|
||
|
||
def authorization_handler(event, context): | ||
"""Lambda authorizer function to allow or deny a request.""" | ||
|
||
logging.info("Event: %s", event) | ||
logging.info("Context: %s", context) | ||
|
||
api_key_trusted = "" if "x-hydrocron-key" not in event["headers"].keys() else event["headers"]["x-hydrocron-key"] | ||
|
||
if api_key_trusted and api_key_trusted == STORED_API_KEY_TRUSTED: | ||
response_policy = create_policy("trusted_partner", "Allow", event["methodArn"], STORED_API_KEY_TRUSTED) | ||
logging.info("Created policy for truster partner.") | ||
|
||
else: | ||
response_policy = create_policy("default_user", "Allow", event["methodArn"], STORED_API_KEY_DEFAULT) | ||
logging.info("Created policy for default user.") | ||
|
||
logging.info("Response: %s", response_policy) | ||
return json.loads(response_policy) | ||
|
||
|
||
def create_policy(principle_id, effect, method_arn, api_key=""): | ||
"""Create IAM policy to return in authorizer response.""" | ||
|
||
authorization_response = { | ||
"principalId": principle_id, | ||
"policyDocument": { | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "execute-api:Invoke", | ||
"Effect": effect, | ||
"Resource": method_arn, | ||
} | ||
] | ||
}, | ||
"usageIdentifierKey": api_key | ||
} | ||
|
||
return json.dumps(authorization_response) |
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
Oops, something went wrong.