routinator 0.15.1

An RPKI relying party software.
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Logging.

use std::{fmt, fs, io, mem, process, slice};
use std::io::Write;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};
use bytes::Bytes;
use chrono::{DateTime, Utc};
use log::{LevelFilter, Record, error};
use crate::config::{Config, LogTarget};
use crate::error::Failed;
use crate::utils::date::{format_iso_date, format_local_iso_date};
use crate::utils::fmt::WriteOrPanic;
use crate::utils::sync::{Mutex, RwLock};

//------------ LogMessage ----------------------------------------------------

/// The data of a single logged item.
#[derive(Clone, Debug)]
pub struct LogMessage {
    pub when: DateTime<Utc>,
    pub level: log::Level,
    pub content: String,
}

impl LogMessage {
    fn from_record(record: &Record<'_>) -> Self {
        Self {
            when: Utc::now(),
            level: record.level(),
            content: record.args().to_string(),
        }
    }
}


//------------ LogBook -------------------------------------------------------

#[derive(Clone, Debug, Default)]
pub struct LogBook {
    /// The messages logged into this log book.
    messages: Vec<LogMessage>,
}

impl LogBook {
    pub fn is_empty(&self) -> bool {
        self.messages.is_empty()
    }
}

impl<'a> IntoIterator for &'a LogBook {
    type Item = &'a LogMessage;
    type IntoIter = slice::Iter<'a, LogMessage>;

    fn into_iter(self) -> Self::IntoIter {
        self.messages.iter()
    }
}


//------------ LogBookWriter -------------------------------------------------

#[derive(Clone, Debug)]
pub struct LogBookWriter {
    /// The book to write messages to.
    book: LogBook,

    /// The prefix for writing messages to the process log.
    ///
    /// If this is `None`, we don’t write to the process log.
    process_prefix: Option<String>,
}

impl LogBookWriter {
    /// Creates a new log book writer.
    ///
    /// If `process_prefix` is not `None`, the writer will also write to the
    /// process log, prefixing every line with the given format output. This
    /// is the prefix exactly, i.e., there will not be white space or
    /// characters separating the prefix from the actual log output. Such
    /// a separator has to be part of the prefix.
    pub fn new(process_prefix: Option<String>) -> Self {
        Self {
            book: Default::default(),
            process_prefix,
        }
    }

    pub fn append(&mut self, mut other: LogBookWriter) {
        self.book.messages.append(&mut other.book.messages);
    }

    pub fn sort(&mut self) {
        self.book.messages.sort_unstable_by_key(|message| message.when);
    }

    /// Converts the writer into the final log book.
    pub fn into_book(self) -> LogBook {
        self.book
    }

    pub fn log(&mut self, level: log::Level, args: fmt::Arguments<'_>) {
        self.log_record(
            &log::Record::builder().level(level).args(args).build()
        )
    }

    pub fn trace(&mut self, args: fmt::Arguments<'_>) {
        self.log(log::Level::Trace, args);
    }

    pub fn debug(&mut self, args: fmt::Arguments<'_>) {
        self.log(log::Level::Debug, args);
    }

    pub fn info(&mut self, args: fmt::Arguments<'_>) {
        self.log(log::Level::Info, args);
    }

    pub fn warn(&mut self, args: fmt::Arguments<'_>) {
        self.log(log::Level::Info, args);
    }

    pub fn error(&mut self, args: fmt::Arguments<'_>) {
        self.log(log::Level::Error, args);
    }

    /// Writes a log record.
    pub fn log_record(&mut self, record: &Record<'_>) {
        let logger = log::logger();

        // We use the level filter from the global log which should be set
        // up correctly according to our configuration.
        if !logger.enabled(record.metadata()) {
            return
        }
        self.book.messages.push(LogMessage::from_record(record));
        if let Some(prefix) = self.process_prefix.as_ref() {
            logger.log(
                &log::Record::builder()
                    .args(format_args!("{}{}", prefix, record.args()))
                    .metadata(record.metadata().clone())
                    .module_path(record.module_path())
                    .file(record.file())
                    .line(record.line())
                    .build()
            );
        }
    }
}


//------------ Logger --------------------------------------------------------

/// Format and write log messages.
pub struct Logger {
    /// Where to write messages to.
    target: Mutex<LogBackend>,

    /// An additional target for showing the log in the HTTP server.
    output: Option<Arc<Mutex<String>>>,

    /// The maximum log level.
    log_level: log::LevelFilter,
}

/// The actual target for logging
enum LogBackend {
    #[cfg(unix)]
    Syslog(SyslogLogger),
    File {
        file: fs::File,
        path: PathBuf,
    },
    Stderr {
        stderr: io::Stderr,
        timestamp: bool,
    }
}

impl Logger {
    /// Initialize logging.
    ///
    /// All diagnostic output of Routinator is done via logging, never to
    /// stderr directly. Thus, it is important to initialize logging before
    /// doing anything else that may result in such output. This function
    /// does exactly that. It sets a maximum log level of `warn`, leading
    /// only printing important information, and directs all logging to
    /// stderr.
    pub fn init() -> Result<(), Failed> {
        log::set_max_level(LevelFilter::Warn);
        if let Err(err) = log::set_logger(&GLOBAL_LOGGER) {
            eprintln!("Failed to initialize logger: {err}.\nAborting.");
            return Err(Failed)
        }
        Ok(())
    }

    /// Switches logging to the configured target.
    ///
    /// Once the configuration has been successfully loaded, logging should
    /// be switched to whatever the user asked for via this method.
    #[allow(unused_variables)] // for cfg(not(unix))
    pub fn switch_logging(
        config: &Config,
        daemon: bool,
        with_output: bool
    ) -> Result<Option<LogOutput>, Failed> {
        let (output, res) = if with_output {
            let output = LogOutput::new();
            (Some(output.0), Some(output.1))
        }
        else {
            (None, None)
        };
        let logger = Logger::new(config, daemon, output)?;
        GLOBAL_LOGGER.switch(logger);
        log::set_max_level(config.log_level);
        Ok(res)
    }

    /// Rotates the log file if necessary.
    pub fn rotate_log() -> Result<(), Failed> {
        GLOBAL_LOGGER.rotate()
    }

    /// Creates a new logger from config and additional information.
    fn new(
        config: &Config, daemon: bool, output: Option<Arc<Mutex<String>>>
    ) -> Result<Self, Failed> {
        let target = match config.log_target {
            #[cfg(unix)]
            LogTarget::Default(facility) => {
                if daemon { 
                    Self::new_syslog_target(facility)?
                }
                else {
                    Self::new_stderr_target(false)
                }
            }
            #[cfg(unix)]
            LogTarget::Syslog(facility) => {
                Self::new_syslog_target(facility)?
            }
            LogTarget::File(ref path) => {
                Self::new_file_target(path.clone())?
            }
            LogTarget::Stderr => {
                Self::new_stderr_target(daemon)
            }
        };
        Ok(Self {
            target: Mutex::new(target),
            output,
            log_level: config.log_level,
        })
    }

    /// Creates a syslog target.
    #[cfg(unix)]
    fn new_syslog_target(
        facility: syslog::Facility
    ) -> Result<LogBackend, Failed> {
        SyslogLogger::new(facility).map(LogBackend::Syslog)
    }

    fn new_file_target(path: PathBuf) -> Result<LogBackend, Failed> {
        Ok(LogBackend::File {
            file: match Self::open_log_file(&path) {
                Ok(file) => file,
                Err(err) => {
                    error!(
                        "Failed to open log file '{}': {}",
                        path.display(), err
                    );
                    return Err(Failed)
                }
            },
            path
        })
    }

    /// Opens a log file.
    fn open_log_file(path: &PathBuf) -> Result<fs::File, io::Error> {
        fs::OpenOptions::new().create(true).append(true).open(path)
    }

    /// Configures the stderr target.
    fn new_stderr_target(timestamp: bool) -> LogBackend {
        LogBackend::Stderr {
            stderr: io::stderr(),
            timestamp,
        }
    }

    /// Logs a message.
    ///
    /// This method may exit the whole process if logging fails.
    fn log(&self, record: &log::Record) {
        if self.should_ignore(record) {
            return;
        }

        if let Some(output) = self.output.as_ref() {
            writeln!(output.lock(), "{}", record.args());
        }

        if let Err(err) = self.try_log(record) {
            self.log_failure(err);
        }
    }

    /// Tries logging a message and returns an error if there is one.
    fn try_log(&self, record: &log::Record) -> Result<(), io::Error> {
        match self.target.lock().deref_mut() {
            #[cfg(unix)]
            LogBackend::Syslog(ref mut logger) => logger.log(record),
            LogBackend::File { ref mut file, .. } => {
                writeln!(
                    file, "[{}] [{}] {}",
                    format_local_iso_date(chrono::Local::now()),
                    record.level(),
                    record.args()
                )
            }
            LogBackend::Stderr{ ref mut stderr, timestamp } => {
                // We never fail when writing to stderr.
                if *timestamp {
                    let _ = write!(stderr, "[{}] ",
                        format_local_iso_date(chrono::Local::now()),
                    );
                }
                let _ = writeln!(
                    stderr, "[{}] {}", record.level(), record.args()
                );
                Ok(())
            }
        }
    }

    /// Handles an error that happened during logging.
    fn log_failure(&self, err: io::Error) -> ! {
        // We try to write a meaningful message to stderr and then abort.
        match self.target.lock().deref() {
            #[cfg(unix)]
            LogBackend::Syslog(_) => {
                eprintln!("Logging to syslog failed: {err}. Exiting.");
            }
            LogBackend::File { ref path, .. } => {
                eprintln!(
                    "Logging to file {} failed: {}. Exiting.",
                    path.display(),
                    err
                );
            }
            LogBackend::Stderr { ..  } => {
                // We never fail when writing to stderr.
            }
        }
        process::exit(1)
    }

    /// Flushes the logging backend.
    fn flush(&self) {
        match self.target.lock().deref_mut() {
            #[cfg(unix)]
            LogBackend::Syslog(ref mut logger) => logger.flush(),
            LogBackend::File { ref mut file, .. } => {
                let _ = file.flush();
            }
            LogBackend::Stderr { ref mut stderr, .. } => {
                let _  = stderr.lock().flush();
            }
        }
    }

    /// Determines whether a log record should be ignored.
    ///
    /// This filters out messages by libraries that we don’t really want to
    /// see.
    fn should_ignore(&self, record: &log::Record) -> bool {
        let module = match record.module_path() {
            Some(module) => module,
            None => return false,
        };

        // log::Level sorts more important first.

        if record.level() > log::Level::Error {
            // From rustls, only log errors.
            if module.starts_with("rustls") {
                return true
            }
        }
        if self.log_level >= log::LevelFilter::Trace {
            // Don’t filter anything else if we are in trace.
            return false
        }

        // Ignore these modules unless INFO or more important.
        record.level() > log::Level::Info && (
               module.starts_with("tokio_reactor")
            || module.starts_with("hyper")
            || module.starts_with("reqwest")
            || module.starts_with("h2")
        )
    }

    /// Rotates the log target if necessary.
    ///
    /// This method exits the whole process when rotating fails.
    fn rotate(&self) -> Result<(), Failed> {
        if let LogBackend::File {
            ref mut file, ref path
        } = self.target.lock().deref_mut() {
            // This tries to open the file. If this fails, it writes a
            // message to both the old file and stderr and then exits.
            *file = match Self::open_log_file(path) {
                Ok(file) => file,
                Err(err) => {
                    let _ = writeln!(file,
                        "Re-opening log file {} failed: {}. Exiting.",
                        path.display(), err
                    );
                    eprintln!(
                        "Re-opening log file {} failed: {}. Exiting.",
                        path.display(), err
                    );
                    return Err(Failed)
                }
            }
        }
        Ok(())
    }
}


//------------ SyslogLogger --------------------------------------------------

/// A syslog logger.
///
/// This is essentially [`syslog::BasicLogger`] but that one keeps the logger
/// behind a mutex – which we already do – and doesn’t return error – which
/// we do want to see.
#[cfg(unix)]
struct SyslogLogger(
    syslog::Logger<syslog::LoggerBackend, syslog::Formatter3164>
);

#[cfg(unix)]
impl SyslogLogger {
    /// Creates a new syslog logger.
    fn new(facility: syslog::Facility) -> Result<Self, Failed> {
        let process = std::env::current_exe().ok().and_then(|path|
            path.file_name()
                .and_then(std::ffi::OsStr::to_str)
                .map(ToString::to_string)
        ).unwrap_or_else(|| String::from("routinator"));
        let formatter = syslog::Formatter3164 {
            facility,
            hostname: None,
            process,
            pid: std::process::id(),
        };
        let logger = syslog::unix(formatter.clone()).or_else(|_| {
            syslog::tcp(formatter.clone(), ("127.0.0.1", 601))
        }).or_else(|_| {
            syslog::udp(formatter, ("127.0.0.1", 0), ("127.0.0.1", 514))
        });
        match logger {
            Ok(logger) => Ok(Self(logger)),
            Err(err) => {
                error!("Cannot connect to syslog: {err}");
                Err(Failed)
            }
        }
    }

    /// Tries logging.
    fn log(&mut self, record: &log::Record) -> Result<(), io::Error> {
        match record.level() {
            log::Level::Error => self.0.err(record.args()),
            log::Level::Warn => self.0.warning(record.args()),
            log::Level::Info => self.0.info(record.args()),
            log::Level::Debug => self.0.debug(record.args()),
            log::Level::Trace => {
                // Syslog doesn’t have trace, use debug instead.
                self.0.debug(record.args())
            }
        }.map_err(|err| {
            match err {
                syslog::Error::Io(err) => err,
                err => io::Error::other(err),
            }
        })
    }

    /// Flushes the logger.
    ///
    /// Ignores any errors.
    fn flush(&mut self) {
        let _ = self.0.backend.flush();
    }
}


//------------ GlobalLogger --------------------------------------------------

/// The global logger.
///
/// A value of this type can go into a static. Until a proper logger is
/// installed, it just writes all log output to stderr.
struct GlobalLogger {
    /// The real logger. Can only be set once.
    inner: OnceLock<Logger>,
}

/// The static for the log crate.
static GLOBAL_LOGGER: GlobalLogger = GlobalLogger::new();

impl GlobalLogger {
    /// Creates a new provisional logger.
    const fn new() -> Self {
        GlobalLogger { inner: OnceLock::new() }
    }

    /// Switches to the proper logger.
    fn switch(&self, logger: Logger) {
        if self.inner.set(logger).is_err() {
            panic!("Tried to switch logger more than once.")
        }
    }

    /// Performs a log rotation.
    fn rotate(&self) -> Result<(), Failed> {
        match self.inner.get() {
            Some(logger) => logger.rotate(),
            None => Ok(()),
        }
    }
}


impl log::Log for GlobalLogger {
    fn enabled(&self, _: &log::Metadata<'_>) -> bool {
        true
    }

    fn log(&self, record: &log::Record<'_>) {
        match self.inner.get() {
            Some(logger) => logger.log(record),
            None => {
                let _ = writeln!(
                    io::stderr().lock(), "[{}] {}",
                    record.level(), record.args()
                );
            }
        }
    }

    fn flush(&self) {
        if let Some(logger) = self.inner.get() {
            logger.flush()
        }
    }
}


//------------ LogOutput -----------------------------------------------------

#[derive(Debug)]
pub struct LogOutput {
    queue: Arc<Mutex<String>>,
    current: RwLock<Bytes>,
}

impl LogOutput {
    fn new() -> (Arc<Mutex<String>>, Self) {
        let queue = Arc::new(Mutex::new(String::new()));
        let res = LogOutput {
            queue: queue.clone(),
            current: RwLock::new(
                "Initial validation ongoing. Please wait.".into(),
            )
        };
        (queue, res)
    }

    pub fn start(&self) {
        let new_string = format!(
            "Log from validation run started at {}\n\n",
            format_iso_date(Utc::now())
        );
        let _ = mem::replace(self.queue.lock().deref_mut(), new_string);
    }

    pub fn flush(&self) {
        let content = mem::take(self.queue.lock().deref_mut());
        *self.current.write() = content.into();
    }

    pub fn get_output(&self) -> Bytes {
        self.current.read().clone()
    }
}