235 lines
7.8 KiB
Rust
235 lines
7.8 KiB
Rust
use crate::{command, config::Logs};
|
|
use anyhow::{bail, Context, Result};
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::Value;
|
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
|
|
|
const MAX_LOOKBACK_SECONDS: u64 = 30 * 24 * 60 * 60;
|
|
|
|
#[derive(Debug, Default, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct LogsQuery {
|
|
pub since: Option<u64>,
|
|
pub cursor: Option<String>,
|
|
pub limit: Option<usize>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
pub struct LogsResponse {
|
|
pub entries: Vec<LogEntry>,
|
|
pub truncated: bool,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, PartialEq)]
|
|
pub struct LogEntry {
|
|
pub cursor: String,
|
|
pub timestamp_unix_micros: u64,
|
|
pub unit: String,
|
|
pub priority: u8,
|
|
pub message: String,
|
|
pub boot_id: Option<String>,
|
|
pub pid: Option<u32>,
|
|
}
|
|
|
|
pub async fn collect(config: &Logs, query: LogsQuery, timeout: Duration) -> Result<LogsResponse> {
|
|
let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
|
|
if query.cursor.as_ref().is_some_and(|cursor| {
|
|
cursor.is_empty() || cursor.len() > 1024 || cursor.chars().any(char::is_control)
|
|
}) {
|
|
bail!("journal cursor is invalid")
|
|
}
|
|
let limit = query
|
|
.limit
|
|
.unwrap_or(config.max_entries_per_request)
|
|
.min(config.max_entries_per_request);
|
|
if limit == 0 {
|
|
bail!("log limit must be positive")
|
|
}
|
|
|
|
let mut arguments = vec!["--no-pager".to_owned(), "--output=json".to_owned()];
|
|
if let Some(cursor) = query.cursor {
|
|
arguments.push(format!("--after-cursor={cursor}"));
|
|
} else {
|
|
let since = query.since.unwrap_or_else(|| now.saturating_sub(120));
|
|
if since > now.saturating_add(300) || now.saturating_sub(since) > MAX_LOOKBACK_SECONDS {
|
|
bail!("log lookback is outside the allowed range")
|
|
}
|
|
arguments.push(format!("--since=@{since}"));
|
|
}
|
|
for unit in &config.allowed_units {
|
|
arguments.push(format!("--unit={unit}"));
|
|
}
|
|
let argument_refs: Vec<_> = arguments.iter().map(String::as_str).collect();
|
|
let lines = command::run_lines("journalctl", &argument_refs, timeout, limit + 1).await?;
|
|
let truncated = lines.len() > limit;
|
|
let mut entries = lines
|
|
.iter()
|
|
.take(limit)
|
|
.filter(|line| !line.trim().is_empty())
|
|
.map(|line| parse_entry(line, config.max_message_bytes))
|
|
.collect::<Result<Vec<_>>>()?;
|
|
entries.shrink_to_fit();
|
|
Ok(LogsResponse { entries, truncated })
|
|
}
|
|
|
|
fn parse_entry(line: &str, max_message_bytes: usize) -> Result<LogEntry> {
|
|
let value: Value = serde_json::from_str(line).context("parse journald JSON row")?;
|
|
let cursor = string_field(&value, "__CURSOR").context("journal row missing cursor")?;
|
|
if cursor.len() > 1024 {
|
|
bail!("journal cursor is too long")
|
|
}
|
|
let timestamp_unix_micros = string_field(&value, "__REALTIME_TIMESTAMP")
|
|
.and_then(|raw| raw.parse().ok())
|
|
.context("journal row has invalid timestamp")?;
|
|
let mut message = redact_sensitive(
|
|
&string_field(&value, "MESSAGE").unwrap_or_else(|| "[binary message]".into()),
|
|
);
|
|
if message.len() > max_message_bytes {
|
|
let mut boundary = max_message_bytes;
|
|
while !message.is_char_boundary(boundary) {
|
|
boundary -= 1;
|
|
}
|
|
message.truncate(boundary);
|
|
}
|
|
Ok(LogEntry {
|
|
cursor,
|
|
timestamp_unix_micros,
|
|
unit: string_field(&value, "_SYSTEMD_UNIT").unwrap_or_else(|| "unknown".into()),
|
|
priority: string_field(&value, "PRIORITY")
|
|
.and_then(|raw| raw.parse().ok())
|
|
.filter(|priority| *priority <= 7)
|
|
.unwrap_or(6),
|
|
message,
|
|
boot_id: string_field(&value, "_BOOT_ID"),
|
|
pid: string_field(&value, "_PID").and_then(|raw| raw.parse().ok()),
|
|
})
|
|
}
|
|
|
|
fn string_field(value: &Value, name: &str) -> Option<String> {
|
|
value.get(name)?.as_str().map(str::to_owned)
|
|
}
|
|
|
|
fn redact_sensitive(message: &str) -> String {
|
|
const KEYS: &[&str] = &[
|
|
"authorization",
|
|
"password",
|
|
"passwd",
|
|
"api_key",
|
|
"api-key",
|
|
"apikey",
|
|
"token",
|
|
"secret",
|
|
];
|
|
let lower = message.to_ascii_lowercase();
|
|
let bytes = lower.as_bytes();
|
|
let mut output = String::with_capacity(message.len());
|
|
let mut copied = 0;
|
|
let mut search_from = 0;
|
|
while search_from < bytes.len() {
|
|
let candidate = KEYS
|
|
.iter()
|
|
.filter_map(|key| {
|
|
lower[search_from..]
|
|
.find(key)
|
|
.map(|offset| (search_from + offset, *key))
|
|
})
|
|
.min_by_key(|(position, _)| *position);
|
|
let Some((position, key)) = candidate else {
|
|
break;
|
|
};
|
|
let before_is_name = position > 0
|
|
&& (bytes[position - 1].is_ascii_alphanumeric() || bytes[position - 1] == b'_');
|
|
let mut separator = position + key.len();
|
|
if before_is_name {
|
|
search_from = separator;
|
|
continue;
|
|
}
|
|
if bytes
|
|
.get(separator)
|
|
.is_some_and(|byte| matches!(byte, b'\'' | b'"'))
|
|
{
|
|
separator += 1;
|
|
}
|
|
while bytes.get(separator).is_some_and(u8::is_ascii_whitespace) {
|
|
separator += 1;
|
|
}
|
|
if !bytes
|
|
.get(separator)
|
|
.is_some_and(|byte| matches!(byte, b':' | b'='))
|
|
{
|
|
search_from = position + key.len();
|
|
continue;
|
|
}
|
|
separator += 1;
|
|
while bytes.get(separator).is_some_and(u8::is_ascii_whitespace) {
|
|
separator += 1;
|
|
}
|
|
if separator >= bytes.len() {
|
|
break;
|
|
}
|
|
|
|
let mut value_start = separator;
|
|
let quote = bytes[value_start];
|
|
let value_end = if matches!(quote, b'\'' | b'"') {
|
|
value_start += 1;
|
|
message[value_start..]
|
|
.find(char::from(quote))
|
|
.map_or(bytes.len(), |offset| value_start + offset)
|
|
} else {
|
|
if key == "authorization" && lower[value_start..].starts_with("bearer ") {
|
|
value_start += "bearer ".len();
|
|
}
|
|
message[value_start..]
|
|
.char_indices()
|
|
.find(|(_, character)| {
|
|
character.is_ascii_whitespace() || matches!(character, ',' | ';' | '&')
|
|
})
|
|
.map_or(bytes.len(), |(offset, _)| value_start + offset)
|
|
};
|
|
if value_end == value_start {
|
|
search_from = separator;
|
|
continue;
|
|
}
|
|
output.push_str(&message[copied..value_start]);
|
|
output.push_str("[REDACTED]");
|
|
copied = value_end;
|
|
search_from = value_end;
|
|
}
|
|
output.push_str(&message[copied..]);
|
|
output
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn parses_and_truncates_journal_row() {
|
|
let row = r#"{"__CURSOR":"s=cursor","__REALTIME_TIMESTAMP":"1700000000123456","_SYSTEMD_UNIT":"nginx.service","PRIORITY":"4","MESSAGE":"привет world","_PID":"42"}"#;
|
|
let parsed = parse_entry(row, 12).unwrap();
|
|
assert_eq!(parsed.cursor, "s=cursor");
|
|
assert_eq!(parsed.unit, "nginx.service");
|
|
assert_eq!(parsed.priority, 4);
|
|
assert_eq!(parsed.pid, Some(42));
|
|
assert!(parsed.message.len() <= 12);
|
|
assert!(parsed.message.is_char_boundary(parsed.message.len()));
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_missing_cursor() {
|
|
assert!(parse_entry(r#"{"MESSAGE":"hello"}"#, 1024).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn redacts_common_secret_shapes() {
|
|
assert_eq!(
|
|
redact_sensitive("Authorization: Bearer abc123 password='hunter2' token=xyz&ok=1"),
|
|
"Authorization: Bearer [REDACTED] password='[REDACTED]' token=[REDACTED]&ok=1"
|
|
);
|
|
assert_eq!(
|
|
redact_sensitive(r#"{"api_key":"value","message":"safe"}"#),
|
|
r#"{"api_key":"[REDACTED]","message":"safe"}"#
|
|
);
|
|
}
|
|
}
|