diff --git a/CLAUDE.md b/CLAUDE.md index 42a6a3d..f4a65d6 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -454,8 +454,13 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li credential helper **as `claude`** (root never walks a live `~/.ssh`), `/run/triple-c/connected`, then a background re-clone of `failed` repos only (GIT_TOKEN in that clone's env only). The first clone waits up to 5 min for `connected` (`aws_wait_connect`). Terminals the app opens add - the pairs to their own `ExecSpec.env` (`aws_host::terminal_env`); nothing reaches the agent's - snapshot, so cron/scheduled jobs never get them. A failed push is a `workspace-warning`. + the pairs to their own `ExecSpec.env` (`aws_host::terminal_env`, pairs only — no file read); + nothing reaches the agent's snapshot, so cron/scheduled jobs and the setup script never get them. + A failed push is a `workspace-warning`. Hybrid/EFS do **not** link `~/.ssh` to EFS under + copy-at-connect (R6: keys vanish with the task). A secret that still holds credentials when a + copy-at-connect start writes it (`holds_credentials`) is force-deleted and created again (R7: + `PutSecretValue` would keep them as AWSPREVIOUS); `TRIPLE_C_CUSTOM_ENV` is left out too. + The re-clone closes the apply lock's fd (a later connect never waits for it); `--prepare` takes it. The functions both scripts use live in `container/entrypoint-lib.sh` (`/usr/local/lib/triple-c/`), sourced by the entrypoint first; it only defines things. **The `TRIPLEC_` env prefix is reserved** on both hosts (`RESERVED_ENV_PREFIXES`; `workspace.rs` diff --git a/app/src-tauri/src/aws/runtime/tests.rs b/app/src-tauri/src/aws/runtime/tests.rs index 53c5dd4..5868fb3 100644 --- a/app/src-tauri/src/aws/runtime/tests.rs +++ b/app/src-tauri/src/aws/runtime/tests.rs @@ -112,6 +112,12 @@ impl WorkspaceApi for Fake { ) -> Result { unreachable!() } + async fn project_secret_value(&self, _n: &str) -> Result, AwsError> { + unreachable!() + } + async fn delete_project_secret(&self, _n: &str) -> Result<(), AwsError> { + unreachable!() + } async fn put_project_secret( &self, _n: &str, diff --git a/app/src-tauri/src/aws/workspace.rs b/app/src-tauri/src/aws/workspace.rs index e847534..eac3253 100644 --- a/app/src-tauri/src/aws/workspace.rs +++ b/app/src-tauri/src/aws/workspace.rs @@ -154,6 +154,9 @@ pub struct PayloadExtras<'a> { pub connect_only: Option<&'a [String]>, } +/// `workspace_env`'s fingerprint of the custom variables' values. +const CUSTOM_ENV_FINGERPRINT: &str = "TRIPLE_C_CUSTOM_ENV"; + /// A credential, for copy-at-connect: an API key or token Triple-C manages /// (`SECRET_ENV_KEYS`, `GIT_TOKEN` included) or any custom variable — a /// custom value may be a key, and nothing tells which. @@ -212,8 +215,14 @@ fn payload_map( log::warn!("Ignoring the environment variable {k}: Triple-C sets it on AWS"); continue; } - // Copy-at-connect: copied in after each connect, never stored. - if extras.connect_only.is_some_and(|custom| is_credential_key(k, custom)) { + // Copy-at-connect: copied in after each connect, never stored. The + // custom variables' fingerprint goes too: an unsalted hash of their + // values would let a low-entropy one be guessed offline (and nothing + // on AWS reads it; Docker uses it to notice a change). + if extras + .connect_only + .is_some_and(|custom| is_credential_key(k, custom) || k == CUSTOM_ENV_FINGERPRINT) + { continue; } m.insert(k.clone(), v.clone()); @@ -277,7 +286,7 @@ fn describe_payload_key(key: &str) -> String { KEY_REPOS => "the repository list".into(), "CLAUDE_INSTRUCTIONS" => "the instructions".into(), "CLAUDE_CODE_SETTINGS_JSON" => "the Claude Code settings".into(), - "TRIPLE_C_CUSTOM_ENV" => "the custom environment variables".into(), + CUSTOM_ENV_FINGERPRINT => "the custom environment variables".into(), k if k.starts_with("TRIPLEC_GATEWAY_") => "the LLM gateway configuration".into(), k => format!("the environment variable {k}"), } @@ -641,6 +650,14 @@ pub trait WorkspaceApi: Send + Sync { root: &str, tags: &[(String, String)], ) -> impl Future> + Send; + /// The secret's current `SecretString`; `None` when there is no secret. + fn project_secret_value( + &self, + name: &str, + ) -> impl Future, AwsError>> + Send; + /// Delete the secret now, every version with it + /// (`ForceDeleteWithoutRecovery`); no secret is not an error. + fn delete_project_secret(&self, name: &str) -> impl Future> + Send; /// Create the secret (encrypted with `kms_key_arn`, tagged), or put a new /// value into it when it exists. Returns its ARN. fn put_project_secret( @@ -846,9 +863,33 @@ pub async fn prepare_workspace( }; let secret_name = project_secret_name(&env.name, &ok, &project.id); - let secret_arn = api - .put_project_secret(&secret_name, &env.kms_key_arn, &json, &tags) - .await?; + let secret_arn = match extras_custom(cfg, custom_env_keys) { + // Copy-at-connect over a secret that still holds credentials (it was + // Secrets Manager before, or a custom variable has gone): a new value + // would leave them in the secret's version history (AWSPREVIOUS), so + // the secret is deleted outright and made again (ruling R7). + Some(custom) => { + let old = api.project_secret_value(&secret_name).await?; + if old.as_deref().is_some_and(|o| holds_credentials(o, &payload, custom)) { + log::info!( + "Project {}: its secret still held credentials; replacing it so no version keeps them", + project.id + ); + replace_project_secret( + api, + &secret_name, + &env.kms_key_arn, + &json, + &tags, + RECREATE_TIMING, + ) + .await? + } else { + api.put_project_secret(&secret_name, &env.kms_key_arn, &json, &tags).await? + } + } + None => api.put_project_secret(&secret_name, &env.kms_key_arn, &json, &tags).await?, + }; let (task_role, exec_role) = user_role_names(&env.name, &ok); let role_arn = |name: &str| format!("arn:{partition}:iam::{}:role/{name}", who.account); @@ -913,6 +954,75 @@ const MAX_REVISION_PAGES: usize = 20; /// Secrets Manager's `InvalidRequestException` for a name whose secret is /// scheduled for deletion ("... already scheduled for deletion"). +/// Copy-at-connect's custom variable names, or `None` for Secrets Manager. +fn extras_custom<'a>( + cfg: &crate::models::project::AwsWorkspaceConfig, + custom_env_keys: &'a [String], +) -> Option<&'a [String]> { + (cfg.secrets_delivery == SecretsDelivery::CopyAtConnect).then_some(custom_env_keys) +} + +/// Whether a secret's current value `old` holds anything copy-at-connect +/// keeps out: the files, the custom variables' fingerprint, a credential +/// key, or an environment variable the new payload no longer has (a custom +/// variable since removed). `TRIPLEC_*` keys are the payload's own. A value +/// that is not a JSON object counts as holding them. +pub fn holds_credentials(old: &str, new: &BTreeMap, custom: &[String]) -> bool { + let Ok(old) = serde_json::from_str::>(old) else { + return true; + }; + old.keys().any(|k| { + k == KEY_FILES + || k == CUSTOM_ENV_FINGERPRINT + || is_credential_key(k, custom) + || (!k.starts_with("TRIPLEC_") && !new.contains_key(k)) + }) +} + +/// How long a replaced secret's name may stay taken by its deletion. +#[derive(Debug, Clone, Copy)] +pub struct RecreateTiming { + pub interval: std::time::Duration, + pub limit: std::time::Duration, +} + +const RECREATE_TIMING: RecreateTiming = RecreateTiming { + interval: std::time::Duration::from_secs(2), + limit: std::time::Duration::from_secs(30), +}; + +fn is_pending_deletion_error(e: &AwsError) -> bool { + matches!(e, AwsError::Other { message } if message.contains("is scheduled for deletion")) +} + +/// Delete the secret with every version, then create it with `json`, +/// retrying while AWS finishes the (asynchronous) delete. +pub async fn replace_project_secret( + api: &W, + name: &str, + kms_key_arn: &str, + json: &str, + tags: &[(String, String)], + timing: RecreateTiming, +) -> Result { + api.delete_project_secret(name).await?; + let deadline = tokio::time::Instant::now() + timing.limit; + loop { + match api.put_project_secret(name, kms_key_arn, json, tags).await { + Err(e) if is_pending_deletion_error(&e) => { + if tokio::time::Instant::now() + timing.interval > deadline { + return Err(other(format!( + "The project's secret ({name}) was deleted to remove the credentials it held, but AWS has not finished deleting it after {} s. Start the project again in a minute.", + timing.limit.as_secs() + ))); + } + tokio::time::sleep(timing.interval).await; + } + r => return r, + } + } +} + fn is_pending_deletion(message: Option<&str>) -> bool { message.is_some_and(|m| m.contains("scheduled for deletion")) } @@ -1057,6 +1167,41 @@ impl WorkspaceApi for SdkWorkspaceApi { .ok_or_else(|| other(format!("{ACTION} returned no AccessPointId"))) } + async fn project_secret_value(&self, name: &str) -> Result, AwsError> { + const ACTION: &str = "secretsmanager:GetSecretValue"; + match self.secrets.get_secret_value().secret_id(name).send().await { + Ok(out) => Ok(out.secret_string().map(str::to_string)), + Err(e) + if e.as_service_error() + .is_some_and(|s| s.is_resource_not_found_exception()) => + { + Ok(None) + } + Err(e) => Err(classify_sdk_error(ACTION, &e)), + } + } + + async fn delete_project_secret(&self, name: &str) -> Result<(), AwsError> { + const ACTION: &str = "secretsmanager:DeleteSecret"; + match self + .secrets + .delete_secret() + .secret_id(name) + .force_delete_without_recovery(true) + .send() + .await + { + Ok(_) => Ok(()), + Err(e) + if e.as_service_error() + .is_some_and(|s| s.is_resource_not_found_exception()) => + { + Ok(()) + } + Err(e) => Err(classify_sdk_error(ACTION, &e)), + } + } + async fn put_project_secret( &self, name: &str, diff --git a/app/src-tauri/src/aws/workspace/tests.rs b/app/src-tauri/src/aws/workspace/tests.rs index d96fd81..431f8fe 100644 --- a/app/src-tauri/src/aws/workspace/tests.rs +++ b/app/src-tauri/src/aws/workspace/tests.rs @@ -791,6 +791,14 @@ struct Fake { revisions: Mutex>, registered: Mutex>, describes: Mutex, + /// The secret's value before this start (`project_secret_value`). + existing_secret: Mutex>, + /// Names force-deleted. + deleted_secrets: Mutex>, + /// Calls `project_secret_value` got. + secret_reads: Mutex, + /// Puts that fail as "scheduled for deletion" before one succeeds. + pending_puts: Mutex, } impl Fake { @@ -836,6 +844,14 @@ impl WorkspaceApi for Fake { c.push((fs_id.into(), root.into(), tags.to_vec())); Ok(format!("fsap-new{}", c.len())) } + async fn project_secret_value(&self, _name: &str) -> Result, AwsError> { + *self.secret_reads.lock().unwrap() += 1; + Ok(self.existing_secret.lock().unwrap().clone()) + } + async fn delete_project_secret(&self, name: &str) -> Result<(), AwsError> { + self.deleted_secrets.lock().unwrap().push(name.into()); + Ok(()) + } async fn put_project_secret( &self, name: &str, @@ -843,6 +859,15 @@ impl WorkspaceApi for Fake { json: &str, tags: &[(String, String)], ) -> Result { + { + let mut pending = self.pending_puts.lock().unwrap(); + if *pending > 0 { + *pending -= 1; + return Err(AwsError::Other { + message: format!("This project's secret ({name}) is scheduled for deletion, so it can't be written."), + }); + } + } self.secrets.lock().unwrap().push(( name.into(), kms_key_arn.into(), @@ -1595,3 +1620,101 @@ async fn prepare_leaves_the_credentials_out_of_a_copy_at_connect_secret() { assert_eq!(m["TRIPLEC_CREDENTIALS"], "connect"); assert!(m.contains_key("TRIPLEC_AGENT_TOKEN")); } + +fn copy_at_connect_project() -> Project { + let mut p = project(StorageMode::Hybrid); + p.aws.as_mut().unwrap().secrets_delivery = crate::models::project::SecretsDelivery::CopyAtConnect; + p +} + +#[tokio::test] +async fn switching_to_copy_at_connect_replaces_a_secret_that_held_credentials() { + // The secret from a Secrets Manager period: replaced, not overwritten. + let fake = Fake::onboarded(); + *fake.existing_secret.lock().unwrap() = + Some(r#"{"GIT_TOKEN":"ghp_old","TRIPLEC_AGENT_TOKEN":"t","TZ":"UTC"}"#.into()); + *fake.pending_puts.lock().unwrap() = 2; // AWS still finishing the delete + let env = pairs(&[("GIT_TOKEN", "ghp_new"), ("TZ", "UTC")]); + prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE) + .await + .unwrap(); + assert_eq!(fake.deleted_secrets.lock().unwrap().len(), 1); + let m = payload_map(&fake.secrets.lock().unwrap()[0].2); + assert!(!m.contains_key("GIT_TOKEN")); + + // Already clean: a plain put, nothing deleted. + let fake = Fake::onboarded(); + *fake.existing_secret.lock().unwrap() = Some( + r#"{"TRIPLEC_AGENT_TOKEN":"t","TRIPLEC_CREDENTIALS":"connect","TZ":"UTC","TRIPLEC_REPOS":"[]"}"#.into(), + ); + prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE) + .await + .unwrap(); + assert!(fake.deleted_secrets.lock().unwrap().is_empty()); + assert_eq!(fake.secrets.lock().unwrap().len(), 1); + + // No secret yet: created, nothing deleted. + let fake = Fake::onboarded(); + prepare_workspace(&fake, &who(), &env_info(), ©_at_connect_project(), &env, &[], &[], IMAGE) + .await + .unwrap(); + assert!(fake.deleted_secrets.lock().unwrap().is_empty()); + + // Secrets Manager never reads or deletes. + let fake = Fake::onboarded(); + *fake.existing_secret.lock().unwrap() = Some(r#"{"GIT_TOKEN":"x"}"#.into()); + prepare(&fake, &project(StorageMode::Hybrid), &env).await.unwrap(); + assert_eq!(*fake.secret_reads.lock().unwrap(), 0); + assert!(fake.deleted_secrets.lock().unwrap().is_empty()); +} + +#[test] +fn what_counts_as_credentials_left_in_a_secret() { + let new: BTreeMap = + [("TZ", "UTC"), ("TRIPLEC_AGENT_TOKEN", "t")].iter().map(|(k, v)| (k.to_string(), v.to_string())).collect(); + let custom = vec!["MINE".to_string()]; + let holds = |old: &str| holds_credentials(old, &new, &custom); + assert!(holds(r#"{"TRIPLEC_FILES":"{}"}"#)); + assert!(holds(r#"{"TRIPLE_C_CUSTOM_ENV":"abc"}"#)); + assert!(holds(r#"{"ANTHROPIC_API_KEY":""}"#)); + assert!(holds(r#"{"MINE":"v"}"#)); + assert!(holds(r#"{"GONE_CUSTOM":"v","TZ":"UTC"}"#), "a variable the payload no longer has"); + assert!(holds("not json")); + assert!(!holds(r#"{"TZ":"UTC","TRIPLEC_AGENT_TOKEN":"old","TRIPLEC_SETUP_SCRIPT":"x"}"#)); +} + +#[tokio::test] +async fn a_replaced_secret_that_aws_has_not_finished_deleting_is_a_clear_error() { + let fake = Fake::onboarded(); + *fake.pending_puts.lock().unwrap() = 100; + let timing = RecreateTiming { + interval: std::time::Duration::from_millis(1), + limit: std::time::Duration::from_millis(20), + }; + let e = replace_project_secret(&fake, "n", "k", "{}", &[], timing).await.unwrap_err(); + assert!(e.to_string().contains("has not finished deleting it"), "{e}"); + assert_eq!(fake.deleted_secrets.lock().unwrap().as_slice(), ["n"]); +} + +#[test] +fn a_copy_at_connect_payload_drops_the_custom_variables_fingerprint() { + let env = pairs(&[("TRIPLE_C_CUSTOM_ENV", "63ce86"), ("TZ", "UTC")]); + let custom: Vec = Vec::new(); + let with = |connect_only| { + payload_map( + &project_secret_payload_with( + &env, + "tok", + &[], + &[], + true, + None, + StorageMode::Hybrid, + PayloadExtras { connect_only, ..Default::default() }, + ) + .unwrap(), + ) + }; + assert!(!with(Some(&custom)).contains_key("TRIPLE_C_CUSTOM_ENV")); + assert_eq!(with(None)["TRIPLE_C_CUSTOM_ENV"], "63ce86"); +} diff --git a/app/src-tauri/src/commands/terminal_commands.rs b/app/src-tauri/src/commands/terminal_commands.rs index 6fb3ffd..5087030 100644 --- a/app/src-tauri/src/commands/terminal_commands.rs +++ b/app/src-tauri/src/commands/terminal_commands.rs @@ -184,7 +184,7 @@ pub async fn open_terminal_session( // AWS copy-at-connect: the credentials go into this terminal's own // environment (never the agent's). Empty for everything else. - let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()); + let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()).await; let output_event = format!("terminal-output-{}", session_id); let exit_event = format!("terminal-exit-{}", session_id); diff --git a/app/src-tauri/src/docker/aws_mode_tests.rs b/app/src-tauri/src/docker/aws_mode_tests.rs index cee661f..066d026 100644 --- a/app/src-tauri/src/docker/aws_mode_tests.rs +++ b/app/src-tauri/src/docker/aws_mode_tests.rs @@ -2401,3 +2401,63 @@ fn apply_connect_retries_a_clone_that_started_without_credentials_once_it_is_don assert!(c.p("ws/late/README").exists()); assert!(!c.p("clone/no-credentials").exists()); } + +#[test] +fn a_running_re_clone_never_holds_the_apply_lock_and_prepare_waits_for_an_apply() { + let tmp = tempfile::tempdir().unwrap(); + let c = Connect::new(tmp.path()); + // A re-clone that takes a while. + let mut lib = fs::read_to_string(c.p("lib.sh")).unwrap(); + lib.push_str("\nclone_repos() { sleep 6; }\n"); + fs::write(c.p("lib.sh"), lib).unwrap(); + assert!(c.run(&["--prepare"]).status.success()); + fs::write(c.p("run/repos.json"), r#"[{"url":"x","branch":null,"folder":"bad"}]"#).unwrap(); + fs::write(c.p("clone/repos"), "bad\tcloning\nbad\tfailed\n").unwrap(); + fs::write(c.p("clone/done"), "").unwrap(); + assert!(c.run(&[]).status.success()); + assert!(!c.p("clone/done").exists(), "the re-clone is running"); + // The next connect goes straight through. + let started = std::time::Instant::now(); + assert!(c.run(&["--prepare"]).status.success()); + let out = c.run(&[]); + assert!(out.status.success(), "{}", text(&out)); + assert!(!text(&out).contains("another apply"), "{}", text(&out)); + assert!(started.elapsed() < std::time::Duration::from_secs(4), "{:?}", started.elapsed()); + c.wait_done(); + + // An apply in progress (the lock held) holds a prepare back. + let lock = c.p("shm/apply-connect.lock"); + let mut holder = Command::new("flock").arg(&lock).args(["sleep", "2"]).spawn().unwrap(); + std::thread::sleep(std::time::Duration::from_millis(300)); + fs::write(c.p("shm/connect/marker"), "").ok(); + let started = std::time::Instant::now(); + assert!(c.run(&["--prepare"]).status.success()); + assert!(started.elapsed() >= std::time::Duration::from_millis(1200), "{:?}", started.elapsed()); + holder.wait().unwrap(); +} + +#[test] +fn under_copy_at_connect_hybrid_keeps_ssh_off_efs() { + let tmp = tempfile::tempdir().unwrap(); + let (home, mnt) = hybrid_dirs(tmp.path()); + fs::create_dir_all(home.join(".ssh")).unwrap(); + fs::create_dir_all(home.join(".claude")).unwrap(); + let src = tmp.path().join("src"); + fs::create_dir_all(&src).unwrap(); + fs::write(src.join("id_ed25519"), "KEY").unwrap(); + let script = format!( + "{}\ntriplec_aws=1\ntriplec_connect=1\nlink_persistent_home \"$1\" \"$2\" \"\"\nmerge_host_ssh \"$3\"\n", + aws_blocks(&["aws_home", "sync_host_ssh", "merge_host_ssh"]) + .replace("/home/claude", home.to_str().unwrap()) + .replace("triplec_home_mnt=/mnt/triple-c-home", &format!("triplec_home_mnt='{}'", mnt.display())) + .replace("chown -R claude:claude", ": chown") + ); + let out = bash(&script, &[&home, &mnt, &src], &[], &[]); + assert!(out.status.success(), "{}", text(&out)); + assert!(text(&out).contains("~/.ssh does not"), "{}", text(&out)); + assert!(fs::symlink_metadata(home.join(".claude")).unwrap().file_type().is_symlink()); + let ssh = fs::symlink_metadata(home.join(".ssh")).unwrap(); + assert!(ssh.is_dir() && !ssh.file_type().is_symlink(), "~/.ssh is a local directory"); + assert_eq!(fs::read_to_string(home.join(".ssh/id_ed25519")).unwrap(), "KEY"); + assert!(!mnt.join(".ssh").exists(), "nothing written to EFS"); +} diff --git a/app/src-tauri/src/docker/workspace_env.rs b/app/src-tauri/src/docker/workspace_env.rs index c55f35d..998b182 100644 --- a/app/src-tauri/src/docker/workspace_env.rs +++ b/app/src-tauri/src/docker/workspace_env.rs @@ -442,11 +442,25 @@ pub(crate) fn workspace_env_from_settings( project: &Project, settings: &crate::models::AppSettings, ) -> Result { - let shared_claude = super::container::shared_claude_auth(project); let effective_ca_path = resolve_with_global(project.ca_cert_path.as_deref(), settings.ca_cert_path.as_deref()); let resolved_ca = ca_certs::resolve(effective_ca_path)?; - let pairs = workspace_env( + let pairs = workspace_env_with_ca(project, settings, resolved_ca.as_ref()); + Ok((pairs, resolved_ca)) +} + +/// [`workspace_env`] resolved from `settings` like +/// [`workspace_env_from_settings`] but with the CA given (`None`: no CA +/// variables) — so nothing is read from disk. The keychain is (the shared +/// Claude token). Copy-at-connect's terminal environment uses it: the +/// credential pairs never depend on the CA. +pub(crate) fn workspace_env_with_ca( + project: &Project, + settings: &crate::models::AppSettings, + resolved_ca: Option<&ResolvedCa>, +) -> Vec<(String, String)> { + let shared_claude = super::container::shared_claude_auth(project); + workspace_env( project, &WorkspaceEnvInputs { global_aws: &settings.global_aws, @@ -459,11 +473,10 @@ pub(crate) fn workspace_env_from_settings( global_claude_code_settings: settings.global_claude_code_settings.as_ref(), default_git_user_name: settings.default_git_user_name.as_deref(), default_git_user_email: settings.default_git_user_email.as_deref(), - resolved_ca: resolved_ca.as_ref(), + resolved_ca, shared_claude_token: shared_claude.as_ref().map(|(token, _)| token.as_str()), }, - ); - Ok((pairs, resolved_ca)) + ) } #[cfg(test)] diff --git a/app/src-tauri/src/host/aws_host/connect.rs b/app/src-tauri/src/host/aws_host/connect.rs index 202def8..25b1e26 100644 --- a/app/src-tauri/src/host/aws_host/connect.rs +++ b/app/src-tauri/src/host/aws_host/connect.rs @@ -19,6 +19,8 @@ use std::time::Duration; +use zeroize::Zeroizing; + use super::super::{tunnels, ContainerTarget}; use crate::aws::workspace::{credential_pairs, validate_file_key}; use crate::models::project::SecretsDelivery; @@ -38,13 +40,20 @@ pub const PUSH_TIMEOUT: Duration = Duration::from_secs(120); /// The most of the script's output a warning carries. const OUTPUT_TAIL_LINES: usize = 10; +/// A credential value, zeroed when dropped (the settings export's +/// precedent). It narrows, not closes, the window: the values were read from +/// the keychain into a `Project`, and the copies an `ExecSpec` or an upload +/// body needs are plain buffers owned by those layers. +pub type SecretBytes = Zeroizing>; +pub type SecretString = Zeroizing; + /// What copy-at-connect copies in. -#[derive(Clone, Default, PartialEq, Eq)] +#[derive(Clone, Default)] pub struct ConnectBundle { /// `ssh/` / `ca/`, as in the secret's `TRIPLEC_FILES`. - pub files: Vec<(String, Vec)>, + pub files: Vec<(String, SecretBytes)>, /// The credential environment pairs. - pub env: Vec<(String, String)>, + pub env: Vec<(String, SecretString)>, } /// Names only: a bundle holds keys and tokens. @@ -69,43 +78,55 @@ pub fn copies_at_connect(project: &Project) -> bool { /// The bundle for `project`, whose keychain secrets must already be loaded /// (`load_secrets_for_project`): the same sources the secret would use. pub fn connect_credentials(project: &Project, settings: &AppSettings) -> Result { - let (pairs, ca) = super::aws_env(project, settings)?; + let (_, ca) = super::aws_env(project, settings)?; let ssh = project .ssh_key_path .as_deref() .or(settings.default_ssh_key_path.as_deref()); - let files = super::workspace_files(ssh, ca.as_ref())?; - let custom = - crate::docker::workspace_env::custom_env_keys(project, &settings.global_custom_env_vars); - Ok(ConnectBundle { files, env: credential_pairs(&pairs, &custom) }) + let files = super::workspace_files(ssh, ca.as_ref())? + .into_iter() + .map(|(k, v)| (k, Zeroizing::new(v))) + .collect(); + Ok(ConnectBundle { files, env: connect_env(project, settings) }) } -/// [`connect_credentials`] for a project as the store holds it (secrets -/// read from the keychain here). -fn credentials_for(project: &Project, settings: &AppSettings) -> Result { +/// Only the credential pairs of `project` (secrets loaded): no file is read, +/// so a missing SSH or CA file never keeps the API keys from a terminal. +pub fn connect_env(project: &Project, settings: &AppSettings) -> Vec<(String, SecretString)> { + let pairs = crate::docker::workspace_env::workspace_env_with_ca(project, settings, None); + let custom = + crate::docker::workspace_env::custom_env_keys(project, &settings.global_custom_env_vars); + credential_pairs(&pairs, &custom) + .into_iter() + .map(|(k, v)| (k, Zeroizing::new(v))) + .collect() +} + +/// `project` as the store holds it, with its keychain secrets read in. +fn with_secrets(project: &Project) -> Project { let mut p = project.clone(); crate::commands::project_commands::load_secrets_for_project(&mut p); - connect_credentials(&p, settings) + p } /// `connect.env`: one `KEY=` line per pair, so a value /// with a newline or `=` reads back whole and nothing has to be quoted. -pub fn connect_env_file(env: &[(String, String)]) -> Vec { +pub fn connect_env_file(env: &[(String, SecretString)]) -> SecretBytes { use base64::Engine; let b64 = base64::engine::general_purpose::STANDARD; - let mut out = String::new(); + let mut out = Zeroizing::new(String::new()); for (k, v) in env { out.push_str(k); out.push('='); - out.push_str(&b64.encode(v)); + b64.encode_string(v.as_bytes(), &mut out); out.push('\n'); } - out.into_bytes() + Zeroizing::new(out.as_bytes().to_vec()) } /// `KEY=VALUE` strings for an `ExecSpec`. -pub fn exec_env(env: &[(String, String)]) -> Vec { - env.iter().map(|(k, v)| format!("{k}={v}")).collect() +pub fn exec_env(env: &[(String, SecretString)]) -> Vec { + env.iter().map(|(k, v)| format!("{k}={}", v.as_str())).collect() } /// What a push needs from the container: root execs and root uploads. @@ -183,7 +204,10 @@ pub async fn apply_connect(project: &Project, settings: &AppSettings) -> Result< let Some(ep) = tunnels::get_endpoint(&project.id) else { return Ok(false); }; - let bundle = credentials_for(project, settings)?; + let (p, s) = (project.clone(), settings.clone()); + let bundle = tokio::task::spawn_blocking(move || connect_credentials(&with_secrets(&p), &s)) + .await + .map_err(|e| format!("Could not gather the credentials: {e}"))??; let target = ContainerTarget::Agent(ep); tokio::time::timeout(PUSH_TIMEOUT, push_connect_bundle(&target, &bundle)) .await @@ -223,22 +247,29 @@ pub fn spawn_connect_push( /// The environment a terminal the app opens adds to its exec: the /// credential pairs for a copy-at-connect project, nothing otherwise (they -/// are already in the task's environment). A failure to gather them is -/// logged (no values) and the terminal opens without them. -pub fn terminal_env(project: &Project, settings: &AppSettings) -> Vec { - terminal_env_with(project, || credentials_for(project, settings)) +/// are already in the task's environment). Only the pairs ([`connect_env`]: +/// no SSH or CA file is read); the keychain is read off the async runtime. +pub async fn terminal_env(project: &Project, settings: &AppSettings) -> Vec { + if !copies_at_connect(project) { + return Vec::new(); + } + let (p, s) = (project.clone(), settings.clone()); + let gathered = tokio::task::spawn_blocking(move || connect_env(&with_secrets(&p), &s)) + .await + .map_err(|e| format!("could not gather them: {e}")); + terminal_env_with(project, || gathered) } -/// [`terminal_env`] over a given source of the bundle. +/// [`terminal_env`] over a given source of the pairs. pub fn terminal_env_with( project: &Project, - credentials: impl FnOnce() -> Result, + credentials: impl FnOnce() -> Result, String>, ) -> Vec { if !copies_at_connect(project) { return Vec::new(); } match credentials() { - Ok(b) => exec_env(&b.env), + Ok(env) => exec_env(&env), Err(e) => { log::warn!( "Project {}: the terminal opens without the project's credentials: {e}", diff --git a/app/src-tauri/src/host/aws_host/tests.rs b/app/src-tauri/src/host/aws_host/tests.rs index b219cae..2b39550 100644 --- a/app/src-tauri/src/host/aws_host/tests.rs +++ b/app/src-tauri/src/host/aws_host/tests.rs @@ -161,6 +161,12 @@ impl WorkspaceApi for Fake { self.log("create-ap"); Ok(if root.starts_with("/triple-c-workspaces/") { "fsap-ws" } else { "fsap-new" }.into()) } + async fn project_secret_value(&self, _name: &str) -> Result, AwsError> { + Ok(None) + } + async fn delete_project_secret(&self, _name: &str) -> Result<(), AwsError> { + Ok(()) + } async fn put_project_secret( &self, _name: &str, @@ -970,12 +976,12 @@ mod copy_at_connect { fn bundle() -> ConnectBundle { ConnectBundle { files: vec![ - ("ssh/id_ed25519".into(), b"PRIVATE".to_vec()), - ("ca/corp.crt".into(), b"PEM".to_vec()), + ("ssh/id_ed25519".into(), b"PRIVATE".to_vec().into()), + ("ca/corp.crt".into(), b"PEM".to_vec().into()), ], env: vec![ - ("GIT_TOKEN".into(), "ghp_x".into()), - ("MULTI".into(), "a=b\nc".into()), + ("GIT_TOKEN".into(), String::from("ghp_x").into()), + ("MULTI".into(), String::from("a=b\nc").into()), ], } } @@ -1019,7 +1025,7 @@ mod copy_at_connect { // A bad file key is refused before anything is uploaded under it. let t = FakeTarget::default(); - let bad = ConnectBundle { files: vec![("ssh/../x".into(), vec![])], env: vec![] }; + let bad = ConnectBundle { files: vec![("ssh/../x".into(), Vec::new().into())], env: vec![] }; assert!(push_connect_bundle(&t, &bad).await.is_err()); assert!(t.uploads.lock().unwrap().is_empty()); } @@ -1046,8 +1052,9 @@ mod copy_at_connect { ..Default::default() }; let b = connect_credentials(&p, &settings).unwrap(); - assert_eq!(b.files, vec![("ssh/id_ed25519".to_string(), b"KEY".to_vec())]); - let env: HashMap<_, _> = b.env.iter().cloned().collect(); + assert_eq!(b.files.len(), 1); + assert_eq!((b.files[0].0.as_str(), b.files[0].1.as_slice()), ("ssh/id_ed25519", &b"KEY"[..])); + let env: HashMap = b.env.iter().map(|(k, v)| (k.clone(), v.to_string())).collect(); assert_eq!(env["GIT_TOKEN"], "ghp_tok"); assert_eq!(env["MY_KEY"], "v1"); assert_eq!(env["GLOBAL_KEY"], "g"); @@ -1058,9 +1065,22 @@ mod copy_at_connect { ))); } + #[test] + fn a_terminal_gets_the_api_keys_even_when_a_file_is_unreadable() { + let mut p = connect_project(SecretsDelivery::CopyAtConnect); + p.ssh_key_path = Some("/nonexistent/ssh-dir".into()); + p.ca_cert_path = Some("/nonexistent/ca.pem".into()); + p.git_token = Some("ghp_tok".into()); + let settings = AppSettings::default(); + assert!(connect_credentials(&p, &settings).is_err(), "the push needs the files"); + let env: HashMap = + connect_env(&p, &settings).into_iter().map(|(k, v)| (k, v.to_string())).collect(); + assert_eq!(env["GIT_TOKEN"], "ghp_tok"); + } + #[test] fn a_copy_at_connect_terminal_carries_the_credentials_and_a_secrets_manager_one_does_not() { - let creds = || Ok(bundle()); + let creds = || Ok(bundle().env); let p = connect_project(SecretsDelivery::CopyAtConnect); let spec = crate::host::sessions::session_spec( vec!["bash".into(), "-l".into()], diff --git a/app/src-tauri/src/web_terminal/ws_handler.rs b/app/src-tauri/src/web_terminal/ws_handler.rs index 1344e56..56d9542 100644 --- a/app/src-tauri/src/web_terminal/ws_handler.rs +++ b/app/src-tauri/src/web_terminal/ws_handler.rs @@ -306,7 +306,7 @@ async fn handle_open( // AWS copy-at-connect: the credentials go into this terminal's own // environment (never the agent's). Empty for everything else. - let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()); + let env = crate::host::aws_host::terminal_env(&project, &state.settings_store.get()).await; let session_id = uuid::Uuid::new_v4().to_string(); let project_name = project.name.clone(); diff --git a/app/src/components/projects/home/config/AwsConfig.test.tsx b/app/src/components/projects/home/config/AwsConfig.test.tsx index 099c96a..50989bf 100644 --- a/app/src/components/projects/home/config/AwsConfig.test.tsx +++ b/app/src/components/projects/home/config/AwsConfig.test.tsx @@ -164,8 +164,18 @@ describe("HostSection", () => { renderHost(p); const note = screen.getByRole("note", { name: "Copied when you connect" }); expect(note).toHaveTextContent("Private repositories clone after you connect."); - expect(note).toHaveTextContent("Scheduled jobs can't use these credentials while the app is closed."); + expect(note).toHaveTextContent( + "Scheduled jobs and the setup script never get the API keys or custom variables; Git and SSH work once the app has connected.", + ); + expect(note).toHaveTextContent("~/.ssh is not kept on EFS"); + expect(note).toHaveTextContent("/mnt/triple-c-home/.ssh stay there until you remove them"); expect(note).not.toHaveTextContent(/gateway/); + expect(screen.getByText(/repositories and SSH keys included, starts fresh/)).toBeInTheDocument(); + cleanup(); + + // Ephemeral: nothing is kept anyway, so no SSH note. + renderHost({ ...p, aws: { ...p.aws!, storage: "ephemeral" } }); + expect(screen.getByRole("note", { name: "Copied when you connect" })).not.toHaveTextContent("~/.ssh"); cleanup(); p.aws = { ...p.aws!, gateway_sidecar: true }; diff --git a/app/src/components/projects/home/config/HostSection.tsx b/app/src/components/projects/home/config/HostSection.tsx index f3421ae..8861380 100644 --- a/app/src/components/projects/home/config/HostSection.tsx +++ b/app/src/components/projects/home/config/HostSection.tsx @@ -5,6 +5,7 @@ import { useAppState } from "../../../../store/appState"; import { COPY_AT_CONNECT_GATEWAY_NOTE, COPY_AT_CONNECT_HINT, + COPY_AT_CONNECT_SSH_NOTE, COPY_AT_CONNECT_WARNINGS, SECRETS_MANAGER_HINT, EFS_HINT, @@ -225,12 +226,14 @@ export default function HostSection({ project, save, disabled }: Props) {

) : aws.storage === "efs" ? (

- {EFS_HINT}. The Claude login, history and SSH keys are kept on EFS too. + {EFS_HINT}. The Claude login and history + {aws.secrets_delivery === "copy_at_connect" ? "" : ", and SSH keys,"} are kept on EFS too.

) : (

- The Claude login, history and SSH keys are kept on EFS between starts; everything - else, repositories included, starts fresh. + {aws.secrets_delivery === "copy_at_connect" + ? "The Claude login and history are kept on EFS between starts; everything else, repositories and SSH keys included, starts fresh." + : "The Claude login, history and SSH keys are kept on EFS between starts; everything else, repositories included, starts fresh."}

)} {keptWorkspaceFiles && ( @@ -368,6 +371,7 @@ export default function HostSection({ project, save, disabled }: Props) { {COPY_AT_CONNECT_WARNINGS.map((w) => (
  • {w}
  • ))} + {aws.storage !== "ephemeral" &&
  • {COPY_AT_CONNECT_SSH_NOTE}
  • } {aws.gateway_sidecar &&
  • {COPY_AT_CONNECT_GATEWAY_NOTE}
  • } diff --git a/app/src/lib/awsWorkspace.ts b/app/src/lib/awsWorkspace.ts index 52e1e22..2d0151b 100644 --- a/app/src/lib/awsWorkspace.ts +++ b/app/src/lib/awsWorkspace.ts @@ -144,8 +144,11 @@ export const COPY_AT_CONNECT_HINT = "API keys, the Git token, custom environment variables and the SSH and CA files are not stored in AWS: Triple-C copies them into the running workspace each time it connects."; export const COPY_AT_CONNECT_WARNINGS = [ "Private repositories clone after you connect.", - "Scheduled jobs can't use these credentials while the app is closed.", + "Scheduled jobs and the setup script never get the API keys or custom variables; Git and SSH work once the app has connected.", ]; +/** Hybrid/EFS under copy-at-connect (ruling R6): ~/.ssh stays off EFS. */ +export const COPY_AT_CONNECT_SSH_NOTE = + "~/.ssh is not kept on EFS: SSH keys copied in, or made in the workspace, are gone when it stops. Keys an earlier start left in /mnt/triple-c-home/.ssh stay there until you remove them."; export const COPY_AT_CONNECT_GATEWAY_NOTE = "The LLM gateway's keys are still stored in AWS: the gateway starts before Triple-C connects."; export const SECRETS_MANAGER_HINT = diff --git a/container/entrypoint-lib.sh b/container/entrypoint-lib.sh index 6258834..084800b 100644 --- a/container/entrypoint-lib.sh +++ b/container/entrypoint-lib.sh @@ -49,7 +49,12 @@ _triplec_as_user() { # so host keys land on EFS, and before anything writes ~/.claude. # >>> aws_home link_persistent_home() { - local home="$1" persist="$2" owner="$3" name link target + local home="$1" persist="$2" owner="$3" name link target names=".claude .claude.json .ssh" + # Copy-at-connect (ruling R6): ~/.ssh stays in the task's own storage, + # so the keys copied in at connect vanish with the task and never reach + # EFS. (Keys made in the workspace then don't persist either; a .ssh + # already on EFS from an earlier Secrets Manager period is left alone.) + [ "${triplec_connect:-0}" = 1 ] && names=".claude .claude.json" if [ ! -d "$persist" ] || [ -L "$persist" ]; then echo "entrypoint: warning — Hybrid storage, but $persist is not mounted; home state will not persist" return 0 @@ -58,7 +63,7 @@ link_persistent_home() { # claude (Claude Code's own atomic saves, write_claude_json) can always # create files beside the persisted ones. [ -z "$owner" ] || chown "$owner" "$persist" 2>/dev/null - for name in .claude .claude.json .ssh; do + for name in $names; do link="$home/$name" target="$persist/$name" if [ -L "$link" ] && [ "$(readlink "$link")" = "$target" ]; then @@ -86,7 +91,11 @@ link_persistent_home() { ln -s "$target" "$link" || echo "entrypoint: warning — could not link ~/$name to $target" [ -z "$owner" ] || chown -h "$owner" "$link" 2>/dev/null done - echo "entrypoint: Hybrid storage — ~/.claude, ~/.claude.json and ~/.ssh persist on $persist" + if [ "${triplec_connect:-0}" = 1 ]; then + echo "entrypoint: Hybrid storage — ~/.claude and ~/.claude.json persist on $persist; ~/.ssh does not (credentials are copied in at connect)" + else + echo "entrypoint: Hybrid storage — ~/.claude, ~/.claude.json and ~/.ssh persist on $persist" + fi } # The real directory behind a Hybrid ~/.ssh link, printed only when the link diff --git a/container/triple-c-apply-connect b/container/triple-c-apply-connect index 9a8cd43..1514541 100755 --- a/container/triple-c-apply-connect +++ b/container/triple-c-apply-connect @@ -58,12 +58,20 @@ if ! . "$lib"; then fi triplec_aws=1 +# The apply lock, also taken by --prepare so a prepare never empties the +# staging dir under a running apply. +take_lock() { + exec 9>"$shm/apply-connect.lock" && flock -w 120 9 \ + || say "warning — another apply is still running; going ahead" +} + prepare() { umask 077 if [ ! -d "$shm" ] || [ -L "$shm" ]; then say "$shm is missing (the workspace has no agent token?); nothing prepared" exit 1 fi + take_lock rm -rf "$stage" "$env_file" mkdir -m 700 "$stage" "$stage/ssh" "$stage/ca" || exit 1 } @@ -123,6 +131,9 @@ start_retry() { local mode="$1" out="$log_to" [ -w "$out" ] || out=/dev/null ( + # Not the apply lock: this outlives the apply (up to retry_wait plus + # the clone), and a later connect must not wait for it. + exec 9>&- trap '' HUP exec 8>"$shm/clone-retry.lock" && flock 8 if [ "$mode" = after-done ]; then @@ -144,6 +155,10 @@ start_retry() { [ "${#folders[@]}" -gt 0 ] || exit 0 echo "triple-c-apply-connect: cloning again with the project's credentials: ${folders[*]}" json=$(repos_for "${folders[@]}") + # Authentication goes through ~/.git-credentials (written by the + # apply above), not this variable. GIT_TOKEN is exported only for + # whatever in the clone reads it directly (a user's credential + # helper or hook), and only into this clone, never the agent's env. if [ -n "$connect_git_token" ]; then export GIT_TOKEN="$connect_git_token" fi @@ -173,8 +188,7 @@ apply() { say "$stage is missing; run --prepare and upload first" exit 1 fi - exec 9>"$shm/apply-connect.lock" && flock -w 120 9 \ - || say "warning — another apply is still running; going ahead" + take_lock # Empty staging dirs: nothing of that kind is configured. rmdir "$stage/ca" "$stage/ssh" 2>/dev/null