diff --git a/airflow-dbt-transformation/Makefile b/airflow-dbt-transformation/Makefile index 6a1cbe5..b3bfaef 100644 --- a/airflow-dbt-transformation/Makefile +++ b/airflow-dbt-transformation/Makefile @@ -1,4 +1,6 @@ AIRFLOW_CONTAINER = localstack-mwaa-000000000000-us-east-1-my-mwaa-env +# the versions in requirements.txt are pinned to match this Airflow version +AIRFLOW_VERSION = 3.0.6 usage: ## Shows usage for this Makefile @cat Makefile | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' @@ -22,6 +24,7 @@ init: ## Initialize the Airflow environment in LocalStack MWAA awslocal s3 cp requirements.txt s3://snowflake-airflow/ -docker rm -f $(AIRFLOW_CONTAINER) awslocal mwaa create-environment --dag-s3-path /dags \ + --airflow-version $(AIRFLOW_VERSION) \ --requirements-s3-path /requirements.txt \ --execution-role-arn arn:aws:iam::000000000000:role/airflow-role \ --network-configuration {} \ diff --git a/airflow-dbt-transformation/README.md b/airflow-dbt-transformation/README.md index 97bc201..85c178e 100644 --- a/airflow-dbt-transformation/README.md +++ b/airflow-dbt-transformation/README.md @@ -43,6 +43,8 @@ Run the following command to initialize the Airflow environment in LocalStack (t make init ``` +The environment runs Airflow 3.0.6 (`AIRFLOW_VERSION` in the `Makefile`), and `requirements.txt` pins `apache-airflow` and the other packages to versions that match it. Keep the two in sync: without the pins, `pip` upgrades Airflow itself while installing dbt, and the Airflow services then fail to start with `Database migration required`. To use another Airflow version, change `AIRFLOW_VERSION` and take the pins from the [constraints file](https://airflow.apache.org/docs/apache-airflow/stable/installation/installing-from-pypi.html#constraints-files) of that version. + After deploying the Airflow environment, you should be able to request its details, and extract the webserver URL: ``` awslocal mwaa get-environment --name my-mwaa-env diff --git a/airflow-dbt-transformation/requirements.txt b/airflow-dbt-transformation/requirements.txt index b8c42f7..76af33b 100644 --- a/airflow-dbt-transformation/requirements.txt +++ b/airflow-dbt-transformation/requirements.txt @@ -1,4 +1,15 @@ -apache-airflow-providers-snowflake -astronomer-cosmos -dbt-snowflake -snowflake-connector-python +# The environment runs Airflow 3.0.6 on Python 3.12 (`--airflow-version` in the +# Makefile). Pin Airflow and the packages its image already ships, so that +# installing dbt cannot upgrade Airflow itself: an upgraded Airflow no longer +# matches the environment's metadata database and fails to start. +# The first three versions are the ones in the Airflow constraints file: +# https://raw.githubusercontent.com/apache/airflow/constraints-3.0.6/constraints-3.12.txt +# That file cannot be used as `--constraint` here, because dbt needs newer +# protobuf and isodate versions than it allows. +apache-airflow==3.0.6 +apache-airflow-providers-snowflake==6.4.0 +snowflake-connector-python==3.17.2 +astronomer-cosmos==1.16.0 +# the newest dbt releases that work with snowflake-connector-python 3.x +dbt-core==1.10.23 +dbt-snowflake==1.10.5 diff --git a/airflow-dbt-transformation/test.sh b/airflow-dbt-transformation/test.sh index ade83d1..73a2943 100755 --- a/airflow-dbt-transformation/test.sh +++ b/airflow-dbt-transformation/test.sh @@ -25,7 +25,7 @@ poll() { airflow() { docker exec "$CONTAINER" airflow "$@" 2>/dev/null; } diagnostics() { docker inspect "$CONTAINER" --format '{{json .NetworkSettings.Networks}}' | jq -c 'keys' || true - docker exec "$CONTAINER" bash -c 'ls -la /opt/airflow/dags; pip show astronomer-cosmos dbt-snowflake 2>&1 | grep -E "^(Name|Version)"' || true + docker exec "$CONTAINER" bash -c 'ls -la /opt/airflow/dags; pip show apache-airflow astronomer-cosmos dbt-snowflake 2>&1 | grep -E "^(Name|Version)"' || true docker exec "$CONTAINER" airflow dags list-import-errors || true docker exec "$CONTAINER" bash -c 'tail -n 40 $(find /opt/airflow/logs -path "*query_result_data*" -name "*.log" | head -1)' || true docker logs --tail 80 "$CONTAINER" 2>&1 | grep -v SyntaxWarning || true diff --git a/glue-snowflake-integration/README.md b/glue-snowflake-integration/README.md index e3aff7e..8357ed9 100644 --- a/glue-snowflake-integration/README.md +++ b/glue-snowflake-integration/README.md @@ -27,7 +27,7 @@ glue-snowflake-integration/ - Docker with Docker Compose - Terraform, [`tflocal`](https://github.com/localstack/terraform-local) and [`awslocal`](https://github.com/localstack/awscli-local) - [Snowflake CLI](https://docs.snowflake.com/en/developer-guide/snowflake-cli/installation/installation) (`snow`) and `jq`, for `make test` -- Make and `wget` +- Make and `curl` Run `make install` to install `tflocal`, `awslocal` and the Snowflake CLI with pip. diff --git a/glue-snowflake-integration/deploy.sh b/glue-snowflake-integration/deploy.sh index d0ac77a..6776f2b 100755 --- a/glue-snowflake-integration/deploy.sh +++ b/glue-snowflake-integration/deploy.sh @@ -24,8 +24,16 @@ echo "📥 Downloading dependencies..." rm -rf jars && mkdir jars # Glue 4.0 runs Spark 3.3, so use the connector build for Spark 3.3 -wget -q "https://repo1.maven.org/maven2/net/snowflake/snowflake-jdbc/3.20.0/snowflake-jdbc-3.20.0.jar" -P jars -wget -q "https://repo1.maven.org/maven2/net/snowflake/spark-snowflake_2.12/2.16.0-spark_3.3/spark-snowflake_2.12-2.16.0-spark_3.3.jar" -P jars +for url in \ + https://repo1.maven.org/maven2/net/snowflake/snowflake-jdbc/3.20.0/snowflake-jdbc-3.20.0.jar \ + https://repo1.maven.org/maven2/net/snowflake/spark-snowflake_2.12/2.16.0-spark_3.3/spark-snowflake_2.12-2.16.0-spark_3.3.jar; do + # -f fails on an HTTP error, -sS hides the progress bar but prints the error + if ! curl -fsSL --retry 5 --retry-all-errors --retry-delay 3 \ + -o "jars/$(basename "$url")" "$url"; then + echo "❌ Could not download $url" + exit 1 + fi +done # Upload jars to S3 echo "☁️ Uploading JARs to S3..." diff --git a/order-to-cash-cdc/docker-compose.yml b/order-to-cash-cdc/docker-compose.yml index 6e56fec..cffbddf 100644 --- a/order-to-cash-cdc/docker-compose.yml +++ b/order-to-cash-cdc/docker-compose.yml @@ -30,8 +30,11 @@ services: KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093 KAFKA_LISTENERS: INTERNAL://:29092,EXTERNAL://:19092,CONTROLLER://:9093 # INTERNAL is for Kafka Connect on the compose network, EXTERNAL for the - # producer and tests on the host. - KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:29092,EXTERNAL://localhost:${KAFKA_PORT:-19092} + # producer and tests on the host. EXTERNAL is advertised as 127.0.0.1, + # not localhost: the port is published on 127.0.0.1 only, and where + # localhost also resolves to ::1 librdkafka tries the IPv6 address, gets + # connection refused and can fail the request. + KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:29092,EXTERNAL://127.0.0.1:${KAFKA_PORT:-19092} KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,CONTROLLER:PLAINTEXT KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER diff --git a/order-to-cash-cdc/pipeline/settings.py b/order-to-cash-cdc/pipeline/settings.py index 1eb5312..0bcdfcb 100644 --- a/order-to-cash-cdc/pipeline/settings.py +++ b/order-to-cash-cdc/pipeline/settings.py @@ -66,7 +66,8 @@ def load() -> Settings: "warehouse": _env("SNOWFLAKE_WAREHOUSE", "O2C_WH"), "database": _env("O2C_DATABASE", "O2C"), "connector_role": _env("O2C_CONNECTOR_ROLE", "O2C_INGEST"), - "kafka_bootstrap": f"localhost:{_env('KAFKA_PORT', '19092')}", + # 127.0.0.1, as published and advertised by docker-compose.yml (not localhost). + "kafka_bootstrap": f"127.0.0.1:{_env('KAFKA_PORT', '19092')}", "connect_url": f"http://127.0.0.1:{_env('CONNECT_PORT', '18083')}", } diff --git a/order-to-cash-cdc/producer/produce.py b/order-to-cash-cdc/producer/produce.py index d0117ee..cea6c26 100644 --- a/order-to-cash-cdc/producer/produce.py +++ b/order-to-cash-cdc/producer/produce.py @@ -344,7 +344,7 @@ def main() -> None: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--topic", default="orders.cdc") parser.add_argument("--seed", type=int, default=42) - parser.add_argument("--bootstrap", default=f"localhost:{os.getenv('KAFKA_PORT', '19092')}") + parser.add_argument("--bootstrap", default=f"127.0.0.1:{os.getenv('KAFKA_PORT', '19092')}") args = parser.parse_args() scenario = initial_scenario(args.seed) count = publish(scenario.events, args.topic, args.bootstrap) diff --git a/tenantboard/app/provisioning.py b/tenantboard/app/provisioning.py index c346b9d..aaf3e30 100644 --- a/tenantboard/app/provisioning.py +++ b/tenantboard/app/provisioning.py @@ -193,7 +193,15 @@ def clone_database(conn: SnowflakeConnection, source: str, target: str) -> None: fqtn = f"{target}.{SCHEMA}.{table}" if ref.kind == "ROW_ACCESS_POLICY": cols = ", ".join(_identifier(c) for c in ref.arg_columns) - statements.append(f"ALTER TABLE {fqtn} ADD ROW ACCESS POLICY {policy} ON ({cols})") + add = f"ADD ROW ACCESS POLICY {policy} ON ({cols})" + # A table carries at most one row access policy. If the clone still + # references the source's, swap it in one statement so the table is + # never unprotected. + stale = next((h for h in have if h.kind == ref.kind), None) + if stale: + old = ".".join(_identifier(part) for part in (stale.database, stale.schema, stale.name)) + add = f"DROP ROW ACCESS POLICY {old}, {add}" + statements.append(f"ALTER TABLE {fqtn} {add}") elif ref.kind == "MASKING_POLICY" and ref.column: column = _identifier(ref.column) if any(h.kind == ref.kind and h.column == ref.column for h in have): diff --git a/tenantboard/init/01_platform.sf.sql b/tenantboard/init/01_platform.sf.sql index 131944c..1f1c17d 100644 --- a/tenantboard/init/01_platform.sf.sql +++ b/tenantboard/init/01_platform.sf.sql @@ -29,6 +29,10 @@ GRANT ROLE PLATFORM_ADMIN TO ROLE SYSADMIN; CREATE USER IF NOT EXISTS TENANTBOARD_SVC DEFAULT_WAREHOUSE = TB_WH COMMENT = 'Tenantboard API service user (key-pair auth)'; +-- Snowflake activates all of a user's granted roles as secondary roles by +-- default. This user holds every tenant's roles, so each session must run +-- with its primary role only, or IS_ROLE_IN_SESSION is true for every tenant. +ALTER USER TENANTBOARD_SVC SET DEFAULT_SECONDARY_ROLES = (); GRANT ROLE PLATFORM_ADMIN TO USER TENANTBOARD_SVC; GRANT USAGE ON WAREHOUSE TB_WH TO ROLE TENANT_READER; GRANT USAGE ON WAREHOUSE TB_WH TO ROLE PLATFORM_ADMIN;