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