fix(aws): copy-at-connect review fixes — lock, ~/.ssh off EFS, secret history

- triple-c-apply-connect: the background re-clone closes the apply lock's
  fd, so a later connect never waits on it; --prepare takes the lock.
- R6: under copy-at-connect Hybrid/EFS no longer link ~/.ssh to EFS; the
  copied keys vanish with the task (UI says so, and that old keys on EFS
  stay until removed).
- R7: a copy-at-connect start that finds credentials still in the secret
  force-deletes it and creates it again (retrying ~30 s while AWS finishes),
  so no AWSPREVIOUS version keeps them.
- TRIPLE_C_CUSTOM_ENV (unsalted fingerprint) left out of the secret.
- Terminals get only the credential pairs (no file reads; keychain off the
  runtime), so a bad SSH/CA file never drops the API keys.
- ConnectBundle values are Zeroizing; UI copy updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-11-16 08:43:12 -08:00
1 parent 8510797c22
commit 820bfab25c
15 files changed
+502 -59

No files matched your search

+6
View File
@@ -112,6 +112,12 @@ impl WorkspaceApi for Fake {
) -> Result<String, AwsError> {
unreachable!()
}
async fn project_secret_value(&self, _n: &str) -> Result<Option<String>, AwsError> {
unreachable!()
}
async fn delete_project_secret(&self, _n: &str) -> Result<(), AwsError> {
unreachable!()
}
async fn put_project_secret(
&self,
_n: &str,
+151 -6
View File
@@ -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<Output = Result<String, AwsError>> + Send;
/// The secret's current `SecretString`; `None` when there is no secret.
fn project_secret_value(
&self,
name: &str,
) -> impl Future<Output = Result<Option<String>, 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<Output = Result<(), AwsError>> + 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<W: WorkspaceApi>(
};
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<String, String>, custom: &[String]) -> bool {
let Ok(old) = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(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<W: WorkspaceApi>(
api: &W,
name: &str,
kms_key_arn: &str,
json: &str,
tags: &[(String, String)],
timing: RecreateTiming,
) -> Result<String, AwsError> {
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<Option<String>, 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,
+123
View File
@@ -791,6 +791,14 @@ struct Fake {
revisions: Mutex<Vec<TaskDefinitionRef>>,
registered: Mutex<Vec<RegisterTaskDefinitionInput>>,
describes: Mutex<usize>,
/// The secret's value before this start (`project_secret_value`).
existing_secret: Mutex<Option<String>>,
/// Names force-deleted.
deleted_secrets: Mutex<Vec<String>>,
/// Calls `project_secret_value` got.
secret_reads: Mutex<usize>,
/// Puts that fail as "scheduled for deletion" before one succeeds.
pending_puts: Mutex<usize>,
}
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<Option<String>, 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<String, AwsError> {
{
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(), &copy_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(), &copy_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(), &copy_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<String, String> =
[("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<String> = 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");
}
@@ -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);
@@ -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");
}
+18 -5
View File
@@ -442,11 +442,25 @@ pub(crate) fn workspace_env_from_settings(
project: &Project,
settings: &crate::models::AppSettings,
) -> Result<crate::host::aws_host::AwsEnv, String> {
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)]
+57 -26
View File
@@ -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<Vec<u8>>;
pub type SecretString = Zeroizing<String>;
/// What copy-at-connect copies in.
#[derive(Clone, Default, PartialEq, Eq)]
#[derive(Clone, Default)]
pub struct ConnectBundle {
/// `ssh/<file>` / `ca/<file>`, as in the secret's `TRIPLEC_FILES`.
pub files: Vec<(String, Vec<u8>)>,
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<ConnectBundle, String> {
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<ConnectBundle, String> {
/// 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=<base64 of the value>` 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<u8> {
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<String> {
env.iter().map(|(k, v)| format!("{k}={v}")).collect()
pub fn exec_env(env: &[(String, SecretString)]) -> Vec<String> {
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<String> {
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<String> {
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<ConnectBundle, String>,
credentials: impl FnOnce() -> Result<Vec<(String, SecretString)>, String>,
) -> Vec<String> {
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}",
+28 -8
View File
@@ -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<Option<String>, 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<String, String> = 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<String, String> =
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()],
+1 -1
View File
@@ -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();