sqlx_exasol_impl/options/
ssl_mode.rs

1use std::str::FromStr;
2
3use super::{error::ExaConfigError, SSL_MODE};
4
5/// Options for controlling the desired security state of the connection to the Exasol server.
6///
7/// It is used by [`crate::options::builder::ExaConnectOptionsBuilder::ssl_mode`].
8#[derive(Debug, Clone, Copy, Default, PartialEq)]
9pub enum ExaSslMode {
10    /// Establish an unencrypted connection.
11    Disabled,
12
13    /// Establish an encrypted connection if the server supports encrypted connections, falling
14    /// back to an unencrypted connection if an encrypted connection cannot be established.
15    ///
16    /// This is the default if `ssl_mode` is not specified.
17    #[default]
18    Preferred,
19
20    /// Establish an encrypted connection if the server supports encrypted connections.
21    /// The connection attempt fails if an encrypted connection cannot be established.
22    Required,
23
24    /// Like `Required`, but additionally verify the server Certificate Authority (CA)
25    /// certificate against the configured CA certificates. The connection attempt fails
26    /// if no valid matching CA certificates are found.
27    VerifyCa,
28
29    /// Like `VerifyCa`, but additionally perform host name identity verification by
30    /// checking the host name the client uses for connecting to the server against the
31    /// identity in the certificate that the server sends to the client.
32    VerifyIdentity,
33}
34
35impl ExaSslMode {
36    const DISABLED: &str = "disabled";
37    const PREFERRED: &str = "preferred";
38    const REQUIRED: &str = "required";
39    const VERIFY_CA: &str = "verify_ca";
40    const VERIFY_IDENTITY: &str = "verify_identity";
41}
42
43impl FromStr for ExaSslMode {
44    type Err = ExaConfigError;
45
46    fn from_str(s: &str) -> Result<Self, ExaConfigError> {
47        Ok(match &*s.to_ascii_lowercase() {
48            Self::DISABLED => ExaSslMode::Disabled,
49            Self::PREFERRED => ExaSslMode::Preferred,
50            Self::REQUIRED => ExaSslMode::Required,
51            Self::VERIFY_CA => ExaSslMode::VerifyCa,
52            Self::VERIFY_IDENTITY => ExaSslMode::VerifyIdentity,
53            _ => Err(ExaConfigError::InvalidParameter(SSL_MODE))?,
54        })
55    }
56}
57
58impl AsRef<str> for ExaSslMode {
59    fn as_ref(&self) -> &str {
60        match self {
61            ExaSslMode::Disabled => Self::DISABLED,
62            ExaSslMode::Preferred => Self::PREFERRED,
63            ExaSslMode::Required => Self::REQUIRED,
64            ExaSslMode::VerifyCa => Self::VERIFY_CA,
65            ExaSslMode::VerifyIdentity => Self::VERIFY_IDENTITY,
66        }
67    }
68}