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
//! `moat policy` grammar.
use std::path::PathBuf;
use clap::{Args, Subcommand, ValueEnum};
use super::Format;
#[derive(Debug, Subcommand)]
pub enum PolicyCommand {
/// Validate a policy file and print a summary.
Lint(LintArgs),
/// Decide one action against a policy and explain the result.
Check(CheckArgs),
/// Check a change before it is merged: every file it adds, edits, deletes or renames that
/// the policy protects (CI, git hooks, build and package files, agent settings, OpenMoat's
/// own files, secrets), and added lines holding a known token. Exits 0 when nothing is
/// found, 2 when the policy denies something, 3 when something needs a person's review.
CheckDiff(CheckDiffArgs),
/// Compare two policy files: the rules and settings that changed and, with --widen,
/// every call, rule boundary and subject the new one decides less strictly than the
/// old one (deny > review > defer > allow). With --widen, exits 0 when nothing
/// widens, 2 when something does, 3 when a setting changed that is not ranked.
Diff(DiffArgs),
/// Print what OS layers enforce for a policy: the compiled IR and its losses.
Compile(CompileArgs),
/// Add what this release's default policy changed to ~/.moat/policy.yaml, keeping every
/// line you changed; shows the diff and asks first. An upgrade that moves a call toward
/// allow is listed case by case and applied only with --accept-widening (exit 3
/// without it). Refused unless run from an interactive terminal, except with --dry-run.
#[command(
after_help = "Example: openmoat policy upgrade --dry-run (what the new default \
adds, grouped; changes nothing)\n\
Docs: https://github.com/crocodile-labs/openmoat/blob/main/docs/USAGE.md#after-upgrading-openmoat"
)]
Upgrade(UpgradeArgs),
/// Rewrite a policy file in the current policy schema (`version:`), changing no rule:
/// shows the diff, asks, and keeps the previous file beside it. Refused unless run from
/// an interactive terminal, except with --dry-run.
Migrate(MigrateArgs),
/// Install a team's signed policy pack: verify its OpenSSH signature against the
/// keys `openmoat policy trust-key` trusted, then merge its rules into the policy and
/// pin the pack. Refused unless run from an interactive terminal, and over a
/// drifted lock.
Install(InstallArgs),
/// Trust an OpenSSH public key to sign policy packs (and, where the policy sets
/// `require_signed_repo_policy`, repository policies). Refused unless run from an
/// interactive terminal, and over a drifted lock.
TrustKey(TrustKeyArgs),
/// Remove an installed policy pack by name. Refused unless run from an
/// interactive terminal, and over a drifted lock.
Remove(RemoveArgs),
}
#[derive(Debug, Args)]
pub struct InstallArgs {
/// The pack: a policy file with only `version`, `deny`, `review` and `allow`.
pub pack: PathBuf,
/// Its detached signature (`ssh-keygen -Y sign -n openmoat-pack`). Defaults
/// to the pack's path with `.sig` appended.
#[arg(long, value_name = "PATH")]
pub sig: Option<PathBuf>,
/// The name the pack is installed and its rules prefixed under (`pack:<name>:`).
/// Defaults to the file name without its extension.
#[arg(long)]
pub name: Option<String>,
}
#[derive(Debug, Args)]
pub struct TrustKeyArgs {
/// The public key file (`id_ed25519.pub`).
pub key: PathBuf,
/// Who the key belongs to, as signatures will name them (`alice@example.com`).
#[arg(long)]
pub name: String,
}
#[derive(Debug, Args)]
pub struct RemoveArgs {
/// The installed pack's name.
pub name: String,
}
#[derive(Debug, Args)]
pub struct LintArgs {
/// Policy file to validate. Defaults to the installed user policy.
pub file: Option<PathBuf>,
}
#[derive(Debug, Args)]
pub struct CheckArgs {
/// The action: a shell command, a path, a URL or an MCP tool name, depending on --kind.
pub action: String,
/// Policy file to evaluate against. Defaults to the installed user policy.
#[arg(long, short)]
pub policy: Option<PathBuf>,
/// Kind of action.
#[arg(long, value_enum, default_value_t = ActionKind::Shell)]
pub kind: ActionKind,
/// Trusted project root that `${project}` expands to. Defaults to the git root of --cwd.
#[arg(long)]
pub project: Option<PathBuf>,
/// Working directory the action runs in. Defaults to the current directory.
#[arg(long)]
pub cwd: Option<PathBuf>,
/// Output format.
#[arg(long, value_enum, default_value_t = Format::Text)]
pub format: Format,
}
#[derive(Debug, Args)]
pub struct CheckDiffArgs {
/// The revision the change is made against, such as a pull request's base commit. The
/// change is everything from where `--head` left it to `--head`, and the policy files
/// are read as this revision has them. Without it, the unified diff on stdin is checked.
#[arg(long, value_name = "REV")]
pub base: Option<String>,
/// The revision with the change. Defaults to `HEAD`.
#[arg(long, value_name = "REV", requires = "base")]
pub head: Option<String>,
#[command(flatten)]
pub provenance: super::DiffProvenanceArgs,
/// Policy to check against. Defaults to the shipped default policy, so every machine
/// gives the same answer. With --base, a relative path is a file of the repository,
/// read as --base has it. The repository's own `.moat/policy.yaml` at --base adds its
/// deny and review rules.
#[arg(long, short)]
pub policy: Option<PathBuf>,
/// Output format. Defaults to `github` when GitHub Actions runs it, `text` otherwise.
#[arg(long, value_enum)]
pub format: Option<DiffFormat>,
}
#[derive(Debug, Args)]
pub struct DiffArgs {
/// The policy before the change.
pub old: PathBuf,
/// The policy after the change.
pub new: PathBuf,
/// Decide both policies over the bundled cases, cases generated from both policies'
/// rules and a search over every path, host and name, and report what widens.
#[arg(long)]
pub widen: bool,
/// A conformance fixture file (`tests/conformance/*.yaml`) whose calls are decided
/// too; repeatable. Needs --widen.
#[arg(long, value_name = "FILE", requires = "widen")]
pub fixtures: Vec<PathBuf>,
/// Output format.
#[arg(long, value_enum, default_value_t = Format::Text)]
pub format: Format,
}
/// `policy check-diff --format`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum DiffFormat {
Text,
Json,
/// GitHub Actions workflow commands (`::error file=…::…`), then the text.
Github,
}
#[derive(Debug, Args)]
pub struct CompileArgs {
/// Policy file to compile. Defaults to the installed user policy.
#[arg(long, short)]
pub policy: Option<PathBuf>,
/// Trusted project root that `${project}` expands to. Defaults to the git root of --cwd.
#[arg(long)]
pub project: Option<PathBuf>,
/// Working directory to compile for. Defaults to the current directory.
#[arg(long)]
pub cwd: Option<PathBuf>,
/// Output format.
#[arg(long, value_enum, default_value_t = Format::Text)]
pub format: Format,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum ActionKind {
Shell,
FsRead,
FsWrite,
Net,
/// A URL read by a host fetch tool (`WebFetch`).
Fetch,
Mcp,
}
#[derive(Debug, Args)]
#[expect(
clippy::struct_excessive_bools,
reason = "five command-line flags, each of its own: clap reads them as bools"
)]
pub struct UpgradeArgs {
/// Show the changes without writing anything; needs no terminal.
#[arg(long, conflicts_with = "undo")]
pub dry_run: bool,
/// Show the full diff of the policy after the summary of its changes
/// (`d` at the question shows it too).
#[arg(long, conflicts_with = "undo")]
pub diff: bool,
/// Apply without asking (still only from a terminal).
#[arg(long, short = 'y')]
pub yes: bool,
/// Restore the policy the last upgrade replaced, byte for byte.
#[arg(long)]
pub undo: bool,
/// Apply an upgrade that moves a call toward allow or changes a setting the check
/// does not rank, after reading what it widens (still only from a terminal).
#[arg(long, conflicts_with = "undo")]
pub accept_widening: bool,
}
#[derive(Debug, Args)]
pub struct MigrateArgs {
/// Policy file to migrate (a repository policy or a pack's source). Defaults to the
/// installed user policy, which is re-pinned after it is written.
#[arg(long, value_name = "PATH")]
pub file: Option<PathBuf>,
/// Show the diff without writing anything; needs no terminal.
#[arg(long)]
pub dry_run: bool,
/// Apply without asking (still only from a terminal).
#[arg(long, short = 'y')]
pub yes: bool,
}