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
9 changes: 3 additions & 6 deletions crates/storage-cassandra/src/bootstrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 15 additions & 4 deletions crates/storage-cassandra/tests/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::{
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions crates/storage-postgres/src/bootstrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl Bootstrapper for PostgresBootstrapper {
env_password: Option<&str>,
) -> OpResult<AdminBootstrapResult> {
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)")
Expand All @@ -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?;

Expand Down
55 changes: 50 additions & 5 deletions crates/storage-sqlite/src/bootstrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@ impl Bootstrapper for SqliteBootstrapper {
env_password: Option<&str>,
) -> OpResult<AdminBootstrapResult> {
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 = ?)")
Expand All @@ -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),
};
Expand Down Expand Up @@ -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}"
);
}
}
}
11 changes: 8 additions & 3 deletions crates/storage/src/bootstrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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
///
Expand All @@ -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()),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional. Only the SQLite test holds this filter; no test passes an empty value on MongoDB or Cassandra. A unit test here with a recording double, or Some("") as the user in the Cassandra resume test, would hold it for every backend.

admin_password.filter(|s| !s.is_empty()),
)
.await
}

Expand Down
5 changes: 3 additions & 2 deletions docker/README-cassandra.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional. The user line says "unset or empty" and the password line only "Without"; an empty EXTENDDB_ADMIN_PASSWORD also generates a password and prints it in the box.

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
Expand Down
12 changes: 12 additions & 0 deletions docs/manuals/07-upgrade-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name> change-admin-password --admin-name <name> --new-password <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 <name> --admin-password <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`.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional. "Run the same init command with this release" works while the catalog is at migration 12; if a Cassandra catalog migration lands before the release, init refuses the catalog, so "run extenddb migrate first if init refuses" would cover it.


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).
Expand Down
Loading