aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/history/mod.rs
blob: 10e74d8ee0d67e755d9086990225c13b2c6b760b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use core::fmt::Formatter;
use regex::RegexSet;
use std::env;
use std::fmt::Display;

use turtle_common::utils::uuid_v7;

use time::OffsetDateTime;

use crate::history::secrets::SECRET_PATTERNS_RE;

pub mod builder;
mod secrets;

const HISTORY_AUTHOR_ENV: &str = "ATUIN_HISTORY_AUTHOR";
const HISTORY_INTENT_ENV: &str = "ATUIN_HISTORY_INTENT";

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct HistoryId(pub String);

impl Display for HistoryId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<String> for HistoryId {
    fn from(s: String) -> Self {
        Self(s)
    }
}

pub(crate) fn get_hostname() -> String {
    env::var("ATUIN_HOST_NAME")
        .unwrap_or_else(|_| whoami::hostname().unwrap_or_else(|_| "unknown-host".to_string()))
}

pub(crate) fn get_username() -> String {
    env::var("ATUIN_HOST_USER")
        .unwrap_or_else(|_| whoami::username().unwrap_or_else(|_| "unknown-user".to_string()))
}

/// Returns a pair of the hostname and username, separated by a colon.
#[must_use]
pub fn get_host_user() -> String {
    format!("{}:{}", get_hostname(), get_username())
}

/// 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 be added to `History::{serialize,deserialize}` in a backwards
// compatible way (sensible defaults and careful `nfields` handling).
#[derive(Debug, Clone, PartialEq, Eq, sqlx::FromRow)]
pub struct History {
    /// A client-generated ID, used to identify the entry when syncing.
    ///
    /// Stored as `client_id` in the database.
    pub id: HistoryId,

    /// When the command was run.
    pub timestamp: OffsetDateTime,

    /// How long the command took to run.
    pub duration: i64,

    /// The exit code of the command.
    pub exit: i64,

    /// The command that was run.
    pub command: String,

    /// The current working directory when the command was run.
    pub cwd: String,

    /// The session ID, associated with a terminal session.
    pub session: String,

    /// The hostname of the machine the command was run on.
    pub hostname: String,

    /// Who wrote this command (human user or automation/agent identity).
    pub author: String,

    /// Optional rationale for why the command was executed.
    pub intent: Option<String>,

    /// Timestamp, which is set when the entry is deleted, allowing a soft delete.
    pub deleted_at: Option<OffsetDateTime>,
}

impl History {
    #[must_use]
    pub fn author_from_hostname(hostname: &str) -> String {
        hostname
            .split_once(':')
            .map_or_else(|| hostname.to_owned(), |(_, user)| user.to_owned())
    }

    fn normalize_optional_field(field: Option<String>) -> Option<String> {
        field.and_then(|value| {
            let trimmed = value.trim();
            if trimmed.is_empty() {
                None
            } else {
                Some(trimmed.to_owned())
            }
        })
    }

    #[expect(clippy::too_many_arguments)]
    fn new(
        timestamp: OffsetDateTime,
        command: String,
        cwd: String,
        exit: i64,
        duration: i64,
        session: Option<String>,
        hostname: Option<String>,
        author: Option<String>,
        intent: Option<String>,
        deleted_at: Option<OffsetDateTime>,
    ) -> Self {
        let session = session
            .or_else(|| env::var("ATUIN_SESSION").ok())
            .unwrap_or_else(|| uuid_v7().as_simple().to_string());
        let hostname = hostname.unwrap_or_else(get_host_user);
        let author = Self::normalize_optional_field(author)
            .or_else(|| Self::normalize_optional_field(env::var(HISTORY_AUTHOR_ENV).ok()))
            .unwrap_or_else(|| Self::author_from_hostname(hostname.as_str()));
        let intent = Self::normalize_optional_field(intent)
            .or_else(|| Self::normalize_optional_field(env::var(HISTORY_INTENT_ENV).ok()));

        Self {
            id: uuid_v7().as_simple().to_string().into(),
            timestamp,
            command,
            cwd,
            exit,
            duration,
            session,
            hostname,
            author,
            intent,
            deleted_at,
        }
    }

    /// Builder for a history entry that is captured via hook, and sent to the daemon.
    ///
    /// 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`.
    ///
    /// It does, however, include information that can usually be inferred.
    ///
    /// This is because the daemon we are sending a request to lacks the context of the command
    ///
    /// ## Examples
    /// ```rust
    /// use crate::aclient::history::History;
    ///
    /// let history: History = History::daemon()
    ///     .timestamp(time::OffsetDateTime::now_utc())
    ///     .command("ls -la")
    ///     .cwd("/home/user")
    ///     .session("018deb6e8287781f9973ef40e0fde76b")
    ///     .hostname("computer:ellie")
    ///     .build()
    ///     .into();
    /// ```
    ///
    /// Command without any required info cannot be captured, which is forced at compile time:
    ///
    /// ```compile_fail
    /// use crate::aclient::history::History;
    ///
    /// // this will not compile because `hostname` is missing
    /// let history: History = History::daemon()
    ///     .timestamp(time::OffsetDateTime::now_utc())
    ///     .command("ls -la")
    ///     .cwd("/home/user")
    ///     .session("018deb6e8287781f9973ef40e0fde76b")
    ///     .build()
    ///     .into();
    /// ```
    pub fn daemon() -> builder::HistoryDaemonCaptureBuilder {
        builder::HistoryDaemonCapture::builder()
    }

    #[doc(hidden)]
    pub fn from_db() -> builder::HistoryFromDbBuilder {
        builder::HistoryFromDb::builder()
    }

    pub fn should_save(&self, filter: SettingsFilter<'_>) -> bool {
        !(self.command.is_empty()
            || filter.history.is_match(&self.command)
            || filter.cwd.is_match(&self.cwd)
            || (filter.secrets && SECRET_PATTERNS_RE.is_match(&self.command)))
    }
}

#[derive(Debug, Copy, Clone)]
pub struct SettingsFilter<'a> {
    pub history: &'a RegexSet,
    pub cwd: &'a RegexSet,
    pub secrets: bool,
}

#[cfg(test)]
mod tests {
    // use regex::RegexSet;
    //
    // use crate::history::History;

    // // Test that we don't save history where necessary
    // #[test]
    // fn privacy_test() {
    //     let settings = Settings {
    //         cwd_filter: RegexSet::new(["^/supasecret"]).unwrap(),
    //         history_filter: RegexSet::new(["^psql"]).unwrap(),
    //         ..Settings::default()
    //     };
    //
    //     let normal_command: History = History::daemon()
    //         .timestamp(time::OffsetDateTime::now_utc())
    //         .command("echo foo")
    //         .cwd("/")
    //         .build()
    //         .into();
    //
    //     let with_space: History = History::daemon()
    //         .timestamp(time::OffsetDateTime::now_utc())
    //         .command(" echo bar")
    //         .cwd("/")
    //         .build()
    //         .into();
    //
    //     let empty: History = History::daemon()
    //         .timestamp(time::OffsetDateTime::now_utc())
    //         .command("")
    //         .cwd("/")
    //         .build()
    //         .into();
    //
    //     let stripe_key: History = History::daemon()
    //         .timestamp(time::OffsetDateTime::now_utc())
    //         .command("curl foo.com/bar?key=sk_test_1234567890abcdefghijklmnop")
    //         .cwd("/")
    //         .build()
    //         .into();
    //
    //     let secret_dir: History = History::daemon()
    //         .timestamp(time::OffsetDateTime::now_utc())
    //         .command("echo ohno")
    //         .cwd("/supasecret")
    //         .build()
    //         .into();
    //
    //     let with_psql: History = History::daemon()
    //         .timestamp(time::OffsetDateTime::now_utc())
    //         .command("psql")
    //         .cwd("/supasecret")
    //         .build()
    //         .into();
    //
    //     assert!(normal_command.should_save(&settings));
    //     assert!(!with_space.should_save(&settings));
    //     assert!(!empty.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() {
    //     let settings = Settings {
    //         secrets_filter: false,
    //         ..Settings::new().unwrap()
    //     };
    //
    //     let stripe_key: History = History::capture()
    //         .timestamp(time::OffsetDateTime::now_utc())
    //         .command("curl foo.com/bar?key=sk_test_1234567890abcdefghijklmnop")
    //         .cwd("/")
    //         .build()
    //         .into();
    //
    //     assert!(stripe_key.should_save(&settings));
    // }
}