2021-02-13 13:21:49 -07:00
|
|
|
use std::env;
|
|
|
|
|
2023-04-11 09:26:16 -06:00
|
|
|
use atuin_common::utils::uuid_v7;
|
2023-08-19 05:28:39 -06:00
|
|
|
use regex::RegexSet;
|
|
|
|
|
|
|
|
use crate::{secrets::SECRET_PATTERNS, settings::Settings};
|
2023-09-11 02:26:05 -06:00
|
|
|
use time::OffsetDateTime;
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2023-06-15 04:29:40 -06:00
|
|
|
mod builder;
|
|
|
|
|
|
|
|
/// Client-side history entry.
|
|
|
|
///
|
|
|
|
/// Client stores data unencrypted, and only encrypts it before sending to the server.
|
|
|
|
///
|
|
|
|
/// To create a new history entry, use one of the builders:
|
|
|
|
/// - [`History::import()`] to import an entry from the shell history file
|
|
|
|
/// - [`History::capture()`] to capture an entry via hook
|
|
|
|
/// - [`History::from_db()`] to create an instance from the database entry
|
|
|
|
//
|
|
|
|
// ## Implementation Notes
|
|
|
|
//
|
|
|
|
// New fields must should be added to `encryption::{encode, decode}` in a backwards
|
|
|
|
// compatible way. (eg sensible defaults and updating the nfields parameter)
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, sqlx::FromRow)]
|
2020-10-04 17:59:28 -06:00
|
|
|
pub struct History {
|
2023-06-15 04:29:40 -06:00
|
|
|
/// A client-generated ID, used to identify the entry when syncing.
|
|
|
|
///
|
|
|
|
/// Stored as `client_id` in the database.
|
2021-02-13 10:02:52 -07:00
|
|
|
pub id: String,
|
2023-06-15 04:29:40 -06:00
|
|
|
/// When the command was run.
|
2023-09-11 02:26:05 -06:00
|
|
|
pub timestamp: OffsetDateTime,
|
2023-06-15 04:29:40 -06:00
|
|
|
/// How long the command took to run.
|
2021-02-13 10:02:52 -07:00
|
|
|
pub duration: i64,
|
2023-06-15 04:29:40 -06:00
|
|
|
/// The exit code of the command.
|
2021-02-13 10:02:52 -07:00
|
|
|
pub exit: i64,
|
2023-06-15 04:29:40 -06:00
|
|
|
/// The command that was run.
|
2020-10-04 17:59:28 -06:00
|
|
|
pub command: String,
|
2023-06-15 04:29:40 -06:00
|
|
|
/// The current working directory when the command was run.
|
2020-10-04 17:59:28 -06:00
|
|
|
pub cwd: String,
|
2023-06-15 04:29:40 -06:00
|
|
|
/// The session ID, associated with a terminal session.
|
2021-02-13 13:21:49 -07:00
|
|
|
pub session: String,
|
2023-06-15 04:29:40 -06:00
|
|
|
/// The hostname of the machine the command was run on.
|
2021-02-13 13:21:49 -07:00
|
|
|
pub hostname: String,
|
2023-06-15 04:29:40 -06:00
|
|
|
/// Timestamp, which is set when the entry is deleted, allowing a soft delete.
|
2023-09-11 02:26:05 -06:00
|
|
|
pub deleted_at: Option<OffsetDateTime>,
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl History {
|
2023-03-20 03:26:54 -06:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-06-15 04:29:40 -06:00
|
|
|
fn new(
|
2023-09-11 02:26:05 -06:00
|
|
|
timestamp: OffsetDateTime,
|
2021-02-13 13:21:49 -07:00
|
|
|
command: String,
|
|
|
|
cwd: String,
|
|
|
|
exit: i64,
|
|
|
|
duration: i64,
|
|
|
|
session: Option<String>,
|
|
|
|
hostname: Option<String>,
|
2023-09-11 02:26:05 -06:00
|
|
|
deleted_at: Option<OffsetDateTime>,
|
2021-02-14 08:15:26 -07:00
|
|
|
) -> Self {
|
2021-02-14 11:00:41 -07:00
|
|
|
let session = session
|
|
|
|
.or_else(|| env::var("ATUIN_SESSION").ok())
|
2023-04-11 09:26:16 -06:00
|
|
|
.unwrap_or_else(|| uuid_v7().as_simple().to_string());
|
2023-06-12 10:58:46 -06:00
|
|
|
let hostname = hostname.unwrap_or_else(|| {
|
|
|
|
format!(
|
|
|
|
"{}:{}",
|
|
|
|
env::var("ATUIN_HOST_NAME").unwrap_or_else(|_| whoami::hostname()),
|
|
|
|
env::var("ATUIN_HOST_USER").unwrap_or_else(|_| whoami::username())
|
|
|
|
)
|
|
|
|
});
|
2021-02-13 13:21:49 -07:00
|
|
|
|
2021-02-14 08:15:26 -07:00
|
|
|
Self {
|
2023-04-11 09:26:16 -06:00
|
|
|
id: uuid_v7().as_simple().to_string(),
|
2021-02-13 12:37:00 -07:00
|
|
|
timestamp,
|
2020-10-05 10:20:48 -06:00
|
|
|
command,
|
|
|
|
cwd,
|
2021-02-13 10:02:52 -07:00
|
|
|
exit,
|
|
|
|
duration,
|
2021-02-13 13:21:49 -07:00
|
|
|
session,
|
|
|
|
hostname,
|
2023-03-20 03:26:54 -06:00
|
|
|
deleted_at,
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|
|
|
|
}
|
2022-04-25 00:13:30 -06:00
|
|
|
|
2023-06-15 04:29:40 -06:00
|
|
|
/// Builder for a history entry that is imported from shell history.
|
|
|
|
///
|
|
|
|
/// The only two required fields are `timestamp` and `command`.
|
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
/// ```
|
|
|
|
/// use atuin_client::history::History;
|
|
|
|
///
|
|
|
|
/// let history: History = History::import()
|
2023-09-11 02:26:05 -06:00
|
|
|
/// .timestamp(time::OffsetDateTime::now_utc())
|
2023-06-15 04:29:40 -06:00
|
|
|
/// .command("ls -la")
|
|
|
|
/// .build()
|
|
|
|
/// .into();
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// If shell history contains more information, it can be added to the builder:
|
|
|
|
/// ```
|
|
|
|
/// use atuin_client::history::History;
|
|
|
|
///
|
|
|
|
/// let history: History = History::import()
|
2023-09-11 02:26:05 -06:00
|
|
|
/// .timestamp(time::OffsetDateTime::now_utc())
|
2023-06-15 04:29:40 -06:00
|
|
|
/// .command("ls -la")
|
|
|
|
/// .cwd("/home/user")
|
|
|
|
/// .exit(0)
|
|
|
|
/// .duration(100)
|
|
|
|
/// .build()
|
|
|
|
/// .into();
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Unknown command or command without timestamp cannot be imported, which
|
|
|
|
/// is forced at compile time:
|
|
|
|
///
|
|
|
|
/// ```compile_fail
|
|
|
|
/// use atuin_client::history::History;
|
|
|
|
///
|
|
|
|
/// // this will not compile because timestamp is missing
|
|
|
|
/// let history: History = History::import()
|
|
|
|
/// .command("ls -la")
|
|
|
|
/// .build()
|
|
|
|
/// .into();
|
|
|
|
/// ```
|
|
|
|
pub fn import() -> builder::HistoryImportedBuilder {
|
|
|
|
builder::HistoryImported::builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builder for a history entry that is captured via hook.
|
|
|
|
///
|
|
|
|
/// This builder is used only at the `start` step of the hook,
|
|
|
|
/// so it doesn't have any fields which are known only after
|
|
|
|
/// the command is finished, such as `exit` or `duration`.
|
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
/// ```rust
|
|
|
|
/// use atuin_client::history::History;
|
|
|
|
///
|
|
|
|
/// let history: History = History::capture()
|
2023-09-11 02:26:05 -06:00
|
|
|
/// .timestamp(time::OffsetDateTime::now_utc())
|
2023-06-15 04:29:40 -06:00
|
|
|
/// .command("ls -la")
|
|
|
|
/// .cwd("/home/user")
|
|
|
|
/// .build()
|
|
|
|
/// .into();
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Command without any required info cannot be captured, which is forced at compile time:
|
|
|
|
///
|
|
|
|
/// ```compile_fail
|
|
|
|
/// use atuin_client::history::History;
|
|
|
|
///
|
|
|
|
/// // this will not compile because `cwd` is missing
|
|
|
|
/// let history: History = History::capture()
|
2023-09-11 02:26:05 -06:00
|
|
|
/// .timestamp(time::OffsetDateTime::now_utc())
|
2023-06-15 04:29:40 -06:00
|
|
|
/// .command("ls -la")
|
|
|
|
/// .build()
|
|
|
|
/// .into();
|
|
|
|
/// ```
|
|
|
|
pub fn capture() -> builder::HistoryCapturedBuilder {
|
|
|
|
builder::HistoryCaptured::builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builder for a history entry that is imported from the database.
|
|
|
|
///
|
|
|
|
/// All fields are required, as they are all present in the database.
|
|
|
|
///
|
|
|
|
/// ```compile_fail
|
|
|
|
/// use atuin_client::history::History;
|
|
|
|
///
|
|
|
|
/// // this will not compile because `id` field is missing
|
|
|
|
/// let history: History = History::from_db()
|
2023-09-11 02:26:05 -06:00
|
|
|
/// .timestamp(time::OffsetDateTime::now_utc())
|
2023-06-15 04:29:40 -06:00
|
|
|
/// .command("ls -la".to_string())
|
|
|
|
/// .cwd("/home/user".to_string())
|
|
|
|
/// .exit(0)
|
|
|
|
/// .duration(100)
|
|
|
|
/// .session("somesession".to_string())
|
|
|
|
/// .hostname("localhost".to_string())
|
|
|
|
/// .deleted_at(None)
|
|
|
|
/// .build()
|
|
|
|
/// .into();
|
|
|
|
/// ```
|
|
|
|
pub fn from_db() -> builder::HistoryFromDbBuilder {
|
|
|
|
builder::HistoryFromDb::builder()
|
|
|
|
}
|
|
|
|
|
2022-04-25 00:13:30 -06:00
|
|
|
pub fn success(&self) -> bool {
|
|
|
|
self.exit == 0 || self.duration == -1
|
|
|
|
}
|
2023-08-19 05:28:39 -06:00
|
|
|
|
|
|
|
pub fn should_save(&self, settings: &Settings) -> bool {
|
|
|
|
let secret_regex = SECRET_PATTERNS.iter().map(|f| f.1);
|
|
|
|
let secret_regex = RegexSet::new(secret_regex).expect("Failed to build secrets regex");
|
|
|
|
|
|
|
|
!(self.command.starts_with(' ')
|
|
|
|
|| settings.history_filter.is_match(&self.command)
|
|
|
|
|| settings.cwd_filter.is_match(&self.cwd)
|
|
|
|
|| (secret_regex.is_match(&self.command)) && settings.secrets_filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use regex::RegexSet;
|
|
|
|
|
|
|
|
use crate::settings::Settings;
|
|
|
|
|
|
|
|
use super::History;
|
|
|
|
|
|
|
|
// Test that we don't save history where necessary
|
|
|
|
#[test]
|
|
|
|
fn privacy_test() {
|
2023-09-26 07:52:45 -06:00
|
|
|
let settings = Settings {
|
|
|
|
cwd_filter: RegexSet::new(["^/supasecret"]).unwrap(),
|
|
|
|
history_filter: RegexSet::new(["^psql"]).unwrap(),
|
|
|
|
..Settings::default()
|
|
|
|
};
|
2023-08-19 05:28:39 -06:00
|
|
|
|
|
|
|
let normal_command: History = History::capture()
|
2023-09-11 02:26:05 -06:00
|
|
|
.timestamp(time::OffsetDateTime::now_utc())
|
2023-08-19 05:28:39 -06:00
|
|
|
.command("echo foo")
|
|
|
|
.cwd("/")
|
|
|
|
.build()
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let with_space: History = History::capture()
|
2023-09-11 02:26:05 -06:00
|
|
|
.timestamp(time::OffsetDateTime::now_utc())
|
2023-08-19 05:28:39 -06:00
|
|
|
.command(" echo bar")
|
|
|
|
.cwd("/")
|
|
|
|
.build()
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let stripe_key: History = History::capture()
|
2023-09-11 02:26:05 -06:00
|
|
|
.timestamp(time::OffsetDateTime::now_utc())
|
2023-08-19 05:28:39 -06:00
|
|
|
.command("curl foo.com/bar?key=sk_test_1234567890abcdefghijklmnop")
|
|
|
|
.cwd("/")
|
|
|
|
.build()
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let secret_dir: History = History::capture()
|
2023-09-11 02:26:05 -06:00
|
|
|
.timestamp(time::OffsetDateTime::now_utc())
|
2023-08-19 05:28:39 -06:00
|
|
|
.command("echo ohno")
|
|
|
|
.cwd("/supasecret")
|
|
|
|
.build()
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let with_psql: History = History::capture()
|
2023-09-11 02:26:05 -06:00
|
|
|
.timestamp(time::OffsetDateTime::now_utc())
|
2023-08-19 05:28:39 -06:00
|
|
|
.command("psql")
|
|
|
|
.cwd("/supasecret")
|
|
|
|
.build()
|
|
|
|
.into();
|
|
|
|
|
|
|
|
assert!(normal_command.should_save(&settings));
|
|
|
|
assert!(!with_space.should_save(&settings));
|
|
|
|
assert!(!stripe_key.should_save(&settings));
|
|
|
|
assert!(!secret_dir.should_save(&settings));
|
|
|
|
assert!(!with_psql.should_save(&settings));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn disable_secrets() {
|
2023-09-26 07:52:45 -06:00
|
|
|
let settings = Settings {
|
|
|
|
secrets_filter: false,
|
|
|
|
..Settings::default()
|
|
|
|
};
|
2023-08-19 05:28:39 -06:00
|
|
|
|
|
|
|
let stripe_key: History = History::capture()
|
2023-09-11 02:26:05 -06:00
|
|
|
.timestamp(time::OffsetDateTime::now_utc())
|
2023-08-19 05:28:39 -06:00
|
|
|
.command("curl foo.com/bar?key=sk_test_1234567890abcdefghijklmnop")
|
|
|
|
.cwd("/")
|
|
|
|
.build()
|
|
|
|
.into();
|
|
|
|
|
|
|
|
assert!(stripe_key.should_save(&settings));
|
|
|
|
}
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|