-
Notifications
You must be signed in to change notification settings - Fork 214
refactor: replace opaque CI_PROFILES_YML secret with committed template + envsubst #2123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ac63b89
refactor: replace opaque CI_PROFILES_YML secret with committed templa…
devin-ai-integration[bot] c2a16d4
fix: guard secret-dependent steps for docker targets (postgres, click…
devin-ai-integration[bot] cf1f222
refactor: replace shell-based profiles generation with Python CLI scr…
devin-ai-integration[bot] 451ebb3
refactor: use Jinja for-loop for elementary profile, remove legacy fa…
devin-ai-integration[bot] 01ddcd5
feat: use StrictUndefined when secrets are present to catch typos
devin-ai-integration[bot] b0cc1f4
fix: add isinstance(decoded, dict) validation for secrets JSON
devin-ai-integration[bot] a051188
fix: add auth_type:oauth for databricks, catch binascii.Error
devin-ai-integration[bot] a92a5b4
fix: revert docker/cloud split in elementary — run all targets under …
devin-ai-integration[bot] 22c5849
fix: add missing bigquery profile fields (location, priority)
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #!/usr/bin/env python3 | ||
| """Generate ~/.dbt/profiles.yml from a Jinja2 template and an optional secrets JSON.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| import binascii | ||
| import json | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import click | ||
| import yaml | ||
| from jinja2 import BaseLoader, Environment, StrictUndefined, Undefined | ||
|
|
||
|
|
||
| class _NullUndefined(Undefined): | ||
| """Render missing variables as empty strings so docker-only runs don't crash.""" | ||
|
|
||
| def __str__(self) -> str: | ||
| return "" | ||
|
|
||
| def __iter__(self): | ||
| return iter([]) | ||
|
|
||
| def __bool__(self) -> bool: | ||
| return False | ||
|
|
||
|
|
||
| def _yaml_inline(value: Any) -> str: | ||
| """Dump *value* as a compact inline YAML scalar / mapping.""" | ||
| if isinstance(value, Undefined): | ||
| return "{}" | ||
| return yaml.dump(value, default_flow_style=True).strip() | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.option( | ||
| "--template", | ||
| required=True, | ||
| type=click.Path(exists=True, dir_okay=False, path_type=Path), | ||
| help="Path to the Jinja2 profiles template (e.g. profiles.yml.j2).", | ||
| ) | ||
| @click.option( | ||
| "--output", | ||
| required=True, | ||
| type=click.Path(dir_okay=False, path_type=Path), | ||
| help="Destination path for the rendered profiles.yml.", | ||
| ) | ||
| @click.option( | ||
| "--schema-name", | ||
| required=True, | ||
| help="Base schema name (e.g. dbt_pkg_<ref> or py_<ref>).", | ||
| ) | ||
| @click.option( | ||
| "--secrets-json-env", | ||
| default="CI_WAREHOUSE_SECRETS", | ||
| show_default=True, | ||
| help="Name of the env-var holding the base64-encoded JSON secrets blob.", | ||
| ) | ||
| def main( | ||
| template: Path, | ||
| output: Path, | ||
| schema_name: str, | ||
| secrets_json_env: str, | ||
| ) -> None: | ||
| """Render a Jinja2 profiles template into a dbt profiles.yml file. | ||
|
|
||
| Resolution order: | ||
| 1. If the env-var named by ``--secrets-json-env`` is set, decode it and | ||
| use its key/value pairs (plus *schema_name*) as template variables. | ||
| 2. Otherwise render the template with only *schema_name* populated (all | ||
| other variables resolve to empty strings — suitable for docker-only | ||
| targets on fork PRs). | ||
| """ | ||
| output.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| secrets_b64 = os.environ.get(secrets_json_env, "").strip() | ||
|
|
||
| # ── Build template context ────────────────────────────────────────── | ||
| context: dict[str, object] = {"schema_name": schema_name} | ||
|
|
||
| if secrets_b64: | ||
| try: | ||
| decoded: dict = json.loads(base64.b64decode(secrets_b64)) | ||
| except (binascii.Error, json.JSONDecodeError) as e: | ||
| raise click.ClickException( | ||
| f"Failed to decode ${secrets_json_env}: {e}" | ||
| ) from e | ||
| if not isinstance(decoded, dict): | ||
| raise click.ClickException( | ||
| f"Expected JSON object for ${secrets_json_env}, " | ||
| f"got {type(decoded).__name__}" | ||
| ) | ||
| for key, value in decoded.items(): | ||
| context[key.lower()] = value | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| click.echo( | ||
| f"Loaded {len(decoded)} secret(s) from ${secrets_json_env}.", | ||
| err=True, | ||
| ) | ||
| else: | ||
| click.echo( | ||
| "No secrets found — rendering template for docker-only targets.", | ||
| err=True, | ||
| ) | ||
|
|
||
| # ── Render ────────────────────────────────────────────────────────── | ||
| # When secrets are loaded, use StrictUndefined so typos in secret keys | ||
| # fail fast. For docker-only runs (no secrets) use _NullUndefined so | ||
| # cloud placeholders silently resolve to empty strings. | ||
| undefined_cls = StrictUndefined if secrets_b64 else _NullUndefined | ||
| env = Environment( | ||
| loader=BaseLoader(), | ||
| undefined=undefined_cls, | ||
| keep_trailing_newline=True, | ||
| ) | ||
| env.filters["toyaml"] = _yaml_inline | ||
| tmpl = env.from_string(template.read_text()) | ||
| rendered = tmpl.render(**context) | ||
| output.write_text(rendered) | ||
| click.echo(f"Wrote {output}", err=True) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| elementary_tests: | ||
| target: postgres | ||
| outputs: | ||
|
|
||
| # ── Docker targets (plaintext, no secrets needed) ────────────────── | ||
|
|
||
| postgres: &postgres | ||
| type: postgres | ||
| host: 127.0.0.1 | ||
| port: 5432 | ||
| user: admin | ||
| password: admin | ||
| dbname: postgres | ||
| schema: {{ schema_name }} | ||
| threads: 32 | ||
|
|
||
| clickhouse: &clickhouse | ||
| type: clickhouse | ||
| host: localhost | ||
| port: 8123 | ||
| user: default | ||
| password: default | ||
| schema: {{ schema_name }} | ||
| threads: 4 | ||
|
|
||
| # ── Cloud targets (secrets substituted at CI time) ───────────────── | ||
|
|
||
| snowflake: &snowflake | ||
| type: snowflake | ||
| account: {{ snowflake_account }} | ||
| user: {{ snowflake_user }} | ||
| password: {{ snowflake_password }} | ||
| role: {{ snowflake_role }} | ||
| database: {{ snowflake_database }} | ||
| warehouse: {{ snowflake_warehouse }} | ||
| schema: {{ schema_name }} | ||
| threads: 4 | ||
|
|
||
| bigquery: &bigquery | ||
| type: bigquery | ||
| method: service-account-json | ||
| project: {{ bigquery_project }} | ||
| dataset: {{ schema_name }} | ||
| keyfile_json: {{ bigquery_keyfile | toyaml }} | ||
| location: US | ||
| priority: interactive | ||
| threads: 4 | ||
|
|
||
| redshift: &redshift | ||
| type: redshift | ||
| host: {{ redshift_host }} | ||
| user: {{ redshift_user }} | ||
| password: {{ redshift_password }} | ||
| port: {{ redshift_port }} | ||
| dbname: {{ redshift_dbname }} | ||
| schema: {{ schema_name }} | ||
| threads: 4 | ||
|
|
||
| databricks_catalog: &databricks_catalog | ||
| type: databricks | ||
| host: {{ databricks_host }} | ||
| http_path: {{ databricks_http_path }} | ||
| catalog: {{ databricks_catalog }} | ||
| schema: {{ schema_name }} | ||
| auth_type: oauth | ||
| client_id: {{ databricks_client_id }} | ||
| client_secret: {{ databricks_client_secret }} | ||
| threads: 4 | ||
|
|
||
| athena: &athena | ||
| type: athena | ||
| s3_staging_dir: {{ athena_s3_staging_dir }} | ||
| s3_data_dir: {{ athena_s3_data_dir }} | ||
| region_name: {{ athena_region }} | ||
| database: awsdatacatalog | ||
| schema: {{ schema_name }} | ||
| aws_access_key_id: {{ athena_aws_access_key_id }} | ||
| aws_secret_access_key: {{ athena_aws_secret_access_key }} | ||
| threads: 4 | ||
|
|
||
| # The internal CLI dbt_project uses profile "elementary", so we alias the | ||
| # same targets but override the schema to <base>_elementary. | ||
| elementary: | ||
| target: postgres | ||
| outputs: | ||
| {%- set targets = ['postgres', 'clickhouse', 'snowflake', 'bigquery', 'redshift', 'databricks_catalog', 'athena'] %} | ||
| {%- for t in targets %} | ||
| {{ t }}: | ||
| <<: *{{ t }} | ||
| {{ 'dataset' if t == 'bigquery' else 'schema' }}: {{ schema_name }}_elementary | ||
| {%- endfor %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.