Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ tracing-subscriber = { workspace = true }
base64 = { workspace = true }
rcgen = { workspace = true }
rustls = { workspace = true }
# The connection string that `encryption-key reset` and `set-data-connection`
# read from standard input, wiped when it drops.
zeroize = { workspace = true }

# Daemon mode (double-fork, PID files, SIGTERM) is POSIX-only; on Windows the
# server runs with `serve --foreground` under an external supervisor.
Expand All @@ -50,3 +53,6 @@ libc = { workspace = true }

[dev-dependencies]
async-trait = { workspace = true }
# The encryption-key commands' tests run on a SQLite file catalog.
extenddb-storage-sqlite = { path = "../storage-sqlite" }
tempfile = "3"
1 change: 1 addition & 0 deletions crates/app/src/cmd_backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ async fn open_catalog(
options.beside_server = mode == Mode::Read;
options.startup_recovery = false;
options.command = Some(command);
options.key_source = crate::util::command_key_source(app_config)?;
let mut storage_config = app_config.storage.clone();
storage_config.set_instance_id(&format!(
"{}:backup-command",
Expand Down
46 changes: 46 additions & 0 deletions crates/app/src/cmd_catalog_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub async fn run(args: CatalogCheckArgs) -> anyhow::Result<()> {
let Some((data_conn_str,)) = data_conn else {
anyhow::bail!("No data database connection string in settings. Run `extenddb init`.");
};
let data_conn_str = data_connection_text(&catalog_pool, &app_config, data_conn_str).await?;
let data_pool = PgPoolOptions::new()
.max_connections(2)
.connect(&data_conn_str)
Expand Down Expand Up @@ -214,3 +215,48 @@ pub async fn run(args: CatalogCheckArgs) -> anyhow::Result<()> {

Ok(())
}

/// The data database connection string that the row `stored` holds: the
/// row as it is in a catalog without a key ring; with a ring, the row
/// decrypted under a DEK of the ring, which the KEK of `[auth.encryption]
/// key_file` opens (RFC-0000 section 12). A row that no DEK opens is
/// refused, plaintext included, as every reader but `switch` refuses it.
async fn data_connection_text(
catalog: &sqlx::PgPool,
app_config: &config::AppConfig,
stored: String,
) -> anyhow::Result<zeroize::Zeroizing<String>> {
use extenddb_storage::key_ring::{KEY_RING_SETTING, open_ring};
use extenddb_storage::secret_keys::CATALOG_ID_SETTING;
use extenddb_storage::server_components::KeySource;

let setting = async |key: &str| -> anyhow::Result<Option<String>> {
let row: Option<(String,)> = sqlx::query_as("SELECT value FROM settings WHERE key = $1")
.bind(key)
.fetch_optional(catalog)
.await?;
Ok(row.map(|(value,)| value))
};
let Some(ring) = setting(KEY_RING_SETTING).await? else {
return Ok(zeroize::Zeroizing::new(stored));
};
let KeySource::Kek(kek) = extenddb_server::key_file::key_source(&app_config.auth.encryption)
.map_err(anyhow::Error::msg)?
else {
anyhow::bail!(
"The catalog has a key ring, so its data database connection string is encrypted. \
Set [auth.encryption] key_file to the KEK file that the ring names"
);
};
let catalog_id = setting(CATALOG_ID_SETTING).await?;
let open = open_ring(&ring, catalog_id.as_deref(), &kek).map_err(|e| anyhow::anyhow!("{e}"))?;
open.deks
.iter()
.find_map(|dek| dek.decrypt_setting(&stored).ok())
.ok_or_else(|| {
anyhow::anyhow!(
"No DEK of the key ring opens the data_database_connection_string row. Run \
`extenddb encryption-key status`"
)
})
}
Loading
Loading