vibedata-dlt-redshift-mwaa 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.
- vibedata_dlt_redshift_mwaa-0.1.0/.gitignore +8 -0
- vibedata_dlt_redshift_mwaa-0.1.0/CHANGELOG.md +7 -0
- vibedata_dlt_redshift_mwaa-0.1.0/PKG-INFO +33 -0
- vibedata_dlt_redshift_mwaa-0.1.0/README.md +17 -0
- vibedata_dlt_redshift_mwaa-0.1.0/pyproject.toml +32 -0
- vibedata_dlt_redshift_mwaa-0.1.0/src/vibedata/dlt/redshift/__init__.py +20 -0
- vibedata_dlt_redshift_mwaa-0.1.0/src/vibedata/dlt/redshift/bootstrap.py +66 -0
- vibedata_dlt_redshift_mwaa-0.1.0/tests/test_bootstrap.py +62 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
The public API is what the package documents in its README and exports from its top-level module; everything else is internal. While the version is 0.x, a breaking change bumps the minor version.
|
|
4
|
+
|
|
5
|
+
## 0.1.0
|
|
6
|
+
|
|
7
|
+
- First release: `vibedata.dlt.redshift.setup_environment()` mints the database login with Redshift Serverless `GetCredentials` as the task's execution role, from the host and database dlt resolves; `finalize()` from `vibedata-dlt-redshift-core`.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: vibedata-dlt-redshift-mwaa
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: VibeData dlt runtime — Amazon Redshift MWAA Serverless execution unit (a workflow task's dlt load): the database login minted as the task's own execution role.
|
|
5
|
+
Author-email: Vibedata <eng@acceleratedata.ai>
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Requires-Dist: boto3>=1.34.0
|
|
9
|
+
Requires-Dist: dlt<1.32,>=1.30
|
|
10
|
+
Requires-Dist: vibedata-dlt-core[aws]>=0.13.0
|
|
11
|
+
Requires-Dist: vibedata-dlt-redshift-core>=0.1.0
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest-mock>=3.12; extra == 'dev'
|
|
14
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# vibedata-dlt-redshift-mwaa
|
|
18
|
+
|
|
19
|
+
The Redshift execution unit for a dlt pipeline run by an Amazon MWAA Serverless workflow task. Install it in place of `vibedata-dlt-redshift-studio`; the pipeline file is the same:
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from vibedata.dlt.core.providers.aws_secrets_manager import register_aws_secrets_manager_provider
|
|
23
|
+
from vibedata.dlt.redshift import setup_environment, finalize
|
|
24
|
+
|
|
25
|
+
setup_environment() # login from GetCredentials, as the task's execution role
|
|
26
|
+
register_aws_secrets_manager_provider() # location from [providers.aws_secrets_manager] in .dlt/config.toml
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Keep that order. Under the Studio unit, `setup_environment()` registers the provider on Studio's `vd-secrets` profile and the second call is then a no-op; called the other way round, Studio refuses the profile-less registration.
|
|
30
|
+
|
|
31
|
+
`setup_environment()` reads `destination.redshift.credentials.host` and `.database` through dlt, so the committed `config.toml` answers in production and a `DESTINATION__REDSHIFT__CREDENTIALS__*` override answers in a sandbox run. The host must be a Serverless endpoint (a provisioned cluster, or credentials given as one connection string, is refused before any call); the execution role needs `redshift-serverless:GetCredentials` on the workgroup.
|
|
32
|
+
|
|
33
|
+
The login is valid for one hour from `setup_environment()`. A load still running past that fails on its next new connection; split it into separate dlt commands, each of which mints its own.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# vibedata-dlt-redshift-mwaa
|
|
2
|
+
|
|
3
|
+
The Redshift execution unit for a dlt pipeline run by an Amazon MWAA Serverless workflow task. Install it in place of `vibedata-dlt-redshift-studio`; the pipeline file is the same:
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from vibedata.dlt.core.providers.aws_secrets_manager import register_aws_secrets_manager_provider
|
|
7
|
+
from vibedata.dlt.redshift import setup_environment, finalize
|
|
8
|
+
|
|
9
|
+
setup_environment() # login from GetCredentials, as the task's execution role
|
|
10
|
+
register_aws_secrets_manager_provider() # location from [providers.aws_secrets_manager] in .dlt/config.toml
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Keep that order. Under the Studio unit, `setup_environment()` registers the provider on Studio's `vd-secrets` profile and the second call is then a no-op; called the other way round, Studio refuses the profile-less registration.
|
|
14
|
+
|
|
15
|
+
`setup_environment()` reads `destination.redshift.credentials.host` and `.database` through dlt, so the committed `config.toml` answers in production and a `DESTINATION__REDSHIFT__CREDENTIALS__*` override answers in a sandbox run. The host must be a Serverless endpoint (a provisioned cluster, or credentials given as one connection string, is refused before any call); the execution role needs `redshift-serverless:GetCredentials` on the workgroup.
|
|
16
|
+
|
|
17
|
+
The login is valid for one hour from `setup_environment()`. A load still running past that fails on its next new connection; split it into separate dlt commands, each of which mints its own.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "vibedata-dlt-redshift-mwaa"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "VibeData dlt runtime — Amazon Redshift MWAA Serverless execution unit (a workflow task's dlt load): the database login minted as the task's own execution role."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Vibedata", email = "eng@acceleratedata.ai" }]
|
|
13
|
+
|
|
14
|
+
dependencies = [
|
|
15
|
+
"vibedata-dlt-redshift-core>=0.1.0",
|
|
16
|
+
# `[aws]`: the pipeline registers the core AWS Secrets Manager provider (0.13.0).
|
|
17
|
+
"vibedata-dlt-core[aws]>=0.13.0",
|
|
18
|
+
# Imported directly: the login is a boto3 `GetCredentials` call, and host and
|
|
19
|
+
# database are read through dlt's config. Same range as vibedata-dlt-redshift-core.
|
|
20
|
+
"boto3>=1.34.0",
|
|
21
|
+
"dlt>=1.30,<1.32",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
dev = ["pytest>=8.0", "pytest-mock>=3.12"]
|
|
26
|
+
|
|
27
|
+
[tool.hatch.build.targets.wheel]
|
|
28
|
+
packages = ["src/vibedata"]
|
|
29
|
+
|
|
30
|
+
[tool.pytest.ini_options]
|
|
31
|
+
testpaths = ["tests"]
|
|
32
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""VibeData dlt runtime — Amazon Redshift MWAA Serverless execution unit.
|
|
2
|
+
|
|
3
|
+
The installed unit for Redshift pipelines run by an MWAA Serverless workflow task.
|
|
4
|
+
Same facade as the Studio unit, so a pipeline file runs unchanged in both:
|
|
5
|
+
|
|
6
|
+
from vibedata.dlt.redshift import setup_environment, finalize
|
|
7
|
+
|
|
8
|
+
`setup_environment` mints the database login as the task's own execution role
|
|
9
|
+
(see `bootstrap`).
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from vibedata.dlt.redshift_core import finalize
|
|
13
|
+
|
|
14
|
+
from .bootstrap import RedshiftCredentialError, setup_environment
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"setup_environment",
|
|
18
|
+
"finalize",
|
|
19
|
+
"RedshiftCredentialError",
|
|
20
|
+
]
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""The database login for a Redshift Serverless load, minted as the task's own AWS identity.
|
|
2
|
+
|
|
3
|
+
dlt's `redshift` destination signs in with a user and password. Redshift
|
|
4
|
+
Serverless issues both through `GetCredentials`: a temporary password for the
|
|
5
|
+
role-derived user (`IAMR:<role>`), valid for one database and at most an hour,
|
|
6
|
+
so one dlt command is bounded by it. The workgroup and region are the first and
|
|
7
|
+
third labels of the Serverless endpoint
|
|
8
|
+
(`<workgroup>.<account>.<region>.redshift-serverless.amazonaws.com`); host and
|
|
9
|
+
database are read through dlt's own config, so the committed `config.toml`
|
|
10
|
+
answers for production and a run's `DESTINATION__REDSHIFT__CREDENTIALS__*`
|
|
11
|
+
override answers for a sandbox run.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
import os
|
|
18
|
+
|
|
19
|
+
import dlt
|
|
20
|
+
|
|
21
|
+
from vibedata.dlt.redshift_core import apply_schema_check
|
|
22
|
+
|
|
23
|
+
logger = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
DLT_USERNAME_ENV = "DESTINATION__REDSHIFT__CREDENTIALS__USERNAME"
|
|
26
|
+
DLT_PASSWORD_ENV = "DESTINATION__REDSHIFT__CREDENTIALS__PASSWORD"
|
|
27
|
+
SERVERLESS_SUFFIX = ".redshift-serverless.amazonaws.com"
|
|
28
|
+
CREDENTIAL_SECONDS = 3600
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class RedshiftCredentialError(RuntimeError):
|
|
32
|
+
"""Raised when the run cannot mint a database login."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def setup_environment() -> None:
|
|
36
|
+
"""Mint the login under dlt's own config names, then narrow dlt's schema check."""
|
|
37
|
+
username, password = mint_credential()
|
|
38
|
+
os.environ[DLT_USERNAME_ENV] = username
|
|
39
|
+
os.environ[DLT_PASSWORD_ENV] = password
|
|
40
|
+
apply_schema_check()
|
|
41
|
+
logger.info("VibeData dlt environment ready (unit=mwaa)")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def mint_credential() -> tuple[str, str]:
|
|
45
|
+
import boto3
|
|
46
|
+
from botocore.exceptions import BotoCoreError, ClientError
|
|
47
|
+
|
|
48
|
+
host = str(dlt.config.get("destination.redshift.credentials.host") or "").strip().lower()
|
|
49
|
+
database = str(dlt.config.get("destination.redshift.credentials.database") or "").strip()
|
|
50
|
+
labels = host.split(".")
|
|
51
|
+
if not host.endswith(SERVERLESS_SUFFIX) or len(labels) != 6 or not database:
|
|
52
|
+
raise RedshiftCredentialError(
|
|
53
|
+
"destination.redshift.credentials needs a Redshift Serverless host "
|
|
54
|
+
f"(<workgroup>.<account>.<region>{SERVERLESS_SUFFIX}) and a database; "
|
|
55
|
+
f"got host {host!r}, database {database!r}."
|
|
56
|
+
)
|
|
57
|
+
workgroup, _, region = labels[:3]
|
|
58
|
+
try:
|
|
59
|
+
answer = boto3.client("redshift-serverless", region_name=region).get_credentials(
|
|
60
|
+
workgroupName=workgroup, dbName=database, durationSeconds=CREDENTIAL_SECONDS
|
|
61
|
+
)
|
|
62
|
+
except (BotoCoreError, ClientError) as exc:
|
|
63
|
+
raise RedshiftCredentialError(
|
|
64
|
+
f"Redshift Serverless refused a login for database {database!r} on workgroup {workgroup!r}: {exc}"
|
|
65
|
+
) from exc
|
|
66
|
+
return answer["dbUser"], answer["dbPassword"]
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""The MWAA unit's login: GetCredentials for the workgroup and database dlt resolves."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
import boto3
|
|
8
|
+
import pytest
|
|
9
|
+
from botocore.stub import Stubber
|
|
10
|
+
|
|
11
|
+
from vibedata.dlt.redshift import bootstrap
|
|
12
|
+
from vibedata.dlt.redshift.bootstrap import RedshiftCredentialError, setup_environment
|
|
13
|
+
|
|
14
|
+
HOST = "vd-wg.123456789012.eu-north-1.redshift-serverless.amazonaws.com"
|
|
15
|
+
EXPECTED = {"workgroupName": "vd-wg", "dbName": "sales", "durationSeconds": 3600}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@pytest.fixture(autouse=True)
|
|
19
|
+
def env(monkeypatch):
|
|
20
|
+
for var in list(os.environ):
|
|
21
|
+
if var.startswith("DESTINATION__REDSHIFT__") or var.startswith("AWS_"):
|
|
22
|
+
monkeypatch.delenv(var, raising=False)
|
|
23
|
+
monkeypatch.setenv("DESTINATION__REDSHIFT__CREDENTIALS__HOST", HOST)
|
|
24
|
+
monkeypatch.setenv("DESTINATION__REDSHIFT__CREDENTIALS__DATABASE", "sales")
|
|
25
|
+
monkeypatch.setattr(bootstrap, "apply_schema_check", lambda: True)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@pytest.fixture
|
|
29
|
+
def serverless(monkeypatch):
|
|
30
|
+
"""One stubbed client; records which service and region were asked for."""
|
|
31
|
+
client = boto3.session.Session().client(
|
|
32
|
+
"redshift-serverless", region_name="eu-north-1", aws_access_key_id="x", aws_secret_access_key="y"
|
|
33
|
+
)
|
|
34
|
+
calls = []
|
|
35
|
+
monkeypatch.setattr(boto3, "client", lambda service, region_name: calls.append((service, region_name)) or client)
|
|
36
|
+
stubber = Stubber(client)
|
|
37
|
+
stubber.activate()
|
|
38
|
+
return stubber, calls
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_the_login_is_minted_for_the_resolved_workgroup_and_database(serverless):
|
|
42
|
+
stubber, calls = serverless
|
|
43
|
+
stubber.add_response("get_credentials", {"dbUser": "IAMR:wf-role", "dbPassword": "p"}, EXPECTED)
|
|
44
|
+
setup_environment()
|
|
45
|
+
assert calls == [("redshift-serverless", "eu-north-1")]
|
|
46
|
+
assert os.environ["DESTINATION__REDSHIFT__CREDENTIALS__USERNAME"] == "IAMR:wf-role"
|
|
47
|
+
assert os.environ["DESTINATION__REDSHIFT__CREDENTIALS__PASSWORD"] == "p"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_a_refused_login_names_the_database_and_workgroup(serverless):
|
|
51
|
+
stubber, _ = serverless
|
|
52
|
+
stubber.add_client_error("get_credentials", service_error_code="AccessDeniedException", expected_params=EXPECTED)
|
|
53
|
+
with pytest.raises(RedshiftCredentialError, match="'sales' on workgroup 'vd-wg'"):
|
|
54
|
+
setup_environment()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@pytest.mark.parametrize("host", ["cluster.abc.eu-north-1.redshift.amazonaws.com", ""])
|
|
58
|
+
def test_a_host_that_is_not_serverless_is_refused_before_any_call(monkeypatch, serverless, host):
|
|
59
|
+
monkeypatch.setenv("DESTINATION__REDSHIFT__CREDENTIALS__HOST", host)
|
|
60
|
+
with pytest.raises(RedshiftCredentialError, match="Serverless host"):
|
|
61
|
+
setup_environment()
|
|
62
|
+
assert serverless[1] == []
|