parallax-aws 0.1.0__tar.gz
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.
- parallax_aws-0.1.0/.gitignore +16 -0
- parallax_aws-0.1.0/LICENSE +201 -0
- parallax_aws-0.1.0/NOTICE +5 -0
- parallax_aws-0.1.0/PKG-INFO +141 -0
- parallax_aws-0.1.0/README.md +122 -0
- parallax_aws-0.1.0/pyproject.toml +43 -0
- parallax_aws-0.1.0/src/parallax/aws/__init__.py +5 -0
- parallax_aws-0.1.0/src/parallax/aws/_rds.py +126 -0
- parallax_aws-0.1.0/src/parallax/aws/postgres.py +95 -0
- parallax_aws-0.1.0/src/parallax/aws/py.typed +0 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Python build / test artifacts (uv, hatchling, pytest-cov, pyright).
|
|
2
|
+
.venv/
|
|
3
|
+
__pycache__/
|
|
4
|
+
*.pyc
|
|
5
|
+
*.egg-info/
|
|
6
|
+
build/
|
|
7
|
+
dist/
|
|
8
|
+
coverage.xml
|
|
9
|
+
.coverage
|
|
10
|
+
.coverage.*
|
|
11
|
+
.pytest_cache/
|
|
12
|
+
.ruff_cache/
|
|
13
|
+
# An editable install compiles parallax-postgres's Cython sources in place.
|
|
14
|
+
packages/parallax-postgres/src/**/*.c
|
|
15
|
+
*.so
|
|
16
|
+
*.pyd
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: parallax-aws
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Parallax AWS credential providers: RDS IAM database authentication as a Credential Source (the sole botocore declarer).
|
|
5
|
+
Project-URL: Documentation, https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/guide/README.md
|
|
6
|
+
Project-URL: Repository, https://github.com/davidsunglee/parallax
|
|
7
|
+
Project-URL: Changelog, https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/releases.md
|
|
8
|
+
Author: Parallax
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
Requires-Python: >=3.14
|
|
13
|
+
Requires-Dist: botocore>=1.43.99
|
|
14
|
+
Requires-Dist: parallax-core==0.1.0
|
|
15
|
+
Provides-Extra: postgres
|
|
16
|
+
Requires-Dist: parallax-postgres==0.1.0; extra == 'postgres'
|
|
17
|
+
Requires-Dist: psycopg<3.4,>=3.3.6; extra == 'postgres'
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# parallax-aws
|
|
21
|
+
|
|
22
|
+
Parallax's AWS credential providers: the sole declarer of `botocore`. See
|
|
23
|
+
the [Python binding](https://github.com/davidsunglee/parallax/blob/main/languages/python/spec/python.md).
|
|
24
|
+
The distribution includes Apache-2.0 `LICENSE` and `NOTICE`; see the
|
|
25
|
+
[release notes](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/releases.md)
|
|
26
|
+
for publication status.
|
|
27
|
+
|
|
28
|
+
## Authenticating an RDS Postgres login with IAM
|
|
29
|
+
|
|
30
|
+
Install the `postgres` extra — `python -m pip install "parallax-aws[postgres]==0.1.0"` — and each
|
|
31
|
+
endpoint is one call:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from parallax.aws.postgres import rds_endpoint
|
|
35
|
+
from parallax.postgres import PoolOptions, PostgresAdapter
|
|
36
|
+
from parallax.snapshot import connect
|
|
37
|
+
|
|
38
|
+
writer = rds_endpoint(
|
|
39
|
+
host="orders.cluster-abc.us-east-1.rds.amazonaws.com",
|
|
40
|
+
user="orders_service",
|
|
41
|
+
database="orders",
|
|
42
|
+
region="us-east-1",
|
|
43
|
+
pool=PoolOptions(min_size=2, max_size=20),
|
|
44
|
+
)
|
|
45
|
+
with connect(PostgresAdapter(writer=writer), model) as root:
|
|
46
|
+
...
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
What comes back is the ordinary `PostgresEndpoint`, configured, and the
|
|
50
|
+
application composes the adapter around it — nothing sits between the
|
|
51
|
+
application and the adapter at runtime, and nothing was resolved or opened to
|
|
52
|
+
build it. The factory is where three things are stated once, so no deployment
|
|
53
|
+
has to remember them: the connection is encrypted, the token is signed for the
|
|
54
|
+
host, port and user the endpoint's connections then use, and the endpoint to
|
|
55
|
+
sign for is the one the application connects through — so each endpoint signs
|
|
56
|
+
for its own host.
|
|
57
|
+
|
|
58
|
+
`params` keeps the rest of libpq's grammar open — `application_name`,
|
|
59
|
+
`options`, `connect_timeout`, and `sslrootcert` with an `sslmode` of
|
|
60
|
+
`verify-ca` or `verify-full`, which is what AWS's own certificate bundle is for.
|
|
61
|
+
It may strengthen the TLS requirement and may not weaken it, it may not ask for
|
|
62
|
+
the GSS encryption libpq would carry the login over in TLS's place, and it may
|
|
63
|
+
not restate `host`, `port`, `user`, `dbname` or `password`: a value the token
|
|
64
|
+
was not signed for authenticates nothing. `host` names one endpoint for the same
|
|
65
|
+
reason — libpq reads a comma-separated list of them, and a token signed for the
|
|
66
|
+
list authenticates at none of its members. Every refusal is fixed text, because
|
|
67
|
+
`params` is a place other secrets live.
|
|
68
|
+
|
|
69
|
+
## The source on its own
|
|
70
|
+
|
|
71
|
+
`RdsIamCredentials` is a `CredentialSource`, and the part of this package that
|
|
72
|
+
imports no adapter. An application assembling its own connection string uses it
|
|
73
|
+
directly, and a deployment on another engine installs this package without the
|
|
74
|
+
`postgres` extra and gets no database driver with it.
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from parallax.aws import RdsIamCredentials
|
|
78
|
+
from parallax.postgres import PostgresEndpoint
|
|
79
|
+
|
|
80
|
+
host = "orders.cluster-abc.us-east-1.rds.amazonaws.com"
|
|
81
|
+
writer = PostgresEndpoint(
|
|
82
|
+
f"postgresql://orders_service@{host}:5432/orders?sslmode=require",
|
|
83
|
+
credentials=RdsIamCredentials(host=host, port=5432, user="orders_service", region="us-east-1"),
|
|
84
|
+
)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
IAM database authentication is identical on RDS Postgres and Aurora Postgres.
|
|
88
|
+
For a cluster, the endpoint above is the one the application connects through:
|
|
89
|
+
a token proves nothing at an endpoint it was not signed for. Written this way,
|
|
90
|
+
the TLS requirement the factory would have stated is the caller's to spell.
|
|
91
|
+
|
|
92
|
+
## What has to agree outside Parallax
|
|
93
|
+
|
|
94
|
+
- the database login exists and holds the IAM role —
|
|
95
|
+
`CREATE USER orders_service; GRANT rds_iam TO orders_service;`;
|
|
96
|
+
- the AWS identity the process runs as is allowed `rds-db:connect` on
|
|
97
|
+
`arn:aws:rds-db:<region>:<account>:dbuser:<resource-id>/orders_service`;
|
|
98
|
+
- the connection is encrypted. An IAM token is a password, so it travels over
|
|
99
|
+
TLS: `sslmode=require` at least, and `verify-full` with AWS's certificate
|
|
100
|
+
bundle where the deployment can supply one.
|
|
101
|
+
|
|
102
|
+
## What resolving a token costs
|
|
103
|
+
|
|
104
|
+
Signing is local — no AWS endpoint is reached to produce a token. What can block
|
|
105
|
+
is botocore finding or refreshing the AWS credentials it signs with: the
|
|
106
|
+
environment, a profile, an ECS task role, IMDSv2, IRSA, or SSO. The record
|
|
107
|
+
bounds the network all of that reaches over, because the seam requires a source
|
|
108
|
+
to, and it creates its RDS client on first use rather than at construction, so
|
|
109
|
+
composing an adapter stays free of I/O.
|
|
110
|
+
|
|
111
|
+
One thing it does not bound: a helper command a profile's `credential_process`
|
|
112
|
+
names. botocore waits on that command with no timeout, and so does this record —
|
|
113
|
+
the program is yours, and so is bounding its wait. Be exact about what an
|
|
114
|
+
unbounded one costs, because it is not one connection: the RDS client is created
|
|
115
|
+
once under a lock, so a helper that hangs holds every pool worker behind it and
|
|
116
|
+
the application stops connecting rather than connects slowly. A helper that
|
|
117
|
+
cannot promise to answer — one waiting on a person, or on a network of its own
|
|
118
|
+
it has not bounded — belongs behind a session of your own, built on a chain that
|
|
119
|
+
does not run it. An injected session is used exactly as given.
|
|
120
|
+
|
|
121
|
+
There is no token cache and no refresh thread. A connection the server has
|
|
122
|
+
already accepted is never disturbed by its token ageing out, and when a source
|
|
123
|
+
is asked for a new one is the port's rule rather than this package's — see
|
|
124
|
+
`core/spec/m-db-port.md`, "Configuration carries where, and a Credential Source
|
|
125
|
+
carries how".
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
# a session of your own — a profile, static keys, a test double; used as given
|
|
129
|
+
RdsIamCredentials(host=endpoint, port=5432, user="app", region="us-east-1", session=session)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## When a token cannot be produced
|
|
133
|
+
|
|
134
|
+
Every botocore or signing failure surfaces as
|
|
135
|
+
`CredentialResolutionError: RDS IAM token could not be generated`, with the
|
|
136
|
+
native error chained beneath it. The message is fixed text so that nothing a
|
|
137
|
+
provider says can leak through it; what the adapter then reports — a startup
|
|
138
|
+
that fails naming the refusal, or an acquisition failing with
|
|
139
|
+
`reason="credentials_refused"` — is in
|
|
140
|
+
[PostgreSQL connection lifecycle](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/postgresql-lifecycle.md)
|
|
141
|
+
under "Where the password lives".
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# parallax-aws
|
|
2
|
+
|
|
3
|
+
Parallax's AWS credential providers: the sole declarer of `botocore`. See
|
|
4
|
+
the [Python binding](https://github.com/davidsunglee/parallax/blob/main/languages/python/spec/python.md).
|
|
5
|
+
The distribution includes Apache-2.0 `LICENSE` and `NOTICE`; see the
|
|
6
|
+
[release notes](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/releases.md)
|
|
7
|
+
for publication status.
|
|
8
|
+
|
|
9
|
+
## Authenticating an RDS Postgres login with IAM
|
|
10
|
+
|
|
11
|
+
Install the `postgres` extra — `python -m pip install "parallax-aws[postgres]==0.1.0"` — and each
|
|
12
|
+
endpoint is one call:
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
from parallax.aws.postgres import rds_endpoint
|
|
16
|
+
from parallax.postgres import PoolOptions, PostgresAdapter
|
|
17
|
+
from parallax.snapshot import connect
|
|
18
|
+
|
|
19
|
+
writer = rds_endpoint(
|
|
20
|
+
host="orders.cluster-abc.us-east-1.rds.amazonaws.com",
|
|
21
|
+
user="orders_service",
|
|
22
|
+
database="orders",
|
|
23
|
+
region="us-east-1",
|
|
24
|
+
pool=PoolOptions(min_size=2, max_size=20),
|
|
25
|
+
)
|
|
26
|
+
with connect(PostgresAdapter(writer=writer), model) as root:
|
|
27
|
+
...
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
What comes back is the ordinary `PostgresEndpoint`, configured, and the
|
|
31
|
+
application composes the adapter around it — nothing sits between the
|
|
32
|
+
application and the adapter at runtime, and nothing was resolved or opened to
|
|
33
|
+
build it. The factory is where three things are stated once, so no deployment
|
|
34
|
+
has to remember them: the connection is encrypted, the token is signed for the
|
|
35
|
+
host, port and user the endpoint's connections then use, and the endpoint to
|
|
36
|
+
sign for is the one the application connects through — so each endpoint signs
|
|
37
|
+
for its own host.
|
|
38
|
+
|
|
39
|
+
`params` keeps the rest of libpq's grammar open — `application_name`,
|
|
40
|
+
`options`, `connect_timeout`, and `sslrootcert` with an `sslmode` of
|
|
41
|
+
`verify-ca` or `verify-full`, which is what AWS's own certificate bundle is for.
|
|
42
|
+
It may strengthen the TLS requirement and may not weaken it, it may not ask for
|
|
43
|
+
the GSS encryption libpq would carry the login over in TLS's place, and it may
|
|
44
|
+
not restate `host`, `port`, `user`, `dbname` or `password`: a value the token
|
|
45
|
+
was not signed for authenticates nothing. `host` names one endpoint for the same
|
|
46
|
+
reason — libpq reads a comma-separated list of them, and a token signed for the
|
|
47
|
+
list authenticates at none of its members. Every refusal is fixed text, because
|
|
48
|
+
`params` is a place other secrets live.
|
|
49
|
+
|
|
50
|
+
## The source on its own
|
|
51
|
+
|
|
52
|
+
`RdsIamCredentials` is a `CredentialSource`, and the part of this package that
|
|
53
|
+
imports no adapter. An application assembling its own connection string uses it
|
|
54
|
+
directly, and a deployment on another engine installs this package without the
|
|
55
|
+
`postgres` extra and gets no database driver with it.
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from parallax.aws import RdsIamCredentials
|
|
59
|
+
from parallax.postgres import PostgresEndpoint
|
|
60
|
+
|
|
61
|
+
host = "orders.cluster-abc.us-east-1.rds.amazonaws.com"
|
|
62
|
+
writer = PostgresEndpoint(
|
|
63
|
+
f"postgresql://orders_service@{host}:5432/orders?sslmode=require",
|
|
64
|
+
credentials=RdsIamCredentials(host=host, port=5432, user="orders_service", region="us-east-1"),
|
|
65
|
+
)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
IAM database authentication is identical on RDS Postgres and Aurora Postgres.
|
|
69
|
+
For a cluster, the endpoint above is the one the application connects through:
|
|
70
|
+
a token proves nothing at an endpoint it was not signed for. Written this way,
|
|
71
|
+
the TLS requirement the factory would have stated is the caller's to spell.
|
|
72
|
+
|
|
73
|
+
## What has to agree outside Parallax
|
|
74
|
+
|
|
75
|
+
- the database login exists and holds the IAM role —
|
|
76
|
+
`CREATE USER orders_service; GRANT rds_iam TO orders_service;`;
|
|
77
|
+
- the AWS identity the process runs as is allowed `rds-db:connect` on
|
|
78
|
+
`arn:aws:rds-db:<region>:<account>:dbuser:<resource-id>/orders_service`;
|
|
79
|
+
- the connection is encrypted. An IAM token is a password, so it travels over
|
|
80
|
+
TLS: `sslmode=require` at least, and `verify-full` with AWS's certificate
|
|
81
|
+
bundle where the deployment can supply one.
|
|
82
|
+
|
|
83
|
+
## What resolving a token costs
|
|
84
|
+
|
|
85
|
+
Signing is local — no AWS endpoint is reached to produce a token. What can block
|
|
86
|
+
is botocore finding or refreshing the AWS credentials it signs with: the
|
|
87
|
+
environment, a profile, an ECS task role, IMDSv2, IRSA, or SSO. The record
|
|
88
|
+
bounds the network all of that reaches over, because the seam requires a source
|
|
89
|
+
to, and it creates its RDS client on first use rather than at construction, so
|
|
90
|
+
composing an adapter stays free of I/O.
|
|
91
|
+
|
|
92
|
+
One thing it does not bound: a helper command a profile's `credential_process`
|
|
93
|
+
names. botocore waits on that command with no timeout, and so does this record —
|
|
94
|
+
the program is yours, and so is bounding its wait. Be exact about what an
|
|
95
|
+
unbounded one costs, because it is not one connection: the RDS client is created
|
|
96
|
+
once under a lock, so a helper that hangs holds every pool worker behind it and
|
|
97
|
+
the application stops connecting rather than connects slowly. A helper that
|
|
98
|
+
cannot promise to answer — one waiting on a person, or on a network of its own
|
|
99
|
+
it has not bounded — belongs behind a session of your own, built on a chain that
|
|
100
|
+
does not run it. An injected session is used exactly as given.
|
|
101
|
+
|
|
102
|
+
There is no token cache and no refresh thread. A connection the server has
|
|
103
|
+
already accepted is never disturbed by its token ageing out, and when a source
|
|
104
|
+
is asked for a new one is the port's rule rather than this package's — see
|
|
105
|
+
`core/spec/m-db-port.md`, "Configuration carries where, and a Credential Source
|
|
106
|
+
carries how".
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
# a session of your own — a profile, static keys, a test double; used as given
|
|
110
|
+
RdsIamCredentials(host=endpoint, port=5432, user="app", region="us-east-1", session=session)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## When a token cannot be produced
|
|
114
|
+
|
|
115
|
+
Every botocore or signing failure surfaces as
|
|
116
|
+
`CredentialResolutionError: RDS IAM token could not be generated`, with the
|
|
117
|
+
native error chained beneath it. The message is fixed text so that nothing a
|
|
118
|
+
provider says can leak through it; what the adapter then reports — a startup
|
|
119
|
+
that fails naming the refusal, or an acquisition failing with
|
|
120
|
+
`reason="credentials_refused"` — is in
|
|
121
|
+
[PostgreSQL connection lifecycle](https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/postgresql-lifecycle.md)
|
|
122
|
+
under "Where the password lives".
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "parallax-aws"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Parallax AWS credential providers: RDS IAM database authentication as a Credential Source (the sole botocore declarer)."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.14"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE", "NOTICE"]
|
|
9
|
+
authors = [{ name = "Parallax" }]
|
|
10
|
+
dependencies = [
|
|
11
|
+
"parallax-core==0.1.0",
|
|
12
|
+
# Declared for the AWS credential chain rather than for the token: the
|
|
13
|
+
# presign is local, but finding the credentials to sign with — environment,
|
|
14
|
+
# profiles, IMDSv2, ECS task roles, IRSA, SSO, and the automatic refresh of
|
|
15
|
+
# every temporary credential those produce — is botocore's. boto3 adds
|
|
16
|
+
# nothing: `generate_db_auth_token` is a botocore RDS-client operation, so an
|
|
17
|
+
# application already using boto3 shares this one install.
|
|
18
|
+
"botocore>=1.43.99",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
# The engine-specific slice `parallax.aws.postgres` composes a
|
|
22
|
+
# `PostgresAdapter`, and this extra is what keeps the rest of the distribution
|
|
23
|
+
# free of one: a deployment on another engine installs the credential provider
|
|
24
|
+
# and no database driver arrives with it. The slice also imports psycopg
|
|
25
|
+
# directly, so the extra declares the driver beside the adapter that brings its
|
|
26
|
+
# binary build.
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
postgres = ["parallax-postgres==0.1.0", "psycopg>=3.3.6,<3.4"]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Documentation = "https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/guide/README.md"
|
|
32
|
+
Repository = "https://github.com/davidsunglee/parallax"
|
|
33
|
+
Changelog = "https://github.com/davidsunglee/parallax/blob/main/languages/python/docs/releases.md"
|
|
34
|
+
|
|
35
|
+
[build-system]
|
|
36
|
+
requires = ["hatchling"]
|
|
37
|
+
build-backend = "hatchling.build"
|
|
38
|
+
|
|
39
|
+
[tool.hatch.build.targets.wheel]
|
|
40
|
+
packages = ["src/parallax"]
|
|
41
|
+
|
|
42
|
+
[tool.hatch.build.targets.sdist]
|
|
43
|
+
include = ["src/parallax", "README.md", "LICENSE", "NOTICE"]
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import threading
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import TYPE_CHECKING, Protocol, cast
|
|
6
|
+
|
|
7
|
+
import botocore.session
|
|
8
|
+
from botocore.config import Config
|
|
9
|
+
|
|
10
|
+
from parallax.core.db_port import CredentialResolutionError, Password
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from botocore.client import BaseClient
|
|
14
|
+
from botocore.session import Session
|
|
15
|
+
|
|
16
|
+
__all__ = ["RdsIamCredentials"]
|
|
17
|
+
|
|
18
|
+
_TOKEN_REFUSAL = "RDS IAM token could not be generated"
|
|
19
|
+
|
|
20
|
+
# What one credential-chain call may cost before the source gives up. The chain
|
|
21
|
+
# reaches the instance metadata service, an ECS task-role endpoint, STS or SSO,
|
|
22
|
+
# and botocore's own default is sixty seconds a side with five attempts — a wait
|
|
23
|
+
# the seam forbids, because a source runs where nothing above it can interrupt
|
|
24
|
+
# it. The budget is attempts in total, the first one included.
|
|
25
|
+
_CHAIN_CONNECT_TIMEOUT = 2.0
|
|
26
|
+
_CHAIN_READ_TIMEOUT = 2.0
|
|
27
|
+
_CHAIN_ATTEMPTS = 2
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class _RdsTokenClient(Protocol):
|
|
31
|
+
"""The one RDS-client operation this module calls.
|
|
32
|
+
|
|
33
|
+
botocore attaches ``generate_db_auth_token`` to an RDS client when the
|
|
34
|
+
client is created, so it is on no class a type checker can read it off.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def generate_db_auth_token(
|
|
38
|
+
self, DBHostname: str, Port: int, DBUsername: str, Region: str
|
|
39
|
+
) -> str: ...
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _bounded_session() -> Session:
|
|
43
|
+
"""A botocore session whose credential chain bounds its own network calls.
|
|
44
|
+
|
|
45
|
+
The default client configuration reaches every client the session creates,
|
|
46
|
+
the STS and SSO clients the chain builds internally included. The instance
|
|
47
|
+
metadata service is reached through a fetcher rather than a client and takes
|
|
48
|
+
its bounds from the session's own configuration, which is why that pair is
|
|
49
|
+
set beside the client default rather than covered by it.
|
|
50
|
+
"""
|
|
51
|
+
session = botocore.session.get_session()
|
|
52
|
+
session.set_default_client_config(
|
|
53
|
+
Config(
|
|
54
|
+
connect_timeout=_CHAIN_CONNECT_TIMEOUT,
|
|
55
|
+
read_timeout=_CHAIN_READ_TIMEOUT,
|
|
56
|
+
retries={"total_max_attempts": _CHAIN_ATTEMPTS, "mode": "standard"},
|
|
57
|
+
)
|
|
58
|
+
)
|
|
59
|
+
session.set_config_variable("metadata_service_timeout", _CHAIN_CONNECT_TIMEOUT)
|
|
60
|
+
session.set_config_variable("metadata_service_num_attempts", _CHAIN_ATTEMPTS)
|
|
61
|
+
return session
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class _TokenSigner:
|
|
65
|
+
"""The RDS client a source signs on, created once and then kept.
|
|
66
|
+
|
|
67
|
+
Creating a client runs the AWS credential chain, which on EC2 or ECS is a
|
|
68
|
+
metadata-service call, so it cannot happen while configuration is being
|
|
69
|
+
built. botocore clients are thread-safe and botocore sessions are not, so
|
|
70
|
+
the one creation is serialized here and every pool worker afterwards signs
|
|
71
|
+
on the same client — which is also why a chain that blocks blocks all of
|
|
72
|
+
them.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
__slots__ = ("_client", "_lock", "_region", "_session")
|
|
76
|
+
|
|
77
|
+
def __init__(self, session: Session | None, region: str) -> None:
|
|
78
|
+
self._session = session
|
|
79
|
+
self._region = region
|
|
80
|
+
self._lock = threading.Lock()
|
|
81
|
+
self._client: _RdsTokenClient | None = None
|
|
82
|
+
|
|
83
|
+
def client(self) -> _RdsTokenClient:
|
|
84
|
+
with self._lock:
|
|
85
|
+
if self._client is None:
|
|
86
|
+
session = self._session if self._session is not None else _bounded_session()
|
|
87
|
+
created: BaseClient = session.create_client("rds", region_name=self._region)
|
|
88
|
+
self._client = cast("_RdsTokenClient", created)
|
|
89
|
+
return self._client
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass(frozen=True, slots=True)
|
|
93
|
+
class RdsIamCredentials:
|
|
94
|
+
"""Authenticates an RDS or Aurora Postgres login with an IAM token.
|
|
95
|
+
|
|
96
|
+
``host``, ``port`` and ``user`` are what the token is signed for and must be
|
|
97
|
+
what the connection then uses: a token signed for one endpoint proves
|
|
98
|
+
nothing at another, so a cluster reached through its own endpoint is signed
|
|
99
|
+
for that endpoint. ``region`` is the token's credential scope.
|
|
100
|
+
|
|
101
|
+
``session`` is a botocore session to resolve AWS credentials through. Left
|
|
102
|
+
absent, the record builds one whose credential chain bounds its own network
|
|
103
|
+
calls; an injected one is used exactly as given and never reconfigured, so
|
|
104
|
+
its owner keeps whatever bounds it was built with — and chooses the chain,
|
|
105
|
+
which is what a deployment whose profile runs an unbounded
|
|
106
|
+
``credential_process`` helper does instead.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
host: str
|
|
110
|
+
port: int
|
|
111
|
+
user: str
|
|
112
|
+
region: str
|
|
113
|
+
session: Session | None = field(default=None, repr=False)
|
|
114
|
+
_signer: _TokenSigner = field(init=False, repr=False, compare=False)
|
|
115
|
+
|
|
116
|
+
def __post_init__(self) -> None:
|
|
117
|
+
object.__setattr__(self, "_signer", _TokenSigner(self.session, self.region))
|
|
118
|
+
|
|
119
|
+
def resolve(self) -> Password:
|
|
120
|
+
try:
|
|
121
|
+
token = self._signer.client().generate_db_auth_token(
|
|
122
|
+
DBHostname=self.host, Port=self.port, DBUsername=self.user, Region=self.region
|
|
123
|
+
)
|
|
124
|
+
except Exception as exc:
|
|
125
|
+
raise CredentialResolutionError(_TOKEN_REFUSAL) from exc
|
|
126
|
+
return Password(token)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
import psycopg.conninfo
|
|
6
|
+
|
|
7
|
+
from parallax.aws._rds import RdsIamCredentials
|
|
8
|
+
from parallax.postgres import PoolOptions, PostgresEndpoint
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from collections.abc import Mapping
|
|
12
|
+
|
|
13
|
+
from botocore.session import Session
|
|
14
|
+
|
|
15
|
+
from parallax.postgres import OnDemandOptions
|
|
16
|
+
|
|
17
|
+
__all__ = ["rds_endpoint"]
|
|
18
|
+
|
|
19
|
+
# What the token is signed for, plus the secret it stands in for. A connection
|
|
20
|
+
# that used any other value for one of these would present a token signed for
|
|
21
|
+
# something else, so the factory states them and `params` may not restate them.
|
|
22
|
+
_RESERVED = frozenset({"host", "port", "user", "dbname", "password"})
|
|
23
|
+
_WEAKER_THAN_REQUIRE = frozenset({"disable", "allow", "prefer"})
|
|
24
|
+
|
|
25
|
+
_HOST_REFUSAL = (
|
|
26
|
+
"host must name one endpoint; a token is signed for the host it names, not for a list."
|
|
27
|
+
)
|
|
28
|
+
_RESERVED_REFUSAL = (
|
|
29
|
+
"params must not name host, port, user, dbname or password; the factory states those."
|
|
30
|
+
)
|
|
31
|
+
_SSLMODE_REFUSAL = (
|
|
32
|
+
"params must not weaken sslmode below require; an IAM token travels only over TLS."
|
|
33
|
+
)
|
|
34
|
+
# libpq offers GSS encryption in TLS's place rather than beneath it, and takes it
|
|
35
|
+
# whenever it is available unless it is switched off, whatever `sslmode` says.
|
|
36
|
+
_GSSENCMODE_REFUSAL = "params must not enable gssencmode; an IAM token travels only over TLS."
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def rds_endpoint(
|
|
40
|
+
*,
|
|
41
|
+
host: str,
|
|
42
|
+
user: str,
|
|
43
|
+
database: str,
|
|
44
|
+
region: str,
|
|
45
|
+
port: int = 5432,
|
|
46
|
+
params: Mapping[str, str] | None = None,
|
|
47
|
+
session: Session | None = None,
|
|
48
|
+
pool: PoolOptions | OnDemandOptions | None = None,
|
|
49
|
+
) -> PostgresEndpoint:
|
|
50
|
+
"""Configure a Postgres endpoint whose login authenticates with an RDS IAM token.
|
|
51
|
+
|
|
52
|
+
``host`` is the endpoint the application connects through — a cluster's
|
|
53
|
+
writer or reader endpoint for Aurora — and, with ``port`` and ``user``, what
|
|
54
|
+
each token is signed for, so every endpoint signs for its own host. It names
|
|
55
|
+
one endpoint: libpq reads a comma-separated ``host`` as a list to try in
|
|
56
|
+
turn, and a token signed for that whole list authenticates at none of them.
|
|
57
|
+
``region`` is the token's credential scope and ``session`` an optional
|
|
58
|
+
botocore session to resolve AWS credentials through.
|
|
59
|
+
|
|
60
|
+
``params`` carries the rest of libpq's grammar — ``application_name``,
|
|
61
|
+
``options``, ``connect_timeout``, ``sslrootcert`` — so this factory never
|
|
62
|
+
becomes a second spelling of the connection string. It may raise
|
|
63
|
+
``sslmode`` to ``verify-ca`` or ``verify-full``; it may not weaken it, it
|
|
64
|
+
may not ask for the GSS encryption libpq would carry the token over
|
|
65
|
+
instead, and it may not name what the token is bound to. Every refusal is
|
|
66
|
+
fixed text, because ``params`` is a place other secrets live. ``pool`` is
|
|
67
|
+
the endpoint's retention, :class:`~parallax.postgres.PoolOptions`' defaults
|
|
68
|
+
where omitted.
|
|
69
|
+
|
|
70
|
+
Nothing here resolves a token, reaches AWS, or opens anything: the result is
|
|
71
|
+
configuration, exactly as a directly constructed endpoint is.
|
|
72
|
+
"""
|
|
73
|
+
if "," in host:
|
|
74
|
+
raise ValueError(_HOST_REFUSAL)
|
|
75
|
+
requested = dict(params or {})
|
|
76
|
+
if _RESERVED & requested.keys():
|
|
77
|
+
raise ValueError(_RESERVED_REFUSAL)
|
|
78
|
+
if requested.get("sslmode", "require") in _WEAKER_THAN_REQUIRE:
|
|
79
|
+
raise ValueError(_SSLMODE_REFUSAL)
|
|
80
|
+
if requested.get("gssencmode", "disable") != "disable":
|
|
81
|
+
raise ValueError(_GSSENCMODE_REFUSAL)
|
|
82
|
+
conninfo = psycopg.conninfo.make_conninfo(
|
|
83
|
+
host=host,
|
|
84
|
+
port=port,
|
|
85
|
+
user=user,
|
|
86
|
+
dbname=database,
|
|
87
|
+
**{"sslmode": "require", "gssencmode": "disable", **requested},
|
|
88
|
+
)
|
|
89
|
+
return PostgresEndpoint(
|
|
90
|
+
conninfo,
|
|
91
|
+
credentials=RdsIamCredentials(
|
|
92
|
+
host=host, port=port, user=user, region=region, session=session
|
|
93
|
+
),
|
|
94
|
+
pool=PoolOptions() if pool is None else pool,
|
|
95
|
+
)
|
|
File without changes
|