You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by "ozewr (via GitHub)" <gi...@apache.org> on 2024/12/27 14:20:17 UTC

[PR] feat:add tls support fot memcached [opendal]

ozewr opened a new pull request, #5471:
URL: https://github.com/apache/opendal/pull/5471

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #5419.
   
   # Rationale for this change
   see #5419
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   # What changes are included in this PR?
   Modified the `opendal::services::Memcached` to support TLS connections.
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   Users can enable TLS using `.tls()` and provide the CA file using `.cafile().` 
   example:
   ```Rust
   let memcached = Memcached::default()
           .endpoint(r#"tcp://example.app.local:11211"#)
           .tls(true)
           .cafile(path);
   ```
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking-changes` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "ozewr (via GitHub)" <gi...@apache.org>.
ozewr commented on code in PR #5471:
URL: https://github.com/apache/opendal/pull/5471#discussion_r2035214105


##########
core/src/services/memcached/binary.rs:
##########
@@ -98,11 +98,11 @@ pub struct Response {
 }
 
 pub struct Connection {
-    io: BufReader<TcpStream>,
+    io: BufReader<Box<dyn Connect>>,
 }

Review Comment:
   This might not be a good implementation.https://github.com/apache/opendal/blob/f66cf1f3dfc1d43e52d8cdb7a7225f72dbcbabd7/core/src/services/memcached/backend.rs#L262-L264
   This implementation returns a concrete type `Self::Connection`. If we try to use generics here, it would cause a conflict where a single function  attempts to return two different  types.That's because IO is a concrete type.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "ozewr (via GitHub)" <gi...@apache.org>.
ozewr commented on code in PR #5471:
URL: https://github.com/apache/opendal/pull/5471#discussion_r1901444044


##########
core/src/services/memcached/backend.rs:
##########
@@ -126,6 +148,22 @@ impl Builder for MemcachedBuilder {
                     .with_context("endpoint", &endpoint),
             );
         };
+        if self.config.enable_tls {
+            rustls::crypto::aws_lc_rs::default_provider()

Review Comment:
   How about adding a new boolean field? If the user selects true, they will need to provide it themselves.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on code in PR #5471:
URL: https://github.com/apache/opendal/pull/5471#discussion_r1898563618


##########
core/src/services/memcached/backend.rs:
##########
@@ -82,6 +87,18 @@ impl MemcachedBuilder {
         self.config.default_ttl = Some(ttl);
         self
     }
+
+    /// Set the tls connect on.
+    pub fn tls(mut self, tls: bool) -> Self {
+        self.config.tls = Some(tls);
+        self
+    }
+
+    /// Set the tls connect on.
+    pub fn cafile(mut self, cafile: PathBuf) -> Self {

Review Comment:
   I believe we should provide two options here:
   
   - We should load users' CA by default (via `rustls_native_certs`).
   - We should provide `key` and `cert` otpion for users to use their own cert.



##########
core/src/services/memcached/binary.rs:
##########
@@ -90,18 +96,200 @@ impl PacketHeader {
     }
 }
 
+pub enum Connection {
+    Tls(TlsConnection),
+    Tcp(TcpConnection),
+}
+
+impl Connection {
+    pub async fn version(&mut self) -> Result<String> {
+        match self {
+            Self::Tls(conn) => conn.version().await,
+            Self::Tcp(conn) => conn.version().await,
+        }
+    }
+    pub async fn get(&mut self, key: &str) -> Result<Option<Vec<u8>>> {
+        match self {
+            Self::Tls(conn) => conn.get(key).await,
+            Self::Tcp(conn) => conn.get(key).await,
+        }
+    }
+
+    pub async fn set(&mut self, key: &str, val: &[u8], expiration: u32) -> Result<()> {
+        match self {
+            Self::Tls(conn) => conn.set(key, val, expiration).await,
+            Self::Tcp(conn) => conn.set(key, val, expiration).await,
+        }
+    }
+    pub async fn delete(&mut self, key: &str) -> Result<()> {
+        match self {
+            Self::Tls(conn) => conn.delete(key).await,
+            Self::Tcp(conn) => conn.delete(key).await,
+        }
+    }
+}
+
 pub struct Response {
     header: PacketHeader,
     _key: Vec<u8>,
     _extras: Vec<u8>,
     value: Vec<u8>,
 }
 
-pub struct Connection {
+pub struct TlsConnection {
+    io: BufReader<TlsStream<TcpStream>>,
+}
+
+impl TlsConnection {
+    pub fn new(io: TlsStream<TcpStream>) -> Self {
+        Self {
+            io: BufReader::new(io),
+        }
+    }
+
+    pub async fn auth(&mut self, username: &str, password: &str) -> Result<()> {

Review Comment:
   It seems like we're repeating a significant amount of code. Perhaps we could work directly with `AsyncWrite`? I believe both `TlsStream<TcpStream>` and `TcpStream` have implemented it.



##########
core/src/services/memcached/config.rs:
##########
@@ -40,4 +41,8 @@ pub struct MemcachedConfig {
     pub password: Option<String>,
     /// The default ttl for put operations.
     pub default_ttl: Option<Duration>,
+    /// default is false
+    pub tls: Option<bool>,
+    /// Path to the CA certificate for TLS verification.
+    pub cafile: Option<PathBuf>,

Review Comment:
   Please don't use `PathBuf` in config, use `String` instead.



##########
core/src/services/memcached/backend.rs:
##########
@@ -82,6 +87,18 @@ impl MemcachedBuilder {
         self.config.default_ttl = Some(ttl);
         self
     }
+
+    /// Set the tls connect on.
+    pub fn tls(mut self, tls: bool) -> Self {

Review Comment:
   Let's use `enable_tls` to align with other config in opendal.



##########
core/src/services/memcached/backend.rs:
##########
@@ -150,6 +175,9 @@ impl Builder for MemcachedBuilder {
             endpoint,
             username: self.config.username.clone(),
             password: self.config.password.clone(),
+            tls: self.config.tls.clone(),
+            cafile: self.config.cafile.clone(),
+            host,

Review Comment:
   Why we need a seperate `host`?



##########
core/src/services/memcached/backend.rs:
##########
@@ -265,14 +312,71 @@ impl bb8::ManageConnection for MemcacheConnectionManager {
 
     /// TODO: Implement unix stream support.
     async fn connect(&self) -> Result<Self::Connection, Self::Error> {
-        let conn = TcpStream::connect(&self.address)
-            .await
-            .map_err(new_std_io_error)?;
-        let mut conn = binary::Connection::new(conn);
+        let conn = if self.tls.unwrap_or(false) {
+            let mut root_cert_store = rustls::RootCertStore::empty();
+
+            if let Some(cafile) = &self.cafile {
+                for cert in CertificateDer::pem_file_iter(cafile).map_err(|err| match err {
+                    rustls::pki_types::pem::Error::Io(err) => new_std_io_error(err),
+                    _ => unreachable!(),
+                })? {
+                    root_cert_store
+                        .add(cert.map_err(|err| match err {
+                            rustls::pki_types::pem::Error::Io(err) => new_std_io_error(err),
+                            _ => unreachable!(),
+                        })?)
+                        .map_err(|err| {
+                            Error::new(ErrorKind::Unexpected, "tls connect failed").set_source(err)
+                        })?;
+                }
+            } else {
+                root_cert_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
+            }
 
-        if let (Some(username), Some(password)) = (self.username.as_ref(), self.password.as_ref()) {
-            conn.auth(username, password).await?;
-        }
+            let config = rustls::ClientConfig::builder()
+                .with_root_certificates(root_cert_store)

Review Comment:
   I suppose we don't need to manually install root certificates.



##########
core/src/services/memcached/config.rs:
##########
@@ -40,4 +41,8 @@ pub struct MemcachedConfig {
     pub password: Option<String>,
     /// The default ttl for put operations.
     pub default_ttl: Option<Duration>,
+    /// default is false
+    pub tls: Option<bool>,

Review Comment:
   Use `bool` is good enough that we make it default to `false`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "ozewr (via GitHub)" <gi...@apache.org>.
ozewr commented on code in PR #5471:
URL: https://github.com/apache/opendal/pull/5471#discussion_r1898783609


##########
core/src/services/memcached/backend.rs:
##########
@@ -150,6 +175,9 @@ impl Builder for MemcachedBuilder {
             endpoint,
             username: self.config.username.clone(),
             password: self.config.password.clone(),
+            tls: self.config.tls.clone(),
+            cafile: self.config.cafile.clone(),
+            host,

Review Comment:
   Because  `ServerName` requires it, I don't want to return an additional error.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "ozewr (via GitHub)" <gi...@apache.org>.
ozewr commented on PR #5471:
URL: https://github.com/apache/opendal/pull/5471#issuecomment-2566072790

   > Would you like to add a test for memcached with tls enabled?
   
   No problem, I will complete it later.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "ozewr (via GitHub)" <gi...@apache.org>.
ozewr commented on code in PR #5471:
URL: https://github.com/apache/opendal/pull/5471#discussion_r1901539591


##########
core/src/services/memcached/backend.rs:
##########
@@ -265,14 +325,85 @@ impl bb8::ManageConnection for MemcacheConnectionManager {
 
     /// TODO: Implement unix stream support.
     async fn connect(&self) -> Result<Self::Connection, Self::Error> {
-        let conn = TcpStream::connect(&self.address)
-            .await
-            .map_err(new_std_io_error)?;
-        let mut conn = binary::Connection::new(conn);
+        let conn = if self.enable_tls {
+            let mut root_cert_store = rustls::RootCertStore::empty();
+
+            let native_certs = rustls_native_certs::load_native_certs();
+            if native_certs.errors.is_empty() {

Review Comment:
   you mean just use `.expect()` or `.unwrap()`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "ozewr (via GitHub)" <gi...@apache.org>.
ozewr commented on PR #5471:
URL: https://github.com/apache/opendal/pull/5471#issuecomment-2567537294

   Does the Memcached service require any special configuration? During testing, I found many incorrect test results, and some tests occasionally pass while failing at other times during repeated tests. 
   Here is my Memcached service configuration: 
   ```
   memcached --protocol=auto -p 11212 --enable-ssl -o ssl_chain_cert=./server.crt,ssl_key=./server.key,ssl_verify_mode=2,ssl_ca_cert=./client_ca/client_ca.crt
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on code in PR #5471:
URL: https://github.com/apache/opendal/pull/5471#discussion_r2031080907


##########
core/src/services/memcached/binary.rs:
##########
@@ -98,11 +98,11 @@ pub struct Response {
 }
 
 pub struct Connection {
-    io: BufReader<TcpStream>,
+    io: BufReader<Box<dyn Connect>>,
 }

Review Comment:
   How about using:
   
   ```rust
   pub struct Connection<IO: AsyncRead + AsyncWrite>{
       io: IO,
   }
   ```
   
   So we don't need to `Box<dyn Connect>` here?



##########
core/src/services/memcached/binary.rs:
##########
@@ -287,3 +291,12 @@ pub async fn parse_response(reader: &mut TcpStream) -> Result<Response> {
         value,
     })
 }
+
+#[async_trait::async_trait]
+pub trait Connect:
+    AsyncWrite + std::marker::Unpin + tokio::io::AsyncRead + std::marker::Send
+{
+}
+
+impl Connect for TcpStream {}
+impl Connect for TlsStream<TcpStream> {}

Review Comment:
   Maybe we don't this trait? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on code in PR #5471:
URL: https://github.com/apache/opendal/pull/5471#discussion_r1901437073


##########
core/src/services/memcached/backend.rs:
##########
@@ -265,14 +325,85 @@ impl bb8::ManageConnection for MemcacheConnectionManager {
 
     /// TODO: Implement unix stream support.
     async fn connect(&self) -> Result<Self::Connection, Self::Error> {
-        let conn = TcpStream::connect(&self.address)
-            .await
-            .map_err(new_std_io_error)?;
-        let mut conn = binary::Connection::new(conn);
+        let conn = if self.enable_tls {
+            let mut root_cert_store = rustls::RootCertStore::empty();
+
+            let native_certs = rustls_native_certs::load_native_certs();
+            if native_certs.errors.is_empty() {
+                for cert in rustls_native_certs::load_native_certs().expect("unreachable!") {
+                    root_cert_store.add(cert).map_err(|err| {
+                        Error::new(ErrorKind::Unexpected, "tls connect failed").set_source(err)
+                    })?;
+                }
+            }
 
-        if let (Some(username), Some(password)) = (self.username.as_ref(), self.password.as_ref()) {
-            conn.auth(username, password).await?;
-        }
+            let config = if let (Some(cert_path), Some(key_path)) = (&self.tls_cert, &self.tls_key)
+            {
+                let cert_chain = CertificateDer::pem_file_iter(cert_path)
+                    .map_err(|err| {
+                        Error::new(ErrorKind::Unexpected, "tls connect failed").set_source(err)
+                    })?
+                    .filter_map(Result::ok)
+                    .collect();
+                let key_der = PrivateKeyDer::from_pem_file(key_path).map_err(|err| {
+                    Error::new(ErrorKind::Unexpected, "tls connect failed").set_source(err)
+                })?;
+
+                rustls::ClientConfig::builder()

Review Comment:
   It's better for us to build this config once and use it for every connection. Loading and parsing certs is a heavy work.



##########
core/Cargo.toml:
##########
@@ -47,7 +47,7 @@ rust-version = "1.75"
 version = "0.50.1"
 
 [features]
-default = ["reqwest/rustls-tls", "executors-tokio", "services-memory"]
+default = ["reqwest/rustls-tls", "executors-tokio", "services-memory" ,"services-memcached"]

Review Comment:
   Please do not add `services-memcached` as the default. We want users to enable only the memory service by default.



##########
core/src/services/memcached/backend.rs:
##########
@@ -126,6 +148,22 @@ impl Builder for MemcachedBuilder {
                     .with_context("endpoint", &endpoint),
             );
         };
+        if self.config.enable_tls {
+            rustls::crypto::aws_lc_rs::default_provider()

Review Comment:
   I'm a bit concerned about this. I don't want to make decisions for users when it comes to choosing a crypto provider. Let's use ring as default for now.



##########
core/src/services/memcached/backend.rs:
##########
@@ -265,14 +325,85 @@ impl bb8::ManageConnection for MemcacheConnectionManager {
 
     /// TODO: Implement unix stream support.
     async fn connect(&self) -> Result<Self::Connection, Self::Error> {
-        let conn = TcpStream::connect(&self.address)
-            .await
-            .map_err(new_std_io_error)?;
-        let mut conn = binary::Connection::new(conn);
+        let conn = if self.enable_tls {
+            let mut root_cert_store = rustls::RootCertStore::empty();
+
+            let native_certs = rustls_native_certs::load_native_certs();
+            if native_certs.errors.is_empty() {

Review Comment:
   We don't care about those errors.



##########
core/src/services/memcached/backend.rs:
##########
@@ -265,14 +325,85 @@ impl bb8::ManageConnection for MemcacheConnectionManager {
 
     /// TODO: Implement unix stream support.
     async fn connect(&self) -> Result<Self::Connection, Self::Error> {
-        let conn = TcpStream::connect(&self.address)
-            .await
-            .map_err(new_std_io_error)?;
-        let mut conn = binary::Connection::new(conn);
+        let conn = if self.enable_tls {
+            let mut root_cert_store = rustls::RootCertStore::empty();
+
+            let native_certs = rustls_native_certs::load_native_certs();
+            if native_certs.errors.is_empty() {
+                for cert in rustls_native_certs::load_native_certs().expect("unreachable!") {
+                    root_cert_store.add(cert).map_err(|err| {
+                        Error::new(ErrorKind::Unexpected, "tls connect failed").set_source(err)

Review Comment:
   Please give a more meaning error message like `loading tls cert failed`



##########
core/src/services/memcached/binary.rs:
##########
@@ -90,26 +95,135 @@ impl PacketHeader {
     }
 }
 
+pub enum Connection {
+    Tls(TlsConnection),
+    Tcp(TcpConnection),
+}
+
+impl Connection {
+    pub async fn version(&mut self) -> Result<String> {
+        match self {
+            Self::Tls(conn) => conn.version().await,
+            Self::Tcp(conn) => conn.version().await,
+        }
+    }
+    pub async fn get(&mut self, key: &str) -> Result<Option<Vec<u8>>> {
+        match self {
+            Self::Tls(conn) => conn.get(key).await,
+            Self::Tcp(conn) => conn.get(key).await,
+        }
+    }
+
+    pub async fn set(&mut self, key: &str, val: &[u8], expiration: u32) -> Result<()> {
+        match self {
+            Self::Tls(conn) => conn.set(key, val, expiration).await,
+            Self::Tcp(conn) => conn.set(key, val, expiration).await,
+        }
+    }
+    pub async fn delete(&mut self, key: &str) -> Result<()> {
+        match self {
+            Self::Tls(conn) => conn.delete(key).await,
+            Self::Tcp(conn) => conn.delete(key).await,
+        }
+    }
+}
+
 pub struct Response {
     header: PacketHeader,
     _key: Vec<u8>,
     _extras: Vec<u8>,
     value: Vec<u8>,
 }
 
-pub struct Connection {
+pub struct TlsConnection {
+    io: BufReader<TlsStream<TcpStream>>,
+}
+
+impl TlsConnection {
+    pub fn new(io: TlsStream<TcpStream>) -> Self {
+        Self {
+            io: BufReader::new(io),
+        }
+    }
+}
+
+impl Conn for TlsConnection {
+    type T = TlsStream<TcpStream>;
+    fn get_conn(&mut self) -> &mut Self::T {
+        self.io.get_mut()
+    }
+}
+pub struct TcpConnection {
     io: BufReader<TcpStream>,
 }
 
-impl Connection {
+impl TcpConnection {
     pub fn new(io: TcpStream) -> Self {
         Self {
             io: BufReader::new(io),
         }
     }
+}
+
+impl Conn for TcpConnection {
+    type T = TcpStream;
+    fn get_conn(&mut self) -> &mut Self::T {
+        self.io.get_mut()
+    }
+}
+
+pub async fn parse_response<T: AsyncWriteExt + std::marker::Unpin + tokio::io::AsyncRead>(
+    reader: &mut T,
+) -> Result<Response> {
+    let header = PacketHeader::read::<T>(reader)
+        .await
+        .map_err(new_std_io_error)?;
+
+    if header.vbucket_id_or_status != constants::OK_STATUS
+        && header.vbucket_id_or_status != constants::KEY_NOT_FOUND
+    {
+        return Err(
+            Error::new(ErrorKind::Unexpected, "unexpected status received")
+                .with_context("message", format!("{}", header.vbucket_id_or_status)),
+        );
+    }
+
+    let mut extras = vec![0x0; header.extras_length as usize];
+    reader
+        .read_exact(extras.as_mut_slice())
+        .await
+        .map_err(new_std_io_error)?;
+
+    let mut key = vec![0x0; header.key_length as usize];
+    reader
+        .read_exact(key.as_mut_slice())
+        .await
+        .map_err(new_std_io_error)?;
 
-    pub async fn auth(&mut self, username: &str, password: &str) -> Result<()> {
-        let writer = self.io.get_mut();
+    let mut value = vec![
+        0x0;
+        (header.total_body_length - u32::from(header.key_length) - u32::from(header.extras_length))
+            as usize
+    ];
+    reader
+        .read_exact(value.as_mut_slice())
+        .await
+        .map_err(new_std_io_error)?;
+
+    Ok(Response {
+        header,
+        _key: key,
+        _extras: extras,
+        value,
+    })
+}
+#[async_trait::async_trait]
+pub trait Conn {

Review Comment:
   This trait is unnecessary since we only have two variants. We can use an enum to represent it.



##########
core/src/services/memcached/backend.rs:
##########
@@ -265,14 +325,85 @@ impl bb8::ManageConnection for MemcacheConnectionManager {
 
     /// TODO: Implement unix stream support.
     async fn connect(&self) -> Result<Self::Connection, Self::Error> {
-        let conn = TcpStream::connect(&self.address)
-            .await
-            .map_err(new_std_io_error)?;
-        let mut conn = binary::Connection::new(conn);
+        let conn = if self.enable_tls {
+            let mut root_cert_store = rustls::RootCertStore::empty();
+
+            let native_certs = rustls_native_certs::load_native_certs();
+            if native_certs.errors.is_empty() {
+                for cert in rustls_native_certs::load_native_certs().expect("unreachable!") {

Review Comment:
   We are loading certs for twice here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat:add tls support fot memcached [opendal]

Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on code in PR #5471:
URL: https://github.com/apache/opendal/pull/5471#discussion_r1899639507


##########
core/src/services/memcached/backend.rs:
##########
@@ -265,14 +329,100 @@ impl bb8::ManageConnection for MemcacheConnectionManager {
 
     /// TODO: Implement unix stream support.
     async fn connect(&self) -> Result<Self::Connection, Self::Error> {
-        let conn = TcpStream::connect(&self.address)
-            .await
-            .map_err(new_std_io_error)?;
-        let mut conn = binary::Connection::new(conn);
+        let conn = if self.enable_tls {
+            let mut root_cert_store = rustls::RootCertStore::empty();
+
+            let native_certs = rustls_native_certs::load_native_certs();
+            if native_certs.errors.is_empty() {
+                for cert in rustls_native_certs::load_native_certs().expect("unreachable!") {
+                    root_cert_store.add(cert).map_err(|err| {
+                        Error::new(ErrorKind::Unexpected, "tls connect failed").set_source(err)
+                    })?;
+                }
+            }
 
-        if let (Some(username), Some(password)) = (self.username.as_ref(), self.password.as_ref()) {
-            conn.auth(username, password).await?;
-        }
+            if let Some(cafile) = &self.cafile {

Review Comment:
   We don't need to handle cafile.



##########
core/src/services/memcached/backend.rs:
##########
@@ -82,6 +86,30 @@ impl MemcachedBuilder {
         self.config.default_ttl = Some(ttl);
         self
     }
+
+    /// Set the tls connect on.
+    pub fn enable_tls(mut self, enable_tls: bool) -> Self {
+        self.config.enable_tls = enable_tls;
+        self
+    }
+
+    /// Set the tls cafile path.
+    pub fn cafile(mut self, cafile: &str) -> Self {

Review Comment:
   We don't need to support specify `cafile`. Users that need this can just use `SSL_CERT_DIR`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(services/memcached): add opt-in TLS connections [opendal]

Posted by "Xuanwo (via GitHub) via commits" <co...@opendal.apache.org>.
Xuanwo merged PR #5471:
URL: https://github.com/apache/opendal/pull/5471


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@opendal.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org