♻️ fermata: unify botignore.toml into .botsecrets [policy] sections
Delete botignore.toml as a config file entirely. Per-operation policy rules (read/write/bash) now live in .botsecrets under [policy.read], [policy.write], and [policy.bash]. This reduces fermata's config story to two files: .botignore (gitignore syntax) and .botsecrets (TOML). Changes: - Add PolicyConfig, OpRules, BashRules to secrets/config.rs with layer merge support - Rewrite policy.rs to load from SecretsConfig instead of BotignoreToml - Remove toml_config.rs module entirely - Remove botignore.toml from project root strong markers - Update all tests to use .botsecrets [policy.*] sections - Update error messages to reference .botsecrets All 194 tests pass. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
77520819f6
commit
ac11512f62
@@ -7,7 +7,6 @@ pub mod op;
|
|||||||
pub mod policy;
|
pub mod policy;
|
||||||
pub mod project;
|
pub mod project;
|
||||||
pub mod secrets;
|
pub mod secrets;
|
||||||
pub mod toml_config;
|
|
||||||
|
|
||||||
pub use decision::{Decision, Reason, Rule};
|
pub use decision::{Decision, Reason, Rule};
|
||||||
pub use extract::{extract_path_candidates, Confidence, PathCandidate};
|
pub use extract::{extract_path_candidates, Confidence, PathCandidate};
|
||||||
|
|||||||
+18
-20
@@ -1,7 +1,7 @@
|
|||||||
use crate::core::botignore::{BotignoreError, BotignoreSet};
|
use crate::core::botignore::{BotignoreError, BotignoreSet};
|
||||||
use crate::core::decision::{Decision, Reason, Rule};
|
use crate::core::decision::{Decision, Reason, Rule};
|
||||||
use crate::core::op::Op;
|
use crate::core::op::Op;
|
||||||
use crate::core::toml_config::{BotignoreToml, TomlConfigError};
|
use crate::core::secrets::config::{PolicyConfig, SecretsConfig, SecretsConfigError};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
@@ -10,15 +10,15 @@ pub enum PolicyError {
|
|||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Botignore(#[from] BotignoreError),
|
Botignore(#[from] BotignoreError),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Toml(#[from] TomlConfigError),
|
SecretsConfig(#[from] SecretsConfigError),
|
||||||
#[error("invalid pattern in botignore.toml: {0}")]
|
#[error("invalid pattern in .botsecrets [policy]: {0}")]
|
||||||
BadPattern(String),
|
BadPattern(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Policy {
|
pub struct Policy {
|
||||||
root: PathBuf,
|
root: PathBuf,
|
||||||
botignore: BotignoreSet,
|
botignore: BotignoreSet,
|
||||||
toml: BotignoreToml,
|
policy: PolicyConfig,
|
||||||
read_globs: globset::GlobSet,
|
read_globs: globset::GlobSet,
|
||||||
write_globs: globset::GlobSet,
|
write_globs: globset::GlobSet,
|
||||||
read_patterns: Vec<String>,
|
read_patterns: Vec<String>,
|
||||||
@@ -28,19 +28,20 @@ pub struct Policy {
|
|||||||
impl Policy {
|
impl Policy {
|
||||||
pub fn load(root: &Path) -> Result<Self, PolicyError> {
|
pub fn load(root: &Path) -> Result<Self, PolicyError> {
|
||||||
let botignore = BotignoreSet::load(root)?;
|
let botignore = BotignoreSet::load(root)?;
|
||||||
let toml = BotignoreToml::load(root)?;
|
let secrets_config = SecretsConfig::load(root)?;
|
||||||
|
let policy = secrets_config.policy.unwrap_or_default();
|
||||||
|
|
||||||
let (read_globs, read_patterns) = compile_globs(
|
let (read_globs, read_patterns) = compile_globs(
|
||||||
toml.read.as_ref().map(|r| r.patterns.as_slice()).unwrap_or(&[]),
|
policy.read.as_ref().map(|r| r.patterns.as_slice()).unwrap_or(&[]),
|
||||||
)?;
|
)?;
|
||||||
let (write_globs, write_patterns) = compile_globs(
|
let (write_globs, write_patterns) = compile_globs(
|
||||||
toml.write.as_ref().map(|r| r.patterns.as_slice()).unwrap_or(&[]),
|
policy.write.as_ref().map(|r| r.patterns.as_slice()).unwrap_or(&[]),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
root: root.to_path_buf(),
|
root: root.to_path_buf(),
|
||||||
botignore,
|
botignore,
|
||||||
toml,
|
policy,
|
||||||
read_globs,
|
read_globs,
|
||||||
write_globs,
|
write_globs,
|
||||||
read_patterns,
|
read_patterns,
|
||||||
@@ -49,35 +50,32 @@ impl Policy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn check_command(&self, command: &str) -> Result<Decision, PolicyError> {
|
pub fn check_command(&self, command: &str) -> Result<Decision, PolicyError> {
|
||||||
let bash = match self.toml.bash.as_ref() {
|
let bash = match self.policy.bash.as_ref() {
|
||||||
Some(b) => b,
|
Some(b) => b,
|
||||||
None => return Ok(Decision::Allow),
|
None => return Ok(Decision::Allow),
|
||||||
};
|
};
|
||||||
|
|
||||||
// 1. Deny wins over everything else.
|
|
||||||
if let Some(pat) = match_command(command, &bash.deny)? {
|
if let Some(pat) = match_command(command, &bash.deny)? {
|
||||||
return Ok(Decision::Deny(Reason {
|
return Ok(Decision::Deny(Reason {
|
||||||
message: format!("blocked by botignore.toml [bash.deny]: {}", pat),
|
message: format!("blocked by .botsecrets [policy.bash.deny]: {}", pat),
|
||||||
rule: Some(Rule {
|
rule: Some(Rule {
|
||||||
source: self.root.join("botignore.toml"),
|
source: self.root.join(".botsecrets"),
|
||||||
pattern: pat,
|
pattern: pat,
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Allow prefixes — if any matches, allow.
|
|
||||||
for prefix in &bash.allow_prefixes {
|
for prefix in &bash.allow_prefixes {
|
||||||
if command_matches_prefix(command, prefix) {
|
if command_matches_prefix(command, prefix) {
|
||||||
return Ok(Decision::Allow);
|
return Ok(Decision::Allow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Ask patterns.
|
|
||||||
if let Some(pat) = match_command(command, &bash.ask)? {
|
if let Some(pat) = match_command(command, &bash.ask)? {
|
||||||
return Ok(Decision::Ask(Reason {
|
return Ok(Decision::Ask(Reason {
|
||||||
message: format!("requires confirmation [bash.ask]: {}", pat),
|
message: format!("requires confirmation [policy.bash.ask]: {}", pat),
|
||||||
rule: Some(Rule {
|
rule: Some(Rule {
|
||||||
source: self.root.join("botignore.toml"),
|
source: self.root.join(".botsecrets"),
|
||||||
pattern: pat,
|
pattern: pat,
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
@@ -97,11 +95,11 @@ impl Policy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. botignore.toml namespace-specific rules.
|
// 2. .botsecrets [policy] namespace-specific rules.
|
||||||
let (set, patterns) = match op {
|
let (set, patterns) = match op {
|
||||||
Op::Read => (&self.read_globs, &self.read_patterns),
|
Op::Read => (&self.read_globs, &self.read_patterns),
|
||||||
Op::Write => (&self.write_globs, &self.write_patterns),
|
Op::Write => (&self.write_globs, &self.write_patterns),
|
||||||
Op::Execute => return Ok(Decision::Allow), // path-based check_command handles bash
|
Op::Execute => return Ok(Decision::Allow),
|
||||||
};
|
};
|
||||||
|
|
||||||
let rel = path.strip_prefix(&self.root).unwrap_or(path);
|
let rel = path.strip_prefix(&self.root).unwrap_or(path);
|
||||||
@@ -109,9 +107,9 @@ impl Policy {
|
|||||||
if let Some(idx) = matches.first() {
|
if let Some(idx) = matches.first() {
|
||||||
let pattern = patterns[*idx].clone();
|
let pattern = patterns[*idx].clone();
|
||||||
return Ok(Decision::Deny(Reason {
|
return Ok(Decision::Deny(Reason {
|
||||||
message: format!("blocked by botignore.toml [{:?}]: {}", op, pattern),
|
message: format!("blocked by .botsecrets [policy.{:?}]: {}", op, pattern),
|
||||||
rule: Some(Rule {
|
rule: Some(Rule {
|
||||||
source: self.root.join("botignore.toml"),
|
source: self.root.join(".botsecrets"),
|
||||||
pattern,
|
pattern,
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
|
|||||||
+3
-3
@@ -1,11 +1,11 @@
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
/// Strong markers that definitively identify a project root.
|
/// Strong markers that definitively identify a project root.
|
||||||
const STRONG_MARKERS: &[&str] = &["botignore.toml", ".botignore.toml", ".botsecrets", ".git"];
|
const STRONG_MARKERS: &[&str] = &[".botsecrets", ".git"];
|
||||||
|
|
||||||
/// Walk upward from `target` (or its parent if `target` is a file) looking
|
/// Walk upward from `target` (or its parent if `target` is a file) looking
|
||||||
/// for the nearest project root. Strong markers (`botignore.toml`,
|
/// for the nearest project root. Strong markers (`.botsecrets`, `.git`)
|
||||||
/// `.botignore.toml`, `.git`) stop the walk immediately. A `.botignore`
|
/// stop the walk immediately. A `.botignore`
|
||||||
/// file is remembered as a fallback but does not stop the walk — the search
|
/// file is remembered as a fallback but does not stop the walk — the search
|
||||||
/// continues upward for a stronger boundary. If none is found, the
|
/// continues upward for a stronger boundary. If none is found, the
|
||||||
/// `.botignore` location is used.
|
/// `.botignore` location is used.
|
||||||
|
|||||||
@@ -38,6 +38,47 @@ pub enum SecretsConfigError {
|
|||||||
// Config types
|
// Config types
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Per-operation policy rules (formerly `botignore.toml`).
|
||||||
|
///
|
||||||
|
/// Lives under `[policy]` in `.botsecrets`:
|
||||||
|
/// ```toml
|
||||||
|
/// [policy.read]
|
||||||
|
/// patterns = [".env*", "secrets/**"]
|
||||||
|
///
|
||||||
|
/// [policy.write]
|
||||||
|
/// patterns = ["vendor/**", "*.lock"]
|
||||||
|
///
|
||||||
|
/// [policy.bash]
|
||||||
|
/// deny = ["rm -rf /"]
|
||||||
|
/// allow_prefixes = ["cargo", "npm"]
|
||||||
|
/// ask = ["docker"]
|
||||||
|
/// ```
|
||||||
|
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct PolicyConfig {
|
||||||
|
#[serde(default)]
|
||||||
|
pub read: Option<OpRules>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub write: Option<OpRules>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub bash: Option<BashRules>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct OpRules {
|
||||||
|
#[serde(default)]
|
||||||
|
pub patterns: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct BashRules {
|
||||||
|
#[serde(default)]
|
||||||
|
pub deny: Vec<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub ask: Vec<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub allow_prefixes: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Top-level `.botsecrets` configuration.
|
/// Top-level `.botsecrets` configuration.
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct SecretsConfig {
|
pub struct SecretsConfig {
|
||||||
@@ -53,6 +94,8 @@ pub struct SecretsConfig {
|
|||||||
pub enforcement: EnforcementConfig,
|
pub enforcement: EnforcementConfig,
|
||||||
#[serde(default, rename = "file")]
|
#[serde(default, rename = "file")]
|
||||||
pub file_overrides: Vec<FileOverride>,
|
pub file_overrides: Vec<FileOverride>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub policy: Option<PolicyConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
@@ -252,6 +295,7 @@ impl Default for SecretsConfig {
|
|||||||
heuristic: HeuristicConfig::default(),
|
heuristic: HeuristicConfig::default(),
|
||||||
enforcement: EnforcementConfig::default(),
|
enforcement: EnforcementConfig::default(),
|
||||||
file_overrides: Vec::new(),
|
file_overrides: Vec::new(),
|
||||||
|
policy: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -320,6 +364,8 @@ struct PartialSecretsConfig {
|
|||||||
enforcement: Option<PartialEnforcementConfig>,
|
enforcement: Option<PartialEnforcementConfig>,
|
||||||
#[serde(default, rename = "file")]
|
#[serde(default, rename = "file")]
|
||||||
file: Option<Vec<FileOverride>>,
|
file: Option<Vec<FileOverride>>,
|
||||||
|
#[serde(default)]
|
||||||
|
policy: Option<PolicyConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Deserialize)]
|
#[derive(Debug, Clone, Default, Deserialize)]
|
||||||
@@ -414,6 +460,11 @@ impl SecretsConfig {
|
|||||||
if let Some(overrides) = layer.file {
|
if let Some(overrides) = layer.file {
|
||||||
self.file_overrides = overrides;
|
self.file_overrides = overrides;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// policy (replace whole section when present)
|
||||||
|
if layer.policy.is_some() {
|
||||||
|
self.policy = layer.policy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::path::Path;
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
|
||||||
pub enum TomlConfigError {
|
|
||||||
#[error("io error: {0}")]
|
|
||||||
Io(#[from] std::io::Error),
|
|
||||||
#[error("toml parse error: {0}")]
|
|
||||||
Parse(#[from] toml::de::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
|
||||||
pub struct OpRules {
|
|
||||||
#[serde(default)]
|
|
||||||
pub patterns: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
|
||||||
pub struct BashRules {
|
|
||||||
#[serde(default)]
|
|
||||||
pub deny: Vec<String>,
|
|
||||||
#[serde(default)]
|
|
||||||
pub ask: Vec<String>,
|
|
||||||
#[serde(default)]
|
|
||||||
pub allow_prefixes: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
|
||||||
pub struct BotignoreToml {
|
|
||||||
pub read: Option<OpRules>,
|
|
||||||
pub write: Option<OpRules>,
|
|
||||||
pub bash: Option<BashRules>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BotignoreToml {
|
|
||||||
/// Load `<root>/botignore.toml` if present, else return an empty config.
|
|
||||||
pub fn load(root: &Path) -> Result<Self, TomlConfigError> {
|
|
||||||
let path = root.join("botignore.toml");
|
|
||||||
if !path.exists() {
|
|
||||||
return Ok(Self::default());
|
|
||||||
}
|
|
||||||
let text = std::fs::read_to_string(&path)?;
|
|
||||||
let cfg = toml::from_str(&text)?;
|
|
||||||
Ok(cfg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,36 +2,36 @@ use dirigent_fermata::core::{Decision, Policy};
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
fn project_with(toml: &str) -> TempDir {
|
fn project_with(botsecrets: &str) -> TempDir {
|
||||||
let tmp = TempDir::new().unwrap();
|
let tmp = TempDir::new().unwrap();
|
||||||
fs::write(tmp.path().join("botignore.toml"), toml).unwrap();
|
fs::write(tmp.path().join(".botsecrets"), botsecrets).unwrap();
|
||||||
tmp
|
tmp
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deny_substring_blocks() {
|
fn deny_substring_blocks() {
|
||||||
let tmp = project_with("[bash]\ndeny = [\"rm -rf /\"]\n");
|
let tmp = project_with("[policy.bash]\ndeny = [\"rm -rf /\"]\n");
|
||||||
let p = Policy::load(tmp.path()).unwrap();
|
let p = Policy::load(tmp.path()).unwrap();
|
||||||
assert!(matches!(p.check_command("sudo rm -rf / now").unwrap(), Decision::Deny(_)));
|
assert!(matches!(p.check_command("sudo rm -rf / now").unwrap(), Decision::Deny(_)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deny_glob_blocks() {
|
fn deny_glob_blocks() {
|
||||||
let tmp = project_with("[bash]\ndeny = [\"git push --force*\"]\n");
|
let tmp = project_with("[policy.bash]\ndeny = [\"git push --force*\"]\n");
|
||||||
let p = Policy::load(tmp.path()).unwrap();
|
let p = Policy::load(tmp.path()).unwrap();
|
||||||
assert!(matches!(p.check_command("git push --force-with-lease").unwrap(), Decision::Deny(_)));
|
assert!(matches!(p.check_command("git push --force-with-lease").unwrap(), Decision::Deny(_)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ask_returns_ask() {
|
fn ask_returns_ask() {
|
||||||
let tmp = project_with("[bash]\nask = [\"rm *\"]\n");
|
let tmp = project_with("[policy.bash]\nask = [\"rm *\"]\n");
|
||||||
let p = Policy::load(tmp.path()).unwrap();
|
let p = Policy::load(tmp.path()).unwrap();
|
||||||
assert!(matches!(p.check_command("rm somefile").unwrap(), Decision::Ask(_)));
|
assert!(matches!(p.check_command("rm somefile").unwrap(), Decision::Ask(_)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn allow_prefixes_allows() {
|
fn allow_prefixes_allows() {
|
||||||
let tmp = project_with("[bash]\nallow_prefixes = [\"make test\"]\n");
|
let tmp = project_with("[policy.bash]\nallow_prefixes = [\"make test\"]\n");
|
||||||
let p = Policy::load(tmp.path()).unwrap();
|
let p = Policy::load(tmp.path()).unwrap();
|
||||||
assert_eq!(p.check_command("make test").unwrap(), Decision::Allow);
|
assert_eq!(p.check_command("make test").unwrap(), Decision::Allow);
|
||||||
assert_eq!(p.check_command("make test-unit").unwrap(), Decision::Allow);
|
assert_eq!(p.check_command("make test-unit").unwrap(), Decision::Allow);
|
||||||
@@ -46,7 +46,7 @@ fn no_rules_means_allow() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deny_takes_precedence_over_allow_prefix() {
|
fn deny_takes_precedence_over_allow_prefix() {
|
||||||
let tmp = project_with("[bash]\ndeny = [\"rm -rf /\"]\nallow_prefixes = [\"rm\"]\n");
|
let tmp = project_with("[policy.bash]\ndeny = [\"rm -rf /\"]\nallow_prefixes = [\"rm\"]\n");
|
||||||
let p = Policy::load(tmp.path()).unwrap();
|
let p = Policy::load(tmp.path()).unwrap();
|
||||||
assert!(matches!(p.check_command("rm -rf /").unwrap(), Decision::Deny(_)));
|
assert!(matches!(p.check_command("rm -rf /").unwrap(), Decision::Deny(_)));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ use dirigent_fermata::core::{Decision, Op, Policy};
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
fn make_project(botignore: &str, toml_text: &str) -> TempDir {
|
fn make_project(botignore: &str, botsecrets_policy: &str) -> TempDir {
|
||||||
let tmp = TempDir::new().unwrap();
|
let tmp = TempDir::new().unwrap();
|
||||||
fs::write(tmp.path().join(".botignore"), botignore).unwrap();
|
fs::write(tmp.path().join(".botignore"), botignore).unwrap();
|
||||||
if !toml_text.is_empty() {
|
if !botsecrets_policy.is_empty() {
|
||||||
fs::write(tmp.path().join("botignore.toml"), toml_text).unwrap();
|
fs::write(tmp.path().join(".botsecrets"), botsecrets_policy).unwrap();
|
||||||
}
|
}
|
||||||
tmp
|
tmp
|
||||||
}
|
}
|
||||||
@@ -42,8 +42,8 @@ fn unmatched_path_allowed() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn toml_read_block_applies_only_to_read() {
|
fn policy_read_block_applies_only_to_read() {
|
||||||
let tmp = make_project("", "[read]\npatterns = [\"secrets/**\"]\n");
|
let tmp = make_project("", "[policy.read]\npatterns = [\"secrets/**\"]\n");
|
||||||
let policy = Policy::load(tmp.path()).unwrap();
|
let policy = Policy::load(tmp.path()).unwrap();
|
||||||
let target = tmp.path().join("secrets/key.pem");
|
let target = tmp.path().join("secrets/key.pem");
|
||||||
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
||||||
@@ -53,8 +53,8 @@ fn toml_read_block_applies_only_to_read() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn toml_write_block_applies_only_to_write() {
|
fn policy_write_block_applies_only_to_write() {
|
||||||
let tmp = make_project("", "[write]\npatterns = [\"vendor/**\"]\n");
|
let tmp = make_project("", "[policy.write]\npatterns = [\"vendor/**\"]\n");
|
||||||
let policy = Policy::load(tmp.path()).unwrap();
|
let policy = Policy::load(tmp.path()).unwrap();
|
||||||
let target = tmp.path().join("vendor/lib.rs");
|
let target = tmp.path().join("vendor/lib.rs");
|
||||||
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
||||||
|
|||||||
+3
-26
@@ -3,13 +3,11 @@ use std::fs;
|
|||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn finds_botignore_toml_first() {
|
fn finds_botsecrets_as_strong_marker() {
|
||||||
let tmp = TempDir::new().unwrap();
|
let tmp = TempDir::new().unwrap();
|
||||||
let root = tmp.path();
|
let root = tmp.path();
|
||||||
fs::create_dir_all(root.join("sub/deep")).unwrap();
|
fs::create_dir_all(root.join("sub/deep")).unwrap();
|
||||||
fs::write(root.join("botignore.toml"), "").unwrap();
|
fs::write(root.join(".botsecrets"), "").unwrap();
|
||||||
fs::write(root.join(".botignore.toml"), "").unwrap();
|
|
||||||
fs::create_dir_all(root.join(".git")).unwrap();
|
|
||||||
|
|
||||||
let target = root.join("sub/deep/file.rs");
|
let target = root.join("sub/deep/file.rs");
|
||||||
fs::write(&target, "").unwrap();
|
fs::write(&target, "").unwrap();
|
||||||
@@ -18,24 +16,8 @@ fn finds_botignore_toml_first() {
|
|||||||
assert_eq!(found, root);
|
assert_eq!(found, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn finds_dot_botignore_toml() {
|
|
||||||
let tmp = TempDir::new().unwrap();
|
|
||||||
let root = tmp.path();
|
|
||||||
fs::create_dir_all(root.join("sub")).unwrap();
|
|
||||||
fs::write(root.join(".botignore.toml"), "").unwrap();
|
|
||||||
|
|
||||||
let target = root.join("sub/file.rs");
|
|
||||||
fs::write(&target, "").unwrap();
|
|
||||||
|
|
||||||
let found = find_project_root(&target).unwrap();
|
|
||||||
assert_eq!(found, root);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn botignore_alone_does_not_stop_walk() {
|
fn botignore_alone_does_not_stop_walk() {
|
||||||
// A bare .botignore is a policy file, not a project boundary.
|
|
||||||
// The walk should continue past it to find a real root marker.
|
|
||||||
let tmp = TempDir::new().unwrap();
|
let tmp = TempDir::new().unwrap();
|
||||||
let root = tmp.path();
|
let root = tmp.path();
|
||||||
fs::create_dir_all(root.join("a/b")).unwrap();
|
fs::create_dir_all(root.join("a/b")).unwrap();
|
||||||
@@ -45,15 +27,12 @@ fn botignore_alone_does_not_stop_walk() {
|
|||||||
let target = root.join("a/b/file.rs");
|
let target = root.join("a/b/file.rs");
|
||||||
fs::write(&target, "").unwrap();
|
fs::write(&target, "").unwrap();
|
||||||
|
|
||||||
// Should find root (with .git), NOT root/a (with .botignore)
|
|
||||||
let found = find_project_root(&target).unwrap();
|
let found = find_project_root(&target).unwrap();
|
||||||
assert_eq!(found, root);
|
assert_eq!(found, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn botignore_used_as_fallback() {
|
fn botignore_used_as_fallback() {
|
||||||
// If only .botignore exists (no strong marker), it serves as a fallback
|
|
||||||
// root so that policy is still enforced.
|
|
||||||
let tmp = TempDir::new().unwrap();
|
let tmp = TempDir::new().unwrap();
|
||||||
let root = tmp.path();
|
let root = tmp.path();
|
||||||
fs::create_dir_all(root.join("sub")).unwrap();
|
fs::create_dir_all(root.join("sub")).unwrap();
|
||||||
@@ -68,7 +47,6 @@ fn botignore_used_as_fallback() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn strong_marker_preferred_over_botignore_fallback() {
|
fn strong_marker_preferred_over_botignore_fallback() {
|
||||||
// .botignore at a/b/, .git at root — walk past .botignore, use root.
|
|
||||||
let tmp = TempDir::new().unwrap();
|
let tmp = TempDir::new().unwrap();
|
||||||
let root = tmp.path();
|
let root = tmp.path();
|
||||||
fs::create_dir_all(root.join("a/b/c")).unwrap();
|
fs::create_dir_all(root.join("a/b/c")).unwrap();
|
||||||
@@ -78,7 +56,6 @@ fn strong_marker_preferred_over_botignore_fallback() {
|
|||||||
let target = root.join("a/b/c/file.rs");
|
let target = root.join("a/b/c/file.rs");
|
||||||
fs::write(&target, "").unwrap();
|
fs::write(&target, "").unwrap();
|
||||||
|
|
||||||
// Should find root (with .git), not a/b (with .botignore)
|
|
||||||
let found = find_project_root(&target).unwrap();
|
let found = find_project_root(&target).unwrap();
|
||||||
assert_eq!(found, root);
|
assert_eq!(found, root);
|
||||||
}
|
}
|
||||||
@@ -110,7 +87,7 @@ fn walks_up_from_file_path_not_cwd() {
|
|||||||
let tmp = TempDir::new().unwrap();
|
let tmp = TempDir::new().unwrap();
|
||||||
let root = tmp.path();
|
let root = tmp.path();
|
||||||
fs::create_dir_all(root.join("a/b/c")).unwrap();
|
fs::create_dir_all(root.join("a/b/c")).unwrap();
|
||||||
fs::write(root.join("a/botignore.toml"), "").unwrap();
|
fs::write(root.join("a/.botsecrets"), "").unwrap();
|
||||||
|
|
||||||
let target = root.join("a/b/c/file.rs");
|
let target = root.join("a/b/c/file.rs");
|
||||||
fs::write(&target, "").unwrap();
|
fs::write(&target, "").unwrap();
|
||||||
|
|||||||
+44
-21
@@ -1,47 +1,70 @@
|
|||||||
use dirigent_fermata::core::toml_config::{BotignoreToml, OpRules, BashRules};
|
use dirigent_fermata::core::secrets::config::SecretsConfig;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parses_full_config() {
|
fn parses_policy_sections() {
|
||||||
let src = r#"
|
let src = r#"
|
||||||
[read]
|
[policy.read]
|
||||||
patterns = [".env*", "secrets/**"]
|
patterns = [".env*", "secrets/**"]
|
||||||
|
|
||||||
[write]
|
[policy.write]
|
||||||
patterns = ["vendor/**", "*.lock"]
|
patterns = ["vendor/**", "*.lock"]
|
||||||
|
|
||||||
[bash]
|
[policy.bash]
|
||||||
deny = ["rm -rf /", "git push --force*"]
|
deny = ["rm -rf /", "git push --force*"]
|
||||||
ask = ["rm:*"]
|
ask = ["rm:*"]
|
||||||
allow_prefixes = ["make test", "git checkout:*"]
|
allow_prefixes = ["make test", "git checkout:*"]
|
||||||
"#;
|
"#;
|
||||||
let cfg: BotignoreToml = toml::from_str(src).unwrap();
|
let cfg = SecretsConfig::from_toml(src).unwrap();
|
||||||
assert_eq!(cfg.read.unwrap().patterns, vec![".env*", "secrets/**"]);
|
let policy = cfg.policy.unwrap();
|
||||||
assert_eq!(cfg.write.unwrap().patterns, vec!["vendor/**", "*.lock"]);
|
assert_eq!(policy.read.unwrap().patterns, vec![".env*", "secrets/**"]);
|
||||||
let bash = cfg.bash.unwrap();
|
assert_eq!(policy.write.unwrap().patterns, vec!["vendor/**", "*.lock"]);
|
||||||
|
let bash = policy.bash.unwrap();
|
||||||
assert_eq!(bash.deny, vec!["rm -rf /", "git push --force*"]);
|
assert_eq!(bash.deny, vec!["rm -rf /", "git push --force*"]);
|
||||||
assert_eq!(bash.ask, vec!["rm:*"]);
|
assert_eq!(bash.ask, vec!["rm:*"]);
|
||||||
assert_eq!(bash.allow_prefixes, vec!["make test", "git checkout:*"]);
|
assert_eq!(bash.allow_prefixes, vec!["make test", "git checkout:*"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_config_is_valid() {
|
fn empty_config_has_no_policy() {
|
||||||
let cfg: BotignoreToml = toml::from_str("").unwrap();
|
let cfg = SecretsConfig::from_toml("").unwrap();
|
||||||
assert!(cfg.read.is_none());
|
assert!(cfg.policy.is_none());
|
||||||
assert!(cfg.write.is_none());
|
|
||||||
assert!(cfg.bash.is_none());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn loads_from_disk_when_present() {
|
fn loads_policy_from_disk() {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
std::fs::write(tmp.path().join("botignore.toml"), "[read]\npatterns = [\".env\"]\n").unwrap();
|
std::fs::write(
|
||||||
let cfg = BotignoreToml::load(tmp.path()).unwrap();
|
tmp.path().join(".botsecrets"),
|
||||||
assert_eq!(cfg.read.unwrap().patterns, vec![".env"]);
|
"[policy.read]\npatterns = [\".env\"]\n",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let cfg = SecretsConfig::load(tmp.path()).unwrap();
|
||||||
|
let policy = cfg.policy.unwrap();
|
||||||
|
assert_eq!(policy.read.unwrap().patterns, vec![".env"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn loads_empty_when_missing() {
|
fn no_botsecrets_means_no_policy() {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
let cfg = BotignoreToml::load(tmp.path()).unwrap();
|
let cfg = SecretsConfig::load(tmp.path()).unwrap();
|
||||||
assert!(cfg.read.is_none());
|
assert!(cfg.policy.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn policy_coexists_with_secrets_config() {
|
||||||
|
let src = r#"
|
||||||
|
[files]
|
||||||
|
patterns = [".env"]
|
||||||
|
|
||||||
|
[policy.read]
|
||||||
|
patterns = ["secrets/**"]
|
||||||
|
|
||||||
|
[policy.bash]
|
||||||
|
deny = ["rm -rf /"]
|
||||||
|
"#;
|
||||||
|
let cfg = SecretsConfig::from_toml(src).unwrap();
|
||||||
|
assert_eq!(cfg.files.patterns, vec![".env"]);
|
||||||
|
let policy = cfg.policy.unwrap();
|
||||||
|
assert_eq!(policy.read.unwrap().patterns, vec!["secrets/**"]);
|
||||||
|
assert_eq!(policy.bash.unwrap().deny, vec!["rm -rf /"]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ fn fixture() -> TempDir {
|
|||||||
let root = tmp.path();
|
let root = tmp.path();
|
||||||
fs::write(root.join(".botignore"), ".env\n.env.*\nconf/cert/**\nconf/mitmproxy/**\n").unwrap();
|
fs::write(root.join(".botignore"), ".env\n.env.*\nconf/cert/**\nconf/mitmproxy/**\n").unwrap();
|
||||||
fs::write(
|
fs::write(
|
||||||
root.join("botignore.toml"),
|
root.join(".botsecrets"),
|
||||||
r#"
|
r#"
|
||||||
[read]
|
[policy.read]
|
||||||
patterns = [
|
patterns = [
|
||||||
"conf/localtestsettings.yaml",
|
"conf/localtestsettings.yaml",
|
||||||
"conf/localsettings.yaml",
|
"conf/localsettings.yaml",
|
||||||
@@ -19,14 +19,14 @@ patterns = [
|
|||||||
".claude/self-reflections/**",
|
".claude/self-reflections/**",
|
||||||
]
|
]
|
||||||
|
|
||||||
[write]
|
[policy.write]
|
||||||
patterns = [
|
patterns = [
|
||||||
"conf/localtestsettings.yaml",
|
"conf/localtestsettings.yaml",
|
||||||
"conf/localsettings.yaml",
|
"conf/localsettings.yaml",
|
||||||
"conf/default-secrets.yaml",
|
"conf/default-secrets.yaml",
|
||||||
]
|
]
|
||||||
|
|
||||||
[bash]
|
[policy.bash]
|
||||||
deny = ["localtestsettings.yaml", "localsettings.yaml", "default-secrets.yaml", ".env"]
|
deny = ["localtestsettings.yaml", "localsettings.yaml", "default-secrets.yaml", ".env"]
|
||||||
ask = ["rm *", "mv *"]
|
ask = ["rm *", "mv *"]
|
||||||
allow_prefixes = ["make test"]
|
allow_prefixes = ["make test"]
|
||||||
|
|||||||
Reference in New Issue
Block a user