Skip to main content

Extension to urllib3 adding support for AWS Signature Version 4

Project description

urllib3 SigV4

Extension to urllib3 adding support for signing the requests with AWS Signature Version 4. It uses the Boto3 library for handling the AWS credentials and the actual signing process.

Installation

Use pip to install the package:

pip install urllib3_sigv4

Usage

This library provides a drop-in replacement for two main components of urllib3, the PoolManager class and the top-level request method. It adds a new optional parameter which determines if and how the requests should be signed.

Creating a Signer

First, create an instance of the SigV4RequestSigner class which defines the parameters for request signing:

from urllib3_sigv4 import SigV4RequestSigner

signer = SigV4RequestSigner(
    "lambda",
    region="eu-central-1",
    access_key="AKIAIOSFODNN7EXAMPLE",
    secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
)

The first parameter is mandatory and identifies the AWS service we want to make requests to (AWS Lambda in this case). The region, access_key and secret_key parameters are optional and will be inferred from the environment if not passed (via the default Boto3 session, see here and here for more details).

Making Requests

To make signed requests to an AWS service, pass the signer instance via the signer parameter when creating the PoolManager:

from urllib3_sigv4 import PoolManager, SigV4RequestSigner

signer = SigV4RequestSigner("lambda")
http = PoolManager(signer=signer)

response = http.request(
    "POST",
    "https://my-lambda-url-id.lambda-url.eu-central-1.on.aws",
    json={"name": "John Doe", "age": 30}
)
print(response.json())

You can also provide the signer in individual request method calls to override the default behavior:

from urllib3_sigv4 import PoolManager, SigV4RequestSigner

signer = SigV4RequestSigner("lambda")
http = PoolManager()

# The same as when using urllib3's PoolManager.
response = http.request("GET", "https://httpbin.org/get")
print(response.json())

# This request will be signed.
response = http.request(
    "POST",
    "https://my-lambda-url-id.lambda-url.eu-central-1.on.aws",
    json={"name": "John Doe", "age": 30},
    signer=signer
)
print(response.json())

You can also use a convenience top-level request method which uses a module-global PoolManager instance:

from urllib3_sigv4 import SigV4RequestSigner, request

signer = SigV4RequestSigner("lambda")

response = request(
    "POST",
    "https://my-lambda-url-id.lambda-url.eu-central-1.on.aws",
    json={"name": "John Doe", "age": 30},
    signer=signer
)
print(response.json())

Reference

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

urllib3_sigv4-0.4.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

urllib3_sigv4-0.4.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file urllib3_sigv4-0.4.0.tar.gz.

File metadata

  • Download URL: urllib3_sigv4-0.4.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for urllib3_sigv4-0.4.0.tar.gz
Algorithm Hash digest
SHA256 05daee4e78673db85d0c4b552a675ddfe6667e40a3c26f45487644065e4ccc6e
MD5 6fe37340c25918e7f7b596da9ea901c0
BLAKE2b-256 38ea6aeb50bc75145c0cf776950292d947ce05f94f2f33da75f7b8d724f85d06

See more details on using hashes here.

File details

Details for the file urllib3_sigv4-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for urllib3_sigv4-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4509618c667bcdaaf959582bb05513d915896b84904d30c409f21647e2ea96b6
MD5 1875b8c477e1ed167feef32bf51dd2d0
BLAKE2b-256 4f6f4450f5d54af065496d6c418f5b3aefbe95b6f9acf60c286d579714bd23de

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