geyser: add TLS config to gRPC server (#183)

This commit is contained in:
Liam Vovk 2023-09-18 21:35:13 -07:00 committed by Kirill Fomichev
parent a3906e0ca2
commit d0bf7d0a81
No known key found for this signature in database
GPG Key ID: 6AA0144D5E0C0C0A
4 changed files with 30 additions and 3 deletions

View File

@ -12,6 +12,8 @@ The minor version will be incremented upon a breaking change and the patch versi
### Features ### Features
- geyser: add optional TLS to gRPC server config ([#183](https://github.com/rpcpool/yellowstone-grpc/pull/183)).
### Fixes ### Fixes
### Breaking ### Breaking

View File

@ -62,6 +62,8 @@ impl ConfigLog {
pub struct ConfigGrpc { pub struct ConfigGrpc {
/// Address of Grpc service. /// Address of Grpc service.
pub address: SocketAddr, pub address: SocketAddr,
/// TLS config
pub tls_config: Option<ConfigGrpcServerTls>,
/// Capacity of the channel per connection /// Capacity of the channel per connection
#[serde( #[serde(
default = "ConfigGrpc::channel_capacity_default", default = "ConfigGrpc::channel_capacity_default",
@ -92,6 +94,13 @@ impl ConfigGrpc {
} }
} }
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConfigGrpcServerTls {
pub cert_path: String,
pub key_path: String,
}
#[derive(Debug, Default, Clone, Deserialize)] #[derive(Debug, Default, Clone, Deserialize)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct ConfigGrpcFilters { pub struct ConfigGrpcFilters {

View File

@ -37,13 +37,17 @@ use {
}, },
}, },
tokio::{ tokio::{
fs,
sync::{broadcast, mpsc, Notify, RwLock, Semaphore}, sync::{broadcast, mpsc, Notify, RwLock, Semaphore},
time::{sleep, Duration, Instant}, time::{sleep, Duration, Instant},
}, },
tokio_stream::wrappers::ReceiverStream, tokio_stream::wrappers::ReceiverStream,
tonic::{ tonic::{
codec::CompressionEncoding, codec::CompressionEncoding,
transport::server::{Server, TcpIncoming}, transport::{
server::{Server, TcpIncoming},
Identity, ServerTlsConfig,
},
Request, Response, Result as TonicResult, Status, Streaming, Request, Response, Result as TonicResult, Status, Streaming,
}, },
tonic_health::server::health_reporter, tonic_health::server::health_reporter,
@ -679,7 +683,7 @@ pub struct GrpcService {
} }
impl GrpcService { impl GrpcService {
pub fn create( pub async fn create(
config: ConfigGrpc, config: ConfigGrpc,
block_fail_action: ConfigBlockFailAction, block_fail_action: ConfigBlockFailAction,
) -> Result< ) -> Result<
@ -705,6 +709,17 @@ impl GrpcService {
// Messages to clients combined by commitment // Messages to clients combined by commitment
let (broadcast_tx, _) = broadcast::channel(config.channel_capacity); let (broadcast_tx, _) = broadcast::channel(config.channel_capacity);
// gRPC server builder with optional TLS
let mut server_builder = Server::builder();
if let Some(tls_config) = &config.tls_config {
let (cert, key) = tokio::try_join!(
fs::read(&tls_config.cert_path),
fs::read(&tls_config.key_path)
)?;
server_builder = server_builder
.tls_config(ServerTlsConfig::new().identity(Identity::from_pem(cert, key)))?;
}
// Create Server // Create Server
let service = GeyserServer::new(Self { let service = GeyserServer::new(Self {
config, config,
@ -732,7 +747,7 @@ impl GrpcService {
let (mut health_reporter, health_service) = health_reporter(); let (mut health_reporter, health_service) = health_reporter();
health_reporter.set_serving::<GeyserServer<Self>>().await; health_reporter.set_serving::<GeyserServer<Self>>().await;
Server::builder() server_builder
.http2_keepalive_interval(Some(Duration::from_secs(5))) .http2_keepalive_interval(Some(Duration::from_secs(5)))
.add_service(health_service) .add_service(health_service)
.add_service(service) .add_service(service)

View File

@ -81,6 +81,7 @@ impl GeyserPlugin for Plugin {
let (grpc_channel, grpc_shutdown, prometheus) = runtime.block_on(async move { let (grpc_channel, grpc_shutdown, prometheus) = runtime.block_on(async move {
let (grpc_channel, grpc_shutdown) = let (grpc_channel, grpc_shutdown) =
GrpcService::create(config.grpc, config.block_fail_action) GrpcService::create(config.grpc, config.block_fail_action)
.await
.map_err(|error| GeyserPluginError::Custom(error))?; .map_err(|error| GeyserPluginError::Custom(error))?;
let prometheus = PrometheusService::new(config.prometheus) let prometheus = PrometheusService::new(config.prometheus)
.map_err(|error| GeyserPluginError::Custom(Box::new(error)))?; .map_err(|error| GeyserPluginError::Custom(Box::new(error)))?;