fuchsia_triage/plugins/
sandbox_errors.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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use super::helpers::analyze_logs;
use super::Plugin;
use crate::act::Action;
use crate::metrics::fetch::FileDataFetcher;
use regex::Regex;
use std::collections::BTreeSet;

pub struct SandboxErrorsPlugin();

impl Plugin for SandboxErrorsPlugin {
    fn name(&self) -> &'static str {
        "sandbox_errors"
    }

    fn display_name(&self) -> &'static str {
        "Sandbox Errors"
    }

    fn run_structured(&self, inputs: &FileDataFetcher<'_>) -> Vec<Action> {
        let mut results = Vec::new();

        let mut error_tuples: BTreeSet<(String, String)> = BTreeSet::new();
        let err_ref = &mut error_tuples;

        let re = Regex::new(r"`([^`]+)` is not allowed to connect to `([^`]+)` because this service is not present in the component's sandbox")
            .expect("regex compilation");
        analyze_logs(inputs, re, |mut pattern_match| {
            err_ref.insert((
                <&str>::from(pattern_match.remove(1)).to_string(),
                <&str>::from(pattern_match.remove(1)).to_string(),
            ));
        });

        for (name, service) in error_tuples.iter() {
            results.push(Action::new_synthetic_warning(format!(
                "[WARNING]: {} tried to use {}, which was not declared in its sandbox",
                name, service
            )));
        }
        results
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metrics::fetch::TextFetcher;

    #[fuchsia::test]
    fn test_sandbox_errors() {
        let expected_warnings: Vec<String> = vec![
            "[WARNING]: my_component tried to use fuchsia.example.Id, which was not declared in its sandbox",
            "[WARNING]: my_component tried to use fuchsia.example.Test, which was not declared in its sandbox",
            "[WARNING]: test tried to use fuchsia.example.Test, which was not declared in its sandbox",
        ]
        .into_iter()
        .map(|s| s.to_string())
        .collect::<Vec<_>>();

        // Test these cases:
        // - Sandbox failure
        // - Duplicate failure (suppressed).
        // - Same component, different service (not duplicate)
        // - Different component, same service (not duplicate)
        // - Unrelated log line.
        let fetcher: TextFetcher = r#"
[100.100] `my_component` is not allowed to connect to `fuchsia.example.Test` because this service is not present in the component's sandbox
[110.100] `my_component` is not allowed to connect to `fuchsia.example.Test` because this service is not present in the component's sandbox
[120.100] `my_component` is not allowed to connect to `fuchsia.example.Id` because this service is not present in the component's sandbox
[130.100] `test` is not allowed to connect to `fuchsia.example.Test` because this service is not present in the component's sandbox
[140.100] `test` is not allowed to connect to `component 2`
"#
        .into();

        let empty_diagnostics_vec = Vec::new();

        let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
        inputs.klog = &fetcher;
        assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);

        let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
        inputs.syslog = &fetcher;
        assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);
    }
}