Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cache SECRET_PATTERNS's RegexSet #2570

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 3 additions & 6 deletions crates/atuin-client/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use atuin_common::record::DecryptedData;
use atuin_common::utils::uuid_v7;

use eyre::{bail, eyre, Result};
use regex::RegexSet;

use crate::secrets::SECRET_PATTERNS_RE;
use crate::settings::Settings;
use crate::utils::get_host_user;
use crate::{secrets::SECRET_PATTERNS, settings::Settings};
use time::OffsetDateTime;

mod builder;
Expand Down Expand Up @@ -374,13 +374,10 @@ impl History {
}

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)
|| (settings.secrets_filter && SECRET_PATTERNS_RE.is_match(&self.command)))
}
}

Expand Down
11 changes: 10 additions & 1 deletion crates/atuin-client/src/secrets.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// This file will probably trigger a lot of scanners. Sorry.

use regex::RegexSet;
use std::sync::LazyLock;

pub enum TestValue<'a> {
Single(&'a str),
Multiple(&'a [&'a str]),
}

// A list of (name, regex, test), where test should match against regex
/// A list of `(name, regex, test)`, where `test` should match against `regex`.
pub static SECRET_PATTERNS: &[(&str, &str, TestValue)] = &[
(
"AWS Access Key ID",
Expand Down Expand Up @@ -114,6 +117,12 @@ pub static SECRET_PATTERNS: &[(&str, &str, TestValue)] = &[
),
];

/// The `regex` expressions from [`SECRET_PATTERNS`] compiled into a `RegexSet`.
pub static SECRET_PATTERNS_RE: LazyLock<RegexSet> = LazyLock::new(|| {
let exprs = SECRET_PATTERNS.iter().map(|f| f.1);
RegexSet::new(exprs).expect("Failed to build secrets regex")
});

#[cfg(test)]
mod tests {
use regex::Regex;
Expand Down