asterust-junos 0.5.12

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `host`
//! `system { syslog { host <address> { ... } } }`.
//!
//! # See also
//!
//! - [host (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/host-edit-system.html)
//! - [tlsdetails](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tlsdetails.html)
//! - [transport (System Syslog)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-syslog-host-transport.html)
//! - [match-strings](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/match-string-edit-system-syslog-file.html)

use super::{syslog_rules, SyslogRule};
use crate::{arg_of, args_of, child_block, has_leaf, list_values, named_children};
use asterust::config::ConfigLine;

/// The `tlsdetails` block of a syslog host: how the TLS connection is authenticated.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SyslogTlsDetails {
    pub local_certificate: Option<String>,
    pub trusted_ca_group: Option<String>,
    /// The `ca-profiles` of the trusted CA group.
    pub ca_profiles: Vec<String>,
}

/// A `host <address>` destination.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SyslogHost {
    /// Address, hostname, or `other-routing-engine`.
    pub address: String,
    pub rules: Vec<SyslogRule>,
    pub port: Option<String>,
    pub source_address: Option<String>,
    pub routing_instance: Option<String>,
    pub facility_override: Option<String>,
    pub log_prefix: Option<String>,
    pub allow_duplicates: bool,
    pub explicit_priority: bool,
    pub exclude_hostname: bool,
    /// The regular expression from `match`.
    pub match_regex: Option<String>,
    /// The substrings from `match-strings`; a message is logged if it contains any of them.
    pub match_strings: Vec<String>,
    /// `tcp`, `tls` or `udp`.
    pub transport: Option<String>,
    pub tls_details: Option<SyslogTlsDetails>,
}

pub(super) fn parse_host(address: &str, host: &ConfigLine) -> SyslogHost {
    SyslogHost {
        address: address.to_string(),
        rules: syslog_rules(host),
        port: arg_of(host, "port").map(str::to_string),
        source_address: arg_of(host, "source-address").map(str::to_string),
        routing_instance: arg_of(host, "routing-instance").map(str::to_string),
        facility_override: arg_of(host, "facility-override").map(str::to_string),
        log_prefix: arg_of(host, "log-prefix").map(str::to_string),
        allow_duplicates: has_leaf(host, "allow-duplicates"),
        explicit_priority: has_leaf(host, "explicit-priority"),
        exclude_hostname: has_leaf(host, "exclude-hostname"),
        match_regex: arg_of(host, "match").map(str::to_string),
        match_strings: args_of(host, "match-strings")
            .into_iter()
            .flat_map(list_values)
            .collect(),
        transport: arg_of(host, "transport").map(str::to_string),
        tls_details: child_block(host, "tlsdetails").map(|tls| {
            let group = named_children(tls, "trusted-ca-group").next();
            SyslogTlsDetails {
                local_certificate: arg_of(tls, "local-certificate").map(str::to_string),
                trusted_ca_group: group.map(|(name, _)| name.to_string()),
                ca_profiles: group
                    .map(|(_, block)| {
                        args_of(block, "ca-profiles")
                            .into_iter()
                            .flat_map(list_values)
                            .collect()
                    })
                    .unwrap_or_default(),
            }
        }),
    }
}