vector_core/
ipallowlist.rs

1use serde::{Deserialize, Serialize};
2use std::cell::RefCell;
3use vector_config::GenerateError;
4
5use ipnet::IpNet;
6use vector_config::{configurable_component, Configurable, Metadata, ToValue};
7use vector_config_common::schema::{InstanceType, SchemaGenerator, SchemaObject};
8
9/// List of allowed origin IP networks. IP addresses must be in CIDR notation.
10#[configurable_component]
11#[derive(Clone, Debug, PartialEq, Eq)]
12#[serde(deny_unknown_fields, transparent)]
13#[configurable(metadata(docs::human_name = "Allowed IP network origins"))]
14#[configurable(metadata(docs::examples = "ip_allow_list_example()"))]
15pub struct IpAllowlistConfig(pub Vec<IpNetConfig>);
16
17const fn ip_allow_list_example() -> [&'static str; 4] {
18    [
19        "192.168.0.0/16",
20        "127.0.0.1/32",
21        "::1/128",
22        "9876:9ca3:99ab::23/128",
23    ]
24}
25
26/// IP network
27#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(deny_unknown_fields, transparent)]
29pub struct IpNetConfig(pub IpNet);
30
31impl ToValue for IpNetConfig {
32    fn to_value(&self) -> serde_json::Value {
33        serde_json::Value::String(self.0.to_string())
34    }
35}
36
37impl Configurable for IpNetConfig {
38    fn generate_schema(
39        _: &RefCell<SchemaGenerator>,
40    ) -> std::result::Result<SchemaObject, GenerateError> {
41        Ok(SchemaObject {
42            instance_type: Some(InstanceType::String.into()),
43            ..Default::default()
44        })
45    }
46
47    fn metadata() -> Metadata {
48        Metadata::with_description("IP network")
49    }
50}
51
52impl From<IpAllowlistConfig> for Vec<IpNet> {
53    fn from(value: IpAllowlistConfig) -> Self {
54        value.0.iter().map(|net| net.0).collect()
55    }
56}