diff --git a/crates/storage-cassandra/src/bootstrapper.rs b/crates/storage-cassandra/src/bootstrapper.rs index d6c10a3d2..4b16fa29e 100644 --- a/crates/storage-cassandra/src/bootstrapper.rs +++ b/crates/storage-cassandra/src/bootstrapper.rs @@ -808,15 +808,12 @@ impl Bootstrapper for CassandraBootstrapper { let keyspace = self.engine.catalog_keyspace(); let username = env_user.unwrap_or("admin"); - let from_env = env_user.is_some() && env_password.is_some(); println!("--- Creating admin user..."); - // Generate or use provided password - let password = if let Some(pw) = env_password { - pw.to_string() - } else { - generate_random_password() + let (password, from_env) = match env_password { + Some(p) => (p.to_owned(), true), + None => (generate_random_password(), false), }; // Hash password (using bcrypt in blocking task to avoid blocking async runtime) diff --git a/crates/storage-cassandra/tests/migrations.rs b/crates/storage-cassandra/tests/migrations.rs index d61f0e4ca..0b13e9f72 100644 --- a/crates/storage-cassandra/tests/migrations.rs +++ b/crates/storage-cassandra/tests/migrations.rs @@ -1614,6 +1614,7 @@ async fn init_refuses_an_account_keyspace_at_simple_strategy_until_it_is_altered .await; let admin = resume().await.expect("init finishes after ALTER KEYSPACE"); assert_eq!(admin.username, "admin"); + assert!(!admin.already_existed && !admin.from_env && admin.generated_password.is_some()); assert_eq!(store.list_all_accounts().await.expect("list"), accounts); drop(drops); helpers::flush_pending_drops(&session).await; @@ -1625,7 +1626,8 @@ async fn init_refuses_an_account_keyspace_at_simple_strategy_until_it_is_altered /// `init`, a new process, takes the keyspace as `init` does, and its steps /// under the lock run no migration, keep the key, create no second account /// and write the admin user; the `init` after that is refused as -/// initialized. +/// initialized. The next `init` has an admin password from the environment +/// and no user name, and gets no generated password back. #[tokio::test] async fn init_finishes_a_catalog_stopped_before_its_admin_user() { use extenddb_storage::bootstrapper::{ @@ -1699,13 +1701,22 @@ async fn init_finishes_a_catalog_stopped_before_its_admin_user() { .expect("decided") ); next.hold(&lock); - let admin = write_bootstrap_rows(&next, None, None).await.expect("rows"); + let admin = write_bootstrap_rows(&next, None, Some("Supplied-pw-1")) + .await + .expect("rows"); next.record_data_connection() .await .expect("data connection"); migrations.release(lock).await; - assert_eq!(admin.username, "admin"); - assert!(!admin.already_existed && admin.generated_password.is_some()); + assert_eq!( + ( + admin.username.as_str(), + admin.already_existed, + admin.from_env, + admin.generated_password + ), + ("admin", false, true, None) + ); assert_eq!( store.get_setting("encryption_key").await.expect("read"), key diff --git a/crates/storage-postgres/src/bootstrapper.rs b/crates/storage-postgres/src/bootstrapper.rs index 31ab86a89..163234d29 100755 --- a/crates/storage-postgres/src/bootstrapper.rs +++ b/crates/storage-postgres/src/bootstrapper.rs @@ -306,7 +306,7 @@ impl Bootstrapper for PostgresBootstrapper { env_password: Option<&str>, ) -> OpResult { let pool = self.app_pool(&self.config.catalog_db).await?; - let admin_name = env_user.filter(|s| !s.is_empty()).unwrap_or("admin"); + let admin_name = env_user.unwrap_or("admin"); let exists: bool = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM admin_users WHERE admin_name = $1)") @@ -327,8 +327,8 @@ impl Bootstrapper for PostgresBootstrapper { println!("--- Creating admin user '{admin_name}'..."); let (password, from_env) = match env_password { - Some(p) if !p.is_empty() => (p.to_owned(), true), - _ => (generate_random_password(), false), + Some(p) => (p.to_owned(), true), + None => (generate_random_password(), false), }; let hash = hash_password_async(password.clone()).await?; diff --git a/crates/storage-sqlite/src/bootstrapper.rs b/crates/storage-sqlite/src/bootstrapper.rs index 55a72c80a..60dc1a4a4 100644 --- a/crates/storage-sqlite/src/bootstrapper.rs +++ b/crates/storage-sqlite/src/bootstrapper.rs @@ -253,10 +253,7 @@ impl Bootstrapper for SqliteBootstrapper { env_password: Option<&str>, ) -> OpResult { let pool = self.pool().await?; - let username = env_user - .filter(|s| !s.is_empty()) - .unwrap_or("admin") - .to_owned(); + let username = env_user.unwrap_or("admin").to_owned(); let exists: bool = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM admin_users WHERE admin_name = ?)") @@ -273,7 +270,7 @@ impl Bootstrapper for SqliteBootstrapper { }); } - let (password, from_env) = match env_password.filter(|s| !s.is_empty()) { + let (password, from_env) = match env_password { Some(p) => (p.to_owned(), true), None => (helpers::generate_random_password(), false), }; @@ -548,4 +545,52 @@ mod init_tests { ); let _ = std::fs::remove_dir_all(&dir); } + + /// `write_bootstrap_rows` passes an empty `EXTENDDB_ADMIN_USER` or + /// `EXTENDDB_ADMIN_PASSWORD` on as no value, and returns a password only + /// when it generated one. + #[tokio::test] + async fn empty_admin_variables_are_unset_and_a_supplied_password_is_not_returned() { + // (EXTENDDB_ADMIN_USER, EXTENDDB_ADMIN_PASSWORD, admin name, from the environment) + let cases = [ + (Some(""), Some(""), "admin", false), + (None, Some("Supplied-pw-1"), "admin", true), + (Some("ops"), None, "ops", false), + (Some("ops"), Some("Supplied-pw-1"), "ops", true), + ]; + let timeout = Duration::from_secs(10); + for (row, (user, password, name, from_env)) in cases.into_iter().enumerate() { + let dir = std::env::temp_dir().join(format!( + "extenddb-admin-env-{}", + uuid::Uuid::new_v4().simple() + )); + let path = dir.join("extenddb.sqlite").to_string_lossy().into_owned(); + let bootstrapper = SqliteBootstrapper::new(path); + let migrations = SqliteCatalogMigrations; + let target = bootstrapper.init_target(); + bootstrapper.create_catalog_db().await.expect("file"); + let lock = migrations.lock(&target, timeout).await.expect("lock"); + let status = migrations + .status_under_lock(&lock, &target) + .await + .expect("status"); + run_with_status(&migrations, &status, &lock, "1.0.0-test", RunKind::Create) + .await + .expect("create"); + bootstrapper.hold(&lock); + let admin = write_bootstrap_rows(&bootstrapper, user, password).await; + migrations.release(lock).await; + let _ = std::fs::remove_dir_all(&dir); + let admin = admin.expect("rows"); + assert_eq!( + ( + admin.username.as_str(), + admin.from_env, + admin.generated_password.is_some() + ), + (name, from_env, !from_env), + "row {row}" + ); + } + } } diff --git a/crates/storage/src/bootstrapper.rs b/crates/storage/src/bootstrapper.rs index 06757ba70..9876e2e2f 100755 --- a/crates/storage/src/bootstrapper.rs +++ b/crates/storage/src/bootstrapper.rs @@ -70,7 +70,9 @@ pub trait Bootstrapper: Send + Sync { /// Create the default account. Idempotent — skips if any account exists. async fn bootstrap_default_account(&self) -> OpResult<()>; - /// Create the initial admin user. + /// Create the initial admin user. `init` passes `None` for an + /// `EXTENDDB_ADMIN_USER` or `EXTENDDB_ADMIN_PASSWORD` that is unset or + /// empty ([`write_bootstrap_rows`]). async fn bootstrap_admin_user( &self, env_user: Option<&str>, @@ -151,7 +153,7 @@ pub async fn finishes_a_stopped_init( /// writes its row only when the catalog lacks it, so on a catalog an earlier /// `init` stopped in this writes the rows that `init` did not. /// `admin_user` and `admin_password` come from `EXTENDDB_ADMIN_USER` and -/// `EXTENDDB_ADMIN_PASSWORD`. +/// `EXTENDDB_ADMIN_PASSWORD`. An empty value is passed on as no value. /// /// # Errors /// @@ -165,7 +167,10 @@ pub async fn write_bootstrap_rows( bootstrapper.bootstrap_default_account().await?; // REQ-AUTH-003 bootstrapper - .bootstrap_admin_user(admin_user, admin_password) + .bootstrap_admin_user( + admin_user.filter(|s| !s.is_empty()), + admin_password.filter(|s| !s.is_empty()), + ) .await } diff --git a/docker/README-cassandra.md b/docker/README-cassandra.md index fb39e9282..2a17d4e6d 100644 --- a/docker/README-cassandra.md +++ b/docker/README-cassandra.md @@ -83,8 +83,9 @@ Pass the passwords through the environment rather than as arguments: or check its password and writes the value you supplied into the config, so a wrong value surfaces only when `serve` fails to connect. * `EXTENDDB_ADMIN_USER` and `EXTENDDB_ADMIN_PASSWORD`: the first ExtendDB - administrator. Set both; with only the password set, `init` prints that - password in its output, and so in the container's logs. + administrator, named `admin` when `EXTENDDB_ADMIN_USER` is unset or empty. + Without `EXTENDDB_ADMIN_PASSWORD`, `init` generates a password and prints it + in its output, and so in the container's logs. `init` writes only the first of the `--cassandra-contact-points` into `extenddb.toml`; add the others to `contact_points` there afterwards. The file diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index 5d74b7584..f4cae4197 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -445,6 +445,18 @@ No catalog migration or configuration change is required. **Before upgrading** a PostgreSQL deployment, check that the environment of the ExtendDB process does not set `PGHOSTADDR`, unless every connection is to go to that address. Check that no `.pgpass` entry that ExtendDB reads has a `\` in its password. Check any log alert that matches the whole text of a PostgreSQL error. SQLite deployments get SQLite 3.54.1. MongoDB and Cassandra deployments see only the Rust version change, and only in a build from source. The upgrade needs no catalog migration and no configuration change. +### v1.1.0: `init` takes an empty admin variable as unset on every backend + +**What changed.** `extenddb init` takes an empty `EXTENDDB_ADMIN_USER` or `EXTENDDB_ADMIN_PASSWORD` as unset on every backend: it names the admin `admin`, or generates a password. On MongoDB, 1.0.0 took the empty string as the name or the password. On Cassandra, 1.0.0 took an empty password, and an empty name stopped `init` with an error. On Cassandra, `init` with `EXTENDDB_ADMIN_PASSWORD` set and `EXTENDDB_ADMIN_USER` not set no longer prints the supplied password (#1339). + +**Who is affected.** MongoDB and Cassandra catalogs that `init` of an earlier release created with an empty `EXTENDDB_ADMIN_PASSWORD` or an empty `EXTENDDB_ADMIN_USER`, and Cassandra catalogs that it created with only `EXTENDDB_ADMIN_PASSWORD` set. + +- An empty `EXTENDDB_ADMIN_PASSWORD` gave the admin an empty password. Log in with an empty `EXTENDDB_PASSWORD` and give the admin a password: `EXTENDDB_PASSWORD= extenddb manage --user change-admin-password --admin-name --new-password `. +- On MongoDB, an empty `EXTENDDB_ADMIN_USER` gave the admin an empty name, and `list-admins` shows it as `"admin_name": ""`. That admin logs in with `--user ''`, and `change-admin-password --admin-name ''` changes its password. `delete-admin --admin-name ''` fails with `HTTP 404`. To delete the admin, first create another one: `extenddb manage --user '' create-admin --admin-name --admin-password `. Then delete the document of the admin with the empty name in `mongosh`: `db.getSiblingDB("extenddb_catalog").admin_users.deleteOne({_id: ""})`. +- On Cassandra, `init` with an empty `EXTENDDB_ADMIN_USER` stopped with `Error: Create admin user: Internal error: Database error` and wrote no admin, because Cassandra refuses an empty key. Run the same `init` command with this release. It finishes the catalog and creates the admin `admin`. + +The output of a Cassandra `init` from an earlier release with only `EXTENDDB_ADMIN_PASSWORD` set holds that password, so change it if that output was kept. No catalog migration is required. + ### v1.0.0: Cassandra reads and writes at `LOCAL_QUORUM` **What changed.** The Cassandra backend runs its reads and writes at `LOCAL_QUORUM`. 0.1.15 ran many of them at `ONE`, the driver's default, in each area: items (the reads of GetItem, Query and Scan among them), transactions, TTL, streams, the catalog, IAM and `init`. A read with `ConsistentRead` `false` also runs at `LOCAL_QUORUM`. A lightweight transaction runs Paxos at `LOCAL_SERIAL` and commits at `LOCAL_QUORUM`. 0.1.15 ran many of its lightweight transactions at those levels already, and others, among them the admin user creation and most IAM writes, at the driver's defaults, `SERIAL` and `ONE`. Batches ran at `LOCAL_QUORUM` before and still do. No setting changes these levels (#945).