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

Kong 세션 생성 시 알림 보내기

원문 보기 위키 갱신

Kong 세션 생성 시 알림 보내기 (Event Hooks)

Kong에서 세션이 생성될 때 알림을 받고 싶으신가요? Event Hooks를 사용하면 sessions 엔티티에 CRUD 이벤트가 발생할 때 웹훅 엔드포인트로 알림을 보낼 수 있어요. Admin API로 사용 가능한 이벤트 훅 소스와 sessions 스키마를 먼저 확인한 다음, source는 crud, event는 sessions로 지정하고 webhook-custom 핸들러가 여러분의 엔드포인트를 가리키도록 이벤트 훅을 만들면 돼요

출처: 문서

본문

TL;DR

Kong에서 세션이 생성될 때 알림을 보낼 수 있나요? 네. sessions 엔티티에 대한 CRUD 이벤트가 발생하면 Kong Event Hooks를 사용해 웹훅 엔드포인트로 알림을 보내세요. Admin API를 통해 사용 가능한 이벤트 훅 소스와 sessions 스키마를 확인한 다음, source: crud, event: sessions, 그리고 여러분의 엔드포인트를 가리키는 webhook-custom 핸들러로 이벤트 훅을 만드세요.

Kong 세션이 생성될 때 알림 보내기

인가된 세션이 생성될 때 알림을 보내도록 Kong을 설정할 수 있을까요? 예를 들어, Kong Manager에 로그인할 때요?

Kong Event Hooks를 사용하면 웹훅 엔드포인트로 알림을 보낼 수 있어요. Kong Event Hooks에 대한 자세한 내용은 아래 문서 링크에서 확인할 수 있어요.

이 요구 사항을 위해 어떤 이벤트 훅 소스를 사용할 수 있는지 확인해 볼 수 있어요. 특히 우리는 sessions 엔티티에 대한 CRUD 이벤트에 관심이 있어요:

curl -sk -X GET 'https://api.kong.lan:8444/event-hooks/sources' -H 'Kong-Admin-Token: password' | jq '.data.crud.sessions'
{
  "fields": [
    "operation",
    "entity",
    "old_entity",
    "schema"
  ]
}

위 결과에서 이벤트의 소스는 crud이고 이벤트는 sessions를 위한 것임을 알 수 있어요. 또한 웹훅 알림에 사용할 수 있는 필드가 operation, entity, old_entity, schema라는 것도 볼 수 있어요.

그런데 sessions 엔티티에는 어떤 파라미터가 있을까요? 이 정보를 위해 sessions 엔티티에 대해 /schemas 엔드포인트로 요청을 보낼 수 있어요:

curl -sk -X GET 'https://api.kong.lan:8444/schemas/sessions' -H 'Kong-Admin-Token: password' | jq
{
  "fields": [
    {
      "id": {
        "uuid": true,
        "auto": true,
        "len_min": 1,
        "type": "string",
        "description": "A string representing a UUID (universally unique identifier)."
      }
    },
    {
      "session_id": {
        "required": true,
        "len_min": 1,
        "unique": true,
        "type": "string"
      }
    },
    {
      "expires": {
        "type": "integer"
      }
    },
    {
      "data": {
        "type": "string",
        "len_min": 1
      }
    },
    {
      "created_at": {
        "type": "integer",
        "timestamp": true,
        "auto": true,
        "description": "An integer representing an automatic Unix timestamp in seconds."
      }
    },
    {
      "ttl": {
        "type": "number",
        "between": [
          0,
          100000000
        ],
        "description": "Time-to-live value for data"
      }
    }
  ],
  "entity_checks": []
}

이제 이벤트 훅에 필요한 모든 정보를 갖췄어요. 다음과 같은 Admin API 호출로 이벤트 훅을 만들 수 있어요:

curl -s -X POST 'https://api.kong.lan:8444/event-hooks' \
-H 'content-type: application/json' \
-H 'Kong-Admin-Token: password' \
--data-raw '{
	"source": "crud",
	"event": "sessions",
	"handler": "webhook-custom",
	"config": {
		"body_format": true,
		"method": "POST",
		"payload_format": true,
		"headers_format": false,
		"payload": {
			"text": "webhook-custom for sessions : operation {{ operation }}, entity.session_id {{ entity.session_id }}, entity.expires {{ entity.expires }}, entity.data {{ entity.data }}, entity.created_at {{ entity.created_at }} schema {{ schema }}"
		},
		"url": "<webhook_endpoint>",
		"headers": {
			"content-type": "application/json"
		},
		"ssl_verify": false
	}
}'

관련 리소스

더 알아보기 (Learn more)