A patch for boto3 to use the AWS CLI credential cache. This uses
Project description
Boto3 MFA Cache
This package patches boto3 to use the AWS CLI credential cache. This uses the same cache as the AWS CLI,
which is stored in ~/.aws/cli/cache. This allows you to use MFA with boto3 without having to
enter your credentials every time. The environment variable AWS_CREDENTIAL_CACHE can be set to a
different path if ~/.aws/cli/cache is not what you want to use.
This pull is the primary reference for this package: https://github.com/boto/botocore/pull/1338
Best Practices
This is designed for use with role-assumption based MFA. That's you if your ~/.aws/config looks something like this:
[default]
role_arn = arn:aws:iam::123456789012:role/MyRole
source_profile = base
region = us-east-2
mfa_serial = arn:aws:iam::123456789012:mfa/my-device
duration_seconds = 43200
This project is only useful on development environments that require MFA. Servers and other automated resources should not require MFA, and thus should not require this package.
python -m pip install boto3-mfa-cache
Methodology
The patch works by loading a .pth file that imports a monkeypatch replacing boto3.setup_default_session
with a patched version. The patched version applies the JSONFileCache from botocore (also used by the CLI)
to the default credential provider.
import os
import boto3
import botocore.session
from botocore.utils import JSONFileCache
def _setup_default_session_patch(**kwargs):
cli_cache_path = os.environ.get(
"AWS_CREDENTIAL_CACHE",
os.path.join(os.path.expanduser("~"), ".aws", "cli", "cache")
)
os.makedirs(cli_cache_path, exist_ok=True)
botocore_session = botocore.session.Session()
file_cache = JSONFileCache(cli_cache_path)
botocore_session.get_component("credential_provider").get_provider("assume-role").cache = file_cache
boto3.DEFAULT_SESSION = boto3.Session(botocore_session=botocore_session, **kwargs)
boto3.setup_default_session = _setup_default_session_patch
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file boto3_mfa_cache-1.0.0.tar.gz.
File metadata
- Download URL: boto3_mfa_cache-1.0.0.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e82d5a38a05e4b94a314c12e16991f3ade365e010d1fe1ed35f465ac2b4335d
|
|
| MD5 |
25e5d865d994a830b67f275845010a08
|
|
| BLAKE2b-256 |
c0710f9c1988ac29d033b8498b4b91cd1b1fcc15ae946041c453462350fd6419
|
File details
Details for the file boto3_mfa_cache-1.0.0-py3-none-any.whl.
File metadata
- Download URL: boto3_mfa_cache-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18157c1becbadc86aa8a49eaaccef6cb61016861050e018c1d836c3a3d4035b1
|
|
| MD5 |
8aad51fc6117613735e95473f6009f85
|
|
| BLAKE2b-256 |
5349d2d409a4e3ca7e39ad30d9f53a999c79ac0686a96076a0dfb15946271ba7
|