Pyjo.GCP.Credentials.AWS - Credentials of an external account of AWS

import Pyjo.GCP.Credentials.File

# The file of gcloud iam workload-identity-pools create-cred-config --aws
provider = Pyjo.GCP.Credentials.File.new(path='/etc/gcloud/aws.json')
print(provider.credentials().token)

# Or the provider itself

from Pyjo.GCP.Credentials.AWS import AWS

provider = AWS(
    audience='//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/aws',
    subject_token_type='urn:ietf:params:aws:token-type:aws4_request',
)

Pyjo.GCP.Credentials.AWS is the provider of the credentials of an external account of Workload Identity Federation of AWS, of a file of the type external_account with the environment_id aws1 of credential_source, as gcloud iam workload-identity-pools create-cred-config --aws writes it:

{
  "type": "external_account",
  "audience": "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/aws",
  "subject_token_type": "urn:ietf:params:aws:token-type:aws4_request",
  "token_url": "https://sts.googleapis.com/v1/token",
  "credential_source": {
    "environment_id": "aws1",
    "region_url": "http://169.254.169.254/latest/meta-data/placement/availability-zone",
    "url": "http://169.254.169.254/latest/meta-data/iam/security-credentials",
    "regional_cred_verification_url": "https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15",
    "imdsv2_session_token_url": "http://169.254.169.254/latest/api/token"
  }
}

The subject token is the request of GetCallerIdentity of the STS of AWS, signed with Signature Version 4 and not sent, which the Security Token Service of Google sends to AWS: the credentials of AWS are the ones of AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN, or of the role of the instance metadata of EC2, and the region is the one of AWS_REGION or AWS_DEFAULT_REGION, or of the availability zone of the instance metadata, as google-auth has them, without the profiles and the other providers of the AWS CLI.

See also

Pyjo.GCP.Credentials.ExternalAccount, Pyjo.GCP.Credentials.File, https://google.aip.dev/auth/4117, https://cloud.google.com/iam/docs/workload-identity-federation-with-other-clouds, https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html.

Classes

class Pyjo.GCP.Credentials.AWS.Pyjo_GCP_Credentials_AWS(*, region_url: str | None = None, url: str | None = None, imdsv2_session_token_url: str | None = None, regional_cred_verification_url: str = 'https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15', **kwargs: Any)[source]

Pyjo.GCP.Credentials.AWS inherits all attributes and methods from Pyjo.GCP.Credentials.ExternalAccount and implements the following new ones.

__init__(*, region_url: str | None = None, url: str | None = None, imdsv2_session_token_url: str | None = None, regional_cred_verification_url: str = 'https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15', **kwargs: Any) → None[source]
provider = Pyjo.GCP.Credentials.AWS.new(audience=audience, subject_token_type=token_type)

Construct a new Pyjo.GCP.Credentials.AWS object, with the options of Pyjo.GCP.Credentials.ExternalAccount, of the credentials and the region of the environment without the URLs of the instance metadata.

async aws_credentials() → tuple[str, str, str | None][source]
access_key_id, secret_access_key, session_token = await provider.aws_credentials()

The credentials of AWS of the environment, or of the role of the instance metadata of url, or a Pyjo.Exception without them.

async aws_region() → str[source]
region = await provider.aws_region()

The region of AWS of the environment, or of the availability zone of the instance metadata of region_url without its letter, or a Pyjo.Exception without it.

imdsv2_session_token_url
url = provider.imdsv2_session_token_url

The URL of the session token of IMDSv2 of the requests of the instance metadata, or None, for IMDSv1.

classmethod options_of(info: Mapping[str, Any]) → dict[str, Any][source]
options = AWS.options_of(info)

The options of the URLs of credential_source of the JSON object of a file, or a Pyjo.Exception of one without the environment_id aws1 or regional_cred_verification_url.

region: str | None
region = provider.region

The region of AWS, once the provider has found it, or None.

region_url
url = provider.region_url

The URL of the availability zone of the instance metadata, of the region without AWS_REGION and AWS_DEFAULT_REGION, or None.

regional_cred_verification_url
url = provider.regional_cred_verification_url

The URL of GetCallerIdentity of the subject token, with {region} of the region.

async subject_token() → str[source]
token = await provider.subject_token()

The request of GetCallerIdentity of regional_cred_verification_url, signed with the credentials of AWS, with x-goog-cloud-target-resource of the audience, as the percent-encoded JSON object of its URL, its method and its headers.

url
url = provider.url

The URL of the security credentials of the instance metadata, of the name of the role and of its credentials without AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, or None.

Pyjo.GCP.Credentials.AWS.SUBJECT_TOKEN_TYPE = 'urn:ietf:params:aws:token-type:aws4_request'

The type of the subject token of AWS, a signed request of AWS, of which the kind of a file is AWS.

Pyjo.GCP.Credentials.AWS.VERIFICATION_URL = 'https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15'

The URL of GetCallerIdentity of the STS of AWS of a region, of the request of the subject token.

Pyjo.GCP.Credentials.AWS.sign(url: str, method: str, region: str, access_key_id: str, secret_access_key: str, session_token: str | None = None, time: float = 0) → dict[str, str][source]
headers = Pyjo.GCP.Credentials.AWS.sign(url, 'POST', 'eu-central-1', access_key_id, secret_access_key)

The headers of a request of AWS with an empty body, signed with Signature Version 4, as RequestSigner of google-auth signs it: Authorization, host, x-amz-date and x-amz-security-token of a session token, of the service of the first label of the host, or a Pyjo.Exception of a URL that is not of HTTPS.