-
Notifications
You must be signed in to change notification settings - Fork 55
Add sealed ETL denial analysis (replacement for #699) #709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
richiemsft
wants to merge
12
commits into
user/saulg/learning-mode-endtoend
from
user/saulg/learning-mode-analyze-replacement
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ad8c430
learning_mode_core: cross-platform denial model + NDJSON emitter
richiemsft 1c4b342
Add Windows sealed-ETL denial decoder (PR4 pr4-etl-parse)
richiemsft 3d6cd54
Add decode-composition tests over real event shapes (pr4-analyze-tests)
richiemsft 52b9691
Update decoder terminology for capture modes
richiemsft 9f9df31
Update PR4 workspace lockfile
richiemsft 2ec77aa
Harden Learning Mode ETL analysis
richiemsft c77b2cd
Fix TDH buffer alignment
richiemsft 1fd65e6
Harden denial decoder contracts
richiemsft 206750e
Use RFC 7464 terminology
richiemsft 4739ac9
Enforce canonical denial file paths
richiemsft 767bf87
Clarify decoder diagnostic boundaries
richiemsft af3d5bc
Address PR 709 review feedback
richiemsft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/backends/learning_mode/windows/examples/lm_analyze.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Decode a sealed learning-mode `.etl` into the captureDenials RFC 7464 | ||
| //! JSON text sequence, or dump its raw ETW events for schema discovery. | ||
| //! | ||
| //! This is a developer diagnostic for inspecting captured traces manually. | ||
| //! End users and SDK agents do not invoke it: the BaseContainer runner seals | ||
| //! the trace and reports its path, while this example performs the manual | ||
| //! analysis until runner integration consumes the trace automatically. | ||
| //! | ||
| //! Usage: | ||
| //! | ||
| //! ```text | ||
| //! # Emit the DeniedResource JSON text sequence (0x1E-framed) to stdout: | ||
| //! cargo run -p learning_mode_windows --example lm_analyze -- <path-to.etl> --exit-code <code> | ||
| //! | ||
| //! # Dump every decoded event (id + property name/value pairs): | ||
| //! cargo run -p learning_mode_windows --example lm_analyze -- <path-to.etl> --raw | ||
| //! ``` | ||
| //! | ||
| //! Exit codes: `0` = decoded; `2` = wrong platform / bad args; `1` = decode | ||
| //! failed. | ||
|
|
||
| #[cfg(not(target_os = "windows"))] | ||
| fn main() { | ||
| eprintln!("lm_analyze is Windows-only"); | ||
| std::process::exit(2); | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| fn main() { | ||
| std::process::exit(windows_impl::run()); | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| mod windows_impl { | ||
| use std::collections::BTreeMap; | ||
| use std::io::Write; | ||
| use std::path::Path; | ||
|
|
||
| use learning_mode_core::{emit, DenialAnalyzer, DenialSummary}; | ||
| use learning_mode_windows::{visit_raw_events, EtlDenialAnalyzer}; | ||
|
|
||
| pub fn run() -> i32 { | ||
| let args: Vec<String> = std::env::args().skip(1).collect(); | ||
| let Some(etl_path) = args.first() else { | ||
| eprintln!("usage: lm_analyze <path-to.etl> [--raw | --exit-code <code>]"); | ||
| return 2; | ||
| }; | ||
| let raw = args.iter().any(|a| a == "--raw"); | ||
| let path = Path::new(etl_path); | ||
|
|
||
| if raw { | ||
| dump_raw(path) | ||
| } else { | ||
| let Some(exit_code) = parse_exit_code(&args) else { | ||
| eprintln!("--exit-code <code> is required unless --raw is used"); | ||
| return 2; | ||
| }; | ||
| emit_json_sequence(path, exit_code) | ||
| } | ||
| } | ||
|
|
||
| fn parse_exit_code(args: &[String]) -> Option<i32> { | ||
| let index = args.iter().position(|arg| arg == "--exit-code")?; | ||
| args.get(index + 1)?.parse().ok() | ||
| } | ||
|
|
||
| /// Decodes denials and writes the RFC 7464 JSON text sequence to stdout. | ||
| fn emit_json_sequence(path: &Path, exit_code: i32) -> i32 { | ||
| let analysis = match EtlDenialAnalyzer.analyze(path) { | ||
| Ok(d) => d, | ||
| Err(e) => { | ||
| eprintln!("analyze failed: {e}"); | ||
| return 1; | ||
| } | ||
| }; | ||
| let summary = DenialSummary::new( | ||
| exit_code, | ||
| analysis.denials.len(), | ||
| analysis.denied_resources_truncated, | ||
| ); | ||
| let stdout = std::io::stdout(); | ||
| let mut handle = stdout.lock(); | ||
| if let Err(e) = emit::write_stream(&mut handle, &analysis.denials, &summary) { | ||
| eprintln!("write failed: {e}"); | ||
| return 1; | ||
| } | ||
| eprintln!( | ||
| "lm_analyze: {} unique denial(s){}", | ||
| analysis.denials.len(), | ||
| if analysis.denied_resources_truncated { | ||
| " (truncated)" | ||
| } else { | ||
| "" | ||
| } | ||
| ); | ||
| 0 | ||
| } | ||
|
|
||
| /// Dumps every decoded event, plus a per-event-id histogram, so the | ||
| /// real provider/ID/field schema can be confirmed against hardware. | ||
| fn dump_raw(path: &Path) -> i32 { | ||
| let mut histogram: BTreeMap<(String, u16), usize> = BTreeMap::new(); | ||
| let stdout = std::io::stdout(); | ||
| let mut out = stdout.lock(); | ||
| let event_count = { | ||
| let mut visitor = |ev: &learning_mode_windows::DecodedEventParts| { | ||
| let provider = format!("{:?}", ev.provider); | ||
| *histogram | ||
| .entry((provider.clone(), ev.event_id)) | ||
| .or_default() += 1; | ||
| let props: Vec<String> = ev.props.iter().map(|(k, v)| format!("{k}={v}")).collect(); | ||
| writeln!( | ||
| out, | ||
| "provider {} event {} | {}", | ||
| provider, | ||
| ev.event_id, | ||
| props.join(" | ") | ||
| ) | ||
| }; | ||
| match visit_raw_events(path, &mut visitor) { | ||
| Ok(count) => count, | ||
| Err(e) => { | ||
| eprintln!("decode failed: {e}"); | ||
| return 1; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| if writeln!(out, "--- {event_count} event(s) total ---").is_err() { | ||
| return 1; | ||
| } | ||
| for ((provider, id), count) in &histogram { | ||
| if writeln!(out, " provider {provider:?} id {id}: {count}").is_err() { | ||
| return 1; | ||
| } | ||
| } | ||
| 0 | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.