Skip to content

Commit 90184a4

Browse files
wenermeburmecia
andauthored
feat: allow vault secret lookup by name in _id options (supabase#591)
* feat: allow vault secret lookup by name in `_id` options Change `get_vault_secret()` to fall back to name-based lookup when the input is not a valid UUID. This allows all FDW `*_id` options (e.g. `conn_string_id`, `api_key_id`, `bearer_token_id`) to accept either a vault secret UUID or a human-readable name, making configuration easier: ```sql -- Before: only UUID worked OPTIONS (conn_string_id 'a1b2c3d4-e5f6-...') -- Now: name also works OPTIONS (conn_string_id 'my_mysql_prod') ``` Since vault secret names are human-readable labels that cannot be valid UUIDs, there is no ambiguity. Existing UUID-based usage is completely unaffected. * feat: update get_vault_secret to accept secret name or ID --------- Co-authored-by: Bo Lu <[email protected]>
1 parent 2baf2d2 commit 90184a4

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

supabase-wrappers/src/utils.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,19 @@ pub fn create_async_runtime() -> Result<Runtime, CreateRuntimeError> {
355355
Ok(Builder::new_current_thread().enable_all().build()?)
356356
}
357357

358-
/// Get decrypted secret from Vault by secret ID
358+
/// Get decrypted secret from Vault by secret ID or name
359359
///
360-
/// Get decrypted secret as string from Vault by secret ID. Vault is an extension for storing
361-
/// encrypted secrets, [see more details](https://github.com/supabase/vault).
362-
pub fn get_vault_secret(secret_id: &str) -> Option<String> {
363-
match Uuid::try_parse(secret_id) {
360+
/// If the value is a valid UUID, look up by `id` or `key_id`.
361+
/// Otherwise, fall back to lookup by `name`, since vault secret names
362+
/// cannot be valid UUIDs (they are human-readable labels).
363+
///
364+
/// This allows all FDW `*_id` options (e.g. `conn_string_id`, `api_key_id`)
365+
/// to accept either a UUID or a vault secret name.
366+
///
367+
/// Vault is an extension for storing encrypted secrets,
368+
/// [see more details](https://github.com/supabase/vault).
369+
pub fn get_vault_secret(secret_id_or_name: &str) -> Option<String> {
370+
match Uuid::try_parse(secret_id_or_name) {
364371
Ok(sid) => {
365372
let sid = sid.into_bytes();
366373
match Spi::get_one_with_args::<String>(
@@ -371,19 +378,13 @@ pub fn get_vault_secret(secret_id: &str) -> Option<String> {
371378
Err(err) => {
372379
report_error(
373380
PgSqlErrorCode::ERRCODE_FDW_ERROR,
374-
&format!("query vault failed \"{secret_id}\": {err}"),
381+
&format!("query vault failed \"{secret_id_or_name}\": {err}"),
375382
);
376383
None
377384
}
378385
}
379386
}
380-
Err(err) => {
381-
report_error(
382-
PgSqlErrorCode::ERRCODE_FDW_ERROR,
383-
&format!("invalid secret id \"{secret_id}\": {err}"),
384-
);
385-
None
386-
}
387+
Err(_) => get_vault_secret_by_name(secret_id_or_name),
387388
}
388389
}
389390

0 commit comments

Comments
 (0)