Skip to content

Commit d014e40

Browse files
npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7wpfleger96
andcommitted
refactor(relay): replace AdminConfig bool pair with AdminAuth enum
AdminConfig previously modelled the auth mode as token: Option<AdminToken> plus insecure_no_auth: bool, leaving two invalid states representable and bridging the gap with an .expect() on the hot request path. Replace with an AdminAuth::Token(AdminToken) | AdminAuth::InsecureNoAuth enum so the type system rules out the invalid states and the .expect() is deleted. Config-parse behaviour is byte-identical: same error strings, same WARN messages, same fail-closed matrix, same env var names. Existing tests are updated mechanically (constructor call sites and one pattern-match assertion). Also adds a one-sentence note to docs/admin/README.md that token-mode issues one by-design 401 probe per fresh browser session so operators do not alert on it as an attack. Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 4fce52d commit d014e40

5 files changed

Lines changed: 45 additions & 35 deletions

File tree

‎crates/buzz-relay/src/api/admin/auth.rs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use axum::http::{header, HeaderMap};
22

33
use super::error::ApiError;
4-
use crate::config::AdminToken;
4+
use crate::config::{AdminAuth, AdminToken};
55
use crate::state::AppState;
66

77
pub(crate) fn is_admin_host(state: &AppState, headers: &HeaderMap) -> bool {
@@ -21,15 +21,12 @@ pub fn authorize(state: &AppState, headers: &HeaderMap) -> Result<(), ApiError>
2121
.as_ref()
2222
.ok_or_else(ApiError::not_found)?;
2323
// Credential first: an unauthenticated caller learns nothing about which
24-
// Host or Origin the deployment expects. In insecure_no_auth mode the
24+
// Host or Origin the deployment expects. In InsecureNoAuth mode the
2525
// bearer check is skipped — the operator has asserted that network-layer
2626
// controls substitute for it.
27-
if !config.insecure_no_auth {
28-
let token = config
29-
.token
30-
.as_ref()
31-
.expect("token is always Some when insecure_no_auth is false");
32-
authorize_bearer(token, headers)?;
27+
match &config.auth {
28+
AdminAuth::Token(token) => authorize_bearer(token, headers)?,
29+
AdminAuth::InsecureNoAuth => {}
3330
}
3431
if !is_admin_host(state, headers) {
3532
return Err(ApiError::forbidden());

‎crates/buzz-relay/src/api/admin/mod.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ mod tests {
344344
hex::decode_to_slice(TOKEN, &mut token).expect("test token is hex");
345345
config.admin = Some(crate::config::AdminConfig {
346346
host: "admin.example".to_string(),
347-
token: Some(crate::config::AdminToken::from_bytes(token)),
348-
insecure_no_auth: false,
347+
auth: crate::config::AdminAuth::Token(crate::config::AdminToken::from_bytes(token)),
349348
web_dir: None,
350349
});
351350
let pool = sqlx::PgPool::connect_lazy(&config.database_url).expect("lazy pg pool");
@@ -387,8 +386,7 @@ mod tests {
387386
config.redis_url = "redis://127.0.0.1:1".to_string();
388387
config.admin = Some(crate::config::AdminConfig {
389388
host: "admin.example".to_string(),
390-
token: None,
391-
insecure_no_auth: true,
389+
auth: crate::config::AdminAuth::InsecureNoAuth,
392390
web_dir: None,
393391
});
394392
let pool = sqlx::PgPool::connect_lazy(&config.database_url).expect("lazy pg pool");

‎crates/buzz-relay/src/config.rs‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,28 @@ pub enum ConfigError {
2424
InvalidValue(String),
2525
}
2626

27+
/// Authentication mode for the deployment-admin API.
28+
///
29+
/// Exactly one variant is active; the invalid states (`token: None` +
30+
/// `insecure_no_auth: false`, or `token: Some` + `insecure_no_auth: true`)
31+
/// are not representable.
32+
#[derive(Debug, Clone)]
33+
pub enum AdminAuth {
34+
/// Operator bearer credential required on every request.
35+
Token(AdminToken),
36+
/// Bearer authentication disabled. The operator has explicitly asserted
37+
/// that the admin API is protected at the network layer (reverse proxy,
38+
/// VPN, firewall). `Host`/`Origin` checks remain active as defense-in-depth.
39+
InsecureNoAuth,
40+
}
41+
2742
/// Deny-by-default read-only deployment-admin configuration.
2843
#[derive(Debug, Clone)]
2944
pub struct AdminConfig {
3045
/// Exact admin HTTP authority.
3146
pub host: String,
32-
/// Operator bearer credential required on every admin API request.
33-
/// `None` when the operator has set `BUZZ_ADMIN_INSECURE_NO_AUTH=true`.
34-
pub token: Option<AdminToken>,
35-
/// When `true`, bearer authentication is disabled. The operator has
36-
/// explicitly asserted that the admin API is protected at the network
37-
/// layer (reverse proxy, VPN, firewall). `Host`/`Origin` checks remain
38-
/// active as defense-in-depth.
39-
pub insecure_no_auth: bool,
47+
/// Authentication mode selected at startup.
48+
pub auth: AdminAuth,
4049
/// Optional admin SPA bundle directory.
4150
pub web_dir: Option<std::path::PathBuf>,
4251
}
@@ -984,15 +993,15 @@ impl Config {
984993
));
985994
}
986995

987-
let token = if insecure_no_auth {
996+
let auth = if insecure_no_auth {
988997
tracing::warn!(
989998
"BUZZ_ADMIN_INSECURE_NO_AUTH=true — the admin API is \
990999
unauthenticated; the operator has asserted that access is \
9911000
controlled at the network layer (reverse proxy, VPN, firewall)"
9921001
);
993-
None
1002+
AdminAuth::InsecureNoAuth
9941003
} else {
995-
Some(parse_admin_token()?)
1004+
AdminAuth::Token(parse_admin_token()?)
9961005
};
9971006

9981007
let web_dir = std::env::var("BUZZ_ADMIN_WEB_DIR")
@@ -1009,8 +1018,7 @@ impl Config {
10091018
}
10101019
Some(AdminConfig {
10111020
host,
1012-
token,
1013-
insecure_no_auth,
1021+
auth,
10141022
web_dir,
10151023
})
10161024
}
@@ -1244,13 +1252,14 @@ mod tests {
12441252
.expect("config with a valid admin token")
12451253
.admin
12461254
.expect("admin surface is configured");
1247-
let mut expected = [0u8; 32];
1248-
hex::decode_to_slice(VALID_ADMIN_TOKEN, &mut expected).expect("hex fixture");
12491255
assert_eq!(admin.host, "admin.example");
1250-
assert!(!admin.insecure_no_auth);
1251-
let token = admin.token.expect("token must be present in token mode");
1252-
assert!(token.matches(&expected));
1253-
assert!(!token.matches(&[0u8; 32]));
1256+
assert!(
1257+
matches!(admin.auth, crate::config::AdminAuth::Token(ref t) if {
1258+
let mut expected = [0u8; 32];
1259+
hex::decode_to_slice(VALID_ADMIN_TOKEN, &mut expected).expect("hex fixture");
1260+
t.matches(&expected) && !t.matches(&[0u8; 32])
1261+
})
1262+
);
12541263
}
12551264
}
12561265

@@ -1315,8 +1324,10 @@ mod tests {
13151324
.admin
13161325
.expect("admin surface is configured");
13171326
assert_eq!(admin.host, "admin.example");
1318-
assert!(admin.insecure_no_auth);
1319-
assert!(admin.token.is_none());
1327+
assert!(matches!(
1328+
admin.auth,
1329+
crate::config::AdminAuth::InsecureNoAuth
1330+
));
13201331
}
13211332

13221333
#[test]

‎crates/buzz-relay/src/router.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,7 @@ mod tests {
531531
config.web_dir = Some(web_dir.to_path_buf());
532532
config.admin = Some(crate::config::AdminConfig {
533533
host: "admin.example".to_string(),
534-
token: Some(crate::config::AdminToken::from_bytes([7u8; 32])),
535-
insecure_no_auth: false,
534+
auth: crate::config::AdminAuth::Token(crate::config::AdminToken::from_bytes([7u8; 32])),
536535
web_dir: Some(admin_dir.to_path_buf()),
537536
});
538537
let pool = sqlx::PgPool::connect_lazy(&config.database_url).expect("lazy pg pool");

‎docs/admin/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ re-prompted. Attachment bytes are fetched through the authenticated API and
6464
rendered from object URLs, because `<img src>` and `<a href>` cannot carry an
6565
`Authorization` header.
6666

67+
In token mode, each new browser session issues one unauthenticated probe that
68+
returns `401` by design before the token is entered. If you alert on admin-API
69+
`401`s, exclude these single-probe sequences (one `401` immediately followed by
70+
authenticated requests from the same session) to avoid false positives.
71+
6772
The shared token authenticates the deployment operator role, not a person. It
6873
carries no per-operator identity, attribution, or individual revocation —
6974
rotating it revokes access for everyone at once.

0 commit comments

Comments
 (0)