Skip to main content

Secure Lambda authorizer for HTTP API behind CloudFront

Project description

Secure Authorizer for Amazon API Gateway

This is a Lambda authorizer for Amazon API Gateway that provides secure API key validation using AWS Secrets Manager. This authorizer is compatible with key rotation through AWS Secrets Manager versioning, implements constant-time comparison to prevent timing attacks, and includes comprehensive logging for security monitoring.

Features

  • AWS Secrets Manager Integration: Securely retrieves API keys using AWS Secrets Manager
  • Key Rotation Compatibility: Validates requests against both current and pending secret versions
  • Timing Attack Prevention: Uses constant-time comparison for secure string matching
  • Comprehensive Logging: Detailed logging for security monitoring and troubleshooting

Installation

pip install amazon-api-gateway-secure-authorizer

Configuration

Environment Variables

  • SECRET_NAME: The name or ARN of the secret in AWS Secrets Manager
  • SECRET_KEY_NAME: The key name within the secret's JSON structure that contains the API key
  • HEADER_NAME: (Optional) The name of the header containing the API key (default: "x-origin-verify")

AWS Secrets Manager Setup

  1. Create a new secret in AWS Secrets Manager with the following JSON structure:
{
    "your_key_name": "your-api-key-value"
}
  1. Ensure your Lambda function has the following IAM permissions:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "secretsmanager:GetSecretValue"
            ],
            "Resource": "arn:aws:secretsmanager:region:account-id:secret:secret-name"
        }
    ]
}

Usage

Lambda Handler

The Lambda function handler is located at:

secure_authorizer.authorizer.handler

Example Event

{
    "headers": {
        "x-origin-verify": "your-api-key-value"
    }
}

Deployment Steps

  1. Set up AWS Secrets Manager:

    • Create a new secret with your API key
    • Note the secret name and key name for environment variables
  2. Configure Lambda Environment:

    SECRET_NAME="your-secret-name"
    SECRET_KEY_NAME="your-key-name"
    HEADER_NAME="x-origin-verify"  # Optional, this is the default
    
  3. Deploy Lambda Function:

    • Set handler to secure_authorizer.authorizer.handler
    • Configure IAM role with necessary Secrets Manager permissions
    • Set memory and timeout appropriately (see Performance Recommendations)
  4. Configure API Gateway:

    • Create a Lambda authorizer
    • Link it to your Lambda function
    • Configure authorization caching (see Performance Recommendations)

Key Rotation

This authorizer supports seamless key rotation using AWS Secrets Manager's versioning:

  1. Create a new version of your secret in AWS Secrets Manager
  2. The new version will be automatically available as 'AWSPENDING'
  3. The authorizer will accept both current and pending versions during rotation
  4. Once rotation is complete, the new version becomes 'AWSCURRENT'

Performance Recommendations

  • Memory: 512MB
  • Timeout: 5 seconds
  • Architecture: ARM64
  • Authorization Caching: 300-3600 seconds based on your security requirements

Security Features

  • Secure Secret Storage: Utilizes AWS Secrets Manager for encrypted key storage
  • Key Rotation Support: Enables zero-downtime key rotation
  • Constant-time Comparison: Prevents timing attacks during API key validation
  • Detailed Security Logging: Helps identify potential security issues

Error Handling

The authorizer implements comprehensive error handling for various scenarios:

  • Missing environment variables
  • Invalid or missing headers
  • Secrets Manager access issues
  • JSON parsing errors
  • API key validation failures

All errors are logged with appropriate detail levels while maintaining security.

Logging

The authorizer provides detailed logging at different levels:

  • INFO: Successful authorizations
  • WARNING: Failed authorizations
  • ERROR: Configuration issues, Secrets Manager errors
  • DEBUG: Additional validation details

Comparison with Simple Authorizer

Advantages over the simple authorizer:

  • Secure secret storage in AWS Secrets Manager
  • Support for key rotation

License

This project is licensed under the MIT License. See the LICENSE file for details.

Disclaimer

This software product is not affiliated with, endorsed by, or sponsored by Amazon Web Services (AWS) or Amazon.com, Inc. The use of the term "AWS" is solely for descriptive purposes to indicate that the software is compatible with AWS services. Amazon Web Services and AWS are trademarks of Amazon.com, Inc. or its affiliates.

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

Built Distribution

File details

Details for the file amazon-api-gateway-secure-authorizer-0.1.0.tar.gz.

File metadata

File hashes

Hashes for amazon-api-gateway-secure-authorizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3e53133a52bcf8a06407f0326eac8a5a3ec2533ddfa2d61d81531807076e484e
MD5 f4614d2c0b02db3e6692c12036854d13
BLAKE2b-256 c3374252746463bd65c429b695790da739a136b89dfc0d8141e504f0a872dc93

See more details on using hashes here.

File details

Details for the file amazon_api_gateway_secure_authorizer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for amazon_api_gateway_secure_authorizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 55942e626796fc617fe694ab8bf60a880e95befc3681e45660f24af7738462e2
MD5 82382b8179aacb859cb4421122dbf05c
BLAKE2b-256 47b47deb3ff2ca897b798cbe12210e8c38238ed206f1a7c82618537ace73e318

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