본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Request Validator: 유효하지 않은 요청을 차단하지 않고 기록하기

원문 보기 위키 갱신

Request Validator: 유효하지 않은 요청을 차단하지 않고 기록하기 (Log invalid requests without blocking)

Request Validator 플러그인을 tap 모드로 실행하면 검증은 평소처럼 수행되지만 실패한 요청은 차단되지 않아요. 대신 Kong Gateway가 검증 실패 세부 정보를 경고 로그로 남기고 요청을 그대로 업스트림 서비스로 전달해요. 이 문서에서는 요청 본문에 name 필드가 있는지 확인하는 validator를 설정하고 enforcement_mode를 log_only로 지정해 실패 요청을 기록만 하는 방법을 안내해요

출처: 문서

본문

유효하지 않은 요청을 차단하지 않고 로그로 기록하기v3.16+

Request Validator 플러그인을 탭(tap) 모드로 실행하세요.

탭 모드에서는 검증이 평소처럼 진행되지만, 실패한 요청은 차단되지 않아요. 대신 Kong Gateway가 검증 세부 정보가 담긴 경고를 로그로 남기고, 요청을 변경하지 않은 채 업스트림 서비스로 프록시해요.

플러그인이 kong 밸리데이터를 사용해 요청 본문에 문자열 값을 가진 name 필드가 포함돼 있는지 검사하도록 구성하고, 검증에 실패한 요청이 차단되는 대신 로그로 기록되도록 config.enforcement_mode를 log_only로 설정하세요.

이 예제에서는 빈 본문({})이 변경 없이 그대로 프록시되며 200 응답이 반환돼요. Kong Gateway는 다음과 같은 로그를 남겨요:

[warn] request-validator: validation failed in log_only mode, validation_category=body_schema, {"message":"required field missing: name"}

When config.verbose_response is enabled, the log names the specific field that failed. When it's disabled, it logs a generic validation-failure message instead.

Set up the plugin

Global

decK

Add this section to your kong.yaml configuration file:

_format_version: "3.0"
plugins:
  - name: request-validator
    config:
      version: kong
      body_schema: '[{"name":{"type":"string","required":true}}]'
      enforcement_mode: log_only
      verbose_response: true

{: data-file="kong.yaml" data-tool="deck" }

Admin API

Make the following request:

curl -i -X POST http://localhost:8001/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "request-validator",
      "config": {
        "version": "kong",
        "body_schema": "[{\"name\":{\"type\":\"string\",\"required\":true}}]",
        "enforcement_mode": "log_only",
        "verbose_response": true
      }
    }
    '
Konnect API

Make the following request:

curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/plugins/ \
    --header "accept: application/json" \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $KONNECT_TOKEN" \
    --data '
    {
      "name": "request-validator",
      "config": {
        "version": "kong",
        "body_schema": "[{\"name\":{\"type\":\"string\",\"required\":true}}]",
        "enforcement_mode": "log_only",
        "verbose_response": true
      }
    }
    '

Make sure to replace the following placeholders with your own values:

  • region: Geographic region where your Kong Konnect is hosted and operates.

  • KONNECT_TOKEN: Your Personal Access Token (PAT) associated with your Konnect account.

  • controlPlaneId: The id of the control plane.

See the Konnect Control Planes Config API reference to learn about region-specific URLs and personal access tokens.

KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
  labels:
    global: 'true'
config:
  version: kong
  body_schema: '[{'name':{'type':'string','required':true}}]'
  enforcement_mode: log_only
  verbose_response: true
plugin: request-validator
" | kubectl apply -f -
Terraform
Prerequisite: Configure your Personal Access Token
terraform {
  required_providers {
    konnect = {
      source  = "kong/konnect"
    }
  }
}

provider "konnect" {
  personal_access_token = "$KONNECT_TOKEN"
  server_url            = "https://us.api.konghq.com/"
}

Add the following to your Terraform configuration to create a Konnect Gateway Plugin:

resource "konnect_gateway_plugin_request_validator" "my_request_validator" {
  enabled = true

  config = {
    version = "kong"
    body_schema = "[{\"name\":{\"type\":\"string\",\"required\":true}}]"
    enforcement_mode = "log_only"
    verbose_response = true
  }

  control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
}

Service

decK

Add this section to your kong.yaml configuration file:

_format_version: "3.0"
plugins:
  - name: request-validator
    service: serviceName|Id
    config:
      version: kong
      body_schema: '[{"name":{"type":"string","required":true}}]'
      enforcement_mode: log_only
      verbose_response: true

{: data-file="kong.yaml" data-tool="deck" }

Make sure to replace the following placeholders with your own values:

  • serviceName|Id: The id or name of the service the plugin configuration will target.
Admin API

Make the following request:

curl -i -X POST http://localhost:8001/services/{serviceName|Id}/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "request-validator",
      "config": {
        "version": "kong",
        "body_schema": "[{\"name\":{\"type\":\"string\",\"required\":true}}]",
        "enforcement_mode": "log_only",
        "verbose_response": true
      }
    }
    '

Make sure to replace the following placeholders with your own values:

  • serviceName|Id: The id or name of the service the plugin configuration will target.
Konnect API

Make the following request:

curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/services/{serviceId}/plugins/ \
    --header "accept: application/json" \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $KONNECT_TOKEN" \
    --data '
    {
      "name": "request-validator",
      "config": {
        "version": "kong",
        "body_schema": "[{\"name\":{\"type\":\"string\",\"required\":true}}]",
        "enforcement_mode": "log_only",
        "verbose_response": true
      }
    }
    '

Make sure to replace the following placeholders with your own values:

  • region: Geographic region where your Kong Konnect is hosted and operates.

  • KONNECT_TOKEN: Your Personal Access Token (PAT) associated with your Konnect account.

  • controlPlaneId: The id of the control plane.

  • serviceId: The id of the service the plugin configuration will target.

See the Konnect Control Planes Config API reference to learn about region-specific URLs and personal access tokens.

KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
config:
  version: kong
  body_schema: '[{'name':{'type':'string','required':true}}]'
  enforcement_mode: log_only
  verbose_response: true
plugin: request-validator
" | kubectl apply -f -

Next, apply the KongPlugin resource by annotating the service resource:

kubectl annotate -n kong service SERVICE_NAME konghq.com/plugins=
Terraform
Prerequisite: Configure your Personal Access Token
terraform {
  required_providers {
    konnect = {
      source  = "kong/konnect"
    }
  }
}

provider "konnect" {
  personal_access_token = "$KONNECT_TOKEN"
  server_url            = "https://us.api.konghq.com/"
}

Add the following to your Terraform configuration to create a Konnect Gateway Plugin:

resource "konnect_gateway_plugin_request_validator" "my_request_validator" {
  enabled = true

  config = {
    version = "kong"
    body_schema = "[{\"name\":{\"type\":\"string\",\"required\":true}}]"
    enforcement_mode = "log_only"
    verbose_response = true
  }

  control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
  service = {
    id = konnect_gateway_service.my_service.id
  }
}

Route

decK

Add this section to your kong.yaml configuration file:

_format_version: "3.0"
plugins:
  - name: request-validator
    route: routeName|Id
    config:
      version: kong
      body_schema: '[{"name":{"type":"string","required":true}}]'
      enforcement_mode: log_only
      verbose_response: true

{: data-file="kong.yaml" data-tool="deck" }

Make sure to replace the following placeholders with your own values:

  • routeName|Id: The id or name of the route the plugin configuration will target.
Admin API

Make the following request:

curl -i -X POST http://localhost:8001/routes/{routeName|Id}/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "request-validator",
      "config": {
        "version": "kong",
        "body_schema": "[{\"name\":{\"type\":\"string\",\"required\":true}}]",
        "enforcement_mode": "log_only",
        "verbose_response": true
      }
    }
    '

Make sure to replace the following placeholders with your own values:

  • routeName|Id: The id or name of the route the plugin configuration will target.
Konnect API

Make the following request:

curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/routes/{routeId}/plugins/ \
    --header "accept: application/json" \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $KONNECT_TOKEN" \
    --data '
    {
      "name": "request-validator",
      "config": {
        "version": "kong",
        "body_schema": "[{\"name\":{\"type\":\"string\",\"required\":true}}]",
        "enforcement_mode": "log_only",
        "verbose_response": true
      }
    }
    '

Make sure to replace the following placeholders with your own values:

  • region: Geographic region where your Kong Konnect is hosted and operates.

  • KONNECT_TOKEN: Your Personal Access Token (PAT) associated with your Konnect account.

  • controlPlaneId: The id of the control plane.

  • routeId: The id of the route the plugin configuration will target.

See the Konnect Control Planes Config API reference to learn about region-specific URLs and personal access tokens.

KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
config:
  version: kong
  body_schema: '[{'name':{'type':'string','required':true}}]'
  enforcement_mode: log_only
  verbose_response: true
plugin: request-validator
" | kubectl apply -f -

Next, apply the KongPlugin resource by annotating the httproute or ingress resource:

Gateway API
kubectl annotate -n kong httproute  konghq.com/plugins=
Ingress
kubectl annotate -n kong ingress  konghq.com/plugins=
Terraform
Prerequisite: Configure your Personal Access Token
terraform {
  required_providers {
    konnect = {
      source  = "kong/konnect"
    }
  }
}

provider "konnect" {
  personal_access_token = "$KONNECT_TOKEN"
  server_url            = "https://us.api.konghq.com/"
}

Add the following to your Terraform configuration to create a Konnect Gateway Plugin:

resource "konnect_gateway_plugin_request_validator" "my_request_validator" {
  enabled = true

  config = {
    version = "kong"
    body_schema = "[{\"name\":{\"type\":\"string\",\"required\":true}}]"
    enforcement_mode = "log_only"
    verbose_response = true
  }

  control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
  route = {
    id = konnect_gateway_route.my_route.id
  }
}

Consumer

decK

Add this section to your kong.yaml configuration file:

_format_version: "3.0"
plugins:
  - name: request-validator
    consumer: consumerName|Id
    config:
      version: kong
      body_schema: '[{"name":{"type":"string","required":true}}]'
      enforcement_mode: log_only
      verbose_response: true

{: data-file="kong.yaml" data-tool="deck" }

Make sure to replace the following placeholders with your own values:

  • consumerName|Id: The id or name of the consumer the plugin configuration will target.
Admin API

Make the following request:

curl -i -X POST http://localhost:8001/consumers/{consumerName|Id}/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "request-validator",
      "config": {
        "version": "kong",
        "body_schema": "[{\"name\":{\"type\":\"string\",\"required\":true}}]",
        "enforcement_mode": "log_only",
        "verbose_response": true
      }
    }
    '

Make sure to replace the following placeholders with your own values:

  • consumerName|Id: The id or name of the consumer the plugin configuration will target.
Konnect API

Make the following request:

curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/consumers/{consumerId}/plugins/ \
    --header "accept: application/json" \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $KONNECT_TOKEN" \
    --data '
    {
      "name": "request-validator",
      "config": {
        "version": "kong",
        "body_schema": "[{\"name\":{\"type\":\"string\",\"required\":true}}]",
        "enforcement_mode": "log_only",
        "verbose_response": true
      }
    }
    '

Make sure to replace the following placeholders with your own values:

  • region: Geographic region where your Kong Konnect is hosted and operates.

  • KONNECT_TOKEN: Your Personal Access Token (PAT) associated with your Konnect account.

  • controlPlaneId: The id of the control plane.

  • consumerId: The id of the consumer the plugin configuration will target.

See the Konnect Control Planes Config API reference to learn about region-specific URLs and personal access tokens.

KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
config:
  version: kong
  body_schema: '[{'name':{'type':'string','required':true}}]'
  enforcement_mode: log_only
  verbose_response: true
plugin: request-validator
" | kubectl apply -f -

Next, apply the KongPlugin resource by annotating the KongConsumer resource:

kubectl annotate -n kong kongconsumer CONSUMER_NAME konghq.com/plugins=
Terraform
Prerequisite: Configure your Personal Access Token
terraform {
  required_providers {
    konnect = {
      source  = "kong/konnect"
    }
  }
}

provider "konnect" {
  personal_access_token = "$KONNECT_TOKEN"
  server_url            = "https://us.api.konghq.com/"
}

Add the following to your Terraform configuration to create a Konnect Gateway Plugin:

resource "konnect_gateway_plugin_request_validator" "my_request_validator" {
  enabled = true

  config = {
    version = "kong"
    body_schema = "[{\"name\":{\"type\":\"string\",\"required\":true}}]"
    enforcement_mode = "log_only"
    verbose_response = true
  }

  control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
  consumer = {
    id = konnect_gateway_consumer.my_consumer.id
  }
}

관련 리소스

더 알아보기 (Learn more)