forked from bottlerocket-os/bottlerocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_request.rs
526 lines (496 loc) · 19.4 KB
/
log_request.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! Provides the list of log requests that `logdog` will run and provides the handler functions to
//! run them.
//!
//! # Static Log Requests
//!
//! At build time, the `build.rs` file checks which variant is being built and creates a symlink
//! file which points to the log requests for the current variant. This file is named `logdog.conf`.
//! We load `logdog.conf` and `logdog.common.conf` files into static strings at compile time, and
//! these provide the list of log requests that `logdog` will run.
use crate::error::{self, Result};
use datastore::deserialization::from_map;
use datastore::serialization::to_pairs;
use glob::{glob, Pattern};
use reqwest::blocking::{Client, Response};
use snafu::{ensure, OptionExt, ResultExt};
use std::collections::HashSet;
use std::fs;
use std::fs::File;
use std::path::Path;
use std::process::{Command, Stdio};
use url::Url;
use walkdir::WalkDir;
/// The directory containing all `logdog` log requests.
pub(crate) const REQUESTS_DIR: &str = "/usr/share/logdog.d";
/// Patterns to filter from settings output. These follow the Unix shell style pattern outlined
/// here: https://docs.rs/glob/0.3.0/glob/struct.Pattern.html.
const SENSITIVE_SETTINGS_PATTERNS: &[&str] = &[
"*.user-data",
"settings.kubernetes.bootstrap-token",
// Can contain a username:password component
"settings.network.https-proxy",
"settings.kubernetes.server-key",
"settings.container-registry.credentials",
// Can be stored in settings.aws.credentials, but user can also add creds here
"settings.aws.config",
"settings.aws.credentials",
];
/// Returns the list of log requests to run by combining `VARIANT_REQUESTS` and `COMMON_REQUESTS`.
/// These are read at compile time from files named `logdog.conf` and `logdog.common.conf`
/// respectively.
pub(crate) fn log_requests() -> Result<Vec<String>> {
Ok(WalkDir::new(REQUESTS_DIR)
.max_depth(1)
.min_depth(1)
.sort_by_file_name()
.into_iter()
.collect::<std::result::Result<Vec<_>, _>>()?
.into_iter()
.filter(|entry| entry.path().is_file())
.map(|entry| {
eprintln!("Checking: {}", entry.path().display());
fs::read_to_string(entry.path()).context(error::FileReadSnafu { from: entry.path() })
})
.collect::<std::result::Result<Vec<_>, _>>()?
.iter()
.flat_map(|commands| commands.lines())
.map(std::borrow::ToOwned::to_owned)
.filter(|command| !command.is_empty() && !command.trim_start().starts_with('#'))
.collect())
}
/// A logdog `LogRequest` represents a line from the config file. It starts with a "mode" that
/// specifies what type of request it is, e.g. `exec ` for a command or `http` for an HTTP get
/// request. Some modes then require a `filename` that determines where the data will be saved in
/// the output tarball. The final field is `instructions` which is any additional information
/// needed by the given mode. For example, an `exec` requests' instructions will include the
/// program and program arguments. An `http` request's instructions will be the URL.
///
/// # Examples
///
/// This request will run the echo program with the arguments `hello` `world` and write the output
/// to a file named `hello.txt`. In this example `exec` is the mode, `hello.txt` is the output
/// filename, and `echo hello world` is the instructions.
///
/// ```text
/// exec hello.txt echo hello world
/// ```
///
/// This request will run an HTTP get request to the url `http://example.com` and write the response
/// body to `example.txt`:
///
/// ```text
/// http example.txt http://example.com
/// ```
///
/// This request will copy a file from `/etc/some/conf` to a file name `some-conf`:
///
/// ```text
/// file some-conf /etc/some/conf
/// ```
///
/// This request will copy files with a known prefix into the tarball; this can be useful for dated
/// log files, for example.
///
/// ```text
/// glob /var/log/my-app.log*
/// ```
#[derive(Debug, Clone)]
struct LogRequest<'a> {
/// The log request mode. For example `exec`, `http`, `file`, or `glob`.
mode: &'a str,
/// The filename that the logs will be written to, if appropriate for the mode.
filename: &'a str,
/// Any additional instructions or commands needed to fulfill the log request. For example, with
/// `exec` this will be a program invocation like `echo hello world`. For an `http` request this
/// will be a URL. For a `file` request this will be the source file path.
instructions: &'a str,
}
/// This is used in error construction.
impl ToString for LogRequest<'_> {
fn to_string(&self) -> String {
if self.instructions.is_empty() {
format!("{} {}", self.mode, self.filename)
} else {
format!("{} {} {}", self.mode, self.filename, self.instructions)
}
}
}
/// Runs a `LogRequest` and writes its output to a file in `tempdir`.
pub(crate) async fn handle_log_request<S, P>(request: S, tempdir: P) -> Result<()>
where
S: AsRef<str>,
P: AsRef<Path>,
{
let request = request.as_ref();
let mut iter = request.splitn(3, ' ');
let mode = iter.next().context(error::ModeMissingSnafu)?;
let req = if mode == "glob" {
// for glob request format is "glob <pattern>"
LogRequest {
mode,
filename: "",
instructions: iter.next().context(error::PatternMissingSnafu)?,
}
} else {
// Get the second token (output filename) and put the remainder of the
// log request into the instructions field (or default to an empty string).
LogRequest {
mode,
filename: iter
.next()
.context(error::FilenameMissingSnafu { request })?,
instructions: iter.next().unwrap_or(""),
}
};
// execute the log request with the correct handler based on the mode field.
match req.mode {
"settings" => handle_settings_request(&req, tempdir).await?,
"exec" => handle_exec_request(&req, tempdir)?,
"http" | "https" => handle_http_request(&req, tempdir)?,
"file" => handle_file_request(&req, tempdir)?,
"glob" => handle_glob_request(&req, tempdir)?,
unmatched => {
return Err(error::Error::UnhandledRequest {
mode: unmatched.into(),
request: request.into(),
})
}
}
Ok(())
}
/// Requests settings from the API, filters them, and writes the output to `tempdir`
async fn handle_settings_request<P>(request: &LogRequest<'_>, tempdir: P) -> Result<()>
where
P: AsRef<Path>,
{
let settings = get_settings().await?;
let mut settings_map = to_pairs(&settings).context(error::SerializeSettingsSnafu)?;
// Filter all settings that match any of the "sensitive" patterns
for pattern in SENSITIVE_SETTINGS_PATTERNS {
let pattern =
Pattern::new(pattern).context(error::ParseGlobPatternSnafu { pattern: *pattern })?;
settings_map.retain(|k, _| !pattern.matches(k.name().as_str()))
}
// Serialize the map back to a `Settings` to remove the escaping so it writes nicely to file
let settings: model::Settings =
from_map(&settings_map).context(error::DeserializeSettingsSnafu)?;
let outpath = tempdir.as_ref().join(request.filename);
let outfile = File::create(&outpath).context(error::FileCreateSnafu { path: &outpath })?;
serde_json::to_writer_pretty(&outfile, &settings)
.context(error::FileWriteSnafu { path: &outpath })?;
Ok(())
}
/// Uses `apiclient` to request all settings from the apiserver and deserializes into a `Settings`
async fn get_settings() -> Result<model::Settings> {
let uri = constants::API_SETTINGS_URI;
let (_status, response_body) = apiclient::raw_request(constants::API_SOCKET, uri, "GET", None)
.await
.context(error::ApiClientSnafu { uri })?;
serde_json::from_str(&response_body).context(error::SettingsJsonSnafu)
}
/// Runs an `exec` `LogRequest`'s `instructions` and writes its output to to `tempdir`.
fn handle_exec_request<P>(request: &LogRequest<'_>, tempdir: P) -> Result<()>
where
P: AsRef<Path>,
{
let split =
shell_words::split(request.instructions).with_context(|_| error::CommandParseSnafu {
command: request.to_string(),
})?;
let (command, args) = split
.split_first()
.with_context(|| error::CommandMissingSnafu {
request: request.to_string(),
})?;
let outpath = tempdir.as_ref().join(request.filename);
let ofile = File::create(&outpath).context(error::CommandOutputFileSnafu { path: &outpath })?;
let stderr_file = ofile
.try_clone()
.context(error::CommandErrFileSnafu { path: &outpath })?;
Command::new(command)
.args(args)
.stdout(Stdio::from(ofile))
.stderr(Stdio::from(stderr_file))
.spawn()
.with_context(|_| error::CommandSpawnSnafu {
command: request.to_string(),
})?
.wait_with_output()
.with_context(|_| error::CommandFinishSnafu {
command: request.to_string(),
})?;
Ok(())
}
/// Executes an `http` `LogRequest` and writes the response body to a file in `tempdir`.
fn handle_http_request<P>(request: &LogRequest<'_>, tempdir: P) -> Result<()>
where
P: AsRef<Path>,
{
ensure!(
!request.instructions.is_empty(),
error::HttpMissingUrlSnafu {
request: request.to_string(),
}
);
let outpath = tempdir.as_ref().join(request.filename);
let response = send_get_request(request.instructions)?;
let data = response
.bytes()
.with_context(|_| error::HttpResponseBytesSnafu {
request: request.to_string(),
})?;
fs::write(&outpath, &data).with_context(|_| error::HttpWriteBytesSnafu {
request: request.to_string(),
path: &outpath,
})?;
Ok(())
}
/// Uses the reqwest library to send a GET request to `URL` and returns the response.
fn send_get_request(url: &str) -> Result<Response> {
let url = Url::parse(url).context(error::HttpUrlParseSnafu { url })?;
let client = Client::builder()
.build()
.with_context(|_| error::HttpClientSnafu { url: url.clone() })?;
let response = client
.get(url.clone())
.send()
.with_context(|_| error::HttpSendSnafu { url: url.clone() })?;
let response = response
.error_for_status()
.context(error::HttpResponseSnafu { url })?;
Ok(response)
}
/// Copies a file from the path given by `request.instructions` to the tempdir with filename given
/// by `request.filename`.
fn handle_file_request<P>(request: &LogRequest<'_>, tempdir: P) -> Result<()>
where
P: AsRef<Path>,
{
ensure!(
!request.instructions.is_empty(),
error::FileFromEmptySnafu {
request: request.to_string()
}
);
let dest = tempdir.as_ref().join(request.filename);
let _ = fs::copy(request.instructions, &dest).with_context(|_| error::FileCopySnafu {
request: request.to_string(),
from: request.instructions,
to: &dest,
})?;
Ok(())
}
/// Copies all files matching the glob pattern given by `request.instructions` to the tempdir with filename and path
/// same as source file.
fn handle_glob_request<P>(request: &LogRequest<'_>, tempdir: P) -> Result<()>
where
P: AsRef<Path>,
{
let mut files = HashSet::new();
let glob_paths = glob(request.instructions).context(error::ParseGlobPatternSnafu {
pattern: request.instructions,
})?;
for path in glob_paths.flatten() {
if path.is_dir() {
// iterate the directory and sub-directory to get all file paths
for e in WalkDir::new(&path).into_iter().flatten() {
if e.path().is_file() {
files.insert(e.into_path());
}
}
} else {
files.insert(path);
}
}
for src_filepath in &files {
// with glob pattern there are chances of multiple targets with same name, therefore
// we maintain source file path and name in destination directory.
// Eg. src file path "/a/b/file" will be converted to "dest_dir/a/b/file"
let relative_path = src_filepath
.strip_prefix("/")
.unwrap_or(src_filepath.as_path());
let dest_filepath = tempdir.as_ref().join(relative_path);
let dest_dir_path = dest_filepath.parent().context(error::RootAsFileSnafu)?;
// create directories in dest file path if it does not exist
fs::create_dir_all(dest_dir_path).context(error::CreateOutputDirectorySnafu {
path: dest_dir_path,
})?;
let _ = fs::copy(src_filepath, &dest_filepath).with_context(|_| error::FileCopySnafu {
request: request.to_string(),
from: src_filepath.to_str().unwrap_or("<unknown>"),
to: &dest_filepath,
})?;
}
Ok(())
}
#[cfg(test)]
mod test {
use crate::log_request::handle_log_request;
use std::fs;
use std::fs::write;
use std::path::PathBuf;
use tempfile::TempDir;
// adds a sub directory and some files to temp directory for file request tests
fn create_source_dir(dir: &TempDir) {
let filenames_content = [
("foo.source", "1"),
("bar.source", "2"),
("for-bar.log", "3"),
];
// add files to temp directory
for entry in filenames_content.iter() {
let filepath = dir.path().join(entry.0);
write(filepath, entry.1).unwrap();
}
let subdir_name_depth1 = "depth1";
// create sub directory
let subdir_path_depth1 = dir.path().join(subdir_name_depth1);
fs::create_dir(&subdir_path_depth1).unwrap();
// Add files to sub directory
for entry in filenames_content.iter() {
let filepath = subdir_path_depth1.join(entry.0);
write(filepath, entry.1).unwrap();
}
let subdir_name_depth2 = "depth2";
// create sub directory
let subdir_path_depth2 = subdir_path_depth1.join(subdir_name_depth2);
fs::create_dir(&subdir_path_depth2).unwrap();
// Add files to sub directory
for entry in filenames_content.iter() {
let filepath = subdir_path_depth2.join(entry.0);
write(filepath, entry.1).unwrap();
}
}
fn get_dest_filepath(src_dir: &TempDir, filepath: &str) -> PathBuf {
src_dir.path().strip_prefix("/").unwrap().join(filepath)
}
fn assert_file_match(dest_dir: &TempDir, filepath: PathBuf, want: &str) {
let outfile = dest_dir.path().join(filepath);
let got = std::fs::read_to_string(outfile).unwrap();
assert_eq!(got, want);
}
#[tokio::test]
async fn file_request() {
let source_dir = TempDir::new().unwrap();
let source_filepath = source_dir.path().join("foo-bar.source");
let want = "123";
write(&source_filepath, want).unwrap();
let request = format!("file foo-bar {}", source_filepath.display());
let outdir = TempDir::new().unwrap();
handle_log_request(&request, outdir.path()).await.unwrap();
let outfile = outdir.path().join("foo-bar");
let got = std::fs::read_to_string(outfile).unwrap();
assert_eq!(got, want);
}
#[tokio::test]
async fn exec_request() {
let want = "hello world! \"quoted\"\n";
let request = r#"exec output-file.txt echo 'hello' "world!" "\"quoted\"""#;
let outdir = TempDir::new().unwrap();
handle_log_request(&request, outdir.path()).await.unwrap();
let outfile = outdir.path().join("output-file.txt");
let got = std::fs::read_to_string(outfile).unwrap();
assert_eq!(got, want);
}
#[tokio::test]
// ensures single file pattern works
async fn glob_single_file_pattern_request() {
let source_dir = TempDir::new().unwrap();
create_source_dir(&source_dir);
let outdir = TempDir::new().unwrap();
let request = format!("glob {}/foo.source", source_dir.path().display());
handle_log_request(&request, outdir.path()).await.unwrap();
assert_file_match(&outdir, get_dest_filepath(&source_dir, "foo.source"), "1");
}
#[tokio::test]
// ensures multiple file pattern works
async fn glob_multiple_files_pattern_request() {
let source_dir = TempDir::new().unwrap();
create_source_dir(&source_dir);
let outdir = TempDir::new().unwrap();
let request = format!("glob {}/*.source", source_dir.path().display());
handle_log_request(&request, outdir.path()).await.unwrap();
assert_file_match(&outdir, get_dest_filepath(&source_dir, "foo.source"), "1");
assert_file_match(&outdir, get_dest_filepath(&source_dir, "bar.source"), "2");
}
#[tokio::test]
// ensures multiple file in nested directory pattern works
async fn glob_nested_file_pattern_request() {
let source_dir = TempDir::new().unwrap();
create_source_dir(&source_dir);
let outdir = TempDir::new().unwrap();
let request = format!("glob {}/**/*.source", source_dir.path().display());
handle_log_request(&request, outdir.path()).await.unwrap();
assert_file_match(&outdir, get_dest_filepath(&source_dir, "foo.source"), "1");
assert_file_match(&outdir, get_dest_filepath(&source_dir, "bar.source"), "2");
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/foo.source"),
"1",
);
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/bar.source"),
"2",
);
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/depth2/foo.source"),
"1",
);
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/depth2/bar.source"),
"2",
);
}
#[tokio::test]
// ensures directory pattern works
async fn glob_dir_pattern_request() {
let source_dir = TempDir::new().unwrap();
create_source_dir(&source_dir);
let outdir = TempDir::new().unwrap();
let request = format!("glob {}/**/", source_dir.path().display());
handle_log_request(&request, outdir.path()).await.unwrap();
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/foo.source"),
"1",
);
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/bar.source"),
"2",
);
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/for-bar.log"),
"3",
);
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/depth2/foo.source"),
"1",
);
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/depth2/bar.source"),
"2",
);
assert_file_match(
&outdir,
get_dest_filepath(&source_dir, "depth1/depth2/for-bar.log"),
"3",
);
}
#[tokio::test]
// ensure if pattern is empty it should not panic
async fn glob_empty_pattern_request() {
let outdir = TempDir::new().unwrap();
let request = "glob";
let err = handle_log_request(&request, outdir.path())
.await
.unwrap_err();
assert!(matches!(err, crate::error::Error::PatternMissing {}));
}
}