Skip to content

Commit

Permalink
add configuration builders
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-maron committed Feb 14, 2024
1 parent aed7e76 commit fb82709
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 78 deletions.
75 changes: 75 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ tracing-subscriber = "0.3.18"
clap = { version = "4.4.18", features = ["derive"] }
async-trait = "0.1.77"
prometheus = { version = "0.13.3" }
lazy_static = "1.4.0"
lazy_static = "1.4.0"
derive_builder = "0.13.1"
1 change: 1 addition & 0 deletions broker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ async-trait.workspace = true
paste = "1.0.14"
prometheus = { workspace = true }
lazy_static = { workspace = true }
derive_builder.workspace = true
29 changes: 16 additions & 13 deletions broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{
};

mod metrics;
use derive_builder::Builder;
use proto::{
crypto::signature::{KeyPair, SignatureScheme},
metrics as proto_metrics,
Expand All @@ -37,7 +38,7 @@ use tokio::{select, spawn, sync::RwLock};
use crate::metrics::RUNNING_SINCE;

/// The broker's configuration. We need this when we create a new one.
/// TODO: clean up these generics. could be a generic type that implements both
#[derive(Builder)]
pub struct Config<BrokerScheme: SignatureScheme> {
/// The user (public) advertise address: what the marshals send to users upon authentication.
/// Users connect to us with this address.
Expand All @@ -46,12 +47,15 @@ pub struct Config<BrokerScheme: SignatureScheme> {
pub public_bind_address: String,

/// Whether or not we want to serve metrics
#[builder(default = "true")]
pub metrics_enabled: bool,

/// The port we want to serve metrics on
#[builder(default = "9090")]
pub metrics_port: u16,

/// The IP/interface we want to serve the metrics on
#[builder(default = "String::from(\"127.0.0.1\")")]
pub metrics_ip: String,

/// The broker (private) advertise address: what other brokers use to connect to us.
Expand All @@ -65,9 +69,12 @@ pub struct Config<BrokerScheme: SignatureScheme> {
pub keypair: KeyPair<BrokerScheme>,

/// An optional TLS cert path
pub maybe_tls_cert_path: Option<String>,
#[builder(default)]
pub tls_cert_path: Option<String>,

/// An optional TLS key path
pub maybe_tls_key_path: Option<String>,
#[builder(default)]
pub tls_key_path: Option<String>,
}

/// The broker `Inner` that we use to share common data between broker tasks.
Expand Down Expand Up @@ -138,8 +145,8 @@ impl<BrokerScheme: SignatureScheme, UserScheme: SignatureScheme> Broker<BrokerSc
keypair,

discovery_endpoint,
maybe_tls_cert_path,
maybe_tls_key_path,
tls_cert_path,
tls_key_path,
} = config;

// Create a unique broker identifier
Expand All @@ -160,8 +167,8 @@ impl<BrokerScheme: SignatureScheme, UserScheme: SignatureScheme> Broker<BrokerSc
let user_listener = bail!(
<UserProtocol as Protocol>::bind(
public_bind_address,
maybe_tls_cert_path.clone(),
maybe_tls_key_path.clone(),
tls_cert_path.clone(),
tls_key_path.clone(),
)
.await,
Connection,
Expand All @@ -174,12 +181,8 @@ impl<BrokerScheme: SignatureScheme, UserScheme: SignatureScheme> Broker<BrokerSc
// Create the broker (private) listener
let private_bind_address = parse_socket_address!(private_bind_address);
let broker_listener = bail!(
<BrokerProtocol as Protocol>::bind(
private_bind_address,
maybe_tls_cert_path,
maybe_tls_key_path,
)
.await,
<BrokerProtocol as Protocol>::bind(private_bind_address, tls_cert_path, tls_key_path,)
.await,
Connection,
format!(
"failed to bind to public (user) bind address {}",
Expand Down
43 changes: 17 additions & 26 deletions broker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The following is the main `Broker` binary, which just instantiates and runs
//! a `Broker` object.
use broker::{Broker, Config};
use broker::{Broker, Config, ConfigBuilder};
use clap::Parser;
use jf_primitives::signatures::{
bls_over_bn254::BLSOverBN254CurveSignatureScheme as BLS, SignatureScheme,
Expand Down Expand Up @@ -56,35 +56,26 @@ async fn main() -> Result<()> {

// Get our local IP address
let private_ip_address = bail!(local_ip(), Connection, "failed to get local IP address");
let private_address = format!("{}:{}", private_ip_address, args.private_bind_port);

// Create deterministic keys for brokers (for now, obviously)
let (private_key, public_key) = BLS::key_gen(&(), &mut DeterministicRng(0)).unwrap();

let broker_config = Config {
// Public addresses: explicitly defined advertise address, bind address is on every interface
// but with the specified port.
public_advertise_address: args.public_advertise_address,
public_bind_address: format!("0.0.0.0:{}", args.public_bind_port),

metrics_enabled: args.metrics_enabled,
metrics_port: args.metrics_port,
metrics_ip: args.metrics_ip,

// Private addresses: bind to the local interface with the specified port
private_advertise_address: format!("{}:{}", private_ip_address, args.private_bind_port),
private_bind_address: format!("{}:{}", private_ip_address, args.private_bind_port),

discovery_endpoint: args.discovery_endpoint,

keypair: KeyPair {
public_key,
private_key,
},

// TODO: clap this
maybe_tls_cert_path: None,
maybe_tls_key_path: None,
};
let broker_config: Config<BLS> = bail!(
ConfigBuilder::default()
.public_advertise_address(args.public_advertise_address)
.public_bind_address(format!("0.0.0.0:{}", args.public_bind_port))
.private_advertise_address(private_address.clone())
.private_bind_address(private_address)
.discovery_endpoint(args.discovery_endpoint)
.keypair(KeyPair {
public_key,
private_key
})
.build(),
Parse,
"failed to build broker configuration"
);

// Create new `Broker`
// Uses TCP from broker connections and Quic for user connections.
Expand Down
3 changes: 2 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ tokio.workspace = true
tracing-subscriber.workspace = true
rand.workspace = true
tracing.workspace = true
clap.workspace = true
clap.workspace = true
derive_builder.workspace = true
1 change: 1 addition & 0 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use retry::Retry;
pub struct Client<Scheme: SignatureScheme, ProtocolType: Protocol>(Retry<Scheme, ProtocolType>);

pub type Config<Scheme, ProtocolType> = retry::Config<Scheme, ProtocolType>;
pub type ConfigBuilder<Scheme, ProtocolType> = retry::ConfigBuilder<Scheme, ProtocolType>;

impl<Scheme: SignatureScheme, ProtocolType: Protocol> Client<Scheme, ProtocolType> {
/// Creates a new `Retry` from a configuration.
Expand Down
36 changes: 22 additions & 14 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
//! We spawn two clients. In a single-broker run, this lets them connect
//! cross-broker.
use std::{marker::PhantomData, time::Duration};
use std::time::Duration;

use clap::Parser;
use client::{Client, Config, KeyPair};
use proto::{connection::protocols::quic::Quic, crypto::rng::DeterministicRng, error::Result};
use client::{Client, ConfigBuilder, KeyPair};
use proto::{
bail,
connection::protocols::quic::Quic,
crypto::rng::DeterministicRng,
error::{Error, Result},
};

use jf_primitives::signatures::{
bls_over_bn254::BLSOverBN254CurveSignatureScheme as BLS, SignatureScheme,
Expand All @@ -31,19 +36,22 @@ async fn main() -> Result<()> {
tracing_subscriber::fmt::init();

// Generate two random keypairs, one for each client

let (private_key, public_key) = BLS::key_gen(&(), &mut DeterministicRng(args.id)).unwrap();

let client = Client::<BLS, Quic>::new(Config {
endpoint: "127.0.0.1:8082".to_string(),
keypair: KeyPair {
public_key,
private_key,
},
subscribed_topics: vec![],
pd: PhantomData,
})
.await?;
// Build the config, the endpoint being where we expect the marshal to be
let config = bail!(
ConfigBuilder::default()
.endpoint("127.0.0.1:8082".to_string())
.keypair(KeyPair {
public_key,
private_key,
})
.build(),
Parse,
"failed to build client config"
);

let client = Client::<BLS, Quic>::new(config).await?;

// We want the first node to send to the second
if args.id != 0 {
Expand Down
4 changes: 4 additions & 0 deletions client/src/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use std::{collections::HashSet, marker::PhantomData, sync::Arc, time::Duration};

use derive_builder::Builder;
use proto::{
connection::{
auth::user::UserAuth,
Expand Down Expand Up @@ -65,6 +66,7 @@ pub struct Inner<Scheme: SignatureScheme, ProtocolType: Protocol> {
}

/// The configuration needed to construct a `Retry` connection.
#[derive(Builder)]
pub struct Config<Scheme: SignatureScheme, ProtocolType: Protocol> {
/// This is the remote address that we authenticate to. It can either be a broker
/// or a marshal.
Expand All @@ -76,9 +78,11 @@ pub struct Config<Scheme: SignatureScheme, ProtocolType: Protocol> {

/// The topics we're currently subscribed to. We need this here so we can send our subscriptions
/// when we connect to a new server.
#[builder(default = "Vec::new()")]
pub subscribed_topics: Vec<Topic>,

/// The phantom data we need to be able to make use of these types
#[builder(default)]
pub pd: PhantomData<ProtocolType>,
}

Expand Down
1 change: 1 addition & 0 deletions marshal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
clap.workspace = true
derive_builder.workspace = true
Loading

0 comments on commit fb82709

Please sign in to comment.