use std::io::Write;
use serde::Serialize;
use crate::export::schema::{HostDto, ReportHeaderDto};
use crate::export::{ExportError, ExportOptions, Exporter};
use crate::scanner::report::ScanReport;
const FORMAT: &str = "jsonl";
pub const REPORT_RECORD: &str = "report";
pub const HOST_RECORD: &str = "host";
#[derive(Debug, Clone, Default)]
pub struct JsonLinesExporter {
options: ExportOptions,
}
impl JsonLinesExporter {
pub fn new(options: ExportOptions) -> Self {
Self { options }
}
pub fn options(&self) -> &ExportOptions {
&self.options
}
}
impl Exporter for JsonLinesExporter {
fn export(&self, report: &ScanReport, out: &mut dyn Write) -> Result<(), ExportError> {
let header = ReportHeaderDto::new(report);
write_line(out, &tagged(REPORT_RECORD, &header))?;
for host in report.hosts() {
let host = HostDto::new(host, &self.options);
write_line(out, &tagged(HOST_RECORD, &host))?;
}
Ok(())
}
}
fn write_line<T: Serialize>(out: &mut dyn Write, record: &T) -> Result<(), ExportError> {
serde_json::to_writer(&mut *out, record).map_err(render_error)?;
out.write_all(b"\n")?;
Ok(())
}
fn tagged<'a, T: Serialize>(tag: &'static str, body: &'a T) -> Tagged<'a, T> {
Tagged { tag, body }
}
#[derive(Serialize)]
struct Tagged<'a, T: Serialize> {
#[serde(rename = "type")]
tag: &'static str,
#[serde(flatten)]
body: &'a T,
}
fn render_error(error: serde_json::Error) -> ExportError {
if error.is_io() {
ExportError::Io(error.into())
} else {
ExportError::Render {
format: FORMAT,
message: error.to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::export::fixture;
use serde_json::Value;
fn lines(report: &ScanReport, options: ExportOptions) -> Vec<Value> {
let mut bytes = Vec::new();
JsonLinesExporter::new(options)
.export(report, &mut bytes)
.expect("the export succeeds");
let text = String::from_utf8(bytes).expect("utf-8");
assert!(text.ends_with('\n'), "every record must terminate its line");
text.lines()
.map(|line| {
assert!(
!line.is_empty(),
"a blank line is not a record and must not be written"
);
serde_json::from_str(line).expect("each line parses on its own")
})
.collect()
}
#[test]
fn the_stream_leads_with_the_report_then_names_every_host() {
let records = lines(&fixture::report(), ExportOptions::new());
assert_eq!(records.len(), 4, "a report record and three hosts");
assert_eq!(records[0]["type"], "report");
assert_eq!(records[0]["schema_version"], 1);
assert_eq!(records[0]["summary"]["hosts_total"], 3);
assert!(
records[0]["hosts"].is_null(),
"the report record must not carry the hosts as well"
);
for record in &records[1..] {
assert_eq!(record["type"], "host");
}
assert_eq!(records[1]["primary_ip"], "192.168.0.1");
assert_eq!(records[3]["primary_ip"], "192.168.0.9");
}
#[cfg(feature = "export-json")]
#[test]
fn a_host_line_is_the_documents_host_object_plus_a_tag() {
let report = fixture::report();
let mut document = Vec::new();
crate::export::JsonExporter::new(ExportOptions::new())
.export(&report, &mut document)
.expect("the document export succeeds");
let document: Value = serde_json::from_slice(&document).expect("valid JSON");
for (index, mut line) in lines(&report, ExportOptions::new())
.into_iter()
.skip(1)
.enumerate()
{
let tag = line
.as_object_mut()
.expect("an object")
.remove("type")
.expect("a tag");
assert_eq!(tag, "host");
assert_eq!(line, document["hosts"][index]);
}
}
#[cfg(feature = "export-json")]
#[test]
fn the_report_record_matches_the_documents_header() {
let report = fixture::report();
let mut document = Vec::new();
crate::export::JsonExporter::new(ExportOptions::new())
.export(&report, &mut document)
.expect("the document export succeeds");
let document: Value = serde_json::from_slice(&document).expect("valid JSON");
let record = lines(&report, ExportOptions::new()).swap_remove(0);
for field in [
"schema_version",
"engine",
"started_at",
"elapsed_us",
"partial",
"summary",
"phases",
] {
assert_eq!(record[field], document[field], "`{field}` disagrees");
}
}
#[test]
fn a_truncated_stream_still_parses_up_to_the_last_whole_line() {
let mut bytes = Vec::new();
JsonLinesExporter::new(ExportOptions::new())
.export(&fixture::report(), &mut bytes)
.expect("the export succeeds");
let text = String::from_utf8(bytes).expect("utf-8");
let cut = text.rfind('\n').expect("a line break");
let truncated = &text[..cut - 5];
let whole: Vec<&str> = truncated
.split_inclusive('\n')
.filter(|line| line.ends_with('\n'))
.collect();
assert_eq!(whole.len(), 3, "three records survived the cut");
for line in whole {
serde_json::from_str::<Value>(line).expect("a surviving line still parses");
}
}
#[test]
fn redaction_applies_to_the_stream() {
let records = lines(
&fixture::report(),
ExportOptions::new().with_redaction(crate::export::Redaction::Standard),
);
assert_eq!(records[1]["hostname"], "roXXXXXal");
assert_eq!(records[1]["hardware"]["mac"], "2c:cf:67:XX:XX:XX");
}
#[test]
fn a_failing_destination_surfaces_as_an_error() {
struct Full;
impl Write for Full {
fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
Err(std::io::Error::new(
std::io::ErrorKind::StorageFull,
"no space left on device",
))
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let error = JsonLinesExporter::new(ExportOptions::new())
.export(&fixture::report(), &mut Full)
.expect_err("a full disk fails the export");
assert!(matches!(error, ExportError::Io(_)), "got {error:?}");
}
}