Skip to content

Commit

Permalink
✨ zb: Add Socket::auth_mechanism
Browse files Browse the repository at this point in the history
The socket impl will now inform us which authentication mechanism should
be used with it. The implementation can choose this based on the socket
type and the target platform.

This will allow us to switch to a single mechanism for each connection
in a following commit.
  • Loading branch information
zeenix committed Oct 4, 2024
1 parent b8da34d commit 467bea5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
6 changes: 5 additions & 1 deletion zbus/src/connection/socket/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;

use async_broadcast::{broadcast, Receiver, Sender};

use crate::{fdo::ConnectionCredentials, Message};
use crate::{fdo::ConnectionCredentials, AuthMechanism, Message};

/// An in-process channel-based socket.
///
Expand Down Expand Up @@ -72,6 +72,10 @@ impl super::ReadHalf for Reader {
async fn peer_credentials(&mut self) -> io::Result<ConnectionCredentials> {
self_credentials().await
}

fn auth_mechanism(&self) -> AuthMechanism {
AuthMechanism::Anonymous
}
}

/// The writer half of a [`Channel`].
Expand Down
13 changes: 12 additions & 1 deletion zbus/src/connection/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
header::{MAX_MESSAGE_SIZE, MIN_MESSAGE_SIZE},
PrimaryHeader,
},
padding_for_8_bytes, Message,
padding_for_8_bytes, AuthMechanism, Message,
};
#[cfg(unix)]
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
Expand Down Expand Up @@ -237,6 +237,13 @@ pub trait ReadHalf: std::fmt::Debug + Send + Sync + 'static {
async fn peer_credentials(&mut self) -> io::Result<ConnectionCredentials> {
Ok(ConnectionCredentials::default())
}

/// The authentication mechanism to use for this socket on the target OS.
///
/// Default is `AuthMechanism::External`.
fn auth_mechanism(&self) -> AuthMechanism {
AuthMechanism::External
}
}

/// The write half of a socket.
Expand Down Expand Up @@ -354,6 +361,10 @@ impl ReadHalf for Box<dyn ReadHalf> {
async fn peer_credentials(&mut self) -> io::Result<ConnectionCredentials> {
(**self).peer_credentials().await
}

fn auth_mechanism(&self) -> AuthMechanism {
(**self).auth_mechanism()
}
}

#[async_trait::async_trait]
Expand Down
10 changes: 10 additions & 0 deletions zbus/src/connection/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ impl ReadHalf for Arc<Async<TcpStream>> {
)
.await
}

#[cfg(not(windows))]
fn auth_mechanism(&self) -> crate::AuthMechanism {
crate::AuthMechanism::Anonymous
}
}

#[cfg(not(feature = "tokio"))]
Expand Down Expand Up @@ -120,6 +125,11 @@ impl ReadHalf for tokio::net::tcp::OwnedReadHalf {
)
.await
}

#[cfg(not(windows))]
fn auth_mechanism(&self) -> crate::AuthMechanism {
crate::AuthMechanism::Anonymous
}
}

#[cfg(feature = "tokio")]
Expand Down
8 changes: 8 additions & 0 deletions zbus/src/connection/socket/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl super::ReadHalf for std::sync::Arc<async_io::Async<vsock::VsockStream>> {
}
}
}

fn auth_mechanism(&self) -> crate::AuthMechanism {
crate::AuthMechanism::Anonymous
}
}

#[cfg(all(feature = "vsock", not(feature = "tokio")))]
Expand Down Expand Up @@ -86,6 +90,10 @@ impl super::ReadHalf for tokio_vsock::ReadHalf {
ret
})
}

fn auth_mechanism(&self) -> crate::AuthMechanism {
crate::AuthMechanism::Anonymous
}
}

#[cfg(feature = "tokio-vsock")]
Expand Down

0 comments on commit 467bea5

Please sign in to comment.