use std::path::{Path, PathBuf};
use ed25519_dalek::SigningKey;
use serde::{Deserialize, Serialize};
use crate::cli::{fail_return, parse_signing_key};
pub(crate) const DEFAULT_HUB: &str = "https://hub.constellate.science";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct Identity {
#[serde(default = "default_version")]
pub version: String,
pub actor_id: String,
#[serde(default = "default_actor_type")]
pub actor_type: String,
pub key_path: String,
pub pubkey: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signer: Option<IdentitySigner>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub(crate) enum IdentitySigner {
File {
key_path: String,
},
Helper {
provider: String,
key_id: String,
public_key: String,
protection_grade: String,
mode: String,
helper_sha256: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pending_source_removal: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pending_vela_binary_sha256: Option<String>,
},
}
#[derive(Debug, Clone)]
pub(crate) struct ProtectedSignerProfile {
pub(crate) provider: String,
pub(crate) protection_grade: String,
pub(crate) mode: vela_signer::ProtectionMode,
pub(crate) helper_sha256: String,
}
#[derive(Debug, Clone, Serialize)]
pub(crate) struct SignerHealth {
pub(crate) state: &'static str,
pub(crate) platform: &'static str,
pub(crate) integration: &'static str,
pub(crate) integration_ready: bool,
pub(crate) binary_version: &'static str,
pub(crate) binary_path: String,
pub(crate) binary_sha256: Option<String>,
pub(crate) binary_pin_state: &'static str,
pub(crate) pinned_binary_sha256: Option<String>,
pub(crate) helper_path: Option<String>,
pub(crate) helper_sha256: Option<String>,
pub(crate) pinned_helper_sha256: Option<String>,
pub(crate) next_action: Option<String>,
}
fn default_version() -> String {
"1.0".to_string()
}
fn default_actor_type() -> String {
"human".to_string()
}
pub(crate) fn vela_home() -> PathBuf {
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
PathBuf::from(home).join(".vela")
}
pub(crate) fn identity_path() -> PathBuf {
vela_home().join("identity.json")
}
pub(crate) fn load_identity() -> Option<Identity> {
let path = identity_path();
let text = std::fs::read_to_string(&path).ok()?;
serde_json::from_str(&text).ok()
}
pub(crate) fn save_identity(identity: &Identity) -> Result<(), String> {
let dir = vela_home();
std::fs::create_dir_all(&dir).map_err(|e| format!("create {}: {e}", dir.display()))?;
let path = identity_path();
let json =
serde_json::to_string_pretty(identity).map_err(|e| format!("serialize identity: {e}"))?;
let temporary = path.with_extension(format!("json.tmp-{}", std::process::id()));
std::fs::write(&temporary, format!("{json}\n"))
.map_err(|e| format!("write {}: {e}", temporary.display()))?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&temporary, std::fs::Permissions::from_mode(0o600))
.map_err(|e| format!("protect {}: {e}", temporary.display()))?;
}
std::fs::rename(&temporary, &path).map_err(|e| format!("install {}: {e}", path.display()))
}
const SETUP_HINT: &str = "no identity configured — run `vela id create --handle <your-name>` once \
(generates a key, stores it, prints the line a maintainer runs to register you)";
pub(crate) fn resolve_actor(flag: Option<&str>) -> String {
if let Some(a) = flag.filter(|s| !s.trim().is_empty()) {
return a.to_string();
}
if let Ok(a) = std::env::var("VELA_ACTOR_ID")
&& !a.trim().is_empty()
{
return a;
}
match load_identity() {
Some(id) => id.actor_id,
None => fail_return(SETUP_HINT),
}
}
pub(crate) fn resolve_decision_actor(flag: Option<&str>) -> String {
let actor = resolve_actor(flag);
if actor.starts_with("agent:") || actor.starts_with("ci:") {
crate::ui::fail_with(
crate::ui::ErrorKind::Custody,
&format!(
"`{actor}` cannot decide: committing truth-bearing state is a key-custody human act"
),
Some(
"a human runs this under their own identity (`vela id show`); agents land drafts with `vela land`",
),
);
}
actor
}
pub(crate) fn resolve_key_path(flag: Option<&Path>) -> Option<PathBuf> {
if let Some(p) = flag {
return Some(p.to_path_buf());
}
if let Ok(p) = std::env::var("VELA_KEY_PATH")
&& !p.trim().is_empty()
{
return Some(PathBuf::from(p));
}
load_identity().and_then(|id| match id.signer {
Some(IdentitySigner::File { key_path }) => Some(PathBuf::from(key_path)),
Some(IdentitySigner::Helper { .. }) => None,
None => Some(PathBuf::from(id.key_path)),
})
}
pub(crate) fn protected_signer_profile() -> Result<ProtectedSignerProfile, String> {
let identity = load_identity().ok_or_else(|| SETUP_HINT.to_string())?;
protected_signer_profile_for(&identity)
}
pub(crate) fn signer_health(identity: &Identity) -> SignerHealth {
let platform = std::env::consts::OS;
let (integration, integration_ready, integration_repair) =
platform_signer_integration(platform);
let binary = std::env::current_exe().ok();
let binary_sha256 = binary
.as_deref()
.and_then(|path| vela_signer::contract::file_sha256(path).ok());
let helper = binary
.as_deref()
.and_then(|path| signer_helper_path(path).ok());
let helper_sha256 = helper
.as_deref()
.and_then(|path| vela_signer::contract::file_sha256(path).ok());
let (binary_pin_state, pinned_binary_sha256, binary_pin_ready) =
match crate::config::binary_pin::pin_state() {
Ok(crate::config::binary_pin::PinState::Match(pin)) => {
("match", Some(format!("sha256:{}", pin.sha256)), true)
}
Ok(crate::config::binary_pin::PinState::Mismatch { pinned, .. }) => {
("mismatch", Some(format!("sha256:{}", pinned.sha256)), false)
}
Ok(crate::config::binary_pin::PinState::Unpinned) => ("unpinned", None, false),
Err(_) => ("unavailable", None, false),
};
let (pinned_helper_sha256, incomplete) = match &identity.signer {
Some(IdentitySigner::Helper {
helper_sha256,
pending_source_removal,
pending_vela_binary_sha256,
..
}) => (
Some(helper_sha256.clone()),
pending_source_removal.is_some() || pending_vela_binary_sha256.is_some(),
),
_ => (None, false),
};
let protected = pinned_helper_sha256.is_some();
let (state, next_action) = if incomplete {
(
"incomplete",
Some(
"vela id protect --user-presence --remove-source-key --mode session --json"
.to_string(),
),
)
} else if protected && !integration_ready {
("missing_integration", integration_repair)
} else if let Some(pinned) = pinned_helper_sha256.as_ref() {
if protected_backend_ready(helper_sha256.as_deref(), pinned, binary_pin_ready) {
("ready", None)
} else {
(
"stale",
Some(
"vela id protect --user-presence --remove-source-key --mode session --json"
.to_string(),
),
)
}
} else {
(
"file_key",
(identity.actor_type == "human").then(|| {
"vela id protect --user-presence --remove-source-key --mode session --json"
.to_string()
}),
)
};
SignerHealth {
state,
platform,
integration,
integration_ready,
binary_version: env!("CARGO_PKG_VERSION"),
binary_path: binary
.as_deref()
.map(|path| path.display().to_string())
.unwrap_or_else(|| "unavailable".to_string()),
binary_sha256,
binary_pin_state,
pinned_binary_sha256,
helper_path: helper.map(|path| path.display().to_string()),
helper_sha256,
pinned_helper_sha256,
next_action,
}
}
fn protected_backend_ready(
observed_helper_sha256: Option<&str>,
pinned_helper_sha256: &str,
binary_pin_ready: bool,
) -> bool {
observed_helper_sha256 == Some(pinned_helper_sha256) && binary_pin_ready
}
fn platform_signer_integration(platform: &str) -> (&'static str, bool, Option<String>) {
match platform {
"macos" => ("Keychain + LocalAuthentication", true, None),
"windows" => ("Credential Manager + Windows Hello", true, None),
"linux" => {
let pkcheck = std::env::var_os("PATH").is_some_and(|path| {
std::env::split_paths(&path).any(|directory| directory.join("pkcheck").is_file())
});
let policy = [
"/usr/share/polkit-1/actions/science.vela.signer.policy",
"/usr/local/share/polkit-1/actions/science.vela.signer.policy",
]
.iter()
.any(|path| Path::new(path).is_file());
(
"Secret Service + polkit",
pkcheck && policy,
(!(pkcheck && policy)).then(|| {
"re-run the provenance-verified Vela installer to install pkcheck support and science.vela.signer.policy"
.to_string()
}),
)
}
_ => (
"file-key compatibility only",
false,
Some("protected human signing is unsupported on this platform".to_string()),
),
}
}
fn protected_signer_profile_for(identity: &Identity) -> Result<ProtectedSignerProfile, String> {
let expected_key_id = format!("{}:{}", identity.actor_id, identity.pubkey);
match &identity.signer {
Some(IdentitySigner::Helper {
provider,
key_id,
public_key,
protection_grade,
mode,
helper_sha256,
pending_source_removal: None,
..
}) => {
if identity.actor_type != "human"
|| identity.actor_id.starts_with("agent:")
|| identity.actor_id.starts_with("ci:")
|| public_key != &identity.pubkey
|| key_id != &expected_key_id
{
return Err("protected identity actor/key binding is invalid".to_string());
}
if provider != "os_store" {
return Err(format!(
"unsupported protected signer provider '{provider}'"
));
}
if !matches!(
protection_grade.as_str(),
"user_session" | "app_isolated" | "external_confirmed" | "hardware_nonexportable"
) {
return Err(format!(
"unsupported protected signer grade '{protection_grade}'"
));
}
if helper_sha256.len() != 71
|| !helper_sha256.starts_with("sha256:")
|| !helper_sha256[7..]
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
{
return Err("protected signer helper digest is invalid".to_string());
}
Ok(ProtectedSignerProfile {
provider: provider.clone(),
protection_grade: protection_grade.clone(),
mode: parse_protection_mode(mode)?,
helper_sha256: helper_sha256.clone(),
})
}
Some(IdentitySigner::Helper {
pending_source_removal: Some(path),
..
}) => Err(format!(
"protected identity migration is incomplete; remove the plaintext source {path} by rerunning `vela id protect --user-presence --remove-source-key`"
)),
_ => Err(
"this decision requires a user-presence protected identity; run `vela id protect --user-presence --remove-source-key`"
.to_string(),
),
}
}
fn parse_protection_mode(value: &str) -> Result<vela_signer::ProtectionMode, String> {
match value {
"session" => Ok(vela_signer::ProtectionMode::Session),
"always" => Ok(vela_signer::ProtectionMode::Always),
_ => Err(format!("unsupported protected signer mode '{value}'")),
}
}
pub(crate) fn signer_helper_path(vela_binary: &Path) -> Result<PathBuf, String> {
let directory = vela_binary
.parent()
.ok_or_else(|| "running Vela binary has no parent directory".to_string())?;
#[cfg(target_os = "windows")]
let helper = directory.join("vela-signer.exe");
#[cfg(not(target_os = "windows"))]
let helper = directory.join("vela-signer");
if !helper.is_file() {
return Err(format!(
"pinned signer helper is missing at {}; reinstall the complete Vela package",
helper.display()
));
}
Ok(helper)
}
pub(crate) fn resolve_signing_key(flag: Option<&Path>) -> SigningKey {
match resolve_signing_key_opt(flag) {
Some(key) => key,
None => fail_return(SETUP_HINT),
}
}
pub(crate) fn resolve_signing_key_opt(flag: Option<&Path>) -> Option<SigningKey> {
let path = resolve_key_path(flag)?;
let hex = std::fs::read_to_string(&path)
.unwrap_or_else(|e| fail_return(&format!("read key {}: {e}", path.display())));
Some(parse_signing_key(hex.trim()))
}
pub(crate) fn resolve_co_author_provenance(
co_author: Option<&str>,
generated_by: Option<&str>,
) -> Option<vela_protocol::provenance::Provenance> {
let id = co_author
.map(str::to_string)
.or_else(|| std::env::var("VELA_CO_AUTHOR").ok())
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())?;
let generated_by = generated_by
.map(str::to_string)
.or_else(|| std::env::var("VELA_GENERATED_BY").ok())
.unwrap_or_default();
Some(vela_protocol::provenance::Provenance {
machine_contributions: vec![vela_protocol::provenance::MachineContribution {
id,
class: String::new(),
role: "drafted".to_string(),
tool: String::new(),
generated_by,
authority: "none".to_string(),
}],
..Default::default()
})
}
#[cfg(test)]
mod protected_profile_tests {
use super::*;
fn identity() -> Identity {
let public_key = "4".repeat(64);
Identity {
version: "2.0".to_string(),
actor_id: "reviewer:test".to_string(),
actor_type: "human".to_string(),
key_path: String::new(),
pubkey: public_key.clone(),
signer: Some(IdentitySigner::Helper {
provider: "os_store".to_string(),
key_id: format!("reviewer:test:{public_key}"),
public_key,
protection_grade: "user_session".to_string(),
mode: "session".to_string(),
helper_sha256: format!("sha256:{}", "a".repeat(64)),
pending_source_removal: None,
pending_vela_binary_sha256: None,
}),
}
}
#[test]
fn protected_profile_requires_exact_actor_key_and_helper_bindings() {
let profile = protected_signer_profile_for(&identity()).unwrap();
assert_eq!(profile.provider, "os_store");
assert_eq!(profile.mode, vela_signer::ProtectionMode::Session);
let mut wrong_key_id = identity();
if let Some(IdentitySigner::Helper { key_id, .. }) = &mut wrong_key_id.signer {
*key_id = "reviewer:other:deadbeef".to_string();
}
assert!(protected_signer_profile_for(&wrong_key_id).is_err());
let mut wrong_public_key = identity();
if let Some(IdentitySigner::Helper { public_key, .. }) = &mut wrong_public_key.signer {
*public_key = "5".repeat(64);
}
assert!(protected_signer_profile_for(&wrong_public_key).is_err());
let mut wrong_helper = identity();
if let Some(IdentitySigner::Helper { helper_sha256, .. }) = &mut wrong_helper.signer {
*helper_sha256 = format!("sha256:{}", "A".repeat(64));
}
assert!(protected_signer_profile_for(&wrong_helper).is_err());
}
#[test]
fn unsupported_platform_never_claims_protected_signer_readiness() {
let (integration, ready, repair) = platform_signer_integration("unsupported-test-os");
assert_eq!(integration, "file-key compatibility only");
assert!(!ready);
assert!(repair.unwrap().contains("unsupported"));
}
#[test]
fn protected_backend_requires_both_helper_and_binary_pins() {
let helper = format!("sha256:{}", "a".repeat(64));
assert!(protected_backend_ready(Some(&helper), &helper, true));
assert!(!protected_backend_ready(Some(&helper), &helper, false));
assert!(!protected_backend_ready(None, &helper, true));
}
#[test]
fn protected_profile_rejects_agents_unknown_modes_and_incomplete_migrations() {
let mut agent = identity();
agent.actor_id = "agent:test".to_string();
agent.actor_type = "agent".to_string();
assert!(protected_signer_profile_for(&agent).is_err());
let mut wrong_mode = identity();
if let Some(IdentitySigner::Helper { mode, .. }) = &mut wrong_mode.signer {
*mode = "forever".to_string();
}
assert!(protected_signer_profile_for(&wrong_mode).is_err());
let mut incomplete = identity();
if let Some(IdentitySigner::Helper {
pending_source_removal,
..
}) = &mut incomplete.signer
{
*pending_source_removal = Some("/tmp/plaintext.key".to_string());
}
let error = protected_signer_profile_for(&incomplete).unwrap_err();
assert!(error.contains("migration is incomplete"));
}
}