Skip to content

Commit f7ace85

Browse files
authored
Full information for README
1 parent 0670a34 commit f7ace85

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,143 @@
11
# requests-auth-aws-sigv4
22
Use AWS signature version 4 Authentication with the python requests module
3+
4+
This package provides an authentication class that can be used with the popular
5+
[requests](https://requests.readthedocs.io/en/master/) package to add the
6+
[AWS Signature Version 4](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html)
7+
authentication information.
8+
9+
The signing code is inspired by the python example provided by AWS.
10+
11+
This package should support any/all AWS API's, including API Gateway API's (execute-api),
12+
Elasticsearch clusters, and others. AWS Credentials may be pulled from the environment
13+
in an easy and familiar way.
14+
The signature is added as a header to the request.
15+
16+
## Installation
17+
18+
```
19+
pip install requests-auth-aws-sigv4
20+
```
21+
22+
## Usage
23+
24+
```python
25+
import requests
26+
from requests_auth_aws_sigv4 import AWSSigV4
27+
28+
r = requests.request('POST', 'https://sts.us-east-1.amazonaws.com',
29+
data=dict(Version='2011-06-15', Action='GetCallerIdentity'),
30+
auth=AWSSigV4('sts'))
31+
print(r.text)
32+
```
33+
34+
If **boto3** is available, it will attempt to use credentials that have been configured for the AWS CLI or SDK's,
35+
as documented in [Boto3 User Guide: Credentials](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html#credentials).
36+
Otherwise, if **boto3** is not available, credentials must be provided using either environment variables or parameters.
37+
38+
#### Example using environment variables
39+
40+
Environment variable names are the same as documented for AWS CLI and SDK's.
41+
42+
```shell
43+
export AWS_ACCESS_KEY_ID=MYACCESSKEY
44+
export AWS_SECRET_ACCESS_KEY=THISISSECRET
45+
export AWS_SESSION_TOKEN=THISISWHERETHESUPERLONGTOKENGOES
46+
```
47+
48+
```python
49+
import requests
50+
from requests_auth_aws_sigv4 import AWSSigV4
51+
52+
aws_auth = AWSSigV4('ec2') # If not provided, check for AWS Credentials from Environment Variables
53+
54+
r = requests.request('GET', 'https://ec2.us-east-1.amazonaws.com?Version=2016-11-15&Action=DescribeRegions',
55+
auth=aws_auth)
56+
print(r.text)
57+
```
58+
59+
#### Example using parameters
60+
61+
Passing credentials as parameters overrides all other possible sources.
62+
63+
```python
64+
import requests
65+
from requests_auth_aws_sigv4 import AWSSigV4
66+
67+
aws_auth = AWSSigV4('ec2',
68+
aws_access_key_id=ACCESS_KEY,
69+
aws_secret_access_key=SECRET_KEY,
70+
aws_session_token=SESSION_TOKEN,
71+
)
72+
73+
r = requests.request('GET', 'https://ec2.us-east-1.amazonaws.com?Version=2016-11-15&Action=DescribeRegions',
74+
auth=aws_auth)
75+
print(r.text)
76+
```
77+
78+
### Usage with Elasticsearch Client (elasticsearch-py)
79+
80+
```python
81+
from elasticsearch import Elasticsearch, RequestsHttpConnection
82+
from requests_auth_aws_sigv4 import AWSSigV4
83+
84+
es_host = 'search-service-foobar.us-east-1.es.amazonaws.com'
85+
aws_auth = AWSSigV4('es')
86+
87+
# use the requests connection_class and pass in our custom auth class
88+
es_client = Elasticsearch(host=es_host,
89+
port=80,
90+
connection_class=RequestsHttpConnection,
91+
http_auth=aws_auth)
92+
es_client.info()
93+
```
94+
95+
### Debug Logging
96+
97+
All log messages are at the module level.
98+
99+
```python
100+
import logging
101+
logging.basicConfig() # Setup basic logging to stdout
102+
log = logging.getLogger('requests_auth_aws_sigv4')
103+
log.setLevel(logging.DEBUG)
104+
```
105+
106+
## Command Line Usage
107+
108+
The module can be run from the command line in a way that is similar to how cURL works.
109+
110+
```shell
111+
$ python3 -m requests_auth_aws_sigv4 https://sampleapi.execute-api.us-east-1.amazonaws.com/test/ -v
112+
> GET /test/ HTTP/1.1
113+
> Host: sampleapi.execute-api.us-east-1.amazonaws.com
114+
> User-Agent: python-requests/2.23.0 auth-aws-sigv4/0.2
115+
> Accept-Encoding: gzip, deflate
116+
> Accept: */*
117+
> Connection: keep-alive
118+
> X-AMZ-Date: 20200513T180549Z
119+
> Authorization: AWS4-HMAC-SHA256 Credential=AKIASAMPLEKEYID/20200513/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-date, Signature=EXAMPLESIGNATUREISHERE
120+
>
121+
< HTTP/1.1 200 OK
122+
< Connection: keep-alive
123+
< Content-Length: 25
124+
< Content-Type: application/json
125+
< Date: Wed, 13 May 2020 18:05:49 GMT
126+
< Server: Server
127+
< x-amz-apigw-id: MeExampleiMFs99=
128+
< x-amzn-RequestId: 7example-7b7b-4343-9a9a-9bbexampleaf
129+
hello
130+
```
131+
132+
## Temporary Security Credentials
133+
134+
Credentials issued from [AWS STS](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html)
135+
to grant temporary access can be used normally. Set the token by passing the `aws_session_token` parameter,
136+
setting the `AWS_SESSION_TOKEN` environment variable, or configure the credential for boto3 as normal.
137+
138+
## Using boto3 (or botocore) for AWS Credentials
139+
140+
The packages **boto3** and **botocore** are not requirements to use this module.
141+
As mentioned above, if **boto3** is available, a boto3.Session will be created to attempt to get credentials
142+
and configure the default region. This will happen automatically if credentials are not provided as parameters.
143+

0 commit comments

Comments
 (0)