【備忘録】Kong管理者向けAPIまとめ

# :8001

### サービス登録

##### Request

```bash
POST /services 
  --data "name=fuga" \
  --data "host=localhost" \
  --data "port=80"
```

##### Response

```json
{
    "host": "localhost",
    "created_at": 1555991214,
    "connect_timeout": 60000,
    "id": "dabb54f0-c5b0-43b8-9671-5d5223b7088e",
    "protocol": "http",
    "name": "fuga",
    "read_timeout": 60000,
    "port": 80,
    "path": null,
    "updated_at": 1555991214,
    "retries": 5,
    "write_timeout": 60000,
    "tags": null
}
```

### ルーティング登録

##### Request

```bash
POST /routes --data "protocols=http" --data "service.id=dabb54f0-c5b0-43b8-9671-5d5223b7088e" --data "paths=/fuga" 
```

##### Response

```json
{
    "updated_at": 1555990743,
    "created_at": 1555990743,
    "strip_path": true,
    "snis": null,
    "hosts": null,
    "name": null,
    "methods": null,
    "sources": null,
    "preserve_host": false,
    "regex_priority": 0,
    "service": {
        "id": "dabb54f0-c5b0-43b8-9671-5d5223b7088e"
    },
    "paths": [
        "/fuga"
    ],
    "destinations": null,
    "id": "3055b130-52d4-48ab-953c-739cab13ae0f",
    "protocols": [
        "http"
    ],
    "tags": null
}
```

### サービス一覧

```bash
GET /services
```

```json
{
    "next": null,
    "data": [
        {
            "host": "localhost",
            "created_at": 1555978112,
            "connect_timeout": 60000,
            "id": "452d7496-2ff0-4165-b66b-ca2369908855",
            "protocol": "http",
            "name": "hoge",
            "read_timeout": 60000,
            "port": 80,
            "path": "/hoge",
            "updated_at": 1555978112,
            "retries": 5,
            "write_timeout": 60000,
            "tags": null
        },
        {
            "host": "localhost",
            "created_at": 1555978167,
            "connect_timeout": 60000,
            "id": "dabb54f0-c5b0-43b8-9671-5d5223b7088e",
            "protocol": "http",
            "name": "fuga",
            "read_timeout": 60000,
            "port": 80,
            "path": "/fuga",
            "updated_at": 1555978167,
            "retries": 5,
            "write_timeout": 60000,
            "tags": null
        }
    ]
}
```

### ルーティング一覧

```bash
GET /routes
```

```json
{
    "next": null,
    "data": [
        {
            "updated_at": 1555990743,
            "created_at": 1555990743,
            "strip_path": true,
            "snis": null,
            "hosts": null,
            "name": null,
            "methods": null,
            "sources": null,
            "preserve_host": false,
            "regex_priority": 0,
            "service": {
                "id": "dabb54f0-c5b0-43b8-9671-5d5223b7088e"
            },
            "paths": [
                "/fuga"
            ],
            "destinations": null,
            "id": "3055b130-52d4-48ab-953c-739cab13ae0f",
            "protocols": [
                "http"
            ],
            "tags": null
        }
    ]
}
```

## プラグイン

### Key Authentication

#### サービスでの有効化

```bash
POST /services/{service.id|service.name}/plugins --data "name=key-auth"
```

#### Consumer作成

##### Request

```bash
POST /consumers --data "username=shigaki"
```

###### Response

```json
{
    "custom_id": null,
    "created_at": 1555978475,
    "id": "59f7ab81-fc83-4f06-abc7-25eb01f9389e",
    "tags": null,
    "username": "shigaki"
}
```

#### Key発行

##### Request

```bash
POST /consumers/shigaki/key-auth
```

##### Response

```json
{
    "key": "Px64k3MwiHXXxAaJqYcHydSzWcxZ0hn5",
    "created_at": 1555978780,
    "consumer": {
        "id": "59f7ab81-fc83-4f06-abc7-25eb01f9389e"
    },
    "id": "e0a2d410-6b2b-44bd-b234-cb878098195e"
}
```

### ACL

#### 有効化

##### Request

```bash
POST /services/{service}/plugins --data "name=acl" --data "config.whitelist=group1" --data "config.whitelist=group2"
```

### Rate Limiting

#### 有効化

##### Request

```bash
POST /services/fuga/plugins --data "name=rate-limiting" --data "config.second=5"
```

##### Response

```json
{
    "created_at": 1555984885,
    "config": {
        "minute": null,
        "policy": "cluster",
        "month": null,
        "redis_timeout": 2000,
        "limit_by": "consumer",
        "hide_client_headers": false,
        "second": 5,
        "day": null,
        "redis_password": null,
        "year": null,
        "redis_database": 0,
        "hour": null,
        "redis_port": 6379,
        "redis_host": null,
        "fault_tolerant": true
    },
    "id": "c689b7ec-34dd-4268-b7b6-1cd5e8addb83",
    "service": {
        "id": "dabb54f0-c5b0-43b8-9671-5d5223b7088e"
    },
    "name": "rate-limiting",
    "protocols": [
        "http",
        "https"
    ],
    "enabled": true,
    "run_on": "first",
    "consumer": null,
    "route": null,
    "tags": null
}
```

### プラグイン一覧

```bash
GET /plugins
```

```json
{
    "next": null,
    "data": [
        {
            "created_at": 1555978245,
            "config": {
                "key_names": [
                    "apikey"
                ],
                "run_on_preflight": true,
                "anonymous": null,
                "hide_credentials": false,
                "key_in_body": false
            },
            "id": "a4abc8e3-a72f-4856-85f8-982ffbc8ab66",
            "service": {
                "id": "dabb54f0-c5b0-43b8-9671-5d5223b7088e"
            },
            "name": "key-auth",
            "protocols": [
                "http",
                "https"
            ],
            "enabled": true,
            "run_on": "first",
            "consumer": null,
            "route": null,
            "tags": null
        },
        {
            "created_at": 1555984885,
            "config": {
                "minute": null,
                "policy": "cluster",
                "month": null,
                "redis_timeout": 2000,
                "limit_by": "consumer",
                "hide_client_headers": false,
                "second": 5,
                "day": null,
                "redis_password": null,
                "year": null,
                "redis_database": 0,
                "hour": null,
                "redis_port": 6379,
                "redis_host": null,
                "fault_tolerant": true
            },
            "id": "c689b7ec-34dd-4268-b7b6-1cd5e8addb83",
            "service": {
                "id": "dabb54f0-c5b0-43b8-9671-5d5223b7088e"
            },
            "name": "rate-limiting",
            "protocols": [
                "http",
                "https"
            ],
            "enabled": true,
            "run_on": "first",
            "consumer": null,
            "route": null,
            "tags": null
        }
    ]
}
```

### 参考文献

- https://docs.konghq.com/1.1.x/admin-api/
- https://docs.konghq.com/hub/kong-inc/key-auth/
- https://docs.konghq.com/hub/kong-inc/acl/
- https://docs.konghq.com/hub/kong-inc/rate-limiting/