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
// SPDX-License-Identifier: Apache-2.0
//! Hosted-client command arguments.
use clap::{Subcommand, ValueEnum};
/// Preset operation ceilings for `heddle auth derive-agent`.
///
/// Each variant expands to a curated set of safe agent operations. `reviewer`
/// and `ci-landing` are strict subsets of the safe ceiling; `contributor` is
/// the full safe ceiling (the named form of the default `--allow`-less
/// derivation). `--scope`/`--allow` stay usable alongside a template and, when
/// combined, may only *narrow* it (they intersect the template's set).
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
pub enum AgentTemplateArg {
/// Read + review: every read RPC plus Pull. No writes, no ref moves.
/// (ListStates/GetState/GetBlame/GetTree/GetBlob/GetDiff/GetCompare/
/// ListActions/ListContext/GetContextHistory/GetDiscussion/ListByState/
/// ListBySymbol/... + Pull + WhoAmI.)
Reviewer,
/// Read + collaboration writes: the reviewer set plus Push, UpdateRef,
/// SetContext/ReviseContext/SupersedeContext, and
/// OpenDiscussion/AppendTurn/ResolveDiscussion. No repo/namespace admin.
/// This is the full safe agent ceiling — the named form of deriving with
/// no --template/--allow.
Contributor,
/// Read + Pull + the Push/UpdateRef a CI lander needs to run ready/land.
/// No context or discussion writes.
#[value(name = "ci-landing")]
CiLanding,
}
impl From<AgentTemplateArg> for heddle_client::device_flow::AgentTemplate {
fn from(arg: AgentTemplateArg) -> Self {
match arg {
AgentTemplateArg::Reviewer => Self::Reviewer,
AgentTemplateArg::Contributor => Self::Contributor,
AgentTemplateArg::CiLanding => Self::CiLanding,
}
}
}
#[derive(Subcommand, Clone, Debug)]
pub enum AuthCommands {
/// Authenticate with a Heddle server
Login {
/// Heddle server address (browser flow). Omit to use the configured
/// default server.
#[arg(long)]
server: Option<String>,
/// Open the authorization URL in the system browser.
#[arg(long)]
open_browser: bool,
/// Install a verified `.hcred` credential file without a browser.
/// The server is taken from the file.
#[arg(long, value_name = "HCRED_PATH", conflicts_with_all = ["server", "open_browser"])]
credential: Option<std::path::PathBuf>,
},
/// Remove stored credentials for a server
Logout {
/// Heddle server address
#[arg(long)]
server: Option<String>,
},
/// Show current authentication status
Status {
/// Heddle server address
#[arg(long)]
server: Option<String>,
},
/// Derive a scoped, short-lived agent token offline
DeriveAgent {
/// Server whose stored credential is the parent.
#[arg(long)]
server: String,
/// Delegation name recorded in the Biscuit chain.
#[arg(long)]
agent_id: Option<String>,
/// Child lifetime in seconds (clamped by the parent expiry).
#[arg(long = "ttl", default_value_t = 3600)]
ttl_secs: u64,
/// Forward-compatible resource scope (`repo:org/name`, `namespace:org`, or a bare repo path).
#[arg(long = "scope")]
scopes: Vec<String>,
/// Narrow the safe operation set (repeatable, using gRPC method names such as `Push`).
#[arg(long = "allow")]
allowed_operations: Vec<String>,
/// Preset operation ceiling. `reviewer` = read-only + Pull;
/// `contributor` = reviewer + Push/UpdateRef + context/discussion
/// writes; `ci-landing` = reviewer + Push/UpdateRef for ready/land.
/// A combined `--allow` may only narrow the template.
#[arg(long, value_enum)]
template: Option<AgentTemplateArg>,
/// Write a single self-verifying `<name>.hcred` credential file to this
/// path instead of installing the child into the keystore.
#[arg(long, value_name = "HCRED_PATH")]
out: Option<std::path::PathBuf>,
},
/// Create a service token for CI/scripts, scoped to a namespace
CreateServiceToken {
/// Display name for the service account (e.g. "github-ci-main")
name: String,
/// Namespace to scope the token to (e.g. "heddle/platform")
#[arg(long)]
namespace: String,
/// Heddle server address
#[arg(long)]
server: Option<String>,
/// Write the `.hcred` credential file to this path
/// (default: ~/.heddle/service-accounts/<name>.hcred)
#[arg(long, value_name = "HCRED_PATH")]
out: Option<std::path::PathBuf>,
},
}
impl From<AuthCommands> for heddle_client::AuthCommand {
fn from(command: AuthCommands) -> Self {
match command {
AuthCommands::Login {
server,
open_browser,
credential,
} => heddle_client::AuthCommand::Login {
server,
open_browser,
credential,
},
AuthCommands::Logout { server } => heddle_client::AuthCommand::Logout { server },
AuthCommands::Status { server } => heddle_client::AuthCommand::Status { server },
AuthCommands::DeriveAgent {
server,
agent_id,
ttl_secs,
scopes,
allowed_operations,
template,
out,
} => heddle_client::AuthCommand::DeriveAgent {
server,
agent_id,
ttl_secs,
scopes,
allowed_operations,
template: template.map(Into::into),
out,
},
AuthCommands::CreateServiceToken {
name,
namespace,
server,
out,
} => heddle_client::AuthCommand::CreateServiceToken {
name,
namespace,
server,
out,
},
}
}
}
#[cfg(test)]
mod tests {
use clap::Parser;
use crate::cli::{AuthCommands, Cli, Commands};
#[test]
fn login_parses_credential_path() {
let cli = Cli::try_parse_from([
"heddle",
"auth",
"login",
"--credential",
"/run/secrets/agent.hcred",
])
.expect("credential login flag parses");
let Commands::Auth {
command:
AuthCommands::Login {
server,
credential,
open_browser,
},
} = cli.command
else {
panic!("expected auth login");
};
assert_eq!(server, None, "server comes from the credential file");
assert_eq!(
credential.as_deref(),
Some(std::path::Path::new("/run/secrets/agent.hcred"))
);
assert!(!open_browser);
}
#[test]
fn login_credential_conflicts_with_browser_flags() {
for conflicting in [
vec!["--credential", "/run/secrets/agent.hcred", "--server", "grpc.heddle.sh"],
vec!["--credential", "/run/secrets/agent.hcred", "--open-browser"],
] {
let mut args = vec!["heddle", "auth", "login"];
args.extend(conflicting);
assert!(
Cli::try_parse_from(args).is_err(),
"--credential must not combine with the browser-login flags"
);
}
}
#[test]
fn interactive_login_needs_no_flags() {
Cli::try_parse_from(["heddle", "auth", "login"])
.expect("interactive login may resolve the configured default server");
}
#[test]
fn derive_agent_parses_repeatable_scopes_and_operation_narrowing() {
let cli = Cli::try_parse_from([
"heddle",
"auth",
"derive-agent",
"--server",
"grpc.heddle.test",
"--ttl",
"900",
"--scope",
"repo:acme/api",
"--scope",
"namespace:acme",
"--allow",
"Push",
"--allow",
"GetState",
])
.expect("derive-agent flags parse");
let Commands::Auth {
command:
AuthCommands::DeriveAgent {
server,
ttl_secs,
scopes,
allowed_operations,
..
},
} = cli.command
else {
panic!("expected auth derive-agent");
};
assert_eq!(server, "grpc.heddle.test");
assert_eq!(ttl_secs, 900);
assert_eq!(scopes, ["repo:acme/api", "namespace:acme"]);
assert_eq!(allowed_operations, ["Push", "GetState"]);
assert!(
Cli::try_parse_from([
"heddle",
"auth",
"derive-agent",
"--server",
"grpc.heddle.test",
"--stdout",
])
.is_err(),
"token-only child export is unsafe because it cannot carry its proof key"
);
}
}