auditor 0.11.0

AccoUnting Data handlIng Toolbox for Opportunistic Resources
Documentation
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2021-2022 AUDITOR developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use crate::telemetry::deserialize_log_level;
use rustls::ServerConfig;
use secrecy::{ExposeSecret, SecretString};
use serde_aux::field_attributes::deserialize_number_from_string;
use sqlx::ConnectOptions;
use sqlx::postgres::{PgConnectOptions, PgSslMode};
use std::collections::HashMap;
use tracing_subscriber::filter::LevelFilter;

#[derive(serde::Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Settings {
    #[serde(default)]
    pub environment: Environment,
    pub database: DatabaseSettings,
    pub application: AuditorSettings,
    #[serde(default = "default_metrics")]
    pub metrics: MetricsSettings,
    #[serde(default = "default_log_level")]
    #[serde(deserialize_with = "deserialize_log_level")]
    pub log_level: LevelFilter,
    #[serde(default = "default_logging")]
    pub logging: LoggingSettings,
    pub tls_config: Option<TLSConfig>,
    pub rbac_config: Option<RbacConfig>,
    #[serde(default = "default_ignore_record_exists_error")]
    pub ignore_record_exists_error: bool,
    pub archival_config: Option<ArchivalConfig>,
}

#[derive(serde::Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct LoggingSettings {
    #[serde(default = "default_log_dir")]
    pub log_dir: String,
    #[serde(default = "default_log_file_prefix")]
    pub log_file_prefix: String,
    #[serde(default = "default_log_to_file")]
    pub log_to_file: bool,
    #[serde(default = "default_log_file_size")]
    pub log_file_size: u64,
    #[serde(default = "default_number_of_rotated_backups")]
    pub number_of_rotated_backups: usize,
}

fn default_log_dir() -> String {
    "logs".to_string()
}

fn default_log_file_prefix() -> String {
    "auditor_logs".to_string()
}

fn default_log_to_file() -> bool {
    false
}

fn default_log_file_size() -> u64 {
    1024
}

fn default_number_of_rotated_backups() -> usize {
    5
}

fn default_logging() -> LoggingSettings {
    LoggingSettings {
        log_dir: default_log_dir(),
        log_file_prefix: default_log_file_prefix(),
        log_to_file: default_log_to_file(),
        log_file_size: default_log_file_size(),
        number_of_rotated_backups: default_number_of_rotated_backups(),
    }
}

#[derive(Debug, Clone, serde::Deserialize)]
pub enum CompressionType {
    Gzip,
    Snappy,
}

#[derive(Debug, Clone, serde::Deserialize)]
pub struct ArchivalConfig {
    pub archive_older_than_months: i32,
    pub archive_path: String,
    #[serde(default = "default_archive_file_prefix")]
    pub archive_file_prefix: String,
    pub cron_schedule: String, // e.g., "0 0 2 1 * *" // Monthly
    #[serde(default = "default_compression_type")]
    pub compression_type: CompressionType,
}

fn default_compression_type() -> CompressionType {
    CompressionType::Gzip
}

fn default_archive_file_prefix() -> String {
    "auditor".to_string()
}

#[derive(serde::Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct RbacConfig {
    #[serde(default = "default_enforce_rbac")]
    pub enforce_rbac: bool,
    #[serde(default = "default_base_policies")]
    pub base_policies: Vec<Vec<String>>,
    pub monitoring_role_cn: Option<Vec<String>>,
    pub write_access_cn: Option<Vec<String>>,
    pub read_access_cn: Option<Vec<String>>,
    pub data_access_rules: Option<Vec<Cn>>,
}

fn default_ignore_record_exists_error() -> bool {
    false
}

fn default_enforce_rbac() -> bool {
    false
}

#[derive(serde::Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct Cn {
    pub reader_cn: String,
    pub meta_info: HashMap<String, Vec<String>>,
}

fn default_base_policies() -> Vec<Vec<String>> {
    vec![
        vec![
            "monitoring_role".to_string(),
            "/metrics".to_string(),
            "GET".to_string(),
        ],
        vec![
            "write_access_base".to_string(),
            "/record".to_string(),
            "POST".to_string(),
        ],
        vec![
            "write_access_base".to_string(),
            "/record".to_string(),
            "PUT".to_string(),
        ],
        vec![
            "write_access_base".to_string(),
            "/records".to_string(),
            "POST".to_string(),
        ],
        vec![
            "write_access_base".to_string(),
            "/healthcheck".to_string(),
            "GET".to_string(),
        ],
        vec![
            "read_access_base".to_string(),
            "/records".to_string(),
            "GET".to_string(),
        ],
        vec![
            "read_access_base".to_string(),
            "/record/*".to_string(),
            "GET".to_string(),
        ],
        vec![
            "read_access_base".to_string(),
            "/healthcheck".to_string(),
            "GET".to_string(),
        ],
    ]
}

//Set the default values for TLSConfig options
#[derive(serde::Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct TLSConfig {
    pub use_tls: bool,
    pub https_addr: Option<Vec<String>>,
    #[serde(default = "default_https_port")]
    pub https_port: u16,
    pub ca_cert_path: Option<String>,
    pub server_cert_path: Option<String>,
    pub server_key_path: Option<String>,
}

fn default_https_port() -> u16 {
    8443u16
}

impl TLSConfig {
    /// Checks if TLS is enabled and required paths are provided.
    pub fn validate_tls_paths(&self) -> Result<(), &'static str> {
        if self.use_tls {
            if self.ca_cert_path.is_none() {
                return Err("ca_cert_path is required when use_tls is true");
            }
            if self.server_cert_path.is_none() {
                return Err("server_cert_path is required when use_tls is true");
            }
            if self.server_key_path.is_none() {
                return Err("server_key_path is required when use_tls is true");
            }
        }
        Ok(())
    }
}

#[derive(Debug)]
pub struct TLSParams {
    pub config: ServerConfig,
    pub https_addr: Option<Vec<String>>,
    pub https_port: u16,
    pub use_tls: bool,
}

fn default_log_level() -> LevelFilter {
    LevelFilter::INFO
}

#[derive(serde::Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct AuditorSettings {
    #[serde(default = "default_addr")]
    pub addr: Vec<String>,
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub port: u16,
    #[serde(default = "default_workers")]
    pub web_workers: usize,
}

fn default_addr() -> Vec<String> {
    vec!["127.0.0.1".to_string()]
}

fn default_workers() -> usize {
    if let Ok(num) = std::thread::available_parallelism() {
        std::cmp::min(num.get(), 4)
    } else {
        tracing::warn!("Cannot determine how many web workers to use. Fall back to 2.");
        2
    }
}

#[derive(serde::Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct DatabaseSettings {
    pub username: String,
    pub password: SecretString,
    #[serde(deserialize_with = "deserialize_number_from_string")]
    pub port: u16,
    pub host: String,
    pub database_name: String,
    pub require_ssl: bool,
}

#[derive(serde::Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct MetricsSettings {
    pub database: DatabaseMetricsSettings,
}

#[serde_with::serde_as]
#[derive(serde::Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct DatabaseMetricsSettings {
    #[serde(default = "default_db_metrics_frequency")]
    #[serde_as(as = "serde_with::DurationSeconds<i64>")]
    pub frequency: chrono::Duration,
    pub metrics: Vec<crate::metrics::DatabaseMetricsOptions>,
    #[serde(default = "default_meta_key_site")]
    pub meta_key_site: String,
    #[serde(default = "default_meta_key_group")]
    pub meta_key_group: String,
    #[serde(default = "default_meta_key_user")]
    pub meta_key_user: String,
}

fn default_meta_key_site() -> String {
    "site".to_string()
}

fn default_meta_key_group() -> String {
    "group".to_string()
}

fn default_meta_key_user() -> String {
    "user".to_string()
}

fn default_db_metrics_frequency() -> chrono::Duration {
    chrono::Duration::try_seconds(30).expect("This should never fail")
}

fn default_metrics() -> MetricsSettings {
    MetricsSettings {
        database: DatabaseMetricsSettings {
            frequency: default_db_metrics_frequency(),
            metrics: vec![],
            meta_key_site: default_meta_key_site(),
            meta_key_group: default_meta_key_group(),
            meta_key_user: default_meta_key_user(),
        },
    }
}

impl DatabaseSettings {
    /// Returns the connection options for the PostgreSQL database without database name
    pub fn without_db(&self) -> PgConnectOptions {
        let ssl_mode = if self.require_ssl {
            PgSslMode::Require
        } else {
            PgSslMode::Prefer
        };
        PgConnectOptions::new()
            .host(&self.host)
            .username(&self.username)
            .password(self.password.expose_secret())
            .port(self.port)
            .ssl_mode(ssl_mode)
    }

    /// Returns the connection options for the PostgreSQL database with database name
    pub fn with_db(&self) -> PgConnectOptions {
        self.without_db()
            .database(&self.database_name)
            .log_statements(tracing::log::LevelFilter::Trace)
    }
}

/// Loads the configuration from a file `configuration.{yaml,json,toml,...}`
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
    let base_path = std::env::current_dir().expect("Failed to determine the current directory");
    let configuration_directory = base_path.join("configuration");

    // if the directory doesn't exist, we're probably in the wrong directory. Let's get inside
    // "auditor/configuration" then!
    let configuration_directory = if configuration_directory.exists() {
        configuration_directory
    } else {
        base_path.join("auditor").join("configuration")
    };

    let environment: Environment = std::env::var("AUDITOR_ENVIRONMENT")
        .ok()
        .map(Environment::try_from)
        .transpose()
        .map_err(config::ConfigError::Message)?
        .unwrap_or_default();

    let settings = config::Config::builder()
        .add_source(config::File::from(configuration_directory.join("base")).required(false))
        .add_source(
            config::File::from(configuration_directory.join(environment.as_str())).required(false),
        );

    let settings = match std::env::args().nth(1) {
        Some(file) => settings.add_source(
            config::File::from(file.as_ref())
                .required(false)
                .format(config::FileFormat::Yaml),
        ),
        None => settings,
    };

    let settings = settings.add_source(
        config::Environment::with_prefix("AUDITOR")
            .separator("__")
            .prefix_separator("_")
            .list_separator(",")
            .with_list_parse_key("application.addr")
            .with_list_parse_key("rbac_config.read_access_cn")
            .with_list_parse_key("rbac_config.write_access_cn")
            .try_parsing(true),
    );

    settings.build()?.try_deserialize()
}

// The possible runtime environment for AUDITOR.
#[derive(serde::Deserialize, Debug, Default)]
#[serde(try_from = "String")]
pub enum Environment {
    #[default]
    Local,
    Production,
}

impl Environment {
    pub fn as_str(&self) -> &'static str {
        match self {
            Environment::Local => "local",
            Environment::Production => "production",
        }
    }
}

impl TryFrom<String> for Environment {
    type Error = String;
    fn try_from(s: String) -> Result<Self, Self::Error> {
        match s.to_lowercase().as_str() {
            "local" => Ok(Self::Local),
            "production" => Ok(Self::Production),
            other => Err(format!(
                "{other} is not a supported environment. Use either `local` or `production`."
            )),
        }
    }
}