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
66 changes: 63 additions & 3 deletions ci/smoke-test-dev-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
# 4. MEMORY MODE DOES NOT PERSIST: the same restart loses the table
# (ResourceNotFoundException). This is the negative control for check 3;
# if both modes "persist", the mode switch is broken.
# 5. Labels carry the release identity (version, revision) and --version
# 5. DISABLE SWITCH: an empty EXTENDDB_DEV_DISABLE_EXAMPLE_CREDENTIAL refuses
# to start (read through the real environment, not a unit-test stub), and
# restarting the file-mode container with the switch and a team pair
# removes the example key from the stored database: the example key is
# then rejected and the team key still reads the item.
# 6. Labels carry the release identity (version, revision) and --version
# agrees with them.
# 6. Hardening: runs as 65532 (nonroot), no shell in the runtime image.
# 7. Hardening: runs as 65532 (nonroot), no shell in the runtime image.
#
# Requires: docker, aws, python3. Set EXTENDDB_IMAGE to test a prebuilt image
# (CI does this); otherwise the image is built from Dockerfile.dev.
Expand Down Expand Up @@ -58,10 +63,11 @@ RUN_ID="devsmoke-$$-${RANDOM}"
FILE_CTR="${RUN_ID}-file"
MEM_CTR="${RUN_ID}-mem"
MOVED_CTR="${RUN_ID}-moved"
SWITCH_CTR="${RUN_ID}-switch"
VOLUME="${RUN_ID}-data"

cleanup() {
docker rm -f "$FILE_CTR" "$MEM_CTR" "$MOVED_CTR" >/dev/null 2>&1 || true
docker rm -f "$FILE_CTR" "$MEM_CTR" "$MOVED_CTR" "$SWITCH_CTR" >/dev/null 2>&1 || true
docker volume rm "$VOLUME" >/dev/null 2>&1 || true
if [[ "$BUILT_IMAGE" == 1 ]]; then
docker image rm "$EXTENDDB_IMAGE" >/dev/null 2>&1 || true
Expand Down Expand Up @@ -146,6 +152,60 @@ GOT=$(ddb "$PORT" get-item --table-name smoke --key '{"pk":{"S":"k1"}}' \
[[ "$GOT" == "persisted" ]] \
|| { echo "error: file mode did NOT persist across restart: '$GOT'" >&2; exit 1; }

# A team pair for the disable-switch checks. Not a real credential.
TEAM_KEY_ID="AKIADEVSMOKETEAM0001"
TEAM_SECRET="smoke-team-secret-not-real"

echo "== disable switch: an empty value refuses to start =="
# `-e NAME=` sets the variable empty, as `-e NAME=$VAR` does with VAR unset.
docker run -d --name "$SWITCH_CTR" \
-e EXTENDDB_DEV_DISABLE_EXAMPLE_CREDENTIAL= \
-e EXTENDDB_DEV_ACCESS_KEY_ID="$TEAM_KEY_ID" \
-e EXTENDDB_DEV_SECRET_ACCESS_KEY="$TEAM_SECRET" \
-e EXTENDDB__STORAGE__SQLITE__PATH=:memory: \
"$EXTENDDB_IMAGE" >/dev/null
tries=0
while [[ "$(docker inspect --format '{{.State.Running}}' "$SWITCH_CTR")" == true ]]; do
tries=$((tries + 1))
if [[ $tries -gt 30 ]]; then
echo "error: the server started with the disable switch set empty" >&2
docker logs "$SWITCH_CTR" 2>&1 | tail -20 >&2
exit 1
fi
sleep 1
done
EXIT_CODE=$(docker inspect --format '{{.State.ExitCode}}' "$SWITCH_CTR")
[[ "$EXIT_CODE" != 0 ]] \
|| { echo "error: the server exited 0 with the disable switch set empty" >&2; exit 1; }
docker logs "$SWITCH_CTR" 2>&1 | grep -F "EXTENDDB_DEV_DISABLE_EXAMPLE_CREDENTIAL is set but empty" >/dev/null \
|| { echo "error: no 'set but empty' refusal in the logs" >&2; docker logs "$SWITCH_CTR" 2>&1 | tail -20 >&2; exit 1; }
docker rm -f "$SWITCH_CTR" >/dev/null

echo "== disable switch: a restart with it removes the stored example key =="
docker rm -f "$FILE_CTR" >/dev/null
docker run -d --name "$FILE_CTR" -v "$VOLUME":/var/lib/extenddb \
-e EXTENDDB_DEV_DISABLE_EXAMPLE_CREDENTIAL=1 \
-e EXTENDDB_DEV_ACCESS_KEY_ID="$TEAM_KEY_ID" \
-e EXTENDDB_DEV_SECRET_ACCESS_KEY="$TEAM_SECRET" \
-p 127.0.0.1:0:18080 "$EXTENDDB_IMAGE" >/dev/null
wait_for_health "$FILE_CTR"
PORT=$(host_port "$FILE_CTR")
# The example key was seeded on this volume by the starts above; it must now
# be rejected as unknown, not merely fail some other way.
if OUT=$(ddb "$PORT" list-tables 2>&1); then
echo "error: the example key still verifies after a restart with the switch" >&2; exit 1
fi
grep -F UnrecognizedClientException <<<"$OUT" >/dev/null \
|| { echo "error: expected UnrecognizedClientException for the example key, got: $OUT" >&2; exit 1; }
GOT=$(AWS_ACCESS_KEY_ID="$TEAM_KEY_ID" AWS_SECRET_ACCESS_KEY="$TEAM_SECRET" \
ddb "$PORT" get-item --table-name smoke --key '{"pk":{"S":"k1"}}' \
--query 'Item.v.S' --output text)
[[ "$GOT" == "persisted" ]] \
|| { echo "error: the team key could not read the item after the switch: '$GOT'" >&2; exit 1; }
if docker logs "$FILE_CTR" 2>&1 | grep -F "$TEAM_SECRET" >/dev/null; then
echo "error: the team secret appears in the container logs" >&2; exit 1
fi

echo "== memory mode: works, and must NOT survive a restart (negative control) =="
docker run -d --name "$MEM_CTR" \
-e EXTENDDB__STORAGE__SQLITE__PATH=:memory: \
Expand Down
243 changes: 209 additions & 34 deletions crates/app/src/cmd_serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ pub fn run(args: &ServeArgs, build: BuildInfo) -> anyhow::Result<()> {
// cannot enable `dev-mode` (see the compile_error in the bin crate).
if cfg!(feature = "dev-mode") {
let host = app_config.server.bind_addr.trim();
let loopback = host == "localhost"
|| host
.parse::<std::net::IpAddr>()
.map(|ip| ip.is_loopback())
.unwrap_or(false);
let loopback = is_loopback_host(host);
let allow_any_bind = std::env::var("EXTENDDB_DEV_ALLOW_ANY_BIND")
.is_ok_and(|v| matches!(v.trim(), "1" | "true"));
if !loopback && !allow_any_bind {
Expand All @@ -137,14 +133,10 @@ pub fn run(args: &ServeArgs, build: BuildInfo) -> anyhow::Result<()> {
inside a container, where the published port decides exposure."
);
}
if !loopback && allow_any_bind {
tracing::warn!(
bind_addr = host,
"dev-mode is binding a non-loopback address: plain HTTP with relaxed \
authorization. Publish it on loopback only (-p 127.0.0.1:PORT:PORT) and \
never expose it to a shared network."
);
}
// A permitted non-loopback bind is warned about in the dev-mode banner
// (`dev_mode_banner_lines`), which prints to the console. A
// `tracing::warn!` here would print nothing: no subscriber is installed
// until `serve_inner` runs.
}

let bind_addr = format!("{}:{}", app_config.server.bind_addr, port);
Expand Down Expand Up @@ -206,7 +198,7 @@ pub fn run(args: &ServeArgs, build: BuildInfo) -> anyhow::Result<()> {
print!("{startup_warnings}");
}
if cfg!(feature = "dev-mode") {
print_dev_mode_banner(&app_config, args.foreground)?;
print_dev_mode_banner(&app_config, port, args.foreground)?;
}

// D-3: A PID file lets `extenddb status` and `extenddb stop` find the
Expand Down Expand Up @@ -393,55 +385,238 @@ fn render_startup_warnings(warnings: &[extenddb_storage::config::StartupWarning]
out
}

/// True for a bind address that only this host can reach.
fn is_loopback_host(host: &str) -> bool {
host == "localhost"
|| host
.parse::<std::net::IpAddr>()
.map(|ip| ip.is_loopback())
.unwrap_or(false)
}

/// Print the developer-mode banner lines: the mode notice, the credentials to
/// sign with, and an advisory when an ambient `AWS_ACCESS_KEY_ID` would not
/// verify.
/// sign with, a warning block when the server is not bound to loopback, and an
/// advisory when an ambient `AWS_ACCESS_KEY_ID` would not verify.
///
/// Resolving here (not just in `serve()`) fails fast on a misconfigured
/// `EXTENDDB_DEV_*` pair before daemonizing, and gives the invoking user the
/// credentials without digging through logs. The built-in example pair is
/// printed in full: it is public AWS documentation, not a secret. An
/// operator-supplied secret is never printed.
fn print_dev_mode_banner(app_config: &config::AppConfig, foreground: bool) -> anyhow::Result<()> {
fn print_dev_mode_banner(
app_config: &config::AppConfig,
port: u16,
foreground: bool,
) -> anyhow::Result<()> {
use extenddb_server::dev_credentials;

let dev_creds = dev_credentials::resolve()?;
let lines = dev_mode_banner_lines(
&config::redact_password(app_config.storage.connection_config()),
app_config.server.bind_addr.trim(),
port,
&dev_creds,
dev_credentials::ignored_aws_env_key(&dev_creds).as_deref(),
);
// Same stream split as the main banner: stdout for the daemon parent,
// stderr in foreground mode so supervisors capture one stream.
for msg in lines {
if foreground {
eprintln!("{msg}");
} else {
println!("{msg}");
}
}
Ok(())
}

/// Build the developer-mode banner. Pure, so the wording is unit tested.
///
/// A non-loopback bind gets a multi-line warning block rather than one log
/// line: it is the dev image's normal state (a container must bind 0.0.0.0
/// for port publishing to work), and whether that is safe depends on the
/// host's `-p` mapping, which the server cannot see. So the block cannot say
/// "you are exposed", only what exposure would mean and how to avoid it.
fn dev_mode_banner_lines(
storage: &str,
host: &str,
port: u16,
dev_creds: &[extenddb_server::dev_credentials::DevCredential],
ignored_aws_key: Option<&str>,
) -> Vec<String> {
use extenddb_server::dev_credentials;

let loopback = is_loopback_host(host);
let example_active = dev_credentials::example_pair_active(dev_creds);
let where_ = if loopback {
"on loopback".to_owned()
} else {
format!("on {host}")
};
let mut lines = vec![
format!(
" DEVELOPER MODE: plain HTTP on loopback, authorization open \
(SigV4 still enforced). Serving storage: {}. Not for production.",
config::redact_password(app_config.storage.connection_config()),
" DEVELOPER MODE: plain HTTP {where_}, authorization open \
(SigV4 still enforced). Serving storage: {storage}. Not for production.",
),
format!(
" Sign requests with: {}",
dev_credentials::describe(&dev_creds)
dev_credentials::describe(dev_creds)
),
];
if let Some(ignored) = dev_credentials::ignored_aws_env_key(&dev_creds) {
if !loopback {
let credential = if example_active {
"the signing credential above is public, so anyone who can reach this port \
can read, change and delete every table"
} else {
"whoever holds the signing credential above can read, change and delete \
every table"
};
lines.extend([
String::new(),
format!(" WARNING: dev mode is listening on {host}:{port}, not loopback."),
format!(" Requests are plain HTTP and authorization is open: {credential}."),
format!(
" In a container, publish the port on loopback only: \
-p 127.0.0.1:{port}:{port} (not -p {port}:{port})."
),
" Never expose dev mode to a shared or routable network.".to_owned(),
]);
if example_active {
lines.push(format!(
" On a shared host or CI runner, set {}=1 with your own \
EXTENDDB_DEV_ACCESS_KEY_ID / EXTENDDB_DEV_SECRET_ACCESS_KEY.",
dev_credentials::ENV_DISABLE_EXAMPLE,
));
}
lines.push(String::new());
}
if let Some(ignored) = ignored_aws_key {
let switch_note = dev_credentials::ignored_key_switch_note(ignored, dev_creds)
.map(|note| format!(" {note}"))
.unwrap_or_default();
lines.push(format!(
" Note: AWS_ACCESS_KEY_ID is set but not adopted; requests signed with \
'{ignored}' will be rejected. Set EXTENDDB_DEV_ACCESS_KEY_ID and \
'{ignored}' will be rejected.{switch_note} Set EXTENDDB_DEV_ACCESS_KEY_ID and \
EXTENDDB_DEV_SECRET_ACCESS_KEY to seed an additional credential."
));
}
// Same stream split as the main banner: stdout for the daemon parent,
// stderr in foreground mode so supervisors capture one stream.
for msg in lines {
if foreground {
eprintln!("{msg}");
} else {
println!("{msg}");
}
}
Ok(())
lines
}

#[cfg(test)]
mod tests {
use super::{ServeArgs, render_startup_warnings};
use super::{ServeArgs, dev_mode_banner_lines, is_loopback_host, render_startup_warnings};
use clap::Parser;
use extenddb_server::dev_credentials::{
DevCredential, ENV_DISABLE_EXAMPLE, EXAMPLE_ACCESS_KEY_ID, EXAMPLE_SECRET_ACCESS_KEY,
};
use extenddb_storage::config::StartupWarning;

fn example() -> DevCredential {
DevCredential {
access_key_id: EXAMPLE_ACCESS_KEY_ID.to_owned(),
secret_access_key: EXAMPLE_SECRET_ACCESS_KEY.to_owned(),
is_builtin: true,
}
}

fn team() -> DevCredential {
DevCredential {
access_key_id: "AKIADEVTEAMKEY000001".to_owned(),
secret_access_key: "team-secret".to_owned(),
is_builtin: false,
}
}

#[test]
fn loopback_hosts_are_recognised() {
for host in ["127.0.0.1", "127.1.2.3", "::1", "localhost"] {
assert!(is_loopback_host(host), "{host}");
}
for host in ["0.0.0.0", "::", "192.168.1.10", "example.com", ""] {
assert!(!is_loopback_host(host), "{host}");
}
}

#[test]
fn loopback_dev_banner_has_no_exposure_warning() {
let text =
dev_mode_banner_lines(":memory:", "127.0.0.1", 18080, &[example()], None).join("\n");
assert!(text.contains("plain HTTP on loopback"), "{text}");
assert!(!text.contains("WARNING"), "{text}");
}

#[test]
fn non_loopback_dev_banner_warns_with_the_publish_command() {
let text =
dev_mode_banner_lines(":memory:", "0.0.0.0", 18080, &[example()], None).join("\n");
// The banner used to say "on loopback" whatever the bind was.
assert!(!text.contains("plain HTTP on loopback"), "{text}");
assert!(text.contains("plain HTTP on 0.0.0.0"), "{text}");
assert!(
text.contains("WARNING: dev mode is listening on 0.0.0.0:18080"),
"{text}"
);
assert!(text.contains("credential above is public"), "{text}");
assert!(text.contains("-p 127.0.0.1:18080:18080"), "{text}");
assert!(text.contains(&format!("{ENV_DISABLE_EXAMPLE}=1")), "{text}");
}

#[test]
fn non_loopback_dev_banner_without_the_example_pair_drops_the_public_wording() {
let text = dev_mode_banner_lines(":memory:", "0.0.0.0", 9000, &[team()], None).join("\n");
assert!(
text.contains("WARNING: dev mode is listening on 0.0.0.0:9000"),
"{text}"
);
assert!(text.contains("-p 127.0.0.1:9000:9000"), "{text}");
assert!(!text.contains("is public"), "{text}");
assert!(!text.contains(EXAMPLE_ACCESS_KEY_ID), "{text}");
// The switch is already set; do not tell the operator to set it.
assert!(
!text.contains(&format!("{ENV_DISABLE_EXAMPLE}=1")),
"{text}"
);
assert!(!text.contains("team-secret"), "{text}");
}

#[test]
fn dev_banner_keeps_the_ignored_aws_key_note() {
let text = dev_mode_banner_lines(
":memory:",
"127.0.0.1",
18080,
&[example()],
Some("ASIAFOREIGNSESSION01"),
)
.join("\n");
assert!(
text.contains("'ASIAFOREIGNSESSION01' will be rejected"),
"{text}"
);
assert!(!text.contains("is not accepted because"), "{text}");
}

#[test]
fn dev_banner_names_the_switch_when_it_removed_the_ignored_example_key() {
let text = dev_mode_banner_lines(
":memory:",
"127.0.0.1",
18080,
&[team()],
Some(EXAMPLE_ACCESS_KEY_ID),
)
.join("\n");
assert!(
text.contains(&format!("'{EXAMPLE_ACCESS_KEY_ID}' will be rejected")),
"{text}"
);
assert!(
text.contains(&format!("because {ENV_DISABLE_EXAMPLE} is set")),
"{text}"
);
}

/// Test wrapper so clap has a top-level `Parser` to drive `ServeArgs`.
#[derive(Parser)]
struct TestCli {
Expand Down
Loading
Loading