Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ tracing = "0.1.44"
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
serde = { version = "1.0.229", features = ["derive"] }
serde-saphyr = "2.1.1"
jiff = { version = "1.0.1", features = ["serde"] }
futures = "0.3.34"
async-trait = "0.1.92"
indicatif = "0.18.6"
rpassword = "7.5.4"
dirs = "7.0"
chrono = { version = "0.4.45", features = ["serde"] }
glob = "0.3.4"
whoami = "2.1.3"
sha1 = "0.11.0"
Expand Down
4 changes: 3 additions & 1 deletion docs/audit-logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Each audit event contains:
```json
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": "2026-01-24T12:00:00.000Z",
"timestamp": "2026-01-24T12:00:00.123456Z",
"event_type": "AuthSuccess",
"session_id": "session-123",
"user": "johndoe",
Expand All @@ -115,6 +115,8 @@ Each audit event contains:
}
```

The `timestamp` field is an RFC 3339 UTC string with a `Z` suffix. It carries 0, 3, 6, or 9 fractional-second digits, whichever is the shortest exact representation (for example `2026-01-24T12:00:00Z`, `2026-01-24T12:00:00.123Z`, or `2026-01-24T12:00:00.123456789Z`), so parsers should accept any of these widths.

## Exporters

### File Exporter
Expand Down
3 changes: 1 addition & 2 deletions src/commands/interactive/multiplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use crate::ui::Colorize;
use anyhow::Result;
use chrono;
use rustyline::DefaultEditor;
use rustyline::config::Configurer;
use rustyline::error::ReadlineError;
Expand Down Expand Up @@ -262,7 +261,7 @@ impl InteractiveCommand {
// Print output with node prefix and optional timestamp
for line in output.lines() {
if self.interactive_config.show_timestamps {
let timestamp = chrono::Local::now().format("%H:%M:%S");
let timestamp = jiff::ZonedDateTime::now().strftime("%H:%M:%S");
println!(
"[{} {}] {}",
timestamp.to_string().dimmed(),
Expand Down
2 changes: 1 addition & 1 deletion src/executor/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,7 @@ impl ParallelExecutor {
output_dir.display()
);

let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S");
let timestamp = jiff::Timestamp::now().strftime("%Y%m%d_%H%M%S");

let mut pending_handles = handles;
let mut first_ctrl_c = false;
Expand Down
54 changes: 50 additions & 4 deletions src/server/audit/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! This module defines the core audit event types used throughout the SSH server
//! to track authentication, file operations, and other security-relevant activities.

use chrono::{DateTime, Utc};
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::path::PathBuf;
Expand All @@ -31,8 +31,9 @@ pub struct AuditEvent {
/// Unique event identifier
pub id: String,

/// Timestamp when the event occurred
pub timestamp: DateTime<Utc>,
/// Timestamp when the event occurred (serialized as RFC 3339 UTC with `Z`)
#[serde(with = "super::timestamp")]
pub timestamp: Timestamp,

/// Type of event
pub event_type: EventType,
Expand Down Expand Up @@ -185,7 +186,7 @@ impl AuditEvent {
pub fn new(event_type: EventType, user: String, session_id: String) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
timestamp: Utc::now(),
timestamp: Timestamp::now(),
event_type,
session_id,
user,
Expand Down Expand Up @@ -333,6 +334,51 @@ mod tests {
assert_eq!(deserialized.session_id, event.session_id);
assert_eq!(deserialized.client_ip, event.client_ip);
assert_eq!(deserialized.protocol, event.protocol);
assert_eq!(deserialized.timestamp, event.timestamp);
}

#[test]
fn test_timestamp_serializes_as_rfc3339_utc() {
let mut event = AuditEvent::new(
EventType::SessionStart,
"erin".to_string(),
"session-202".to_string(),
);

let value = serde_json::to_value(&event).unwrap();
let ts = value["timestamp"].as_str().unwrap();
assert!(ts.ends_with('Z'), "timestamp must use Z suffix: {ts}");
assert_eq!(ts.parse::<Timestamp>().unwrap(), event.timestamp);

event.timestamp = "2026-11-20T01:02:03.123456789Z".parse().unwrap();
let value = serde_json::to_value(&event).unwrap();
assert_eq!(value["timestamp"], "2026-11-20T01:02:03.123456789Z");

event.timestamp = "2026-11-20T01:02:03Z".parse().unwrap();
let value = serde_json::to_value(&event).unwrap();
assert_eq!(value["timestamp"], "2026-11-20T01:02:03Z");
}

#[test]
fn test_deserializes_chrono_written_json() {
// Records in the exact shape the previous chrono-based code wrote.
for (raw, nanos) in [
("2026-11-20T01:02:03.123456789Z", 123_456_789),
("2026-11-20T01:02:03.123456Z", 123_456_000),
("2026-11-20T01:02:03.123Z", 123_000_000),
("2026-11-20T01:02:03Z", 0),
] {
let json = format!(
r#"{{"id":"e1","timestamp":"{raw}","event_type":"session_start","session_id":"s1","user":"frank","client_ip":"10.0.0.1","path":null,"dest_path":null,"bytes":null,"result":"success","details":null,"protocol":"ssh"}}"#
);
let event: AuditEvent = serde_json::from_str(&json).unwrap();
assert_eq!(event.timestamp.as_second(), 1_795_136_523);
assert_eq!(event.timestamp.subsec_nanosecond(), nanos);

// Re-serializing yields the identical string chrono produced.
let value = serde_json::to_value(&event).unwrap();
assert_eq!(value["timestamp"], raw);
}
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/server/audit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod exporter;
pub mod file;
pub mod logstash;
pub mod otel;
mod timestamp;

use anyhow::Result;
use std::sync::Arc;
Expand Down
5 changes: 4 additions & 1 deletion src/server/audit/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ impl OtelExporter {
event.event_type, event.user, event.result
);

record.set_timestamp(event.timestamp.into());
record.set_timestamp(
std::time::SystemTime::try_from(event.timestamp)
.unwrap_or_else(|_| std::time::SystemTime::now()),
);
record.set_observed_timestamp(std::time::SystemTime::now());
record.set_severity_number(severity);
record.set_severity_text(severity_to_str(severity));
Expand Down
85 changes: 85 additions & 0 deletions src/server/audit/timestamp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Serde helper for audit event timestamps.
//!
//! Audit timestamps are serialized as RFC 3339 UTC strings with a `Z` suffix
//! and 0, 3, 6, or 9 fractional-second digits: the shortest of those widths
//! that represents the instant exactly. This is the format bssh has always
//! written, so downstream log consumers see no change. Deserialization accepts
//! any RFC 3339 timestamp with up to 9 fractional digits and any offset.

use jiff::Timestamp;
use jiff::fmt::temporal::DateTimePrinter;
use serde::{Deserialize, Deserializer, Serializer};

/// Format a timestamp as RFC 3339 UTC with 0, 3, 6, or 9 fractional digits.
pub(crate) fn format(timestamp: &Timestamp) -> String {
let nanos = timestamp.subsec_nanosecond();
let precision = if nanos == 0 {
0
} else if nanos % 1_000_000 == 0 {
3
} else if nanos % 1_000 == 0 {
6
} else {
9
};
DateTimePrinter::new()
.precision(Some(precision))
.timestamp_to_string(timestamp)
}

pub(crate) fn serialize<S: Serializer>(
timestamp: &Timestamp,
serializer: S,
) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&format(timestamp))
}

pub(crate) fn deserialize<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Timestamp, D::Error> {
Timestamp::deserialize(deserializer)
}

#[cfg(test)]
mod tests {
use super::*;

fn ts(nanos: i32) -> Timestamp {
// 2026-11-20T01:02:03Z
Timestamp::new(1_795_136_523, nanos).unwrap()
}

#[test]
fn test_format_uses_shortest_si_precision() {
assert_eq!(format(&ts(0)), "2026-11-20T01:02:03Z");
assert_eq!(format(&ts(100_000_000)), "2026-11-20T01:02:03.100Z");
assert_eq!(format(&ts(123_000_000)), "2026-11-20T01:02:03.123Z");
assert_eq!(format(&ts(123_400_000)), "2026-11-20T01:02:03.123400Z");
assert_eq!(format(&ts(123_456_000)), "2026-11-20T01:02:03.123456Z");
assert_eq!(format(&ts(123_456_700)), "2026-11-20T01:02:03.123456700Z");
assert_eq!(format(&ts(1)), "2026-11-20T01:02:03.000000001Z");
}

#[test]
fn test_parse_round_trips_every_precision() {
for nanos in [0, 100_000_000, 123_456_000, 123_456_789, 1] {
let original = ts(nanos);
let parsed: Timestamp = format(&original).parse().unwrap();
assert_eq!(parsed, original);
}
}
}
Loading
Loading