forked from metalbear-co/mirrord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.rs
230 lines (205 loc) · 7.87 KB
/
execution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use std::{
collections::{HashMap, HashSet},
time::Duration,
};
use mirrord_config::LayerConfig;
use mirrord_progress::Progress;
use mirrord_protocol::{ClientMessage, DaemonMessage, EnvVars, GetEnvVarsRequest};
#[cfg(target_os = "macos")]
use mirrord_sip::sip_patch;
use serde::Serialize;
use tokio::{
io::{AsyncBufReadExt, BufReader},
process::{Child, Command},
};
use tracing::trace;
use crate::{
connection::{create_and_connect, AgentConnectInfo},
error::{CliError, CliError::IncompatibleWithTargetless},
extract::extract_library,
Result,
};
#[cfg(target_os = "linux")]
const INJECTION_ENV_VAR: &str = "LD_PRELOAD";
#[cfg(target_os = "macos")]
const INJECTION_ENV_VAR: &str = "DYLD_INSERT_LIBRARIES";
/// Struct for holding the execution information
/// What agent to connect to, what environment variables to set
#[derive(Debug, Serialize)]
pub(crate) struct MirrordExecution {
pub environment: HashMap<String, String>,
#[serde(skip)]
child: Child,
/// The path to the patched binary, if patched for SIP sidestepping.
pub patched_path: Option<String>,
}
impl MirrordExecution {
pub(crate) async fn start<P>(
config: &LayerConfig,
// We only need the executable on macos, for SIP handling.
#[cfg(target_os = "macos")] executable: Option<&str>,
progress: &P,
timeout: Option<u64>,
) -> Result<Self>
where
P: Progress + Send + Sync,
{
Self::check_config_for_targetless_agent(config)?;
let lib_path = extract_library(None, progress, true)?;
let mut env_vars = HashMap::new();
let (connect_info, mut connection) = create_and_connect(config, progress).await?;
let (env_vars_exclude, env_vars_include) = match (
config
.feature
.env
.exclude
.clone()
.map(|exclude| exclude.join(";")),
config
.feature
.env
.include
.clone()
.map(|include| include.join(";")),
) {
(Some(exclude), Some(include)) => {
return Err(CliError::InvalidEnvConfig(include, exclude))
}
(Some(exclude), None) => (HashSet::from(EnvVars(exclude)), HashSet::new()),
(None, Some(include)) => (HashSet::new(), HashSet::from(EnvVars(include))),
(None, None) => (HashSet::new(), HashSet::from(EnvVars("*".to_owned()))),
};
if !env_vars_exclude.is_empty() || !env_vars_include.is_empty() {
// TODO: Handle this error. We're just ignoring it here and letting -layer crash later.
let _codec_result = connection
.sender
.send(ClientMessage::GetEnvVarsRequest(GetEnvVarsRequest {
env_vars_filter: env_vars_exclude,
env_vars_select: env_vars_include,
}))
.await;
match tokio::time::timeout(
Duration::from_secs(config.agent.communication_timeout.unwrap_or(30).into()),
connection.receiver.recv(),
)
.await
{
Ok(Some(DaemonMessage::GetEnvVarsResponse(Ok(remote_env)))) => {
trace!("DaemonMessage::GetEnvVarsResponse {:#?}!", remote_env.len());
env_vars.extend(remote_env);
if let Some(overrides) = &config.feature.env.overrides {
env_vars.extend(overrides.iter().map(|(k, v)| (k.clone(), v.clone())));
}
}
Err(_) => return Err(CliError::GetEnvironmentTimeout),
Ok(x) => return Err(CliError::InvalidMessage(format!("{x:#?}"))),
};
}
let lib_path: String = lib_path.to_string_lossy().into();
// Set LD_PRELOAD/DYLD_INSERT_LIBRARIES
// If already exists, we append.
if let Ok(v) = std::env::var(INJECTION_ENV_VAR) {
env_vars.insert(INJECTION_ENV_VAR.to_string(), format!("{v}:{lib_path}"))
} else {
env_vars.insert(INJECTION_ENV_VAR.to_string(), lib_path)
};
// stderr is inherited so we can see logs/errors.
let mut proxy_command =
Command::new(std::env::current_exe().map_err(CliError::CliPathError)?);
// Set timeout when running from extension to be 30 seconds
// since it might need to compile, build until it runs the actual process
// and layer connects
proxy_command
.arg("intproxy")
.stdout(std::process::Stdio::piped())
.stdin(std::process::Stdio::null());
if let Some(timeout) = timeout {
proxy_command.arg("-t").arg(timeout.to_string());
}
match &connect_info {
AgentConnectInfo::DirectKubernetes(name, port) => {
proxy_command.env("MIRRORD_CONNECT_AGENT", name);
proxy_command.env("MIRRORD_CONNECT_PORT", port.to_string());
}
AgentConnectInfo::Operator => {}
};
let mut proxy_process = proxy_command
.spawn()
.map_err(CliError::InternalProxyExecutionFailed)?;
let stdout = proxy_process
.stdout
.take()
.ok_or(CliError::InternalProxyStdoutError)?;
let port: u16 = BufReader::new(stdout)
.lines()
.next_line()
.await
.map_err(CliError::InternalProxyReadError)?
.ok_or(CliError::InternalProxyPortReadError)?
.parse()
.map_err(CliError::InternalProxyPortParseError)?;
// Provide details for layer to connect to agent via internal proxy
env_vars.insert(
"MIRRORD_CONNECT_TCP".to_string(),
format!("127.0.0.1:{port}"),
);
#[cfg(target_os = "macos")]
let patched_path = executable
.and_then(|exe| {
sip_patch(
exe,
&config
.sip_binaries
.clone()
.map(|x| x.to_vec())
.unwrap_or_default(),
)
.transpose() // We transpose twice to propagate a possible error out of this
// closure.
})
.transpose()?;
#[cfg(not(target_os = "macos"))]
let patched_path = None;
Ok(Self {
environment: env_vars,
child: proxy_process,
patched_path,
})
}
/// Some features are incompatible with targetless agents, return error if they are set and
/// there is no target.
///
/// # Errors
///
/// [`IncompatibleWithTargetless`]
fn check_config_for_targetless_agent(config: &LayerConfig) -> Result<()> {
if config.target.is_none() {
if config.feature.network.incoming.is_steal() {
Err(IncompatibleWithTargetless("Steal mode".into()))?
}
if config.agent.ephemeral {
Err(IncompatibleWithTargetless(
"Using an ephemeral container for the agent".into(),
))?
}
if config.agent.pause {
Err(IncompatibleWithTargetless(
"The target pause feature".into(),
))?
}
}
Ok(())
}
/// Wait for the internal proxy to exit.
/// Required when called from extension since sometimes the extension
/// cleans up the process when the parent process exits, so we need the parent to stay alive
/// while the internal proxy is running.
/// See https://github.com/metalbear-co/mirrord/issues/1211
pub(crate) async fn wait(mut self) -> Result<()> {
self.child
.wait()
.await
.map_err(CliError::InternalProxyWaitError)?;
Ok(())
}
}