chore: Clippy fixes on wireguard-broker

This commit is contained in:
Karolin Varner
2024-08-03 15:02:49 +02:00
parent 54ac5eecdb
commit 648a94ead8
9 changed files with 28 additions and 34 deletions

View File

@@ -88,7 +88,7 @@ where
None => return Ok(None), None => return Ok(None),
}; };
let typ = res.get(0).ok_or(invalid_msg_poller())?; let typ = res.first().ok_or(invalid_msg_poller())?;
let typ = msgs::MsgType::try_from(*typ)?; let typ = msgs::MsgType::try_from(*typ)?;
let msgs::MsgType::SetPsk = typ; // Assert type let msgs::MsgType::SetPsk = typ; // Assert type
@@ -113,7 +113,7 @@ where
fn set_psk(&mut self, config: SerializedBrokerConfig) -> Result<(), Self::Error> { fn set_psk(&mut self, config: SerializedBrokerConfig) -> Result<(), Self::Error> {
let config: Result<NetworkBrokerConfig, NetworkBrokerConfigErr> = config.try_into(); let config: Result<NetworkBrokerConfig, NetworkBrokerConfigErr> = config.try_into();
let config = config.map_err(|e| BrokerClientSetPskError::BrokerError(e))?; let config = config.map_err(BrokerClientSetPskError::BrokerError)?;
use BrokerClientSetPskError::*; use BrokerClientSetPskError::*;
const BUF_SIZE: usize = REQUEST_MSG_BUFFER_SIZE; const BUF_SIZE: usize = REQUEST_MSG_BUFFER_SIZE;

View File

@@ -11,12 +11,12 @@ pub struct NetworkBrokerConfig<'a> {
pub psk: &'a Secret<WG_KEY_LEN>, pub psk: &'a Secret<WG_KEY_LEN>,
} }
impl<'a> Into<SerializedBrokerConfig<'a>> for NetworkBrokerConfig<'a> { impl<'a> From<NetworkBrokerConfig<'a>> for SerializedBrokerConfig<'a> {
fn into(self) -> SerializedBrokerConfig<'a> { fn from(src: NetworkBrokerConfig<'a>) -> SerializedBrokerConfig<'a> {
SerializedBrokerConfig { Self {
interface: self.iface.as_bytes(), interface: src.iface.as_bytes(),
peer_id: self.peer_id, peer_id: src.peer_id,
psk: self.psk, psk: src.psk,
additional_params: &[], additional_params: &[],
} }
} }

View File

@@ -42,7 +42,7 @@ impl SetPskRequest {
self.iface_size = iface.len() as u8; self.iface_size = iface.len() as u8;
self.iface_buf = [0; 255]; self.iface_buf = [0; 255];
(&mut self.iface_buf[..iface.len()]).copy_from_slice(iface); self.iface_buf[..iface.len()].copy_from_slice(iface);
Some(()) Some(())
} }

View File

@@ -48,7 +48,7 @@ where
) -> Result<usize, BrokerServerError> { ) -> Result<usize, BrokerServerError> {
use BrokerServerError::*; use BrokerServerError::*;
let typ = req.get(0).ok_or(InvalidMessage)?; let typ = req.first().ok_or(InvalidMessage)?;
let typ = msgs::MsgType::try_from(*typ)?; let typ = msgs::MsgType::try_from(*typ)?;
let msgs::MsgType::SetPsk = typ; // Assert type let msgs::MsgType::SetPsk = typ; // Assert type

View File

@@ -59,7 +59,7 @@ pub mod linux {
// Write the response // Write the response
stdout.write_all(&(res.len() as u64).to_le_bytes())?; stdout.write_all(&(res.len() as u64).to_le_bytes())?;
stdout.write_all(&res)?; stdout.write_all(res)?;
stdout.flush()?; stdout.flush()?;
} }
} }

View File

@@ -121,7 +121,7 @@ async fn direct_broker_process(
// Read the message itself // Read the message itself
let mut res_buf = request; // Avoid allocating memory if we don't have to let mut res_buf = request; // Avoid allocating memory if we don't have to
res_buf.resize(len as usize, 0); res_buf.resize(len, 0);
stdout.read_exact(&mut res_buf[..len]).await?; stdout.read_exact(&mut res_buf[..len]).await?;
// Return to the unix socket connection worker // Return to the unix socket connection worker
@@ -163,7 +163,7 @@ async fn on_accept(queue: mpsc::Sender<BrokerRequest>, mut stream: UnixStream) -
); );
// Read the message itself // Read the message itself
req_buf.resize(len as usize, 0); req_buf.resize(len, 0);
stream.read_exact(&mut req_buf[..len]).await?; stream.read_exact(&mut req_buf[..len]).await?;
// Handle the message // Handle the message

View File

@@ -52,23 +52,17 @@ impl MioBrokerClient {
// This sucks // This sucks
match self.inner.poll_response() { match self.inner.poll_response() {
Ok(res) => { Ok(res) => Ok(res),
return Ok(res); Err(BrokerClientPollResponseError::IoError(e)) => Err(e),
} Err(BrokerClientPollResponseError::InvalidMessage) => bail!("Invalid message"),
Err(BrokerClientPollResponseError::IoError(e)) => { }
return Err(e);
}
Err(BrokerClientPollResponseError::InvalidMessage) => {
bail!("Invalid message");
}
};
} }
} }
impl WireGuardBroker for MioBrokerClient { impl WireGuardBroker for MioBrokerClient {
type Error = anyhow::Error; type Error = anyhow::Error;
fn set_psk<'a>(&mut self, config: SerializedBrokerConfig<'a>) -> anyhow::Result<()> { fn set_psk(&mut self, config: SerializedBrokerConfig<'_>) -> anyhow::Result<()> {
use BrokerClientSetPskError::*; use BrokerClientSetPskError::*;
let e = self.inner.set_psk(config); let e = self.inner.set_psk(config);
match e { match e {
@@ -115,7 +109,7 @@ impl BrokerClientIo for MioBrokerClientIo {
fn send_msg(&mut self, buf: &[u8]) -> Result<(), Self::SendError> { fn send_msg(&mut self, buf: &[u8]) -> Result<(), Self::SendError> {
self.flush()?; self.flush()?;
self.send_or_buffer(&(buf.len() as u64).to_le_bytes())?; self.send_or_buffer(&(buf.len() as u64).to_le_bytes())?;
self.send_or_buffer(&buf)?; self.send_or_buffer(buf)?;
self.flush()?; self.flush()?;
Ok(()) Ok(())
@@ -192,7 +186,7 @@ impl MioBrokerClientIo {
self.send_buf.drain(..written); self.send_buf.drain(..written);
(&self.socket).try_io(|| (&self.socket).flush())?; self.socket.try_io(|| (&self.socket).flush())?;
res res
} }
@@ -204,7 +198,7 @@ impl MioBrokerClientIo {
off += raw_send(&self.socket, buf)?; off += raw_send(&self.socket, buf)?;
} }
self.send_buf.extend((&buf[off..]).iter()); self.send_buf.extend(buf[off..].iter());
Ok(()) Ok(())
} }
@@ -231,7 +225,7 @@ fn raw_send(mut socket: &mio::net::UnixStream, data: &[u8]) -> anyhow::Result<us
} }
})?; })?;
return Ok(off); Ok(off)
} }
fn raw_recv(mut socket: &mio::net::UnixStream, out: &mut [u8]) -> anyhow::Result<usize> { fn raw_recv(mut socket: &mio::net::UnixStream, out: &mut [u8]) -> anyhow::Result<usize> {
@@ -255,5 +249,5 @@ fn raw_recv(mut socket: &mio::net::UnixStream, out: &mut [u8]) -> anyhow::Result
} }
})?; })?;
return Ok(off); Ok(off)
} }

View File

@@ -87,21 +87,20 @@ impl WireGuardBroker for NetlinkWireGuardBroker {
.sock .sock
.get_device(wg::DeviceInterface::from_name(config.iface))?; .get_device(wg::DeviceInterface::from_name(config.iface))?;
if state if !state
.peers .peers
.iter() .iter()
.find(|p| &p.public_key == &config.peer_id.value) .any(|p| p.public_key == config.peer_id.value)
.is_none()
{ {
return Err(SetPskError::NoSuchPeer); return Err(SetPskError::NoSuchPeer);
} }
// Peer update description // Peer update description
let mut set_peer = wireguard_uapi::set::Peer::from_public_key(&config.peer_id); let mut set_peer = wireguard_uapi::set::Peer::from_public_key(config.peer_id);
set_peer set_peer
.flags .flags
.push(wireguard_uapi::linux::set::WgPeerF::UpdateOnly); .push(wireguard_uapi::linux::set::WgPeerF::UpdateOnly);
set_peer.preshared_key = Some(&config.psk.secret()); set_peer.preshared_key = Some(config.psk.secret());
// Device update description // Device update description
let mut set_dev = wireguard_uapi::set::Device::from_ifname(config.iface); let mut set_dev = wireguard_uapi::set::Device::from_ifname(config.iface);

View File

@@ -36,6 +36,7 @@ mod integration_tests {
impl WireGuardBroker for MockServerBroker { impl WireGuardBroker for MockServerBroker {
type Error = SetPskError; type Error = SetPskError;
#[allow(clippy::clone_on_copy)]
fn set_psk(&mut self, config: SerializedBrokerConfig) -> Result<(), Self::Error> { fn set_psk(&mut self, config: SerializedBrokerConfig) -> Result<(), Self::Error> {
loop { loop {
let mut lock = self.inner.try_lock(); let mut lock = self.inner.try_lock();