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:
1 parent
8510797c22
commit
820bfab25c
15 files changed
+502
-59
No files matched your search
@@ -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}",
|
||||
|
||||
@@ -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()],
|
||||
|
||||
Reference in new issue
Block a user