Pyjo.AWS.SigV4 - Signature Version 4 of AWS

from Pyjo.AWS.SigV4 import SigV4

signer = SigV4(service='lambda', region='eu-central-1')
signer.sign(tx.req)

# Or import the module, as in Google Python Style Guide

import Pyjo.AWS.SigV4
import Pyjo.UserAgent

signer = Pyjo.AWS.SigV4.new(service='lambda', region='eu-central-1')
ua = Pyjo.UserAgent.new()
ua.on('start', lambda ua, tx: signer.sign(tx.req))
print(ua.get('https://lambda.eu-central-1.amazonaws.com/2018-10-31/layers').result.json())

Pyjo.AWS.SigV4 signs a request of Pyjo.Message.Request with the Signature Version 4 of AWS, in its header Authorization, with the credentials of the environment by default, as the SDKs of AWS take them.

The event start of Pyjo.UserAgent is the place to sign a request, as it is emitted for each transaction, a redirect too, once the request is complete and before it is written. The request has no Host and no Content-Length then, which the request adds when it is written: sign() sets Host itself, and does not sign Content-Length, which AWS does not require.

See also

Pyjo.AWS, Pyjo.UserAgent.

Classes

class Pyjo.AWS.SigV4.Pyjo_AWS_SigV4(*, service: str, region: str | None = None, access_key_id: str | None = None, secret_access_key: str | None = None, session_token: str | None = None, credentials: Pyjo_AWS_Credentials | None = None, normalize_path: bool = True, double_encode: bool = True, content_sha256: bool = False)[source]

Pyjo.AWS.SigV4 inherits all attributes and methods from Pyjo.Base and implements the following new ones.

__init__(*, service: str, region: str | None = None, access_key_id: str | None = None, secret_access_key: str | None = None, session_token: str | None = None, credentials: Pyjo_AWS_Credentials | None = None, normalize_path: bool = True, double_encode: bool = True, content_sha256: bool = False) → None[source]
signer = Pyjo.AWS.SigV4.new(service='lambda')
signer = Pyjo.AWS.SigV4.new(service='sts', credentials=Pyjo.AWS.Credentials.Chain.new().credentials())
signer = Pyjo.AWS.SigV4.new(service='s3', region='us-east-1', normalize_path=False, double_encode=False,
                            content_sha256=True)

Construct a new Pyjo.AWS.SigV4 object. The keys of Pyjo.AWS.Credentials of credentials come in the place of those of the environment, and of its session token too.

__weakref__

list of weak references to the object

access_key_id
access_key_id = signer.access_key_id
signer.access_key_id = 'AKIDEXAMPLE'

The access key ID, defaults to the value of the AWS_ACCESS_KEY_ID environment variable, unless it is empty.

canonical_request(req: Pyjo_Message_Request) → str[source]
canonical = signer.canonical_request(req)

The canonical request of a request with the headers that sign() sets.

content_sha256
boolean = signer.content_sha256
signer.content_sha256 = True

Send the hash of the body in the header X-Amz-Content-Sha256, false by default, which S3 requires. A request that has the header already is signed with its value, as UNSIGNED-PAYLOAD of S3.

double_encode
boolean = signer.double_encode
signer.double_encode = False

Encode the path of the canonical request once more, after the encoding of the URL, true by default, which every service wants but S3.

normalize_path
boolean = signer.normalize_path
signer.normalize_path = False

Remove the segments . and .. and the empty ones from the path of the canonical request, true by default, which every service wants but S3.

payload_hash(req: Pyjo_Message_Request) → str[source]
digest = signer.payload_hash(req)

The SHA-256 of the body of a request in hexadecimal, or the value of its header X-Amz-Content-Sha256.

region
region = signer.region
signer.region = 'eu-central-1'

The region in the scope of the signature, defaults to the value of the AWS_REGION or AWS_DEFAULT_REGION environment variable or us-east-1.

scope(req: Pyjo_Message_Request) → str[source]
scope = signer.scope(req)

The scope of the signature of a request, as 20150830/us-east-1/service/aws4_request, of the date of its header X-Amz-Date.

secret_access_key
secret_access_key = signer.secret_access_key
signer.secret_access_key = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'

The secret access key, defaults to the value of the AWS_SECRET_ACCESS_KEY environment variable, unless it is empty.

service
service = signer.service
signer.service = 'lambda'

The name of the service in the scope of the signature, as lambda or s3.

session_token
session_token = signer.session_token
signer.session_token = 'AQoDYXdzEPT//////////wEXAMPLE...'

The session token of temporary credentials, as those of a role, defaults to the value of the AWS_SESSION_TOKEN environment variable, unless it is empty, sent in the header X-Amz-Security-Token.

sign(req: Pyjo_Message_Request, date: Pyjo_Date | float | None = None) → Pyjo_Message_Request[source]
req = signer.sign(req)
req = signer.sign(req, Pyjo.Date.new('2015-08-30T12:36:00Z'))
req = signer.sign(req, 1440938160)

Sign a request at a date of Pyjo.Date or the epoch, now by default: set its headers Host, X-Amz-Date, X-Amz-Security-Token with a session_token and X-Amz-Content-Sha256 with content_sha256, and its header Authorization.

signature(req: Pyjo_Message_Request) → str[source]
signature = signer.signature(req)

The signature of a request in hexadecimal, of its string_to_sign().

string_to_sign(req: Pyjo_Message_Request) → str[source]
string = signer.string_to_sign(req)

The string to sign of a request with the headers that sign() sets.