Skip to content

Commit

Permalink
Resolving the TLS url issue (#459)
Browse files Browse the repository at this point in the history
* resolving the url issue

Signed-off-by: limbooverlambda <[email protected]>

* fix formatting

Signed-off-by: limbooverlambda <[email protected]>

* make check fixes

Signed-off-by: limbooverlambda <[email protected]>

---------

Signed-off-by: limbooverlambda <[email protected]>
  • Loading branch information
limbooverlambda authored Jun 27, 2024
1 parent 54fd720 commit ec8dbcc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
47 changes: 30 additions & 17 deletions src/common/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::time::Duration;

use log::info;
use regex::Regex;
use tonic::transport::Certificate;
use tonic::transport::Channel;
use tonic::transport::ClientTlsConfig;
use tonic::transport::Identity;
use tonic::transport::{Certificate, Endpoint};

use crate::internal_err;
use crate::Result;
Expand Down Expand Up @@ -77,27 +77,40 @@ impl SecurityManager {
where
Factory: FnOnce(Channel) -> Client,
{
let addr = "http://".to_string() + &SCHEME_REG.replace(addr, "");

info!("connect to rpc server at endpoint: {:?}", addr);
let channel = if !self.ca.is_empty() {
self.tls_channel(addr).await?
} else {
self.default_channel(addr).await?
};
let ch = channel.connect().await?;

let mut builder = Channel::from_shared(addr)?
.tcp_keepalive(Some(Duration::from_secs(10)))
.keep_alive_timeout(Duration::from_secs(3));
Ok(factory(ch))
}

if !self.ca.is_empty() {
let tls = ClientTlsConfig::new()
.ca_certificate(Certificate::from_pem(&self.ca))
.identity(Identity::from_pem(
&self.cert,
load_pem_file("private key", &self.key)?,
));
builder = builder.tls_config(tls)?;
};
async fn tls_channel(&self, addr: &str) -> Result<Endpoint> {
let addr = "https://".to_string() + &SCHEME_REG.replace(addr, "");
let builder = self.endpoint(addr.to_string())?;
let tls = ClientTlsConfig::new()
.ca_certificate(Certificate::from_pem(&self.ca))
.identity(Identity::from_pem(
&self.cert,
load_pem_file("private key", &self.key)?,
));
let builder = builder.tls_config(tls)?;
Ok(builder)
}

let ch = builder.connect().await?;
async fn default_channel(&self, addr: &str) -> Result<Endpoint> {
let addr = "http://".to_string() + &SCHEME_REG.replace(addr, "");
self.endpoint(addr)
}

Ok(factory(ch))
fn endpoint(&self, addr: String) -> Result<Endpoint> {
let endpoint = Channel::from_shared(addr)?
.tcp_keepalive(Some(Duration::from_secs(10)))
.keep_alive_timeout(Duration::from_secs(3));
Ok(endpoint)
}
}

Expand Down
1 change: 0 additions & 1 deletion src/kv/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::fmt;
use std::ops::Bound;
use std::u8;

#[allow(unused_imports)]
#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion src/kv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
use std::fmt;
use std::u8;

mod bound_range;
pub mod codec;
Expand Down
1 change: 0 additions & 1 deletion src/raw/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use core::ops::Range;
use std::str::FromStr;
use std::sync::Arc;
use std::u32;

use futures::StreamExt;
use log::debug;
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub fn new_prewrite_request(
req.start_version = start_version;
req.lock_ttl = lock_ttl;
// FIXME: Lite resolve lock is currently disabled
req.txn_size = std::u64::MAX;
req.txn_size = u64::MAX;

req
}
Expand Down

0 comments on commit ec8dbcc

Please sign in to comment.