Skip to main content

Enables ApiKey functionality (like in ApiGateway V1) for ApiGateway V2.

Project description

B.CfnCustomApiKeyAuthorizer

Pipeline

An AWS CDK resource that enables protection of your public APIs by using Api Keys (ApiKey and Secret).

Description

This custom authorizer enables Api Key functionality (something similar to ApiGateway V1 version: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-setup-api-key-with-console.html)

APIs created via ApiGateway V2 do not have Api Key authorization functionality out-of-the-box. If you want to protect your V2 API by generating a secret key and giving only for the intended clients - this library is just for you. This library allows you to protect you ApiGatewayV2-based endpoints with a combination of ApiKey and ApiSecret. Refer to usages & examples to understand how to use this library.

The authorizer library exposes these lambda functions that can be called directly:

  • authorizer - ApiKeysAuthorizerFunction - used by a custom (this) authorizer that is attached to your API.
  • deleter - ApiKeysDeleterFunction - allows revoking access to your API i.e. deletes api keys.
  • exists - ApiKeysExistsFunction - allows you to check whether a given API key exists in the database.
  • generator - ApiKeysGeneratorFunction - generates api key and api secret pair and saves in an internal database.
  • validator - ApiKeysValidatorFunction - validates given api key and api secret against the ones in the database.

Remarks

Biomapas aims to modernise life-science industry by sharing its IT knowledge with other companies and the community. This is an open source library intended to be used by anyone. Improvements and pull requests are welcome.

Related technology

  • Python3
  • AWS CDK
  • AWS CloudFormation
  • AWS API Gateway
  • AWS API Gateway Authorizer
  • AWS Lambda

Assumptions

This project assumes you are an expert in infrastructure-as-code via AWS CloudFormation and AWS CDK. You must clearly understand how AWS API Gateway endpoints are protected with Authorizers / Custom Authorizers and how it is managed via CloudFormation or CDK.

  • Excellent knowledge in IaaC (Infrastructure as a Code) principles.
  • Excellent knowledge in API Gateway, Authorizers.
  • Good experience in AWS CDK and AWS CloudFormation.
  • Good Python skills and basics of OOP.

Useful sources

Install

Before installing this library, ensure you have these tools setup:

  • Python / Pip
  • AWS CDK

To install this project from source run:

pip install .

Or you can install it from a PyPi repository:

pip install b-cfn-custom-api-key-authorizer

Usage & Examples

Firstly, create an api and stage:

from aws_cdk.aws_apigatewayv2 import CfnApi, CfnStage

api = CfnApi(...)
api_stage = CfnStage(...)

Create api key custom authorizer:

from b_cfn_custom_api_key_authorizer.custom_authorizer import ApiKeyCustomAuthorizer

authorizer = ApiKeyCustomAuthorizer(
    scope=Stack(...),
    resource_name_prefix='MyCool',
    api=api,
)

Use that authorizer to protect your routes (endpoints):

from aws_cdk.aws_apigatewayv2 import CfnRoute

route = CfnRoute(
    scope=Stack(...),
    id='DummyRoute',
    api_id=api.ref,
    route_key='GET /dummy/endpoint',
    authorization_type='CUSTOM',
    target=f'integrations/{integration.ref}',
    authorizer_id=authorizer.ref
)

Once your infrastructure is deployed, try calling your api endpoint. You will get "Unauthorized" error.

import urllib3

response = urllib3.PoolManager().request(
    method='GET',
    url='https://your-api-url/dummy/endpoint',
    headers={},
)

>>> response.status
>>> 401

Create ApiKey and ApiSecret by invoking a dedicated api keys generator lambda function:

# Your supplied prefix for the infrastrucutre.
resource_name_prefix = 'MyCool'
# Created generator lambda function name.
function_name = 'ApiKeysGeneratorFunction'
# Full function name is a combination of both.
function_name = resource_name_prefix + function_name

response = boto3.client('lambda').invoke(
    FunctionName=function_name,
    InvocationType='RequestResponse',
)

response = json.loads(response['Payload'].read())
api_key = response['ApiKey']
api_secret = response['ApiSecret']

Now try calling the same api with api keys:

import urllib3

response = urllib3.PoolManager().request(
    method='GET',
    url='https://your-api-url/dummy/endpoint',
    headers={
        'ApiKey': api_key,
        'ApiSecret': api_secret
    },
)

>>> response.status
>>> 200

Exposed lambda functions

The authorizer exposes these lambda functions that can be called directly:

  • authorizer - ApiKeysAuthorizerFunction
response = boto3.client('lambda').invoke(
    FunctionName=prefix + 'ApiKeysAuthorizerFunction',
    InvocationType='RequestResponse',
    Payload=json.dumps({
        'ApiKey': '123',
        'ApiSecret': '123'
    }).encode()
)

response = json.loads(response['Payload'].read())

# This will contain a dictionary of IAM based 
# permission either to "allow" or "deny" the request.
print(response)
  • deleter - ApiKeysDeleterFunction
# This does not produce a response.
boto3.client('lambda').invoke(
    FunctionName=prefix + 'ApiKeysDeleterFunction',
    InvocationType='RequestResponse',
    Payload=json.dumps({
        'ApiKey': '123',
    }).encode()
)
  • exists - ApiKeysExistsFunction
response = boto3.client('lambda').invoke(
    FunctionName=prefix + 'ApiKeysExistsFunction',
    InvocationType='RequestResponse',
    Payload=json.dumps({
        'ApiKey': '123',
    }).encode()
)

response = json.loads(response['Payload'].read())

# Check whether your ApiKey/Secret exists in the database.
assert response['Exists'] is True
  • generator - ApiKeysGeneratorFunction
response = boto3.client('lambda').invoke(
    FunctionName=prefix + 'ApiKeysGeneratorFunction',
    InvocationType='RequestResponse',
)

response = json.loads(response['Payload'].read())

api_key = response['ApiKey']
api_secret = response['ApiSecret']
  • validator - ApiKeysValidatorFunction
response = boto3.client('lambda').invoke(
    FunctionName=prefix + 'ApiKeysValidatorFunction',
    InvocationType='RequestResponse',
    Payload=json.dumps({
        'ApiKey': '123',
        'ApiSecret': '123',
    }).encode()
)

response = json.loads(response['Payload'].read())

# Check whether your ApiKey/Secret is valid.
assert response['Valid'] is True

Testing

This package has integration tests based on pytest. To run tests simply run:


pytest b_cfn_custom_api_key_authorizer_test/integration/tests

Contribution

Found a bug? Want to add or suggest a new feature? Contributions of any kind are gladly welcome. You may contact us directly, create a pull-request or an issue in github platform. Lets modernize the world together.

Release history

2.0.1

  • Dollar sign excluded from API secret.

2.0.0

  • Restructured project.
  • All lambda functions are under functions directory.
  • Add deleter function to revoke api keys.
  • Add exists function to check whether given api key exists.
  • Add validator function to validate api key and api secret.
  • Increase the length of api key (15) and api secret (30).
  • Move authentication checking logic to a lambda layer.
  • Add more lambda-level logging.
  • Add more integrations tests (total 11 as of now).
  • Very important security improvement - api secrets are now hashed, to avoid leaks if the database is pawned. This is a standard password-level storage security.
  • Greatly improve documentation.

1.1.0

  • Create a dedicated lambda function to generate api keys. You should not interact with the database directly.

1.0.0

  • Prod-ready version.
  • Added documentation.
  • Added more tests.
  • Some code improvements.

0.1.0

  • Initial testing done. Authorizer works.
  • Need more tests and edge case handling before promoting to 1.0.0.

0.0.1

  • Initial build.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

b_cfn_custom_api_key_authorizer-2.0.1.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file b_cfn_custom_api_key_authorizer-2.0.1.tar.gz.

File metadata

File hashes

Hashes for b_cfn_custom_api_key_authorizer-2.0.1.tar.gz
Algorithm Hash digest
SHA256 3315e63e17c27a4f6e84bcfb4ce2896bd159d119e2c0fdc89f98fd8f3af219bb
MD5 85c12e4f21ae16af4f05c609aa373397
BLAKE2b-256 3423631fa1b7f0737bd1c88a667cfc1aab59b976aaeb533395ccc7f562a0c979

See more details on using hashes here.

File details

Details for the file b_cfn_custom_api_key_authorizer-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for b_cfn_custom_api_key_authorizer-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9c29b72c597edacc28cd933784ee677a724ce9375c65a10e4d95f7373acec413
MD5 982c0c7b6135da75d8ee21f58e8bf770
BLAKE2b-256 7443c794e33ff004abe2825ee4a0b4c83f416d907c21111f20a39a58ed34192f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page