Commit 325c9aa
committed
fix(duckdb): proactively refresh S3 secrets to prevent STS credential expiry
DuckDB's httpfs extension snapshots STS credentials into a secret at
creation time and never re-queries the provider chain, even with
`refresh: auto` configured. When using `CREDENTIAL_CHAIN` with
`chain: sts` (AssumeRole), DuckDB requests the AWS minimum session
duration of 900 seconds (15 minutes). Any SQLMesh plan that takes
longer than 15 minutes to complete — such as large incremental
backfills — fails with HTTP 400 (Bad Request) from S3 once the
token expires.
This was confirmed through controlled experiments (binary search
narrowed the threshold to exactly ~15 minutes, and `duckdb_secrets()`
showed the same `key_id` throughout a run — no refresh ever occurs).
The upstream fix (duckdb/duckdb-httpfs#165) adds credential refresh
at the httpfs layer but has not been merged yet.
This patch adds a timer-based secret refresh mechanism to the DuckDB
engine adapter as a workaround:
- Before each SQL execution, checks if 12 minutes (80% of the 900s
TTL) have elapsed since the last secret creation
- If so, queries `duckdb_secrets()` for existing S3 secret names,
drops them, and recreates from the original config — forcing a
fresh STS AssumeRole call
- Uses double-check locking to prevent concurrent refresh when
`concurrent_tasks > 1`
- Zero overhead for configs without S3 secrets (early return on
null check) and minimal overhead otherwise (monotonic clock
comparison on hot path)
Changes:
- sqlmesh/core/engine_adapter/duckdb.py: Add __init__, _execute
override, and secret refresh methods to DuckDBEngineAdapter
- sqlmesh/core/config/connection.py: Add _extra_engine_config to
DuckDBConnectionConfig to pass secrets config to the adapter
This workaround can be removed once the upstream duckdb-httpfs fix
lands and we upgrade DuckDB.
Refs: duckdb/duckdb-httpfs#165
Refs: duckdb/duckdb-aws#261 parent d8d653f commit 325c9aa
2 files changed
+98
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
561 | 561 | | |
562 | 562 | | |
563 | 563 | | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
564 | 578 | | |
565 | 579 | | |
566 | 580 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
| |||
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
| 29 | + | |
| 30 | + | |
26 | 31 | | |
27 | 32 | | |
28 | 33 | | |
| |||
38 | 43 | | |
39 | 44 | | |
40 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
41 | 125 | | |
42 | 126 | | |
43 | 127 | | |
| |||
0 commit comments