1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* -------------------------------------------------------------------------- *\
 *          Apache 2.0 License Copyright © 2022-2023 The Aurae Authors        *
 *                                                                            *
 *                +--------------------------------------------+              *
 *                |   █████╗ ██╗   ██╗██████╗  █████╗ ███████╗ |              *
 *                |  ██╔══██╗██║   ██║██╔══██╗██╔══██╗██╔════╝ |              *
 *                |  ███████║██║   ██║██████╔╝███████║█████╗   |              *
 *                |  ██╔══██║██║   ██║██╔══██╗██╔══██║██╔══╝   |              *
 *                |  ██║  ██║╚██████╔╝██║  ██║██║  ██║███████╗ |              *
 *                |  ╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝ |              *
 *                +--------------------------------------------+              *
 *                                                                            *
 *                         Distributed Systems Runtime                        *
 *                                                                            *
 * -------------------------------------------------------------------------- *
 *                                                                            *
 *   Licensed under the Apache License, Version 2.0 (the "License");          *
 *   you may not use this file except in compliance with the License.         *
 *   You may obtain a copy of the License at                                  *
 *                                                                            *
 *       http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                            *
 *   Unless required by applicable law or agreed to in writing, software      *
 *   distributed under the License is distributed on an "AS IS" BASIS,        *
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 *   See the License for the specific language governing permissions and      *
 *   limitations under the License.                                           *
 *                                                                            *
\* -------------------------------------------------------------------------- */

use crate::config::client_cert_details::ClientCertDetails;
use crate::config::x509_details::new_x509_details;
use crate::AuthConfig;
use anyhow::Context;

pub struct CertMaterial {
    pub server_root_ca_cert: Vec<u8>,
    pub client_cert: Vec<u8>,
    pub client_key: Vec<u8>,
}

impl CertMaterial {
    pub async fn from_config(config: &AuthConfig) -> anyhow::Result<Self> {
        let server_root_ca_cert =
            tokio::fs::read(&config.ca_crt).await.with_context(|| {
                format!(
                    "Failed to read server root CA certificate from path '{}'",
                    config.ca_crt
                )
            })?;

        let client_cert =
            tokio::fs::read(&config.client_crt).await.with_context(|| {
                format!(
                    "Failed to read client certificate from path '{}'",
                    config.client_crt
                )
            })?;

        let client_key =
            tokio::fs::read(&config.client_key).await.with_context(|| {
                format!(
                    "Failed to read client key from path '{}'",
                    config.client_key
                )
            })?;

        Ok(Self { server_root_ca_cert, client_cert, client_key })
    }

    pub fn get_client_cert_details(&self) -> anyhow::Result<ClientCertDetails> {
        Ok(ClientCertDetails(new_x509_details(self.client_cert.clone())?))
    }
}