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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* -------------------------------------------------------------------------- *\
 *        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.                                           *
 *                                                                            *
\* -------------------------------------------------------------------------- */

//! Configuration used to authenticate with a remote Aurae daemon.
//!
//! [`AuraeConfig::try_default()`] follows an ordered priority for searching for
//! configuration on a client's machine.
//!
//! 1. ${HOME}/.aurae/config
//! 2. /etc/aurae/config
//! 3. /var/lib/aurae/config

pub use self::{
    auth_config::AuthConfig, cert_material::CertMaterial,
    client_cert_details::ClientCertDetails, system_config::AuraeSocket,
    system_config::SystemConfig,
};
use anyhow::{anyhow, Context, Result};
use serde::Deserialize;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use x509_details::X509Details;

mod auth_config;
mod cert_material;
mod client_cert_details;
mod system_config;
mod x509_details;

/// Configuration for AuraeScript client
#[derive(Debug, Clone, Deserialize)]
pub struct AuraeConfig {
    /// Authentication material
    pub auth: AuthConfig,
    /// System configuration
    pub system: SystemConfig,
}

impl AuraeConfig {
    /// Attempt to easy-load Aurae configuration from well-known locations.
    pub fn try_default() -> Result<Self> {
        let home = std::env::var("HOME")
            .expect("missing $HOME environmental variable");

        let search_paths = [
            &format!("{home}/.aurae/config"),
            "/etc/aurae/config",
            "/var/lib/aurae/config",
        ];

        for path in search_paths {
            match Self::parse_from_toml_file(path) {
                Ok(config) => {
                    return Ok(config);
                }
                Err(e) => {
                    eprintln!("warning: failed to parse config at {path}: {e}");
                    continue;
                }
            }
        }

        Err(anyhow!("unable to find valid config file"))
    }

    /// Attempt to parse a config file into memory.
    pub fn parse_from_toml_file<P: AsRef<Path>>(
        path: P,
    ) -> Result<AuraeConfig> {
        let mut config_toml = String::new();
        let mut file = File::open(path)?;

        if file
            .read_to_string(&mut config_toml)
            .with_context(|| "could not read AuraeConfig toml")?
            == 0
        {
            return Err(anyhow!("empty config"));
        }

        AuraeConfig::parse_from_toml(&config_toml)
    }

    pub fn parse_from_toml(config_toml: &str) -> Result<AuraeConfig> {
        Ok(toml::from_str(config_toml)?)
    }

    /// Create a new AuraeConfig from given options
    ///
    /// # Arguments
    ///
    /// * `ca_crt` - Path to ca cert
    /// * `client_crt` - Path to client cert
    /// * `client_key` - Path to client key
    /// * `socket` - Address to auraed
    ///
    /// Note: A new client is required for every independent execution of this process.
    pub fn from_options<
        S1: Into<String>,
        S2: Into<String>,
        S3: Into<String>,
        S4: Into<String>,
    >(
        ca_crt: S1,
        client_crt: S2,
        client_key: S3,
        socket: S4,
    ) -> Self {
        let (ca_crt, client_crt, client_key, socket) = (
            ca_crt.into(),
            client_crt.into(),
            client_key.into(),
            socket.into(),
        );
        let auth = AuthConfig { ca_crt, client_crt, client_key };
        let system = SystemConfig { socket: AuraeSocket::Path(socket.into()) };
        Self { auth, system }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
    use std::str::FromStr;

    fn get_input(socket: &str) -> String {
        const INPUT: &str = r#"
[auth]
ca_crt = "~/.aurae/pki/ca.crt"
client_crt = "~/.aurae/pki/_signed.client.nova.crt"
client_key = "~/.aurae/pki/client.nova.key"

[system]
socket = "#;

        format!("{INPUT}\"{socket}\"")
    }

    #[test]
    fn can_parse_toml_config_socket_path() {
        let input = get_input("/var/run/aurae/aurae.sock");
        let config = AuraeConfig::parse_from_toml(&input).unwrap();
        assert!(
            matches!(config.system.socket, AuraeSocket::Path(path) if Some("/var/run/aurae/aurae.sock") == path.to_str())
        )
    }

    #[test]
    fn can_parse_toml_config_socket_ipv6_with_scope_id() {
        let input = get_input("[fe80::2%4]:8080");
        let config = AuraeConfig::parse_from_toml(&input).unwrap();
        let AuraeSocket::Addr (addr) = config.system.socket else {
            panic!("expected AuraeSocket::Addr");
        };

        let SocketAddr::V6(addr) = addr else {
            panic!("expected v6 addr");
        };

        assert_eq!(*addr.ip(), Ipv6Addr::from_str("fe80::2").unwrap());
        assert_eq!(addr.port(), 8080);
        assert_eq!(addr.scope_id(), 4);
    }

    #[test]
    fn can_parse_toml_config_socket_ipv6_without_scope_id() {
        let input = get_input("[fe80::2]:8080");
        let config = AuraeConfig::parse_from_toml(&input).unwrap();
        let AuraeSocket::Addr (addr) = config.system.socket else {
            panic!("expected AuraeSocket::Addr");
        };

        let SocketAddr::V6(addr) = addr else {
            panic!("expected v6 addr");
        };

        assert_eq!(*addr.ip(), Ipv6Addr::from_str("fe80::2").unwrap());
        assert_eq!(addr.port(), 8080);
        assert_eq!(addr.scope_id(), 0);
    }

    #[test]
    fn can_parse_toml_config_socket_ipv4() {
        let input = get_input("127.1.2.3:1234");
        let config = AuraeConfig::parse_from_toml(&input).unwrap();
        let AuraeSocket::Addr (addr) = config.system.socket else {
            panic!("expected AuraeSocket::Addr");
        };

        let SocketAddr::V4(addr) = addr else {
            panic!("expected v4 addr");
        };

        assert_eq!(*addr.ip(), Ipv4Addr::from_str("127.1.2.3").unwrap());
        assert_eq!(addr.port(), 1234);
    }
}