Skip to content
Merged
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
111 changes: 111 additions & 0 deletions pkgs/fleet/book/reference/bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,117 @@ $ fleet bridge log --kind command --limit 5

Shows the five most recent commands in the central log.

## fleet bridge mint-enroll

```
fleet bridge mint-enroll --agent A [--kind K] [--groups g1,g2] [--notes-identity I] [--ttl SECS]
```

| Flag | Argument | Default | Meaning |
| ------------------ | -------- | -------- | ------------------------------------------------------------------------------------------- |
| `--agent` | `A` | required | The new agent's name; the token is bound to it and enrolls no other. |
| `--kind` | `K` | `agent` | The agent kind the token is bound to. |
| `--groups` | `g1,g2` | — | Groups the mailbox registers with at exchange. |
| `--notes-identity` | `I` | — | A class/name notes identity to register at exchange. |
| `--ttl` | `SECS` | `86400` | The token's lifetime in seconds (60 to 604800). |

### Behaviour

Central verb (owner token). Mints a bootstrap enroll token. Route: `POST /api/<owner-token>/enroll-tokens`. The answer carries the token value **once**: the bridge stores only its SHA-256 hash and can never show it again. Hand the value to the new seat, which runs `fleet agent enroll --name A --kind K --bootstrap <value>`; the exchange burns the token and returns the agent's fleet credential and notes token, likewise once. The PM console's Agents screen mints these from its Provisioning section and shows the value and the command once, on that screen only.

### Exit codes

`0` ok · `2` usage · `3` transport or credentials · `4` the bridge refused the request.

### Example

```console
$ fleet bridge mint-enroll --agent scout-2 --kind muse --groups muse
```

Mints a day-long bootstrap token for `scout-2` and prints it, once.

## fleet bridge enroll-tokens

```
fleet bridge enroll-tokens
```

| Flag | Argument | Default | Meaning |
| ---- | -------- | ------- | -------------------------------- |
| — | — | — | Takes no arguments and no flags. |

### Behaviour

Central verb (owner token). Lists every minted bootstrap token's state: `pending`, `used` (with the exchange's time), `expired` (its TTL lapsed), or `revoked` (with the revocation time), each with its binding (agent, kind, groups, notes identity) and mint/expiry times. Route: `GET /api/<owner-token>/enroll-tokens`. State only: no hash or value is ever shown — the value existed once, in the mint answer.

### Exit codes

`0` ok · `2` usage · `3` transport or credentials · `4` the bridge refused the request.

### Example

```console
$ fleet bridge enroll-tokens
```

Shows which minted tokens are still outstanding, and how each closed one ended.

## fleet bridge revoke-enroll

```
fleet bridge revoke-enroll (--agent A | --token-hash H)
```

| Flag | Argument | Default | Meaning |
| -------------- | -------- | ------- | ------------------------------------------------------------------------------------------------ |
| `--agent` | `A` | — | Take back every still-pending token minted for this agent; the answer counts them. |
| `--token-hash` | `H` | — | Revoke the one token stored under this SHA-256 hash; the answer is `{revoked: true\|false}`. |

### Behaviour

Central verb (owner token). Revokes unused bootstrap tokens. Route: `POST /api/<owner-token>/enroll-tokens/revoke`. Exactly one form is required. The hash is not a credential — it cannot enroll anything — so it may travel where a token value never may. Used tokens are untouched: they became credentials, revoked with `fleet bridge revoke --agent`. Expired tokens are already dead and keep their state.

### Exit codes

`0` ok · `2` usage · `3` transport or credentials · `4` the bridge refused the request.

### Example

```console
$ fleet bridge revoke-enroll --agent scout-2
```

Takes back every outstanding token minted for `scout-2`.

## fleet bridge mint-credential

```
fleet bridge mint-credential --agent A [--scopes s1,s2] [--addr service:<name>]
```

| Flag | Argument | Default | Meaning |
| ---------- | --------------- | ------------ | ------------------------------------------------------------------------------------------------ |
| `--agent` | `A` | required | The agent (or service) the credential belongs to; it acts only as itself. |
| `--scopes` | `s1,s2` | see below | Any of `inbox`, `send`, `read`. Agent mints default to `inbox, send`; service mints to `send, read`. |
| `--addr` | `service:<name>` | — | Make it a service credential, bound to that address; the name must be the agent's own. |

### Behaviour

Central verb (owner token). Mints a per-agent credential directly, for an agent already enrolled or for a service. Route: `POST /api/<owner-token>/fleet/credentials/mint`. Like the enroll mint, the answer carries the credential value **once** and the bridge stores only its hash. Enroll-minted credentials never carry `read`; that scope is for service credentials like the PM stack's.

### Exit codes

`0` ok · `2` usage · `3` transport or credentials · `4` the bridge refused the request.

### Example

```console
$ fleet bridge mint-credential --agent pm-stack --addr service:pm-stack
```

Mints the PM stack's service credential and prints it, once, to be stored in the vault as `bridge.pm-stack-token`.

## fleet bridge hello

```
Expand Down
146 changes: 146 additions & 0 deletions pkgs/fleet/crates/fleet-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ central (owner token):
revoke (--agent A | --identity I)
revoke an agent's fleet credentials, or a notes identity's
tokens (the owner's kill switch, #14368)
mint-enroll --agent A [--kind K] [--groups g1,g2]
[--notes-identity I] [--ttl SECS]
mint a bootstrap enroll token bound to the agent (and kind,
groups, notes identity); the value is printed once, in this
answer only — hand it to the new seat's enroll --bootstrap
enroll-tokens every minted bootstrap token's state
(pending, used, expired, revoked, with its closing times;
never a hash or a value)
revoke-enroll (--agent A | --token-hash H)
revoke unused bootstrap tokens: an agent's pending ones, or
one token by its stored hash (a token value is never needed,
and never travels)
mint-credential --agent A [--scopes s1,s2] [--addr service:<name>]
mint a per-agent credential directly; the value is printed
once, in this answer only

agent (fleet token):
hello [--agent A] [--groups g1,g2] register / refresh groups
Expand Down Expand Up @@ -179,6 +194,51 @@ usage: fleet bridge revoke (--agent A | --identity I)
central (owner token). Revoke every live fleet credential of an
agent (--agent), or every notes token of an identity (--identity):
the per-agent kill switch (#14368). Prints how many were revoked."
}
"mint-enroll" => {
"\
usage: fleet bridge mint-enroll --agent A [--kind K] [--groups g1,g2]
[--notes-identity I] [--ttl SECS]

central (owner token). Mint a bootstrap enroll token (#14368) bound
at mint to the agent's name, kind (default: agent), groups and class
notes identity, expiring after --ttl seconds (60 to 604800, default
86400). The answer carries the token value once: it is stored
hash-only and can never be shown again. Hand the value to the new
seat: fleet agent enroll --name A --kind K --bootstrap <value>."
}
"enroll-tokens" => {
"\
usage: fleet bridge enroll-tokens

central (owner token). Every minted bootstrap token's state
(#14376): pending, used by the exchange, expired, or revoked, each
with the times that closed it. State only: no hash or value is ever
shown — the value existed once, in the mint answer."
}
"revoke-enroll" => {
"\
usage: fleet bridge revoke-enroll (--agent A | --token-hash H)

central (owner token). Revoke unused bootstrap tokens (#14376):
--agent takes back every still-pending token minted for that agent
(the console's form — its list shows no hash); --token-hash revokes
the one token stored under that SHA-256 hash. The hash is not a
credential — it cannot enroll anything — so it may travel where the
token value never may. By hash, a used or already-revoked token
answers {revoked: false}; by agent, the answer counts what was
taken back."
}
"mint-credential" => {
"\
usage: fleet bridge mint-credential --agent A [--scopes s1,s2] [--addr service:<name>]

central (owner token). Mint a per-agent credential directly
(#14368): scopes default to inbox + send; with --addr the credential
is a service credential bound to that address (its own name only),
and service mints default to send + read. The answer carries the
credential value once: it is stored hash-only and can never be shown
again."
}
"hello" => {
"\
Expand Down Expand Up @@ -539,6 +599,11 @@ const VALUE_FLAGS: &[&str] = &[
"--interval",
"--reply-to",
"--up-to",
"--scopes",
"--ttl",
"--notes-identity",
"--token-hash",
"--addr",
];

fn parse(args: &[String], switches: &[&str], values: &[&str]) -> R<Args> {
Expand Down Expand Up @@ -834,6 +899,87 @@ pub fn execute(ctx: &mut Ctx, args: &[String]) -> R<Value> {
),
}
}
"mint-enroll" => {
let a = parse(
rest,
&[],
&["--agent", "--kind", "--groups", "--notes-identity", "--ttl"],
)?;
a.no_positional("mint-enroll")?;
// Checked before any credential is resolved: a usage error
// is a usage error even on a seat with no tokens.
let agent = a
.last("--agent")
.ok_or_else(|| usage("mint-enroll needs --agent NAME"))?;
let mut payload = json!({ "agent": agent });
if let Some(kind) = a.last("--kind") {
payload["kind"] = json!(kind);
}
if let Some(groups) = a.last("--groups") {
payload["groups"] = json!(split_list(&groups));
}
if let Some(identity) = a.last("--notes-identity") {
payload["notes_identity"] = json!(identity);
}
if let Some(ttl) = a.uint("--ttl")? {
payload["ttl_seconds"] = json!(ttl);
}
let token = owner_token(ctx)?;
client_with(ctx, &token).call(
"POST",
&format!("/api/{token}/enroll-tokens"),
Some(&payload),
)
}
"enroll-tokens" => {
let a = parse(rest, &[], &[])?;
a.no_positional("enroll-tokens")?;
let token = owner_token(ctx)?;
client_with(ctx, &token).call("GET", &format!("/api/{token}/enroll-tokens"), None)
}
"revoke-enroll" => {
let a = parse(rest, &[], &["--agent", "--token-hash"])?;
a.no_positional("revoke-enroll")?;
// Exactly one form, checked before any credential is
// resolved, like every usage error here. The hash is not
// a credential (it cannot enroll anything), so it may
// ride in an argument where a token value never may.
let body = match (a.last("--agent"), a.last("--token-hash")) {
(Some(agent), None) => json!({ "agent": agent }),
(None, Some(hash)) => json!({ "token_hash": hash }),
_ => {
return Err(usage(
"revoke-enroll needs exactly one of --agent or --token-hash",
));
}
};
let token = owner_token(ctx)?;
client_with(ctx, &token).call(
"POST",
&format!("/api/{token}/enroll-tokens/revoke"),
Some(&body),
)
}
"mint-credential" => {
let a = parse(rest, &[], &["--agent", "--scopes", "--addr"])?;
a.no_positional("mint-credential")?;
let agent = a
.last("--agent")
.ok_or_else(|| usage("mint-credential needs --agent NAME"))?;
let mut payload = json!({ "agent": agent });
if let Some(scopes) = a.last("--scopes") {
payload["scopes"] = json!(split_list(&scopes));
}
if let Some(addr) = a.last("--addr") {
payload["addr"] = json!(addr);
}
let token = owner_token(ctx)?;
client_with(ctx, &token).call(
"POST",
&format!("/api/{token}/fleet/credentials/mint"),
Some(&payload),
)
}
"hello" => {
let a = parse(rest, &[], &["--agent", "--groups"])?;
a.no_positional("hello")?;
Expand Down
84 changes: 84 additions & 0 deletions pkgs/fleet/crates/fleet-bridge/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ fn every_verb_has_its_own_help() {
"directory",
"credentials",
"revoke",
"mint-enroll",
"enroll-tokens",
"revoke-enroll",
"mint-credential",
"hello",
"inbox",
"post",
Expand Down Expand Up @@ -218,6 +222,13 @@ fn usage_errors_exit_2_before_any_request() {
vec!["revoke"],
vec!["revoke", "--agent", "a", "--identity", "muse/a"],
vec!["revoke", "felix"],
vec!["mint-enroll"],
vec!["mint-enroll", "--agent", "a", "--ttl", "soon"],
vec!["mint-enroll", "--agent", "a", "extra"],
vec!["enroll-tokens", "extra"],
vec!["revoke-enroll"],
vec!["revoke-enroll", "--agent", "a", "--token-hash", "ab12"],
vec!["mint-credential"],
] {
let e = run_with(url, &s, &args, "").unwrap_err();
assert_eq!(e.exit_code(), EXIT_USAGE, "{args:?}: {e}");
Expand Down Expand Up @@ -489,6 +500,79 @@ fn pipeline_directory_credentials_and_revoke_shapes() {
);
}

#[test]
fn provisioning_verb_shapes() {
// #14376: the console's provisioning verbs, as owner-token calls on
// the bridge's own routes. The mint answers carry the values; the
// CLI passes them through untouched and keeps nothing.
let (url, seen) = fake_bridge(|_, _, _| (201, r#"{"ok":true}"#.into()));
let s = secrets(&[(DEFAULT_BRIDGE_OWNER_TOKEN_NAME, OWNER)]);
run_with(
&url,
&s,
&[
"mint-enroll",
"--agent",
"new-1",
"--kind",
"muse",
"--groups",
"muse,code-lanes",
"--notes-identity",
"muse/new-1",
"--ttl",
"3600",
],
"",
)
.unwrap();
run_with(&url, &s, &["enroll-tokens"], "").unwrap();
run_with(&url, &s, &["revoke-enroll", "--token-hash", "ab12"], "").unwrap();
run_with(&url, &s, &["revoke-enroll", "--agent", "new-1"], "").unwrap();
run_with(
&url,
&s,
&[
"mint-credential",
"--agent",
"pm-stack",
"--scopes",
"send,read",
"--addr",
"service:pm-stack",
],
"",
)
.unwrap();
let seen = seen.lock().unwrap();
assert_eq!(seen[0].method, "POST");
assert_eq!(seen[0].url, format!("/api/{OWNER}/enroll-tokens"));
assert_eq!(
seen[0].body.as_ref().unwrap(),
&json!({"agent": "new-1", "kind": "muse", "groups": ["muse", "code-lanes"], "notes_identity": "muse/new-1", "ttl_seconds": 3600})
);
assert_eq!(seen[1].method, "GET");
assert_eq!(seen[1].url, format!("/api/{OWNER}/enroll-tokens"));
assert_eq!(seen[2].url, format!("/api/{OWNER}/enroll-tokens/revoke"));
assert_eq!(
seen[2].body.as_ref().unwrap(),
&json!({"token_hash": "ab12"})
);
assert_eq!(seen[3].url, format!("/api/{OWNER}/enroll-tokens/revoke"));
assert_eq!(
seen[3].body.as_ref().unwrap(),
&json!({"agent": "new-1"})
);
assert_eq!(
seen[4].url,
format!("/api/{OWNER}/fleet/credentials/mint")
);
assert_eq!(
seen[4].body.as_ref().unwrap(),
&json!({"agent": "pm-stack", "scopes": ["send", "read"], "addr": "service:pm-stack"})
);
}

#[test]
fn http_errors_exit_4_and_never_show_the_token() {
let (url, _) = fake_bridge(|_, path, _| {
Expand Down
Loading