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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ autotests = false

[dependencies]
rt = { path = "../rt" }
sys = { path = "../sys" }

[[test]]
name = "admin"
Expand Down
78 changes: 75 additions & 3 deletions crates/admin/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const POOL_LIST: u8 = 7;
const QUEUE_LIST: u8 = 8;
const QUEUE_RETRY: u8 = 9;
const QUEUE_REBIND: u8 = 10;
const KEY_CREATE: u8 = 11;
const KEY_REVOKE: u8 = 12;
const KEY_LIST: u8 = 13;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
Expand Down Expand Up @@ -60,9 +63,34 @@ pub enum Request {
TenantResume(u32),
PoolSet(PoolSpec),
PoolList(u32),
QueueList { tenant: u32, from: u32 },
QueueRetry { tenant: u32, queue: u32 },
QueueRebind { tenant: u32, queue: u32, pool: u32 },
QueueList {
tenant: u32,
from: u32,
},
QueueRetry {
tenant: u32,
queue: u32,
},
QueueRebind {
tenant: u32,
queue: u32,
pool: u32,
},
KeyCreate {
tenant: u32,
smtp: bool,
api: bool,
hooks: bool,
pool: u32,
},
KeyRevoke {
tenant: u32,
id: u32,
},
KeyList {
tenant: u32,
from: u32,
},
}

impl Request {
Expand Down Expand Up @@ -136,6 +164,28 @@ impl Request {
body.extend_from_slice(&queue.to_be_bytes());
body.extend_from_slice(&pool.to_be_bytes());
}
Self::KeyCreate {
tenant,
smtp,
api,
hooks,
pool,
} => {
body.push(KEY_CREATE);
body.extend_from_slice(&tenant.to_be_bytes());
body.push(u8::from(*smtp) | u8::from(*api) << 1 | u8::from(*hooks) << 2);
body.extend_from_slice(&pool.to_be_bytes());
}
Self::KeyRevoke { tenant, id } => {
body.push(KEY_REVOKE);
body.extend_from_slice(&tenant.to_be_bytes());
body.extend_from_slice(&id.to_be_bytes());
}
Self::KeyList { tenant, from } => {
body.push(KEY_LIST);
body.extend_from_slice(&tenant.to_be_bytes());
body.extend_from_slice(&from.to_be_bytes());
}
}
framed(&body)
}
Expand Down Expand Up @@ -190,6 +240,28 @@ impl Request {
queue: fields.u32()?,
pool: fields.u32()?,
},
KEY_CREATE => {
let tenant = fields.u32()?;
let scopes = fields.u8()?;
if scopes & !7 != 0 {
return Err(Error::Body);
}
Self::KeyCreate {
tenant,
smtp: scopes & 1 != 0,
api: scopes & 2 != 0,
hooks: scopes & 4 != 0,
pool: fields.u32()?,
}
}
KEY_REVOKE => Self::KeyRevoke {
tenant: fields.u32()?,
id: fields.u32()?,
},
KEY_LIST => Self::KeyList {
tenant: fields.u32()?,
from: fields.u32()?,
},
_ => return Err(Error::Command),
};
if !fields.0.is_empty() {
Expand Down
15 changes: 13 additions & 2 deletions crates/admin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,19 @@ async fn session<H: Handler>(stream: &UnixStream, handler: &H, state: &Shared) {
Reply::new(Code::Refused, error.to_string())
}
};
let written = timeout(IDLE, stream.write_all(Buf::from_slice(&reply.encode()))).await;
if !matches!(written, Ok((Ok(()), _))) {
let mut encoded = reply.encode();
let written = timeout(IDLE, stream.write_all(Buf::from_slice(&encoded))).await;
let sent = match written {
Ok((result, mut buf)) => {
sys::mem::wipe(&mut buf);
result.is_ok()
}
Err(_) => false,
};
sys::mem::wipe(&mut encoded);
let mut text = reply.text.into_bytes();
sys::mem::wipe(&mut text);
if !sent {
return;
}
}
Expand Down
40 changes: 40 additions & 0 deletions crates/admin/tests/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ fn every_request_round_trips() {
queue: 9,
pool: 2,
},
Request::KeyCreate {
tenant: 5,
smtp: true,
api: false,
hooks: true,
pool: 2,
},
Request::KeyRevoke { tenant: 5, id: 9 },
Request::KeyList { tenant: 5, from: 9 },
Request::TenantSuspend(u32::MAX),
Request::TenantResume(7),
Request::TenantCreate(TenantSpec {
Expand Down Expand Up @@ -163,5 +172,36 @@ fn short_long_or_odd_fields_are_refused() {
assert_eq!(Request::decode(&odd), Err(Error::Body));
assert_eq!(Request::decode(&[VERSION, 4, 0, 0, 0]), Err(Error::Body));
assert_eq!(Request::decode(&[VERSION, 8]), Err(Error::Body));
let mut unknown_scopes = body(
&Request::KeyCreate {
tenant: 5,
smtp: true,
api: true,
hooks: true,
pool: 2,
}
.encode(),
);
unknown_scopes[6] = 8;
assert_eq!(Request::decode(&unknown_scopes), Err(Error::Body));
for request in [
Request::KeyCreate {
tenant: 5,
smtp: true,
api: true,
hooks: false,
pool: 2,
},
Request::KeyRevoke { tenant: 5, id: 9 },
Request::KeyList { tenant: 5, from: 9 },
] {
let short = body(&request.encode());
for cut in 2..short.len() {
assert_eq!(Request::decode(&short[..cut]), Err(Error::Body), "{cut}");
}
let mut long = short;
long.push(0);
assert_eq!(Request::decode(&long), Err(Error::Body));
}
assert_eq!(Request::decode(&[VERSION, 255]), Err(Error::Command));
}
85 changes: 84 additions & 1 deletion crates/bunzzle/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ pub const USAGE: &str = "usage: bunzzle admin <config> status
bunzzle admin <config> pool set <id> <ip>=<weight>[,<ip>=<weight>...]
bunzzle admin <config> queue list <tenant> [from <id>]
bunzzle admin <config> queue retry <tenant> <queue>
bunzzle admin <config> queue rebind <tenant> <queue> <pool>";
bunzzle admin <config> queue rebind <tenant> <queue> <pool>
bunzzle admin <config> key create <tenant> <scopes> [pool <id>]
bunzzle admin <config> key revoke <tenant> <id>
bunzzle admin <config> key list <tenant> [from <id>]";

const MAX_ITEMS: usize = 255;

Expand Down Expand Up @@ -49,6 +52,22 @@ pub fn parse(args: &[String]) -> Option<(&str, Request)> {
queue: queue.parse().ok()?,
pool: pool.parse().ok()?,
},
["key", "create", tenant, scopes] => key_create(tenant, scopes, 0)?,
["key", "create", tenant, scopes, "pool", pool] => {
key_create(tenant, scopes, pool.parse().ok()?)?
}
["key", "revoke", tenant, id] => Request::KeyRevoke {
tenant: tenant.parse().ok()?,
id: id.parse().ok()?,
},
["key", "list", tenant] => Request::KeyList {
tenant: tenant.parse().ok()?,
from: 0,
},
["key", "list", tenant, "from", from] => Request::KeyList {
tenant: tenant.parse().ok()?,
from: from.parse().ok()?,
},
["tenant", "suspend", id] => Request::TenantSuspend(id.parse().ok()?),
["tenant", "resume", id] => Request::TenantResume(id.parse().ok()?),
["tenant", "create", id, pools, options @ ..] => {
Expand Down Expand Up @@ -90,6 +109,31 @@ fn tenant(id: &str, pools: &str, options: &[&str]) -> Option<TenantSpec> {
Some(spec)
}

fn key_create(tenant: &str, scopes: &str, pool: u32) -> Option<Request> {
let mut smtp = false;
let mut api = false;
let mut hooks = false;
for scope in scopes.split(',') {
let seen = match scope {
"smtp" => &mut smtp,
"api" => &mut api,
"hooks" => &mut hooks,
_ => return None,
};
if *seen {
return None;
}
*seen = true;
}
Some(Request::KeyCreate {
tenant: tenant.parse().ok()?,
smtp,
api,
hooks,
pool,
})
}

fn list<T>(text: &str, item: impl Fn(&str) -> Option<T>) -> Option<Vec<T>> {
let items: Vec<T> = text.split(',').map(item).collect::<Option<_>>()?;
(items.len() <= MAX_ITEMS).then_some(items)
Expand Down Expand Up @@ -235,6 +279,38 @@ mod tests {
pool: 2
})
);
assert_eq!(
parsed(&["key", "create", "5", "hooks,smtp,api"]),
Some(Request::KeyCreate {
tenant: 5,
smtp: true,
api: true,
hooks: true,
pool: 0,
})
);
assert_eq!(
parsed(&["key", "create", "5", "api", "pool", "2"]),
Some(Request::KeyCreate {
tenant: 5,
smtp: false,
api: true,
hooks: false,
pool: 2,
})
);
assert_eq!(
parsed(&["key", "revoke", "5", "9"]),
Some(Request::KeyRevoke { tenant: 5, id: 9 })
);
assert_eq!(
parsed(&["key", "list", "5"]),
Some(Request::KeyList { tenant: 5, from: 0 })
);
assert_eq!(
parsed(&["key", "list", "5", "from", "9"]),
Some(Request::KeyList { tenant: 5, from: 9 })
);
assert_eq!(parsed(&["queue", "list"]), None);
assert_eq!(parsed(&["queue", "retry", "5"]), None);
assert_eq!(
Expand Down Expand Up @@ -287,6 +363,13 @@ mod tests {
&["pool", "set", "1", "192.0.2.1"],
&["pool", "set", "1", "192.0.2.1=70000"],
&["pool", "set", "1", "host=1"],
&["key", "create", "5", ""],
&["key", "create", "5", "smtp,smtp"],
&["key", "create", "5", "smtp,unknown"],
&["key", "create", "5", "smtp", "pool"],
&["key", "create", "5", "smtp", "pool=2"],
&["key", "revoke", "5"],
&["key", "list", "5", "from"],
] {
assert_eq!(parsed(bad), None, "{bad:?}");
}
Expand Down
Loading
Loading