tjtjtjのメモ

自分のためのメモです

kong enabling-plugins

kongグにプラグインを追加する方法を学ぶ

ここを読みながら

1. Configure the key-auth plugin

プラグイン登録前確認

$ curl -i -X GET \
  --url http://localhost:8001/services/example-service/plugins/

HTTP/1.1 200 OK
Date: Tue, 24 Sep 2019 10:27:23 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/1.3.0
Content-Length: 23

{"next":null,"data":[]}

プラグイン key-auth 登録

$ curl -i -X POST \
  --url http://localhost:8001/services/example-service/plugins/ \
  --data 'name=key-auth'

HTTP/1.1 201 Created
Date: Tue, 24 Sep 2019 10:29:03 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/1.3.0
Content-Length: 380

{
  "created_at":1569320942,
  "consumer":null,
  "id":"fc1d2fd5-b892-4e44-a364-5b95dcd008ad",
  "service":{"id":"ae3c5ee9-3d44-49bc-8869-1f7f0f58afc1"},
  "name":"key-auth",
  "config":{"key_in_body":false,"key_names":["apikey"],"anonymous":null,"hide_credentials":false,"run_on_preflight":true},
  "route":null,
  "run_on":"first",
  "tags":null,
  "protocols":["grpc","grpcs","http","https"],
  "enabled":true
}

プラグイン登録後確認

$ curl -sS http://localhost:8001/services/example-service/plugins/

{
  "next": null,
  "data": [
    {
      "created_at": 1569320942,
      "consumer": null,
      "id": "fc1d2fd5-b892-4e44-a364-5b95dcd008ad",
      "service": {
        "id": "ae3c5ee9-3d44-49bc-8869-1f7f0f58afc1"
      },
      "enabled": true,
      "name": "key-auth",
      "protocols": [
        "grpc",
        "grpcs",
        "http",
        "https"
      ],
      "run_on": "first",
      "tags": null,
      "route": null,
      "config": {
        "key_in_body": false,
        "run_on_preflight": true,
        "anonymous": null,
        "hide_credentials": false,
        "key_names": [
          "apikey"
        ]
      }
    }
  ]
}

2. Verify that the plugin is properly configured

プラグイン key-auth が有効に働いているか確認

$ curl -i -X GET \
  --url http://localhost:8000/ \
  --header 'Host: example.com' | jq .

HTTP/1.1 401 Unauthorized
:
Server: kong/1.3.0

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

まとめ

  • サービス:example-service にプラグイン:key-auth を紐づけた
  • 紐づけ後、サービス:example-service はapikeyが必要になったようだ