cidrContains() function
출처: 문서
본문
cidrContains() 함수
cidrContains() 함수는 IP 주소가 지정된 CIDR(Classless Inter-Domain Routing) 블록 또는 CIDR 블록 범위 안에 포함되어 있는지 확인하는 데 사용해요. 이 함수는 두 개 이상의 인수를 받아요:
-
첫 번째 인수는 JSON 포인터로, 확인하려는 IP 주소가 담긴 필드의 키나 경로를 나타내요. IPv4와 IPv6 주소 형식을 모두 지원해요.
-
이후의 인수들은 하나 이상의 CIDR 블록이나 IP 주소 범위를 나타내는 문자열이에요. 함수는 첫 번째 인수로 지정된 IP 주소가 이 CIDR 블록들 중 하나와 일치하거나 그 안에 포함되는지 확인해요.
예를 들어, 데이터에 client.ip라는 이름의 IP 주소 필드가 있고 이 필드가 192.168.0.0/16 또는 10.0.0.0/8 CIDR 블록에 속하는지 확인하고 싶다면, 다음과 같이 cidrContains() 함수를 사용할 수 있어요:
cidrContains('/client.ip', '192.168.0.0/16', '10.0.0.0/8')
{% include copy.html %}
이 함수는 IP 주소가 지정된 CIDR 블록 중 하나와 일치하면 true를, 일치하지 않으면 false를 반환해요.
예시
다음 파이프라인은 지정된 CIDR 블록에 속하지 않는 문서를 모두 폐기해요:
cidr-allowlist-pipeline:
source:
http:
path: /events
ssl: true
sslKeyCertChainFile: certs/dp.crt
sslKeyFile: certs/dp.key
processor:
- drop_events:
# Drop events whose client IP is NOT in specific CIDR allowlist
drop_when: 'not cidrContains(/client/ip, "10.0.0.0/8", "192.168.0.0/16", "fd00::/8")'
sink:
- opensearch:
hosts: ["https://opensearch:9200"]
insecure: true
username: admin
password: admin_pass
index_type: custom
index: logs-%{yyyy.MM.dd}
{% include copy.html %}
다음 명령어로 이 파이프라인을 테스트할 수 있어요:
curl -ksS -X POST "https://localhost:2021/events" \
-H "Content-Type: application/json" \
-d '[
{"client":{"ip":"10.23.45.6"},"msg":"allowed 10/8"},
{"client":{"ip":"8.8.8.8"},"msg":"should be dropped"},
{"client":{"ip":"fd00::1234"},"msg":"allowed ULA IPv6"}
]'
{% include copy.html %}
OpenSearch에 저장된 문서에는 다음과 같은 정보가 담겨 있어요:
{
...
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "logs-2025.10.14",
"_id": "Ng1i4pkBLPEKXekW48BU",
"_score": 1,
"_source": {
"client": {
"ip": "10.23.45.6"
},
"msg": "allowed 10/8"
}
},
{
"_index": "logs-2025.10.14",
"_id": "Nw1i4pkBLPEKXekW48BU",
"_score": 1,
"_source": {
"client": {
"ip": "fd00::1234"
},
"msg": "allowed ULA IPv6"
}
}
]
}
}