|
| 1 | +# urllib3 SigV4 |
| 2 | + |
| 3 | +Extension to [urllib3](https://github.com/urllib3/urllib3) adding support for |
| 4 | +signing the requests with AWS Signature Version 4. It uses the |
| 5 | +[Boto3](https://github.com/boto/boto3) library for handling the AWS credentials |
| 6 | +and the actual signing process. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +Use `pip` to install the package: |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install urllib3_sigv4 |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +This library provides a drop-in replacement for two main components of urllib3, |
| 19 | +the [`PoolManager`](https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html) |
| 20 | +class and the top-level [`request`](https://urllib3.readthedocs.io/en/stable/reference/urllib3.request.html) |
| 21 | +method. It adds a new optional parameter which determines if and how the |
| 22 | +requests should be signed. |
| 23 | + |
| 24 | +### Creating a Signer |
| 25 | + |
| 26 | +First, create an instance of the `SigV4RequestSigner` class which defines the |
| 27 | +parameters for request signing: |
| 28 | + |
| 29 | +```python |
| 30 | +from urllib3_sigv4 import SigV4RequestSigner |
| 31 | + |
| 32 | +signer = SigV4RequestSigner( |
| 33 | + "lambda", |
| 34 | + region="eu-central-1", |
| 35 | + access_key="AKIAIOSFODNN7EXAMPLE", |
| 36 | + secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" |
| 37 | +) |
| 38 | +``` |
| 39 | + |
| 40 | +The first parameter is mandatory and identifies the AWS service we want to make |
| 41 | +requests to (AWS Lambda in this case). The `region`, `access_key` and |
| 42 | +`secret_key` parameters are optional and will be inferred from the environment |
| 43 | +if not passed (via the default Boto3 session, see |
| 44 | +[here](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/session.html) |
| 45 | +and [here](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html) |
| 46 | +for more details). |
| 47 | + |
| 48 | +### Making Requests |
| 49 | + |
| 50 | +To make signed requests to an AWS service, pass the signer instance via the |
| 51 | +`signer` parameter when creating the `PoolManager`: |
| 52 | + |
| 53 | +```python |
| 54 | +from urllib3_sigv4 import PoolManager, SigV4RequestSigner |
| 55 | + |
| 56 | +signer = SigV4RequestSigner("lambda") |
| 57 | +http = PoolManager(signer=signer) |
| 58 | + |
| 59 | +response = http.request( |
| 60 | + "POST", |
| 61 | + "https://my-lambda-url-id.lambda-url.eu-central-1.on.aws", |
| 62 | + json={"name": "John Doe", "age": 30} |
| 63 | +) |
| 64 | +print(response.json()) |
| 65 | +``` |
| 66 | + |
| 67 | +You can also provide the signer in individual `request` method calls to |
| 68 | +override the default behavior: |
| 69 | + |
| 70 | +```python |
| 71 | +from urllib3_sigv4 import PoolManager, SigV4RequestSigner |
| 72 | + |
| 73 | +signer = SigV4RequestSigner("lambda") |
| 74 | +http = PoolManager() |
| 75 | + |
| 76 | +# The same as when using urllib3's PoolManager. |
| 77 | +response = http.request("GET", "https://httpbin.org/get") |
| 78 | +print(response.json()) |
| 79 | + |
| 80 | +# This request will be signed. |
| 81 | +response = http.request( |
| 82 | + "POST", |
| 83 | + "https://my-lambda-url-id.lambda-url.eu-central-1.on.aws", |
| 84 | + json={"name": "John Doe", "age": 30}, |
| 85 | + signer=signer |
| 86 | +) |
| 87 | +print(response.json()) |
| 88 | +``` |
| 89 | + |
| 90 | +You can also use a convenience top-level `request` method which uses a |
| 91 | +module-global `PoolManager` instance: |
| 92 | + |
| 93 | +```python |
| 94 | +from urllib3_sigv4 import SigV4RequestSigner, request |
| 95 | + |
| 96 | +signer = SigV4RequestSigner("lambda") |
| 97 | + |
| 98 | +response = request( |
| 99 | + "POST", |
| 100 | + "https://my-lambda-url-id.lambda-url.eu-central-1.on.aws", |
| 101 | + json={"name": "John Doe", "age": 30}, |
| 102 | + signer=signer |
| 103 | +) |
| 104 | +print(response.json()) |
| 105 | +``` |
| 106 | + |
| 107 | +## Reference |
| 108 | + |
| 109 | +- https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_aws-signing.html |
| 110 | +- https://github.com/aws-samples/sigv4-signing-examples |
| 111 | +- https://github.com/awslabs/aws-sdk-python-signers |
0 commit comments