Pyjo.AWS.Credentials.Provider - Base class of the providers of credentials

from Pyjo.AWS.Credentials import Credentials
from Pyjo.AWS.Credentials.Provider import Provider

class Static(Provider):
    async def fetch(self):
        return Credentials(access_key_id='AKIDEXAMPLE', secret_access_key='secret')

credentials = Static().credentials()

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

import Pyjo.AWS.Credentials
import Pyjo.AWS.Credentials.Provider

class Service(Pyjo.AWS.Credentials.Provider.object):
    async def fetch(self):
        tx = await self.start(self.ua.build_tx('GET', 'http://127.0.0.1:8080/credentials'))
        data = tx.res.json()
        return Pyjo.AWS.Credentials.new(
            access_key_id=data['AccessKeyId'], secret_access_key=data['SecretAccessKey']
        )

Service.new().credentials_p().then(lambda credentials: print(credentials.access_key_id)).wait()

Pyjo.AWS.Credentials.Provider is the base class of the providers of Pyjo.AWS.Credentials. A provider gets its credentials in fetch(), keeps them until they are about to expire, and gets them again then.

fetch() is a coroutine, which awaits what it needs to wait for, so that the same code runs blocking, in credentials(), and non-blocking, in credentials_p():

  • a transaction of start(), which ua starts, gives the transaction back with its response, or raises the Pyjo.Exception of an error of the connection;

  • a program of run() gives its exit code, its output and its error, or raises the Pyjo.Exception of a program that cannot be run;

  • the steps() of another provider give its credentials.

Pyjo.Steps runs the coroutine, without an event loop, so it awaits nothing else, not a Pyjo.Promise and not a future of asyncio, which is a TypeError. fetch() may be a generator too, which yields the transactions and the programs, and takes the credentials of another provider with yield from of its steps():

class Service(Provider):
    def fetch(self):
        tx = yield self.start(self.ua.build_tx('GET', 'http://127.0.0.1:8080/credentials'))
        ...

What the coroutine returns is the credentials, or None when the provider is not configured, as an environment without its variables, so that Pyjo.AWS.Credentials.Chain asks the next one. An exception is an error of a provider that is configured, which ends the chain.

See also

Pyjo.AWS.Credentials, Pyjo.AWS.Credentials.Chain, Pyjo.Steps.

Classes

class Pyjo.AWS.Credentials.Provider.Pyjo_AWS_Credentials_Provider(*, ua: Pyjo_UserAgent | None = None, refresh: float = 300)[source]

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

__init__(*, ua: Pyjo_UserAgent | None = None, refresh: float = 300) → None[source]
provider = Provider.new()
provider = Provider.new(ua=Pyjo.UserAgent.new(request_timeout=5), refresh=600)

Construct a new provider.

__weakref__

list of weak references to the object

cached: Pyjo_AWS_Credentials | None
credentials = provider.cached
provider.cached = None

The credentials that the provider got last, which it gives until they are about to expire, or None, which makes it get them again.

credentials() → Pyjo_AWS_Credentials | None[source]
credentials = provider.credentials()

The credentials, cached ones or new ones, blocking, or None when the provider is not configured.

credentials_p() → Pyjo_Promise[source]
promise = provider.credentials_p()

Same as credentials(), but non-blocking, and returns a Pyjo.Promise object. The calls while the provider gets the credentials get the same promise, so that it gets them once.

async fetch() → Pyjo_AWS_Credentials | None[source]
credentials = await provider.fetch()

Get new credentials, a coroutine that awaits what it waits for, as the description of the class says, and returns the credentials or None. A subclass implements it; this one has no credentials.

is_fresh() → bool[source]
boolean = provider.is_fresh()

Whether the provider has cached credentials that are not about to expire.

refresh
seconds = provider.refresh
provider.refresh = 600

The number of seconds before their expiration when the credentials are got again, 300 by default.

run(args: Sequence[str], input: bytes = b'', *, env: Mapping[str, str] | None = None, timeout: float | None = None) → Command[source]
code, out, err = await provider.run(['aws', 'configure', 'export-credentials'])
code, out, err = await provider.run(['/opt/bin/credentials'], input=b'admin', timeout=30)

A program for fetch() to await, with the environment and the timeout of Command, which gives its exit code, its standard output and its standard error.

start(tx: Pyjo_Transaction_HTTP) → Request[source]
tx = await provider.start(provider.ua.build_tx('GET', 'http://169.254.170.2/v2/credentials'))

A transaction for fetch() to await, which ua starts, and which it gives back with its response.

steps() → Pyjo_Steps[Pyjo_AWS_Credentials | None][source]
credentials = await provider.steps()
credentials = yield from provider.steps()

The cached credentials, or the ones of fetch(), which it keeps, for a provider that gets its credentials with the ones of another, as a role with the credentials of its source.

ua
ua = provider.ua
provider.ua = Pyjo.UserAgent.new()

The user agent of the requests of the provider, a new Pyjo.UserAgent object by default.

Pyjo.AWS.Credentials.Provider.write_cache(path: str | PathLike[str], data: dict[str, Any]) → None[source]
Pyjo.AWS.Credentials.Provider.write_cache(path, token)

Write the JSON of a token to a file of a cache of the AWS CLI, replaced at once and readable by its owner only, as the AWS CLI writes it, so that the AWS CLI and the other programs share the token.