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

[feat]: support certificate authentication and add tests #652

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
105 changes: 104 additions & 1 deletion Cargo.lock

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

11 changes: 9 additions & 2 deletions crates/utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl ClusterConfig {
peer_advertise_urls: Vec<String>,
client_listen_urls: Vec<String>,
client_advertise_urls: Vec<String>,
members: HashMap<String, Vec<String>>,
peers: HashMap<String, Vec<String>>,
is_leader: bool,
curp: CurpConfig,
client_config: ClientConfig,
Expand All @@ -196,7 +196,7 @@ impl ClusterConfig {
peer_advertise_urls,
client_listen_urls,
client_advertise_urls,
peers: members,
peers,
is_leader,
curp_config: curp,
client_config,
Expand Down Expand Up @@ -986,6 +986,13 @@ impl TlsConfig {
client_key_path,
}
}

/// Whether the server tls is enabled
#[must_use]
#[inline]
pub fn server_tls_enabled(&self) -> bool {
self.server_cert_path.is_some() && self.server_key_path.is_some()
themanforfree marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Xline metrics push protocol
Expand Down
33 changes: 9 additions & 24 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ pub mod tokio_lock;
/// utils for pass span context
pub mod tracing;

use ::tracing::debug;
pub use parser::*;

/// Get current timestamp in seconds
Expand All @@ -210,30 +211,6 @@ pub fn timestamp() -> u64 {
.as_secs()
}

/// Certs for tests
pub mod certs {
/// Server certificate
#[inline]
#[must_use]
pub fn server_cert() -> &'static [u8] {
include_bytes!("../../../fixtures/server.crt")
}

/// Server private key
#[inline]
#[must_use]
pub fn server_key() -> &'static [u8] {
include_bytes!("../../../fixtures/server.key")
}

/// CA certificate
#[inline]
#[must_use]
pub fn ca_cert() -> &'static [u8] {
include_bytes!("../../../fixtures/ca.crt")
}
}

/// Create a new endpoint from addr
/// # Errors
/// Return error if addr or tls config is invalid
Expand All @@ -243,6 +220,14 @@ pub fn build_endpoint(
addr: &str,
tls_config: Option<&ClientTlsConfig>,
) -> Result<Endpoint, tonic::transport::Error> {
debug!(
"connect to {addr}{}",
if tls_config.is_some() {
" with tls_config"
} else {
""
}
);
let scheme_str = addr.split_once("://").map(|(scheme, _)| scheme);
let endpoint = match scheme_str {
Some(_scheme) => Endpoint::from_str(addr)?,
Expand Down
1 change: 1 addition & 0 deletions crates/xline-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl Client {
let channel = Self::build_channel(addrs.clone(), options.tls_config.as_ref()).await?;
let curp_client = Arc::new(
CurpClientBuilder::new(options.client_config, false)
.tls_config(options.tls_config)
.discover_from(addrs)
.await?
.build::<Command>()
Expand Down
2 changes: 2 additions & 0 deletions crates/xline-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ readme = "README.md"
[dependencies]
anyhow = "1.0.75"
clap = { version = "4.4.4", features = ["derive"] }
futures = "0.3.30"
rand = "0.8.5"
tokio = { version = "0.2.23", package = "madsim-tokio", features = [
"rt-multi-thread",
Expand All @@ -22,6 +23,7 @@ tokio = { version = "0.2.23", package = "madsim-tokio", features = [
"net",
"signal",
] }
tonic = "0.10.2"
utils = { path = "../utils", features = ["parking_lot"] }
workspace-hack = { version = "0.1", path = "../../workspace-hack" }
xline = { path = "../xline" }
Expand Down
28 changes: 0 additions & 28 deletions crates/xline-test-utils/private.pem

This file was deleted.

9 changes: 0 additions & 9 deletions crates/xline-test-utils/public.pem

This file was deleted.

Loading
Loading