pub const DAEMON_TOKEN_ENV: &str = "TCODE_DAEMON_TOKEN";
pub fn daemon_credential_for(base_url: &str) -> Option<String> {
trusty_common::daemon_token::credential_for(
crate::serve::http::TOKEN_APP_NAME,
base_url,
DAEMON_TOKEN_ENV,
)
}
#[cfg(test)]
mod http_credential_tests {
use super::*;
use serial_test::serial;
use trusty_common::credentials::test_sandbox::{CredentialSandbox, assert_secret_eq};
#[test]
#[serial]
fn credential_is_withheld_from_a_non_loopback_url() {
let mut sandbox = CredentialSandbox::enter();
sandbox.set(DAEMON_TOKEN_ENV, "a".repeat(64));
let remote = [
"http://example.test:7882",
"https://10.0.0.5:7882",
"http://192.168.1.4:7882",
"http://127.0.0.1:7882@attacker.example",
"http://127.0.0.1:7882@attacker.example/rpc",
"http://localhost@attacker.example",
"http://user:pass@attacker.example",
];
for url in remote {
assert_secret_eq(
daemon_credential_for(url).as_deref(),
None,
&format!("{url} must get no credential"),
);
}
assert_secret_eq(
daemon_credential_for("http://127.0.0.1:7882").as_deref(),
Some("a".repeat(64).as_str()),
"loopback must get the credential",
);
}
#[test]
#[serial]
fn credential_env_override_wins_for_a_loopback_url() {
let mut sandbox = CredentialSandbox::enter();
sandbox.set(DAEMON_TOKEN_ENV, "b".repeat(64));
assert_secret_eq(
daemon_credential_for("http://localhost:7882").as_deref(),
Some("b".repeat(64).as_str()),
"the env override must win",
);
}
#[test]
#[serial]
fn blank_credential_override_falls_through() {
let mut sandbox = CredentialSandbox::enter();
sandbox.set(DAEMON_TOKEN_ENV, " ");
assert_secret_eq(
daemon_credential_for("http://127.0.0.1:7882").as_deref(),
None,
"a blank override must fall through to the (absent) token file",
);
}
}