Kong api gateway 설정 – 2

Kong api gateway 설정 – 2

여기서는 플러그인 활성화 하고 적용 및 테스트 하는 과정을 기록합니다.

첫번째 내용은 여기에..
Kong api gateway 설정 – 1 | 9to5의 개발하면서 겪은 경험

인증 auth


key auth

이제 인증 플러그인을 활성화 시켜보겠습니다.

Plugins – Key Authentication | Kong – Open-Source API Management and Microservice Management

Enable auth plugin on the service

curl -X POST http://localhost:8001/services/mocha/plugins \
  --data "name=key-auth"

Enable auth plugin on the routes

Routes ID 가 필요합니다.
ID 확인이 필요하면,

curl http://localhost:8001/routes

플러그인 활성화

curl -X POST http://localhost:8001/routes/cbaf40b1-196f-4e2b-9b47-b39d84c681a4/plugins \
    --data "name=key-auth" 

Create a Consumer

curl -X POST http://localhost:8001/consumers/ \
  --data "username=admin"

Create a Key

curl -X POST http://localhost:8001/consumers/admin/key-auth

Output:

{"id":"7026e178-9ab7-4bcc-a76a-2ab6b023b84d","created_at":1531989664000,"key":"fqS0sTQ9jM9qjlHNapnzFTJWfI5KGo8P","consumer_id":"69f5420a-6712-4ca2-8bc9-dcc964ebb4e4"}

이제 키 생성이 완료됐습니다.
위에서 등록한 api server 를 다시 호출 해봅시다.

curl -i -X GET \
  --url http://localhost:8000/ \
  --header 'Host: mocha1'

Output:

HTTP/1.1 401 Unauthorized
Date: Thu, 19 Jul 2018 08:43:33 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
WWW-Authenticate: Key realm="kong"
Server: kong/0.13.1

{"message":"No API key found in request"}

이제 plugin 이 enable 되어서 키 없이는 api 가 호출 되지 않습니다.

키를 넣고 다시 호출 합니다.

curl -i -X GET \
  --url http://localhost:8000/ \
  --header 'apikey: fqS0sTQ9jM9qjlHNapnzFTJWfI5KGo8P' \
  --header 'Host: mocha1'

Response code 200 확인 했습니다.

ldap

Plugins – LDAP Authentication | Kong – Open-Source API Management and Microservice Management

curl -X POST http://localhost:8001/routes/cbaf40b1-196f-4e2b-9b47-b39d84c681a4/plugins \
    --data "name=ldap-auth"  \
    --data "config.hide_credentials=true" \
    --data "config.ldap_host=ldap.example.com" \
    --data "config.ldap_port=389" \
    --data "config.start_tls=false" \
    --data "config.base_dn=dc=example,dc=com" \
    --data "config.verify_ldap_host=false" \
    --data "config.attribute=cn" \
    --data "config.cache_ttl=60" \
    --data "config.header_type=ldap

Allow multiple authentication methods per API and/or Consumer · Issue #590 · Kong/kong · GitHub

Kong의 ldap plugin 은 현재까지는 bind_dn을 입력할 수 가 없습니다.

아래 처럼 PR이 들어가 있는거 같긴 한데… 진행되고 있진 않는 듯합니다.
ldap_auth add bind+search feature by daurnimator · Pull Request #3093 · Kong/kong · GitHub

회사 ldap은 거의 bind_dn 을 사용할 것으로 보기 때문에 과감히 버려줍시다…

로그 logging

syslog 나 tcp-log 는 docker 상태에서는 동작하지 않는 것으로 보입니다.

해서, file-log plugin 을 enable해서 사용하는게 가장 간단한 것으로 보입니다.

file-log

file-log를 사용하기 위해서는 nginx 실행시 user 를 root 로 하는 것이 가장 간단 합니다. 아니면 아래의 에러를 보기 쉽상…..

[error] 76#0: [lua] handler.lua:56: [file-log] failed to open the file: Permission denied, context: ngx.timer, client: xxx.xxx.xxx.xxx, server: 0.0.0.0:8000

첫 번째 글에서 kong 을 docker 로 실행할 때 몇 개의 environment variables 이 추가 되어야합니다.

docker run -d --name kong \
    --link kong-database:kong-database \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=kong-database" \
    -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
    -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
    -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
    -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
    -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
    -e "KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444" \
    -e "KONG_LOG_LEVEL=info" \
    -e "KONG_NGINX_USER=root" \
    -p 8000:8000 \
    -p 8443:8443 \
    -p 8001:8001 \
    -p 8444:8444 \
    kong:0.14.0-alpine

위 명령대로 다시 kong 을 실행하면 모든 로그가 docker logs 로 리다이렉트 되고, file-log를 활성화 시킬 수 있는 요건을 갖추게 됩니다.

plugin 활성화

  • services
curl -X POST http://localhost:8001/services/c5414169-73f8-4220-881a-0fb09a12973a/plugins \
    --data "name=file-log"  \
    --data "config.path=/dev/stdout"
  • plugins
curl -X POST http://localhost:8001/routes/a57bf14c-4c77-4990-8709-fb94ce006d2b/plugins \
    --data "name=file-log"  \
    --data "config.path=/dev/stdout"

로그 확인

docker logs -f --tail 100 kong

위 명령어로 도커 로그를 확인하면 이제 정상적으로 api 호출시에 로그가 찍히는 것을 확인 할 수 있습니다.