typedb_driver/connection/
driver_options.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20use std::{fs, path::Path};
21
22use tonic::transport::{Certificate, ClientTlsConfig};
23
24/// User connection settings for connecting to TypeDB.
25#[derive(Debug, Clone)]
26pub struct DriverOptions {
27    is_tls_enabled: bool,
28    tls_config: Option<ClientTlsConfig>,
29}
30
31impl DriverOptions {
32    /// Creates a credentials with username and password. Specifies the connection must use TLS
33    ///
34    /// # Arguments
35    ///
36    /// * `is_tls_enabled` — Specify whether the connection to TypeDB Server must be done over TLS.
37    /// * `tls_root_ca` — Path to the CA certificate to use for authenticating server certificates.
38    ///
39    /// # Examples
40    ///
41    /// ```rust
42    /// DriverOptions::new(true, Some(&path_to_ca));
43    ///```
44    pub fn new(is_tls_enabled: bool, tls_root_ca: Option<&Path>) -> crate::Result<Self> {
45        let tls_config = Some(if let Some(tls_root_ca) = tls_root_ca {
46            ClientTlsConfig::new().ca_certificate(Certificate::from_pem(fs::read_to_string(tls_root_ca)?))
47        } else {
48            ClientTlsConfig::new().with_native_roots()
49        });
50
51        Ok(Self { is_tls_enabled, tls_config })
52    }
53
54    /// Retrieves whether TLS is enabled for the connection.
55    pub fn is_tls_enabled(&self) -> bool {
56        self.is_tls_enabled
57    }
58
59    pub fn tls_config(&self) -> &Option<ClientTlsConfig> {
60        &self.tls_config
61    }
62}