use std::path::{Path, PathBuf};
use crate::error::CoreError;
pub const BINARY_NAME: &str = "kovra";
pub const INSTALL_SCRIPT: &str = "install.sh";
pub const UNPACK_SCRIPT: &str = "unpack.sh";
pub const RECIPIENT_PUB: &str = "recipient.pub";
pub const PACKAGE_FILE: &str = "package.kovra";
pub const RECIPIENT_COORDINATE: &str = "secret:exchange/recipient/key";
pub const VOLUME_LABEL: &str = "KOVRA";
#[must_use]
pub fn mount_point() -> PathBuf {
Path::new("/Volumes").join(VOLUME_LABEL)
}
#[must_use]
pub fn render_install_script() -> String {
format!(
r##"#!/usr/bin/env bash
# kovra offline-exchange — destination bootstrap (origin-generated).
#
# Installs kovra from this USB, creates a PORTABLE passphrase vault (no Touch ID
# needed), generates your recipient keypair, and writes {pub} back to this USB so
# the sender can seal a package to you. The access token arrives separately (a
# second channel) — never on this USB.
#
# Run it from the USB: ./{install}
set -euo pipefail
HERE="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)"
BIN_DIR="${{KOVRA_BIN_DIR:-$HOME/.local/bin}}"
mkdir -p "$BIN_DIR"
cp "$HERE/{binary}" "$BIN_DIR/{binary}"
chmod +x "$BIN_DIR/{binary}"
# Clear the macOS quarantine flag on the bundled (unsigned) binary.
xattr -d com.apple.quarantine "$BIN_DIR/{binary}" 2>/dev/null || true
export PATH="$BIN_DIR:$PATH"
# A portable vault keyed by a passphrase — no OS keychain, works on any Mac.
if [ -z "${{KOVRA_PASSPHRASE:-}}" ]; then
printf 'Choose a vault passphrase (you will need it to open the package): '
read -r -s KOVRA_PASSPHRASE; printf '\n'
export KOVRA_PASSPHRASE
fi
kovra init
kovra keygen '{coord}' --type ed25519 --sensitivity high \
--description 'kovra offline-exchange recipient identity'
kovra pubkey '{coord}' > "$HERE/{pub}"
echo
echo "{pub} written to the USB. Hand the USB back to the sender so they can run"
echo "'kovra exchange seal'. Keep your passphrase — you'll need it to open the package."
"##,
binary = BINARY_NAME,
install = INSTALL_SCRIPT,
pub = RECIPIENT_PUB,
coord = RECIPIENT_COORDINATE,
)
}
#[must_use]
pub fn render_unpack_script() -> String {
format!(
r##"#!/usr/bin/env bash
# kovra offline-exchange — destination OPEN helper (origin-generated).
#
# Opens {package} on this USB with your custodied recipient identity and imports
# the secrets. You'll be asked for your vault passphrase. For `high` entries,
# supply the access token the sender sent over a SEPARATE channel:
# export KOVRA_EXCHANGE_TOKEN=... (or use `kovra exchange open`)
set -euo pipefail
HERE="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)"
if [ -z "${{KOVRA_PASSPHRASE:-}}" ]; then
printf 'Vault passphrase: '
read -r -s KOVRA_PASSPHRASE; printf '\n'
export KOVRA_PASSPHRASE
fi
args=(unpack --in "$HERE/{package}" --identity '{coord}')
if [ -n "${{KOVRA_EXCHANGE_TOKEN:-}}" ]; then
# Land the token in a temp file OFF the USB; never written to the stick.
tok="$(mktemp -t kovra-token)"
trap 'rm -f "$tok"' EXIT
printf '%s' "$KOVRA_EXCHANGE_TOKEN" > "$tok"
args+=(--token "$tok")
fi
kovra "${{args[@]}}"
echo "Imported. The secrets now live in your local vault."
"##,
package = PACKAGE_FILE,
coord = RECIPIENT_COORDINATE,
)
}
pub fn write_bootstrap(
dest: &Path,
kovra_binary: &Path,
install_script: &str,
) -> Result<(), CoreError> {
std::fs::create_dir_all(dest)
.map_err(|e| CoreError::Io(format!("creating {}: {e}", dest.display())))?;
let bin_dst = dest.join(BINARY_NAME);
std::fs::copy(kovra_binary, &bin_dst).map_err(|e| {
CoreError::Io(format!(
"copying {} to {}: {e}",
kovra_binary.display(),
bin_dst.display()
))
})?;
make_executable(&bin_dst)?;
let script_dst = dest.join(INSTALL_SCRIPT);
std::fs::write(&script_dst, install_script)
.map_err(|e| CoreError::Io(format!("writing {}: {e}", script_dst.display())))?;
make_executable(&script_dst)?;
Ok(())
}
fn make_executable(path: &Path) -> Result<(), CoreError> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755))
.map_err(|e| CoreError::Io(format!("chmod +x {}: {e}", path.display())))?;
}
#[cfg(not(unix))]
{
let _ = path;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn install_script_drives_the_destination_bootstrap() {
let s = render_install_script();
assert!(s.starts_with("#!/usr/bin/env bash"), "has a shebang");
assert!(s.contains("set -euo pipefail"), "fails fast");
assert!(s.contains(&format!("cp \"$HERE/{BINARY_NAME}\"")));
assert!(s.contains("com.apple.quarantine"));
assert!(s.contains("KOVRA_PASSPHRASE"));
assert!(s.contains("kovra init"));
assert!(s.contains(RECIPIENT_COORDINATE));
assert!(s.contains(&format!("\"$HERE/{RECIPIENT_PUB}\"")));
assert!(!s.to_lowercase().contains("private key"));
}
#[test]
fn write_bootstrap_copies_binary_and_writes_executable_script() {
let tmp = tempfile::tempdir().unwrap();
let dest = tmp.path().join("KOVRA");
let fake_bin = tmp.path().join("kovra-bin");
std::fs::write(&fake_bin, b"#!/bin/sh\necho kovra\n").unwrap();
write_bootstrap(&dest, &fake_bin, &render_install_script()).unwrap();
let bin = dest.join(BINARY_NAME);
let script = dest.join(INSTALL_SCRIPT);
assert!(bin.exists() && script.exists());
assert_eq!(std::fs::read(&bin).unwrap(), b"#!/bin/sh\necho kovra\n");
assert!(
std::fs::read_to_string(&script)
.unwrap()
.contains("kovra keygen")
);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
for p in [&bin, &script] {
let mode = std::fs::metadata(p).unwrap().permissions().mode();
assert!(mode & 0o111 != 0, "{} must be executable", p.display());
}
}
}
#[test]
fn mount_point_is_volumes_kovra() {
assert_eq!(mount_point(), Path::new("/Volumes/KOVRA"));
}
#[test]
fn unpack_script_opens_with_recipient_identity_and_offusb_token() {
let s = render_unpack_script();
assert!(s.starts_with("#!/usr/bin/env bash"));
assert!(s.contains(&format!("--in \"$HERE/{PACKAGE_FILE}\"")));
assert!(s.contains(&format!("--identity '{RECIPIENT_COORDINATE}'")));
assert!(s.contains("KOVRA_EXCHANGE_TOKEN"));
assert!(s.contains("mktemp"));
assert!(s.contains("rm -f \"$tok\""));
assert!(!s.to_lowercase().contains("private key"));
}
}