Skip to content

Commit eb7bd39

Browse files
committed
rust: hide tcp module from intercept
1 parent e956171 commit eb7bd39

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

rust/bear/src/bin/wrapper.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
extern crate core;
1919

2020
use anyhow::{Context, Result};
21+
use bear::intercept::create_reporter;
2122
use bear::intercept::supervise::supervise;
22-
use bear::intercept::tcp::ReporterOnTcp;
23-
use bear::intercept::Reporter;
24-
use bear::intercept::KEY_DESTINATION;
2523
use bear::intercept::{Event, Execution, ProcessId};
2624
use std::path::{Path, PathBuf};
2725

@@ -103,14 +101,9 @@ fn report(execution: Execution) -> Result<()> {
103101
execution,
104102
};
105103

106-
// Get the reporter address from the environment
107-
std::env::var(KEY_DESTINATION)
108-
.with_context(|| format!("${} is missing from the environment", KEY_DESTINATION))
109-
// Create a new reporter
110-
.and_then(ReporterOnTcp::new)
111-
.with_context(|| "Cannot create TCP execution reporter")
112-
// Report the execution
113-
.and_then(|reporter| reporter.report(event))
104+
create_reporter()
105+
.with_context(|| "Cannot create execution reporter")?
106+
.report(event)
114107
.with_context(|| "Sending execution failed")
115108
}
116109

rust/bear/src/intercept/mod.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,38 @@
99
//! The module provides abstractions for the reporter and the collector. And it also defines
1010
//! the data structures that are used to represent the events.
1111
12-
use crate::intercept::supervise::supervise;
12+
pub mod supervise;
13+
mod tcp;
14+
1315
use crate::{args, config};
16+
use anyhow::Context;
1417
use serde::{Deserialize, Serialize};
1518
use std::collections::HashMap;
1619
use std::path::{Path, PathBuf};
1720
use std::process::{Command, ExitCode};
1821
use std::sync::mpsc::{channel, Receiver, Sender};
1922
use std::sync::Arc;
20-
use std::{env, fmt, thread};
21-
22-
pub mod supervise;
23-
pub mod tcp;
23+
use std::{fmt, thread};
24+
use supervise::supervise;
2425

2526
/// Declare the environment variables used by the intercept mode.
26-
pub const KEY_DESTINATION: &str = "INTERCEPT_REPORTER_ADDRESS";
27-
pub const KEY_PRELOAD_PATH: &str = "LD_PRELOAD";
27+
const KEY_DESTINATION: &str = "INTERCEPT_COLLECTOR_ADDRESS";
28+
const KEY_PRELOAD_PATH: &str = "LD_PRELOAD";
29+
30+
/// Creates a new reporter instance.
31+
///
32+
/// This function is supposed to be called from another process than the collector.
33+
/// Therefore, the reporter destination is passed as an environment variable.
34+
/// The reporter destination is the address of the collector service.
35+
pub fn create_reporter() -> anyhow::Result<Box<dyn Reporter>> {
36+
let reporter =
37+
// Get the reporter address from the environment variable
38+
std::env::var(KEY_DESTINATION)
39+
.with_context(|| format!("${} is missing from the environment", KEY_DESTINATION))
40+
// Create a new reporter
41+
.and_then(tcp::ReporterOnTcp::new)?;
42+
Ok(Box::new(reporter))
43+
}
2844

2945
/// Represents the remote sink of supervised process events.
3046
///
@@ -299,7 +315,7 @@ impl InterceptEnvironment {
299315
InterceptEnvironment::Wrapper {
300316
bin_dir, address, ..
301317
} => {
302-
let path_original = env::var("PATH").unwrap_or_else(|_| String::new());
318+
let path_original = std::env::var("PATH").unwrap_or_else(|_| String::new());
303319
let path_updated = InterceptEnvironment::insert_to_path(
304320
&path_original,
305321
Self::path_to_string(bin_dir.path()),
@@ -310,7 +326,8 @@ impl InterceptEnvironment {
310326
]
311327
}
312328
InterceptEnvironment::Preload { path, address, .. } => {
313-
let path_original = env::var(KEY_PRELOAD_PATH).unwrap_or_else(|_| String::new());
329+
let path_original =
330+
std::env::var(KEY_PRELOAD_PATH).unwrap_or_else(|_| String::new());
314331
let path_updated = InterceptEnvironment::insert_to_path(
315332
&path_original,
316333
Self::path_to_string(path),

0 commit comments

Comments
 (0)