autoschematic-core 0.7.1

Core shared functionality for Autoschematic.
Documentation
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::{
    collections::HashMap,
    io,
    path::{Path, PathBuf},
    process::Stdio,
    sync::Arc,
    thread::JoinHandle,
    time::SystemTime,
};

use crate::{
    bundle::UnbundleResponseElement,
    config::Spec,
    connector::{
        Connector, ConnectorOutbox, DocIdent, FilterResponse, GetDocResponse, GetResourceResponse, OpExecResponse,
        PlanResponseElement, SkeletonResponse, VirtToPhyResponse,
    },
    diag::DiagnosticResponse,
    grpc_bridge,
    keystore::KeyStore,
    secret::SealedSecret,
    tarpc_bridge::{self},
    util::passthrough_secrets_from_env,
};
use anyhow::bail;
use async_trait::async_trait;

use process_wrap::tokio::*;
use rand::{Rng, distr::Alphanumeric};

/// This module handles unsandboxed execution of connector instances.
pub struct UnsandboxConnectorHandle {
    client: Arc<dyn Connector>,
    socket: PathBuf,
    error_dump: PathBuf,
    read_thread: Option<JoinHandle<()>>,
    child: Box<dyn process_wrap::tokio::TokioChildWrapper>,
}

fn random_socket_path() -> PathBuf {
    loop {
        let socket_s: String = rand::rng().sample_iter(&Alphanumeric).take(20).map(char::from).collect();

        let mut socket = PathBuf::from("/tmp/").join(socket_s);

        socket.set_extension("sock");

        if let Ok(false) = socket.try_exists() {
            tracing::info!("Creating socket at {:?}", socket);
            return socket;
        }
    }
}

fn random_error_dump_path() -> PathBuf {
    loop {
        let dump_s: String = rand::rng().sample_iter(&Alphanumeric).take(20).map(char::from).collect();

        let mut dump = PathBuf::from("/tmp/").join(dump_s);

        dump.set_extension("dump");

        if let Ok(false) = dump.try_exists() {
            return dump;
        }
    }
}

#[async_trait]
impl Connector for UnsandboxConnectorHandle {
    async fn new(name: &str, prefix: &Path, outbox: ConnectorOutbox) -> Result<Arc<dyn Connector>, anyhow::Error> {
        bail!("Connector::new() for UnsandboxConnectorHandle is a stub!")
        // <TarpcConnectorClient as Connector>::new(name, prefix, outbox).await
    }
    async fn init(&self) -> Result<(), anyhow::Error> {
        Connector::init(&self.client).await
    }

    async fn filter(&self, addr: &Path) -> Result<FilterResponse, anyhow::Error> {
        Connector::filter(&self.client, addr).await
    }

    async fn list(&self, subpath: &Path) -> anyhow::Result<Vec<PathBuf>> {
        Connector::list(&self.client, subpath).await
    }

    async fn subpaths(&self) -> anyhow::Result<Vec<PathBuf>> {
        Connector::subpaths(&self.client).await
    }

    async fn get(&self, addr: &Path) -> Result<Option<GetResourceResponse>, anyhow::Error> {
        Connector::get(&self.client, addr).await
    }

    async fn plan(
        &self,
        addr: &Path,
        current: Option<Vec<u8>>,
        desired: Option<Vec<u8>>,
    ) -> Result<Vec<PlanResponseElement>, anyhow::Error> {
        Connector::plan(&self.client, addr, current, desired).await
    }

    async fn op_exec(&self, addr: &Path, op: &str) -> Result<OpExecResponse, anyhow::Error> {
        Connector::op_exec(&self.client, addr, op).await
    }

    async fn addr_virt_to_phy(&self, addr: &Path) -> Result<VirtToPhyResponse, anyhow::Error> {
        Connector::addr_virt_to_phy(&self.client, addr).await
    }

    async fn addr_phy_to_virt(&self, addr: &Path) -> Result<Option<PathBuf>, anyhow::Error> {
        Connector::addr_phy_to_virt(&self.client, addr).await
    }

    async fn get_skeletons(&self) -> Result<Vec<SkeletonResponse>, anyhow::Error> {
        Connector::get_skeletons(&self.client).await
    }

    async fn get_docstring(&self, addr: &Path, ident: DocIdent) -> Result<Option<GetDocResponse>, anyhow::Error> {
        Connector::get_docstring(&self.client, addr, ident).await
    }

    async fn eq(&self, addr: &Path, a: &[u8], b: &[u8]) -> Result<bool, anyhow::Error> {
        Connector::eq(&self.client, addr, a, b).await
    }

    async fn diag(&self, addr: &Path, a: &[u8]) -> Result<Option<DiagnosticResponse>, anyhow::Error> {
        Connector::diag(&self.client, addr, a).await
    }

    async fn unbundle(&self, addr: &Path, resource: &[u8]) -> Result<Vec<UnbundleResponseElement>, anyhow::Error> {
        Connector::unbundle(&self.client, addr, resource).await
    }
}

pub async fn launch_server_binary(
    spec: &Spec,
    shortname: &str,
    prefix: &Path,
    env: &HashMap<String, String>,
    outbox: ConnectorOutbox,
    keystore: Option<Arc<dyn KeyStore>>,
) -> anyhow::Result<UnsandboxConnectorHandle> {
    let mut env = env.clone();

    let socket = random_socket_path();
    let error_dump = random_error_dump_path();

    if let Some(keystore) = keystore {
        env = keystore.unseal_env_map(&env)?;
    } else {
        env = passthrough_secrets_from_env(&env)?;
    }

    if let Some(spec_pre_command) = spec.pre_command()? {
        let mut command = TokioCommandWrap::with_new(spec_pre_command.binary, |command| {
            command.args(spec_pre_command.args);
            command.stderr(io::stderr());
            command.stdout(io::stderr());
            command.kill_on_drop(true);
        });

        #[cfg(unix)]
        {
            command.wrap(ProcessGroup::leader());
        }
        #[cfg(windows)]
        {
            command.wrap(JobObject);
        }

        let status = Box::into_pin(command.spawn()?.wait()).await?;

        if !status.success() {
            bail!(
                "launch_server_binary: pre_command failed: \n {:?} \n {:?}",
                command.command(),
                status.code()
            )
        }
    }

    let spec_command = spec.command()?;

    let mut command = TokioCommandWrap::with_new(spec_command.binary, |command| {
        command.args(spec_command.args);
        command.args([shortname.into(), prefix.into(), socket.clone(), error_dump.clone()]);
        command.stdout(io::stderr());
        command.stderr(io::stderr());
        command.kill_on_drop(true);

        for (key, val) in env {
            command.env(key, val);
        }
    });

    #[cfg(unix)]
    {
        command.wrap(ProcessGroup::leader());
    }
    #[cfg(windows)]
    {
        command.wrap(JobObject);
    }

    // let mut pre_command = None;

    // let mut command = match spec {
    //     Spec::Binary { path, protocol } => {
    //         let mut binary_path = path.clone();
    //         if !binary_path.is_file() {
    //             binary_path = which::which(binary_path)?;
    //         }

    //         if !binary_path.is_file() {
    //             bail!("launch_server_binary: {}: not found", binary_path.display())
    //         }
    //         let mut command = tokio::process::Command::new(binary_path);
    //         let args = [shortname.into(), prefix.into(), socket.clone(), error_dump.clone()];
    //         command.args(args);
    //         command.stdout(io::stderr());
    //         command
    //     }
    //     Spec::Cargo { name, .. } => {
    //         let cargo_home = match std::env::var("CARGO_HOME") {
    //             Ok(p) => PathBuf::from(p),
    //             Err(_) => {
    //                 let Ok(home) = std::env::var("HOME") else {
    //                     bail!("$HOME not set!");
    //                 };
    //                 PathBuf::from(home).join(".cargo")
    //             }
    //         };

    //         // TODO Also parse `binary` and check .cargo/.cargo.toml
    //         let binary_path = cargo_home.join("bin").join(name);

    //         if !binary_path.is_file() {
    //             bail!("launch_server_binary: {}: not found", binary_path.display())
    //         }
    //         let mut command = tokio::process::Command::new(binary_path);
    //         let args = [shortname.into(), prefix.into(), socket.clone(), error_dump.clone()];
    //         command.args(args);
    //         command.stdout(io::stderr());
    //         command
    //     }
    //     Spec::CargoLocal {
    //         path, binary, features, ..
    //     } => {
    //         let manifest_path = path.join("Cargo.toml");
    //         if !manifest_path.is_file() {
    //             bail!("launch_server_binary: No Cargo.toml under {}", path.display())
    //         }

    //         let mut build_command = tokio::process::Command::new("cargo");
    //         build_command.kill_on_drop(true);

    //         build_command.args(["build", "--release", "--manifest-path", manifest_path.to_str().unwrap()]);
    //         if let Some(binary) = binary {
    //             build_command.args(["--bin", binary]);
    //         }
    //         if let Some(features) = features
    //             && !features.is_empty()
    //         {
    //             build_command.args(["--features", &features.join(",")]);
    //         }

    //         pre_command = Some(build_command);

    //         let mut command = tokio::process::Command::new("cargo");
    //         command.kill_on_drop(true);
    //         command.args(["run", "--release", "--manifest-path", manifest_path.to_str().unwrap()]);
    //         if let Some(binary) = binary {
    //             command.args(["--bin", binary]);
    //         }
    //         if let Some(features) = features
    //             && !features.is_empty()
    //         {
    //             command.args(["--features", &features.join(",")]);
    //         }
    //         command.args([String::from("--"), shortname.to_string()]);
    //         command.args([prefix, &socket, &error_dump]);
    //         command.stdout(io::stderr());
    //         command
    //     }
    //     Spec::TypescriptLocal { path } => {
    //         if !path.is_file() {
    //             bail!("launch_server_binary: {}: not found", path.display())
    //         }
    //         let mut command = tokio::process::Command::new("tsx");
    //         let args = [
    //             path.into(),
    //             shortname.into(),
    //             prefix.into(),
    //             socket.clone(),
    //             error_dump.clone(),
    //         ];
    //         command.args(args);
    //         command.stdout(io::stderr());
    //         command
    //     }
    // };

    // for (key, val) in env {
    //     command.env(key, val);
    // }

    // if let Some(mut pre_command) = pre_command {
    //     let output = pre_command
    //         .stdin(Stdio::inherit())
    //         .stdout(Stdio::inherit())
    //         .stderr(Stdio::inherit())
    //         .output()
    //         .await?;

    //     if !output.status.success() {
    //         bail!("Pre-command failed: {:?}: {}", pre_command, output.status)
    //     }
    // }

    let child = command.spawn()?;

    tracing::info!("Launching client at {:?}", socket);

    let client = match spec.protocol() {
        crate::config::Protocol::Tarpc => tarpc_bridge::launch_client(&socket).await?,
        crate::config::Protocol::Grpc => grpc_bridge::launch_client(&socket).await?,
    };

    // let client = match spec {
    //     Spec::Binary { protocol, .. } => match protocol {
    //         crate::config::Protocol::Tarpc => tarpc_bridge::launch_client(&socket).await?,
    //         crate::config::Protocol::Grpc => grpc_bridge::launch_client(&socket).await?,
    //     },
    //     Spec::Cargo { protocol, .. } => match protocol {
    //         crate::config::Protocol::Tarpc => tarpc_bridge::launch_client(&socket).await?,
    //         crate::config::Protocol::Grpc => grpc_bridge::launch_client(&socket).await?,
    //     },
    //     Spec::CargoLocal { protocol, .. } => match protocol {
    //         crate::config::Protocol::Tarpc => tarpc_bridge::launch_client(&socket).await?,
    //         crate::config::Protocol::Grpc => grpc_bridge::launch_client(&socket).await?,
    //     },
    //     Spec::TypescriptLocal { .. } => grpc_bridge::launch_client(&socket).await?,
    // };

    tracing::info!("Launched client.");

    Ok(UnsandboxConnectorHandle {
        client: Arc::new(client),
        socket,
        error_dump,
        read_thread: None,
        child,
    })
}

// TODO this is probably unneeded with kill_on_drop()
impl Drop for UnsandboxConnectorHandle {
    fn drop(&mut self) {
        tracing::info!("DROP on UnsandboxConnectorHandle! Killing subprocess");
        self.child.start_kill().unwrap();
        self.child.try_wait().unwrap();

        match std::fs::remove_file(&self.socket) {
            Ok(_) => {}
            Err(e) => tracing::warn!("Couldn't remove socket {:?}: {}", self.socket, e),
        }

        match std::fs::remove_file(&self.error_dump) {
            Ok(_) => {}
            Err(e) => tracing::warn!("Couldn't remove error_dump {:?}: {}", self.error_dump, e),
        }

        if self.read_thread.is_some() {}
    }
}

// pub fn unseal_secrets_to_folder(
//     keystore: &Box<dyn KeyStore>,
//     prefix: &Path,
//     connector_shortname: &str,
//     secret_mount: &Path,
// ) -> anyhow::Result<()> {
//     for path in WalkDir::new(prefix.join(".secrets").join(connector_shortname))
//         .into_iter()
//         .filter_map(|entry| entry.ok())
//         .map(|entry| PathBuf::from(entry.path()))
//         .filter(|path| path.is_file())
//     {
//         tracing::error!("unseal_secrets: walk: {:?}", &path);
//         let secret_file = std::fs::read_to_string(&path)?;
//         let secrets: Vec<SealedSecret> = serde_json::from_str(&secret_file)?;
//         let secret_text = keystore.unseal_secret(secrets.first().unwrap())?;
//         let out_dir = secret_mount.join(prefix).join(connector_shortname);
//         let out_path = path.strip_prefix(prefix.join(".secrets").join(connector_shortname))?;
//         std::fs::create_dir_all(out_dir.join(out_path).parent().unwrap())?;
//         std::fs::write(out_dir.join(out_path), secret_text)?;
//     }
//     Ok(())
// }

// fn seal_new_secrets_from_folder(
//     keystore: impl KeyStore,
//     prefix: &Path,
//     domain: &str,
//     connector_shortname: &str,
//     newer_than: SystemTime,
//     secret_mount: &Path,
// ) -> anyhow::Result<()> {
//     // For each secret in the sandboxed connector's secret mount,
//     // if it's newer than newer_than, seal it and write it to the repo.
//     for path in WalkDir::new(secret_mount.join(connector_shortname))
//         .into_iter()
//         .filter_map(|entry| entry.ok())
//         .filter(|entry| {
//             entry
//                 .metadata()
//                 .is_ok_and(|metadata| metadata.modified().is_ok_and(|system_time| system_time > newer_than))
//         })
//         .map(|entry| PathBuf::from(entry.path()))
//         .filter(|path| path.is_file())
//     {
//         let secret_file = std::fs::read_to_string(&path)?;
//         let mut sealed_secrets = Vec::new();
//         for key_id in keystore.list()? {
//             let sealed = keystore.seal_secret(domain, &key_id, &secret_file)?;
//             sealed_secrets.push(sealed);
//         }
//         let sealed_json = serde_json::to_string_pretty(&sealed_secrets)?;
//         std::fs::write(prefix.join(".secrets").join(connector_shortname).join(&path), sealed_json)?;
//     }
//     Ok(())
// }