summaryrefslogtreecommitdiff
path: root/crates/vcs_data/src/data/vault/config.rs
blob: 60f6cbb791d2a03a08f1691a16d0722246528487 (plain)
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
227
228
229
230
231
232
233
use std::net::{IpAddr, Ipv4Addr};

use cfg_file::ConfigFile;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::constants::{PORT, SERVER_FILE_VAULT};
use crate::data::member::{Member, MemberId};

pub type VaultName = String;
pub type VaultUuid = Uuid;

#[derive(Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum AuthMode {
    /// Use asymmetric keys: both client and server need to register keys, after which they can connect
    Key,

    /// Use password: the password stays on the server, and the client needs to set the password locally for connection
    #[default]
    Password,

    /// No authentication: generally used in a strongly secure environment, skipping verification directly
    NoAuth,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum LoggerLevel {
    Debug,
    Trace,

    #[default]
    Info,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum ServiceEnabled {
    Enable,

    #[default]
    Disable,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum BehaviourEnabled {
    Yes,

    #[default]
    No,
}

impl Into<bool> for ServiceEnabled {
    fn into(self) -> bool {
        match self {
            ServiceEnabled::Enable => true,
            ServiceEnabled::Disable => false,
        }
    }
}

impl Into<bool> for BehaviourEnabled {
    fn into(self) -> bool {
        match self {
            BehaviourEnabled::Yes => true,
            BehaviourEnabled::No => false,
        }
    }
}

#[derive(Serialize, Deserialize, ConfigFile)]
#[cfg_file(path = SERVER_FILE_VAULT)]
pub struct VaultConfig {
    /// Vault uuid, unique identifier for the vault
    #[serde(rename = "uuid")]
    vault_uuid: VaultUuid,

    /// Vault name, which can be used as the project name and generally serves as a hint
    #[serde(rename = "name")]
    vault_name: VaultName,

    /// Vault admin id, a list of member id representing administrator identities
    #[serde(rename = "admin")]
    vault_admin_list: Vec<MemberId>,

    /// Vault server configuration, which will be loaded when connecting to the server
    #[serde(rename = "profile")]
    server_config: VaultServerConfig,
}

#[derive(Serialize, Deserialize)]
pub struct VaultServerConfig {
    /// Local IP address to bind to when the server starts
    #[serde(rename = "bind")]
    local_bind: IpAddr,

    /// TCP port to bind to when the server starts
    #[serde(rename = "port")]
    port: u16,

    /// Enable logging
    #[serde(rename = "logger")]
    logger: Option<BehaviourEnabled>,

    /// Logger Level
    #[serde(rename = "logger_level")]
    logger_level: Option<LoggerLevel>,

    /// Whether to enable LAN discovery, allowing members on the same LAN to more easily find the upstream server
    #[serde(rename = "lan_discovery")]
    lan_discovery: Option<ServiceEnabled>, // TODO

    /// Authentication mode for the vault server
    /// key: Use asymmetric keys for authentication
    /// password: Use a password for authentication
    /// noauth: No authentication required, requires a strongly secure environment
    #[serde(rename = "auth_mode")]
    auth_mode: Option<AuthMode>, // TODO
}

impl Default for VaultConfig {
    fn default() -> Self {
        Self {
            vault_uuid: Uuid::new_v4(),
            vault_name: "JustEnoughVault".to_string(),
            vault_admin_list: Vec::new(),
            server_config: VaultServerConfig {
                local_bind: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
                port: PORT,
                logger: Some(BehaviourEnabled::default()),
                logger_level: Some(LoggerLevel::default()),
                lan_discovery: Some(ServiceEnabled::default()),
                auth_mode: Some(AuthMode::Key),
            },
        }
    }
}

/// Vault Management
impl VaultConfig {
    /// Change name of the vault.
    pub fn change_name(&mut self, name: impl Into<String>) {
        self.vault_name = name.into()
    }

    /// Add admin
    pub fn add_admin(&mut self, member: &Member) {
        let uuid = member.id();
        if !self.vault_admin_list.contains(&uuid) {
            self.vault_admin_list.push(uuid);
        }
    }

    /// Remove admin
    pub fn remove_admin(&mut self, member: &Member) {
        let id = member.id();
        self.vault_admin_list.retain(|x| x != &id);
    }

    /// Get vault UUID
    pub fn vault_uuid(&self) -> &VaultUuid {
        &self.vault_uuid
    }

    /// Set vault UUID
    pub fn set_vault_uuid(&mut self, vault_uuid: VaultUuid) {
        self.vault_uuid = vault_uuid;
    }

    /// Get vault name
    pub fn vault_name(&self) -> &VaultName {
        &self.vault_name
    }

    /// Set vault name
    pub fn set_vault_name(&mut self, vault_name: VaultName) {
        self.vault_name = vault_name;
    }

    /// Get vault admin list
    pub fn vault_admin_list(&self) -> &Vec<MemberId> {
        &self.vault_admin_list
    }

    /// Set vault admin list
    pub fn set_vault_admin_list(&mut self, vault_admin_list: Vec<MemberId>) {
        self.vault_admin_list = vault_admin_list;
    }

    /// Get server config
    pub fn server_config(&self) -> &VaultServerConfig {
        &self.server_config
    }

    /// Set server config
    pub fn set_server_config(&mut self, server_config: VaultServerConfig) {
        self.server_config = server_config;
    }
}

impl VaultServerConfig {
    /// Get local bind IP address
    pub fn local_bind(&self) -> &IpAddr {
        &self.local_bind
    }

    /// Get port
    pub fn port(&self) -> u16 {
        self.port
    }

    /// Check if LAN discovery is enabled
    pub fn is_lan_discovery_enabled(&self) -> bool {
        self.lan_discovery.clone().unwrap_or_default().into()
    }

    /// Get logger enabled status
    pub fn is_logger_enabled(&self) -> bool {
        self.logger.clone().unwrap_or_default().into()
    }

    /// Get logger level
    pub fn logger_level(&self) -> LoggerLevel {
        self.logger_level.clone().unwrap_or_default()
    }

    /// Get authentication mode
    pub fn auth_mode(&self) -> AuthMode {
        self.auth_mode.clone().unwrap_or_default()
    }
}