developer-toolkit-elasticache 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- developer_toolkit_elasticache/__init__.py +22 -0
- developer_toolkit_elasticache/cli.py +80 -0
- developer_toolkit_elasticache/errors.py +35 -0
- developer_toolkit_elasticache/py.typed +0 -0
- developer_toolkit_elasticache/token_generator.py +240 -0
- developer_toolkit_elasticache-1.0.0.dist-info/METADATA +145 -0
- developer_toolkit_elasticache-1.0.0.dist-info/RECORD +12 -0
- developer_toolkit_elasticache-1.0.0.dist-info/WHEEL +5 -0
- developer_toolkit_elasticache-1.0.0.dist-info/entry_points.txt +2 -0
- developer_toolkit_elasticache-1.0.0.dist-info/licenses/LICENSE +202 -0
- developer_toolkit_elasticache-1.0.0.dist-info/licenses/NOTICE +2 -0
- developer_toolkit_elasticache-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""Developer Toolkit for Amazon ElastiCache."""
|
|
5
|
+
|
|
6
|
+
from developer_toolkit_elasticache.errors import (
|
|
7
|
+
ConfigurationError,
|
|
8
|
+
InvalidParameterError,
|
|
9
|
+
ToolkitInputError,
|
|
10
|
+
)
|
|
11
|
+
from developer_toolkit_elasticache.token_generator import (
|
|
12
|
+
ElastiCacheIAMAuthTokenProvider,
|
|
13
|
+
generate_iam_auth_token,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"ConfigurationError",
|
|
18
|
+
"ElastiCacheIAMAuthTokenProvider",
|
|
19
|
+
"InvalidParameterError",
|
|
20
|
+
"ToolkitInputError",
|
|
21
|
+
"generate_iam_auth_token",
|
|
22
|
+
]
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from developer_toolkit_elasticache import generate_iam_auth_token
|
|
8
|
+
from developer_toolkit_elasticache.errors import ToolkitInputError
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _add_generate_iam_auth_token_args(parser: argparse.ArgumentParser) -> None:
|
|
12
|
+
target = parser.add_mutually_exclusive_group(required=True)
|
|
13
|
+
target.add_argument(
|
|
14
|
+
"--serverless-cache-name",
|
|
15
|
+
help="Serverless cache name (the SigV4 signing host)",
|
|
16
|
+
)
|
|
17
|
+
target.add_argument(
|
|
18
|
+
"--replication-group-id",
|
|
19
|
+
help="Replication group id for a node-based cluster (the SigV4 signing host)",
|
|
20
|
+
)
|
|
21
|
+
parser.add_argument(
|
|
22
|
+
"--user-id",
|
|
23
|
+
required=True,
|
|
24
|
+
help="ElastiCache user id (used to build the User ARN in the IAM policy)",
|
|
25
|
+
)
|
|
26
|
+
parser.add_argument(
|
|
27
|
+
"--region",
|
|
28
|
+
help="AWS region (default: the region resolved from your AWS configuration)",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _cmd_generate_iam_auth_token(args: argparse.Namespace) -> str | None:
|
|
33
|
+
return generate_iam_auth_token(
|
|
34
|
+
user_id=args.user_id,
|
|
35
|
+
region=args.region,
|
|
36
|
+
serverless_cache_name=args.serverless_cache_name,
|
|
37
|
+
replication_group_id=args.replication_group_id,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
42
|
+
"""Build the top-level parser.
|
|
43
|
+
|
|
44
|
+
Each tool contributes one subparser and sets ``func`` to a callable taking the
|
|
45
|
+
parsed namespace. That callable returns the text to write to stdout, or ``None``
|
|
46
|
+
if the tool has nothing to print. It signals a user-fixable failure by raising
|
|
47
|
+
an ``ToolkitInputError`` subclass; ``main`` handles the rest, so adding a tool
|
|
48
|
+
means adding a subparser here and nothing else.
|
|
49
|
+
"""
|
|
50
|
+
parser = argparse.ArgumentParser(
|
|
51
|
+
prog="developer-toolkit-elasticache",
|
|
52
|
+
description="Developer tools for Amazon ElastiCache",
|
|
53
|
+
)
|
|
54
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
55
|
+
|
|
56
|
+
gen = subparsers.add_parser(
|
|
57
|
+
"generate_iam_auth_token",
|
|
58
|
+
help="Generate an IAM auth token for an ElastiCache cache",
|
|
59
|
+
)
|
|
60
|
+
_add_generate_iam_auth_token_args(gen)
|
|
61
|
+
gen.set_defaults(func=_cmd_generate_iam_auth_token)
|
|
62
|
+
|
|
63
|
+
return parser
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def main(argv: list[str] | None = None) -> int:
|
|
67
|
+
args = _build_parser().parse_args(argv)
|
|
68
|
+
|
|
69
|
+
try:
|
|
70
|
+
output = args.func(args)
|
|
71
|
+
except ToolkitInputError as e:
|
|
72
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
73
|
+
return 1
|
|
74
|
+
if output is not None:
|
|
75
|
+
print(output)
|
|
76
|
+
return 0
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
if __name__ == "__main__":
|
|
80
|
+
sys.exit(main())
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""Error types raised by the toolkit.
|
|
5
|
+
|
|
6
|
+
``ToolkitInputError`` is the base for every failure the user can correct. It is also
|
|
7
|
+
the contract with the CLI: a ``ToolkitInputError`` is reported as a single-line
|
|
8
|
+
message instead of a traceback, and anything else is treated as a bug in the
|
|
9
|
+
toolkit and keeps its traceback.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ToolkitInputError(Exception):
|
|
14
|
+
"""Base class for failures the user can correct.
|
|
15
|
+
|
|
16
|
+
Catch this in CLI or application code to handle all user-fixable problems
|
|
17
|
+
uniformly (display a message, exit non-zero) without coupling to a specific
|
|
18
|
+
subclass.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class InvalidParameterError(ToolkitInputError, ValueError):
|
|
23
|
+
"""A parameter value or combination of parameters is not valid.
|
|
24
|
+
|
|
25
|
+
Covers both single-value violations (bad format, out of range) and
|
|
26
|
+
multi-parameter constraint violations (mutually-exclusive arguments,
|
|
27
|
+
missing required combinations).
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ConfigurationError(ToolkitInputError):
|
|
32
|
+
"""The AWS environment is missing required values to sign a token.
|
|
33
|
+
|
|
34
|
+
Covers the missing-configuration cases (no region or no credentials).
|
|
35
|
+
"""
|
|
File without changes
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
from urllib.parse import urlencode
|
|
8
|
+
|
|
9
|
+
from botocore.auth import SigV4QueryAuth
|
|
10
|
+
from botocore.awsrequest import AWSRequest
|
|
11
|
+
from botocore.session import Session
|
|
12
|
+
|
|
13
|
+
from developer_toolkit_elasticache.errors import (
|
|
14
|
+
ConfigurationError,
|
|
15
|
+
InvalidParameterError,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
from botocore.credentials import ReadOnlyCredentials
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
_TOKEN_TTL_SECONDS = 900 # 15 minutes
|
|
23
|
+
|
|
24
|
+
_SIGNING_SERVICE = "elasticache"
|
|
25
|
+
|
|
26
|
+
# The signed request is a presigned URL; the token is that URL without the scheme.
|
|
27
|
+
_URL_SCHEME_PREFIX = "https://"
|
|
28
|
+
|
|
29
|
+
# The cache name is the SigV4 signing host, validating it keeps a malformed or
|
|
30
|
+
# hostile value from being signed against a host and producing a wrong token.
|
|
31
|
+
# Matching documented ElastiCache constraints in cache name.
|
|
32
|
+
_CACHE_NAME_PATTERN = re.compile(r"^[a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)*$")
|
|
33
|
+
|
|
34
|
+
# ElastiCache service UserId pattern, with an optional "default." prefix for
|
|
35
|
+
# service-managed users (e.g. default.iam-user).
|
|
36
|
+
_USER_ID_PATTERN = re.compile(r"^(?:default\.)?[a-zA-Z][a-zA-Z0-9\-]*$")
|
|
37
|
+
|
|
38
|
+
# Honoured on top of botocore's own region resolution — see _resolve_region.
|
|
39
|
+
_REGION_ENV_VAR = "AWS_REGION"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
_NO_REGION_MESSAGE = (
|
|
43
|
+
"No AWS region found. Pass region explicitly, or configure one via "
|
|
44
|
+
"AWS_REGION, AWS_DEFAULT_REGION, or the region setting in your AWS config profile."
|
|
45
|
+
)
|
|
46
|
+
_NO_CREDENTIALS_MESSAGE = (
|
|
47
|
+
"No AWS credentials found. Configure credentials via the environment, "
|
|
48
|
+
"shared config/credentials files, or an instance/container role."
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _validate_and_normalize(parameter: str, value: str, pattern: re.Pattern[str]) -> str:
|
|
53
|
+
"""Lowercase ``value``, check it against ``pattern``, and return the normalized form.
|
|
54
|
+
|
|
55
|
+
ElastiCache stores cache names and user ids lowercase, so a value is normalized
|
|
56
|
+
before both matching and signing; the value signed into the token and the value
|
|
57
|
+
the client sends must match server-side.
|
|
58
|
+
"""
|
|
59
|
+
normalized = value.lower()
|
|
60
|
+
if not pattern.match(normalized):
|
|
61
|
+
# Never echo the caller's value back in the message — name the parameter
|
|
62
|
+
# and the accepted pattern only.
|
|
63
|
+
raise InvalidParameterError(
|
|
64
|
+
f"Invalid value for parameter {parameter!r}: must match {pattern.pattern}"
|
|
65
|
+
)
|
|
66
|
+
return normalized
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _resolve_target(
|
|
70
|
+
serverless_cache_name: str | None, replication_group_id: str | None
|
|
71
|
+
) -> tuple[str, bool]:
|
|
72
|
+
"""Map the mutually-exclusive name arguments to ``(cache_name, serverless)``.
|
|
73
|
+
The chosen value is validated here, under its own parameter name, so the
|
|
74
|
+
error names the argument the caller passed.
|
|
75
|
+
"""
|
|
76
|
+
if serverless_cache_name and not replication_group_id:
|
|
77
|
+
cache_name = _validate_and_normalize(
|
|
78
|
+
"serverless_cache_name", serverless_cache_name, _CACHE_NAME_PATTERN
|
|
79
|
+
)
|
|
80
|
+
return cache_name, True
|
|
81
|
+
if replication_group_id and not serverless_cache_name:
|
|
82
|
+
cache_name = _validate_and_normalize(
|
|
83
|
+
"replication_group_id", replication_group_id, _CACHE_NAME_PATTERN
|
|
84
|
+
)
|
|
85
|
+
return cache_name, False
|
|
86
|
+
raise InvalidParameterError(
|
|
87
|
+
"Invalid parameter combination for 'serverless_cache_name' and "
|
|
88
|
+
"'replication_group_id': exactly one must be provided."
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _resolve_region(region: str | None, session: Session) -> str:
|
|
93
|
+
"""Fall back to a configured region when one was not passed explicitly.
|
|
94
|
+
|
|
95
|
+
The session fallback is read from the same session that vends the credentials
|
|
96
|
+
``AWS_REGION`` is consulted directly because botocore does not: its region only
|
|
97
|
+
resolves from ``AWS_DEFAULT_REGION`` or the config profile.
|
|
98
|
+
"""
|
|
99
|
+
resolved = (
|
|
100
|
+
region or os.environ.get(_REGION_ENV_VAR) or session.get_config_variable("region")
|
|
101
|
+
)
|
|
102
|
+
if not resolved:
|
|
103
|
+
raise ConfigurationError(_NO_REGION_MESSAGE)
|
|
104
|
+
return resolved
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _resolve_credentials(session: Session) -> "ReadOnlyCredentials":
|
|
108
|
+
"""Read a frozen credential set from the session's credential chain.
|
|
109
|
+
|
|
110
|
+
Called on every token request rather than cached, so rotated credentials
|
|
111
|
+
(SSO, assume-role, container, instance metadata) are picked up automatically.
|
|
112
|
+
"""
|
|
113
|
+
credentials = session.get_credentials()
|
|
114
|
+
if credentials is None:
|
|
115
|
+
raise ConfigurationError(_NO_CREDENTIALS_MESSAGE)
|
|
116
|
+
return credentials.get_frozen_credentials()
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _sign_token(
|
|
120
|
+
cache_name: str,
|
|
121
|
+
user_id: str,
|
|
122
|
+
region: str,
|
|
123
|
+
credentials: "ReadOnlyCredentials",
|
|
124
|
+
*,
|
|
125
|
+
serverless: bool = True,
|
|
126
|
+
) -> str:
|
|
127
|
+
"""Core signing logic. Returns the token string.
|
|
128
|
+
|
|
129
|
+
``cache_name`` is the ElastiCache **cache name** (for a serverless cache) or
|
|
130
|
+
the **replication group id** (for a node-based cluster) — the value used as
|
|
131
|
+
the SigV4 signing host. The server recomputes the signature using this name as
|
|
132
|
+
the host.
|
|
133
|
+
Cache names are lowercased at creation time, so the name must be
|
|
134
|
+
signed in lowercase to avoid auth errors.
|
|
135
|
+
|
|
136
|
+
``user_id`` is signed as the ``User`` request parameter, lowercased because
|
|
137
|
+
the service stores the user id lowercase (see ``_validate_and_normalize``).
|
|
138
|
+
(For IAM-enabled users this must equal the Redis/Valkey ACL user name, but
|
|
139
|
+
conceptually it is the ElastiCache user id, not the ACL name.)
|
|
140
|
+
|
|
141
|
+
``serverless`` is part of the signature, so it cannot be appended after signing.
|
|
142
|
+
"""
|
|
143
|
+
user_id = _validate_and_normalize("user_id", user_id, _USER_ID_PATTERN)
|
|
144
|
+
|
|
145
|
+
params = {"Action": "connect", "User": user_id}
|
|
146
|
+
if serverless:
|
|
147
|
+
params["ResourceType"] = "ServerlessCache"
|
|
148
|
+
url = f"{_URL_SCHEME_PREFIX}{cache_name.lower()}/?{urlencode(params)}"
|
|
149
|
+
|
|
150
|
+
request = AWSRequest(method="GET", url=url)
|
|
151
|
+
signer = SigV4QueryAuth(
|
|
152
|
+
credentials, _SIGNING_SERVICE, region, expires=_TOKEN_TTL_SECONDS
|
|
153
|
+
)
|
|
154
|
+
signer.add_auth(request)
|
|
155
|
+
|
|
156
|
+
return request.url.removeprefix(_URL_SCHEME_PREFIX)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def generate_iam_auth_token(
|
|
160
|
+
*,
|
|
161
|
+
user_id: str,
|
|
162
|
+
region: str | None = None,
|
|
163
|
+
serverless_cache_name: str | None = None,
|
|
164
|
+
replication_group_id: str | None = None,
|
|
165
|
+
session: Session | None = None,
|
|
166
|
+
) -> str:
|
|
167
|
+
"""Generate an ElastiCache IAM auth token using the default AWS credential chain.
|
|
168
|
+
|
|
169
|
+
Provide exactly one of ``serverless_cache_name`` (for a serverless cache) or
|
|
170
|
+
``replication_group_id`` (for a node-based cluster); the choice selects the
|
|
171
|
+
resource type baked into the signature. ``user_id`` is the ElastiCache user
|
|
172
|
+
id used to build the User ARN in the IAM policy. Returns the token string
|
|
173
|
+
(like RDS's generate_db_auth_token).
|
|
174
|
+
|
|
175
|
+
``region`` defaults to the region the session resolves, so it only needs to be
|
|
176
|
+
passed to sign for a region other than the configured one.
|
|
177
|
+
|
|
178
|
+
Pass ``session`` to sign with a specific botocore session; by default a new
|
|
179
|
+
session resolves credentials from the standard chain.
|
|
180
|
+
"""
|
|
181
|
+
cache_name, serverless = _resolve_target(serverless_cache_name, replication_group_id)
|
|
182
|
+
session = session or Session()
|
|
183
|
+
resolved_region = _resolve_region(region, session)
|
|
184
|
+
credentials = _resolve_credentials(session)
|
|
185
|
+
return _sign_token(
|
|
186
|
+
cache_name, user_id, resolved_region, credentials, serverless=serverless
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class ElastiCacheIAMAuthTokenProvider:
|
|
191
|
+
"""Generic IAM token provider for ElastiCache.
|
|
192
|
+
|
|
193
|
+
Signs a fresh token on demand each time ``get_token()`` is called, then
|
|
194
|
+
bridges it into whatever client you use (see the ``examples/`` directory).
|
|
195
|
+
|
|
196
|
+
Provide exactly one of ``serverless_cache_name`` (for a serverless cache) or
|
|
197
|
+
``replication_group_id`` (for a node-based cluster).
|
|
198
|
+
``user_id`` is the ElastiCache user id.
|
|
199
|
+
``region`` defaults to the region the session resolves, so it only needs to be
|
|
200
|
+
passed to sign for a region other than the configured one.
|
|
201
|
+
|
|
202
|
+
Credentials come from the default AWS credential chain (env vars, shared
|
|
203
|
+
config/credentials, or the instance/container role). They are re-read on
|
|
204
|
+
every call, so rotated credentials (SSO, assume-role, container, instance
|
|
205
|
+
metadata) are picked up automatically.
|
|
206
|
+
"""
|
|
207
|
+
|
|
208
|
+
def __init__(
|
|
209
|
+
self,
|
|
210
|
+
*,
|
|
211
|
+
user_id: str,
|
|
212
|
+
region: str | None = None,
|
|
213
|
+
serverless_cache_name: str | None = None,
|
|
214
|
+
replication_group_id: str | None = None,
|
|
215
|
+
session: Session | None = None,
|
|
216
|
+
):
|
|
217
|
+
self._cache_name, self._serverless = _resolve_target(
|
|
218
|
+
serverless_cache_name, replication_group_id
|
|
219
|
+
)
|
|
220
|
+
self._user_id = _validate_and_normalize("user_id", user_id, _USER_ID_PATTERN)
|
|
221
|
+
self._session = session or Session()
|
|
222
|
+
# Resolved once here rather than per call: unlike credentials, the region is
|
|
223
|
+
# static configuration, and a missing region should surface at construction
|
|
224
|
+
# instead of at the first connection attempt.
|
|
225
|
+
self._region = _resolve_region(region, self._session)
|
|
226
|
+
|
|
227
|
+
@property
|
|
228
|
+
def user_id(self) -> str:
|
|
229
|
+
return self._user_id
|
|
230
|
+
|
|
231
|
+
def get_token(self) -> str:
|
|
232
|
+
"""Sign and return a fresh IAM auth token."""
|
|
233
|
+
credentials = _resolve_credentials(self._session)
|
|
234
|
+
return _sign_token(
|
|
235
|
+
self._cache_name,
|
|
236
|
+
self._user_id,
|
|
237
|
+
self._region,
|
|
238
|
+
credentials,
|
|
239
|
+
serverless=self._serverless,
|
|
240
|
+
)
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: developer-toolkit-elasticache
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Developer toolkit for Amazon ElastiCache
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
License-File: NOTICE
|
|
18
|
+
Requires-Dist: botocore>=1.43.0
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# Developer Toolkit for Amazon ElastiCache (Python)
|
|
22
|
+
|
|
23
|
+
This is a developer toolkit for working with Amazon ElastiCache, as a Python library and
|
|
24
|
+
CLI.
|
|
25
|
+
It provides a function to generate an [IAM authentication token](https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/auth-iam.html) that Amazon ElastiCache requires as the
|
|
26
|
+
connection password for an IAM-enabled user.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Requires Python 3.10–3.14 and `pip`. Install from PyPI:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
python3 -m pip install developer-toolkit-elasticache
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
If you don't have [`pip`](https://pip.pypa.io) installed, this
|
|
37
|
+
[Python installation guide](https://docs.python-guide.org/starting/installation/) can
|
|
38
|
+
guide you through the process.
|
|
39
|
+
|
|
40
|
+
This installs the library and the `developer-toolkit-elasticache` command. To install
|
|
41
|
+
from source instead, clone the repository and run `python3 -m pip install .` from the
|
|
42
|
+
`python/` directory.
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
To generate a usable token you need:
|
|
47
|
+
|
|
48
|
+
- An Amazon ElastiCache serverless cache or replication group with the following:
|
|
49
|
+
- Valkey 7.2 and above or Redis OSS 7.0 and above
|
|
50
|
+
- In-transit Encryption (TLS) enabled
|
|
51
|
+
- IAM-enabled user that has access to to the cache.
|
|
52
|
+
- AWS credentials on the default credential chain (environment variables, shared
|
|
53
|
+
config files, or an instance/container role), for an identity with the
|
|
54
|
+
`elasticache:Connect` permission to the cache.
|
|
55
|
+
|
|
56
|
+
### Generate a token
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from developer_toolkit_elasticache import generate_iam_auth_token
|
|
60
|
+
|
|
61
|
+
token = generate_iam_auth_token(
|
|
62
|
+
serverless_cache_name="my-cache", # or replication_group_id="my-group"
|
|
63
|
+
user_id="iam-user",
|
|
64
|
+
region="us-east-1",
|
|
65
|
+
)
|
|
66
|
+
```
|
|
67
|
+
Use the returned token as the password when connecting to the cache.
|
|
68
|
+
|
|
69
|
+
**Arguments**
|
|
70
|
+
|
|
71
|
+
- `serverless_cache_name` or `replication_group_id` (string) [one required]
|
|
72
|
+
The serverless cache or node-based replication group to sign for.
|
|
73
|
+
- `user_id` (string) [required]
|
|
74
|
+
The IAM-enabled user to authenticate as.
|
|
75
|
+
- `region` (string) [optional]
|
|
76
|
+
The AWS region. Defaults to your AWS configuration (`AWS_REGION`,
|
|
77
|
+
`AWS_DEFAULT_REGION`, or the `region` in your profile, in that order).
|
|
78
|
+
|
|
79
|
+
**Credentials**
|
|
80
|
+
|
|
81
|
+
Credentials are resolved through the standard AWS credential provider chain
|
|
82
|
+
and are re-read on every call, so rotated credentials are picked up for every new token generation.
|
|
83
|
+
The chain is checked in this order:
|
|
84
|
+
|
|
85
|
+
1. Environment variables (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and
|
|
86
|
+
`AWS_SESSION_TOKEN`).
|
|
87
|
+
2. The shared credentials and config files (`~/.aws/credentials`, `~/.aws/config`),
|
|
88
|
+
selected by `AWS_PROFILE`, including any assume-role or SSO configuration.
|
|
89
|
+
3. Container credentials (Amazon ECS / EKS).
|
|
90
|
+
4. EC2 instance profile credentials (IMDS).
|
|
91
|
+
|
|
92
|
+
### Reconnecting clients
|
|
93
|
+
|
|
94
|
+
Clients such as redis-py and valkey-py request credentials on every reconnection.
|
|
95
|
+
Pass them an `ElastiCacheIAMAuthTokenProvider` and call `get_token()` when the
|
|
96
|
+
connection opens, so each connection uses a fresh token:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from developer_toolkit_elasticache import ElastiCacheIAMAuthTokenProvider
|
|
100
|
+
|
|
101
|
+
auth = ElastiCacheIAMAuthTokenProvider(
|
|
102
|
+
serverless_cache_name="my-cache",
|
|
103
|
+
user_id="iam-user",
|
|
104
|
+
region="us-east-1",
|
|
105
|
+
)
|
|
106
|
+
username, password = auth.user_id, auth.get_token()
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The [`examples/`](examples/) directory has redis-py and valkey-py integrations.
|
|
110
|
+
|
|
111
|
+
### Command line
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
developer-toolkit-elasticache generate_iam_auth_token \
|
|
115
|
+
--serverless-cache-name my-cache \
|
|
116
|
+
--user-id iam-user \
|
|
117
|
+
--region us-east-1
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Add `--region` to sign for a specific region instead of the one resolved from your
|
|
121
|
+
AWS configuration
|
|
122
|
+
|
|
123
|
+
For the command line, the token is written to stdout, so you can capture it
|
|
124
|
+
directly and pass it to a CLI client through an auth environment variable.
|
|
125
|
+
`REDISCLI_AUTH` works with both `redis-cli` and `valkey-cli`; `VALKEYCLI_AUTH`
|
|
126
|
+
works with `valkey-cli` 9.0.0 and later:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
export VALKEYCLI_AUTH=$(developer-toolkit-elasticache generate_iam_auth_token \
|
|
130
|
+
--serverless-cache-name my-cache --user-id iam-user --region us-east-1)
|
|
131
|
+
|
|
132
|
+
# Example: connecting via valkey-cli
|
|
133
|
+
valkey-cli --tls -h <my-cache-configured-endpoint> --user <iam-user>
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Security
|
|
137
|
+
|
|
138
|
+
The token is a bearer credential. Keep it out of logs and shell history, and
|
|
139
|
+
connect over TLS. When using a CLI client, you can use the `REDISCLI_AUTH` /
|
|
140
|
+
`VALKEYCLI_AUTH` environment variable to pass the token more safely than the
|
|
141
|
+
`-a` / `--pass` flags, which expose it in the process list.
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
developer_toolkit_elasticache/__init__.py,sha256=d1McpYztm8AZlkGONxyXa57VHu1OHP6gglqZnQ6YqTI,577
|
|
2
|
+
developer_toolkit_elasticache/cli.py,sha256=6djWrtrn_hnNwZ4GXmiqlARTu-u1YXK4zFx7N2JegUI,2591
|
|
3
|
+
developer_toolkit_elasticache/errors.py,sha256=cLioVsWuB00y7QmKYponHsdb-jxiPjaZqrwrncjav5o,1218
|
|
4
|
+
developer_toolkit_elasticache/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
developer_toolkit_elasticache/token_generator.py,sha256=ztcY7hXIDdjsUC07IuVFUPqWru3NDiiqgJk1AZRFxSM,9327
|
|
6
|
+
developer_toolkit_elasticache-1.0.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
7
|
+
developer_toolkit_elasticache-1.0.0.dist-info/licenses/NOTICE,sha256=t21Yrf2GNLB5hqSIhhwYl8moKa1GyF3wv_lW-F6OmTk,108
|
|
8
|
+
developer_toolkit_elasticache-1.0.0.dist-info/METADATA,sha256=CMUG8t0gRKyv9J-WE-Z5qovzLCIieBvqSxJUj0L_eMo,5169
|
|
9
|
+
developer_toolkit_elasticache-1.0.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
10
|
+
developer_toolkit_elasticache-1.0.0.dist-info/entry_points.txt,sha256=5kiJy6ZQS7qlbm8k8Y9CfF6EFpTEpHXz4vChhxV7wmU,89
|
|
11
|
+
developer_toolkit_elasticache-1.0.0.dist-info/top_level.txt,sha256=yTFZCqHufTISfXTMmiQqPUWyuP-E0cH4svAGu4JHZ1s,30
|
|
12
|
+
developer_toolkit_elasticache-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
developer_toolkit_elasticache
|