|
20 | 20 | StructType, |
21 | 21 | ) |
22 | 22 |
|
| 23 | +try: |
| 24 | + from pyspark.dbutils import DBUtils |
| 25 | +except ImportError: # local syntax checks and non-Databricks execution |
| 26 | + DBUtils = None # type: ignore[assignment] |
| 27 | + |
23 | 28 |
|
24 | 29 | spark = SparkSession.builder.getOrCreate() |
25 | 30 |
|
@@ -49,6 +54,33 @@ def opt(value: Optional[str], env_name: str, default: Optional[str] = None) -> s |
49 | 54 | return env(env_name, default) |
50 | 55 |
|
51 | 56 |
|
| 57 | +def resolve_deploy_key( |
| 58 | + *, |
| 59 | + direct_value: Optional[str], |
| 60 | + secret_scope: Optional[str], |
| 61 | + secret_key: Optional[str], |
| 62 | +) -> str: |
| 63 | + if direct_value is not None: |
| 64 | + return direct_value |
| 65 | + |
| 66 | + env_value = os.getenv("CONVEX_DEPLOY_KEY") |
| 67 | + if env_value: |
| 68 | + return env_value |
| 69 | + |
| 70 | + scope = secret_scope or os.getenv("CONVEX_DEPLOY_KEY_SECRET_SCOPE") |
| 71 | + key = secret_key or os.getenv("CONVEX_DEPLOY_KEY_SECRET_KEY") |
| 72 | + if not scope or not key: |
| 73 | + raise RuntimeError( |
| 74 | + "missing Convex deploy key: provide --deploy-key, CONVEX_DEPLOY_KEY, " |
| 75 | + "or both deploy-key secret scope/key settings" |
| 76 | + ) |
| 77 | + |
| 78 | + if DBUtils is None: |
| 79 | + raise RuntimeError("pyspark.dbutils.DBUtils is unavailable outside Databricks runtime") |
| 80 | + |
| 81 | + return DBUtils(spark).secrets.get(scope=scope, key=key) |
| 82 | + |
| 83 | + |
52 | 84 | @dataclass |
53 | 85 | class Checkpoint: |
54 | 86 | phase: str |
@@ -374,6 +406,8 @@ def parse_args() -> argparse.Namespace: |
374 | 406 | parser = argparse.ArgumentParser(description="Convex CDC Databricks extractor") |
375 | 407 | parser.add_argument("--deployment-url") |
376 | 408 | parser.add_argument("--deploy-key") |
| 409 | + parser.add_argument("--deploy-key-secret-scope") |
| 410 | + parser.add_argument("--deploy-key-secret-key") |
377 | 411 | parser.add_argument("--source-id") |
378 | 412 | parser.add_argument("--table-name") |
379 | 413 | parser.add_argument("--catalog") |
@@ -441,7 +475,11 @@ def run_once() -> None: |
441 | 475 | args = parse_args() |
442 | 476 |
|
443 | 477 | deployment_url = opt(args.deployment_url, "CONVEX_DEPLOYMENT_URL") |
444 | | - deploy_key = opt(args.deploy_key, "CONVEX_DEPLOY_KEY") |
| 478 | + deploy_key = resolve_deploy_key( |
| 479 | + direct_value=args.deploy_key, |
| 480 | + secret_scope=args.deploy_key_secret_scope, |
| 481 | + secret_key=args.deploy_key_secret_key, |
| 482 | + ) |
445 | 483 | source_id = opt(args.source_id, "CONVEX_SOURCE_ID", deployment_url) |
446 | 484 | table_name = args.table_name if args.table_name is not None else os.getenv("CONVEX_TABLE_NAME") |
447 | 485 |
|
|
0 commit comments