Read any secret from a _FILE variable #75

Merged
jcoffey-dev merged 1 commits from feat/migrate-secret-files into main 2026-11-12 23:22:49 +00:00
5 changed files with 173 additions and 14 deletions

No files matched your search

+6
View File
@@ -11,6 +11,12 @@ candidates before a series ships (`27.1-rc.1`). The tag is the version with a
- `completions <shell>` prints a shell completion script (bash, zsh, fish,
elvish or powershell) to standard output, and `man` prints a man page in
roff. Both are generated from the command itself and reach no server.
- Every secret read from an `INBUXA_MIGRATE_*` variable can instead be read
from a file named by the same variable with `_FILE` appended, the Docker
and systemd secrets convention (`INBUXA_MIGRATE_PASSWORD_FILE`,
`INBUXA_MIGRATE_API_KEY_FILE`, and so on). One trailing newline is ignored;
setting both a variable and its `_FILE` form is an error. This keeps a
secret out of both the environment and the command line.
## [27.1-rc.12]
+12 -1
View File
@@ -98,8 +98,19 @@ Secrets are read from the environment, or asked for at a prompt:
INBUXA_MIGRATE_GOOGLE_KEY a Google service account's JSON key,
for --google-key-from-env
Any of these can instead be read from a file, by setting the same name with
`_FILE` appended to the file's path -- the Docker, Compose and systemd secrets
convention:
INBUXA_MIGRATE_PASSWORD_FILE=/run/secrets/imap-password
INBUXA_MIGRATE_API_KEY_FILE=/run/secrets/token
The file holds the secret and nothing else; one trailing newline is ignored.
Setting both a variable and its `_FILE` form is an error. (A Google key is
already a file: `--google-key <path>`.)
The command line takes them too, but a secret there ends up in the shell's
history and in the process list. Use the environment or the prompt.
history and in the process list. Use a file, the environment, or the prompt.
## Installing
+4 -3
View File
@@ -58,9 +58,10 @@ minutes for the server's first byte, and 30 minutes to read a whole
response. A transfer that goes silent for 5 minutes in either direction also
times out. A timed-out request is retried like any other transient failure.
Secrets come from the `INBUXA_MIGRATE_*` environment variables or a prompt;
see [Credentials](../README.md#credentials). The command line takes them too,
but should not be given them.
Secrets come from the `INBUXA_MIGRATE_*` environment variables, a file named
by the same variable with `_FILE` appended (the Docker and systemd secrets
convention), or a prompt; see [Credentials](../README.md#credentials). The
command line takes them too, but should not be given them.
## Import
+2 -3
View File
@@ -1947,12 +1947,11 @@ fn resolve_exchange_ews_auth(args: &ExchangeEwsImportArgs) -> Result<EwsAuth, Er
client_secret: secret.to_owned(),
}));
}
if let Some(token) = secret::from_env(secret::API_KEY_VARS) {
if let Some(token) = secret::from_env(secret::API_KEY_VARS)? {
return Ok(EwsAuth::Bearer { token });
}
if let Some(client_id) = args.ews_client_id.clone()
&& let Ok(client_secret) = std::env::var("INBUXA_MIGRATE_EWS_CLIENT_SECRET")
&& !client_secret.is_empty()
&& let Some(client_secret) = secret::from_env(&["INBUXA_MIGRATE_EWS_CLIENT_SECRET"])?
{
return Ok(EwsAuth::OAuth(OAuthFlow::ClientCredentials {
tenant: tenant(),
+149 -7
View File
@@ -30,23 +30,57 @@ pub fn resolve_any(
if let Some(value) = flag_value {
return Ok(value.to_owned());
}
if let Some(value) = from_env(env_vars) {
if let Some(value) = from_env(env_vars)? {
return Ok(value);
}
if std::io::stdin().is_terminal() {
return prompt_no_echo(prompt_label);
}
Err(Error::Usage(format!(
"no {prompt_label} provided: pass the flag, set ${}, or run on a TTY for an interactive prompt",
"no {prompt_label} provided: pass the flag, set ${0} or ${0}_FILE, or run on a TTY for an interactive prompt",
env_vars[0]
)))
}
/// The first of `env_vars` that is set and not empty.
pub fn from_env(env_vars: &[&str]) -> Option<String> {
env_vars
.iter()
.find_map(|var| std::env::var(var).ok().filter(|v| !v.is_empty()))
/// The first secret found among `env_vars`, in order. For each name `V` the
/// secret is read from `$V` directly, or from the file named by `${V}_FILE`:
/// the Docker, Compose and systemd convention, so a secret can be kept in a
/// file (a mounted secret, a tmpfs path) instead of the environment or the
/// command line. Setting both `$V` and `${V}_FILE` for the same name is a
/// mistake and is rejected. A file's single trailing newline is stripped;
/// any other whitespace is kept, as a secret may contain it.
pub fn from_env(env_vars: &[&str]) -> Result<Option<String>, Error> {
for var in env_vars {
let direct = std::env::var(var).ok().filter(|v| !v.is_empty());
let file_var = format!("{var}_FILE");
let file = std::env::var(&file_var).ok().filter(|v| !v.is_empty());
match (direct, file) {
(Some(_), Some(_)) => {
return Err(Error::Usage(format!(
"set either ${var} or ${file_var}, not both"
)));
}
(Some(value), None) => return Ok(Some(value)),
(None, Some(path)) => return Ok(Some(read_secret_file(&path, &file_var)?)),
(None, None) => {}
}
}
Ok(None)
}
/// Read a secret from the file `${var}` points at, stripping one trailing
/// newline (CRLF or LF). An unreadable or empty file is a usage error.
fn read_secret_file(path: &str, var: &str) -> Result<String, Error> {
let raw = std::fs::read_to_string(path)
.map_err(|e| Error::Usage(format!("${var}: can't read secret file {path}: {e}")))?;
let secret = raw
.strip_suffix('\n')
.map(|s| s.strip_suffix('\r').unwrap_or(s))
.unwrap_or(&raw);
if secret.is_empty() {
return Err(Error::Usage(format!("${var}: secret file {path} is empty")));
}
Ok(secret.to_owned())
}
fn prompt_no_echo(label: &str) -> Result<String, Error> {
@@ -80,3 +114,111 @@ fn set_echo(on: bool) -> bool {
.map(|s| s.success())
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
fn temp_file(contents: &[u8]) -> std::path::PathBuf {
use std::sync::atomic::{AtomicU32, Ordering};
static N: AtomicU32 = AtomicU32::new(0);
let n = N.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!(
"inbuxa-migrate-secret-{}-{n}.tmp",
std::process::id()
));
let mut f = std::fs::File::create(&path).unwrap();
f.write_all(contents).unwrap();
path
}
#[test]
fn secret_file_strips_one_trailing_newline() {
let p = temp_file(b"s3cret\n");
assert_eq!(
read_secret_file(p.to_str().unwrap(), "X_FILE").unwrap(),
"s3cret"
);
std::fs::remove_file(&p).ok();
let p = temp_file(b"s3cret\r\n");
assert_eq!(
read_secret_file(p.to_str().unwrap(), "X_FILE").unwrap(),
"s3cret"
);
std::fs::remove_file(&p).ok();
}
#[test]
fn secret_file_keeps_inner_and_leading_whitespace_and_no_trailing_newline() {
// No trailing newline: taken verbatim.
let p = temp_file(b"a b c");
assert_eq!(
read_secret_file(p.to_str().unwrap(), "X_FILE").unwrap(),
"a b c"
);
std::fs::remove_file(&p).ok();
// Only the final newline goes; a blank line before it stays.
let p = temp_file(b" lead\n\n");
assert_eq!(
read_secret_file(p.to_str().unwrap(), "X_FILE").unwrap(),
" lead\n"
);
std::fs::remove_file(&p).ok();
}
#[test]
fn secret_file_empty_or_just_a_newline_is_an_error() {
let p = temp_file(b"");
assert!(read_secret_file(p.to_str().unwrap(), "X_FILE").is_err());
std::fs::remove_file(&p).ok();
let p = temp_file(b"\n");
assert!(read_secret_file(p.to_str().unwrap(), "X_FILE").is_err());
std::fs::remove_file(&p).ok();
}
#[test]
fn secret_file_missing_is_an_error_naming_the_var() {
let err = read_secret_file("/no/such/secret/file", "MY_SECRET_FILE").unwrap_err();
assert!(format!("{err}").contains("MY_SECRET_FILE"));
}
#[test]
fn from_env_reads_direct_then_file_then_both_is_rejected() {
// Unique names so this test does not race other env-touching tests.
let direct = "INBUXA_MIGRATE_TEST_SECRET_DIRECT";
let base = "INBUXA_MIGRATE_TEST_SECRET";
let file_var = "INBUXA_MIGRATE_TEST_SECRET_FILE";
unsafe {
std::env::remove_var(direct);
std::env::remove_var(base);
std::env::remove_var(file_var);
}
// Nothing set.
assert_eq!(from_env(&[base]).unwrap(), None);
// Direct value.
unsafe { std::env::set_var(base, "plain") };
assert_eq!(from_env(&[base]).unwrap().as_deref(), Some("plain"));
// File value (direct cleared).
unsafe { std::env::remove_var(base) };
let p = temp_file(b"from-file\n");
unsafe { std::env::set_var(file_var, p.to_str().unwrap()) };
assert_eq!(from_env(&[base]).unwrap().as_deref(), Some("from-file"));
// Both set: rejected.
unsafe { std::env::set_var(base, "plain") };
assert!(from_env(&[base]).is_err());
unsafe {
std::env::remove_var(base);
std::env::remove_var(file_var);
}
std::fs::remove_file(&p).ok();
}
}