Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to access the port a GRPC server is listening on #3107

Merged
merged 2 commits into from
Feb 8, 2023
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
37 changes: 21 additions & 16 deletions util/grpc/src/grpcio_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ pub trait ConnectionUriGrpcioServer {
/// Set the channel args to our defaults.
#[must_use]
fn set_default_channel_args(self, env: Arc<Environment>) -> Self;
}

impl ConnectionUriGrpcioServer for ServerBuilder {
fn build_using_uri(self, uri: &impl ConnectionUri, logger: Logger) -> Result<Server> {
/// Get ServerCredentials from a URI
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Like the break out to a separate method for this behavior

fn server_credentials_from_uri(uri: &impl ConnectionUri, logger: &Logger) -> ServerCredentials {
if uri.use_tls() {
let tls_chain_path = uri
.tls_chain_path()
Expand All @@ -89,33 +88,39 @@ impl ConnectionUriGrpcioServer for ServerBuilder {
let reloader = ServerCertReloader::new(&tls_chain_path, &tls_key_path, logger.clone())
.expect("Failed creating ServerCertReloader");

ServerCredentials::with_fetcher(
Box::new(reloader),
CertificateRequestType::DontRequestClientCertificate,
)
} else {
ServerCredentials::insecure()
}
}
}

impl ConnectionUriGrpcioServer for ServerBuilder {
fn build_using_uri(self, uri: &impl ConnectionUri, logger: Logger) -> Result<Server> {
let server_creds = Self::server_credentials_from_uri(uri, &logger);

if uri.use_tls() {
log::debug!(
logger,
"Binding secure gRPC server to {}:{}",
uri.host(),
uri.port(),
);

let server_creds = ServerCredentials::with_fetcher(
Box::new(reloader),
CertificateRequestType::DontRequestClientCertificate,
);

let mut server = self.build()?;
server.add_listening_port(uri.addr(), server_creds)?;
Ok(server)
} else {
log::warn!(
logger,
"Binding insecure gRPC server to {}:{}",
uri.host(),
uri.port(),
);

let mut server = self.build()?;
server.add_listening_port(uri.addr(), ServerCredentials::insecure())?;
Ok(server)
}

let mut server = self.build()?;
server.add_listening_port(uri.addr(), server_creds)?;
Ok(server)
}

/// Set the channel args to our defaults.
Expand Down
10 changes: 10 additions & 0 deletions util/uri/src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ pub struct Uri<Scheme: UriScheme> {
_scheme: PhantomData<Scheme>,
}

impl<Scheme: UriScheme> Uri<Scheme> {
/// Change the port number of this URI.
pub fn set_port(&mut self, port: u16) {
self.url
.set_port(Some(port))
.expect("should never fail on valid url");
self.port = port;
}
}

impl<Scheme: UriScheme> ConnectionUri for Uri<Scheme> {
fn url(&self) -> &Url {
&self.url
Expand Down