微服務之API閘道器:Kong:外掛介紹:認證外掛ip-restriction之黑白名單
阿新 • • 發佈:2019-01-21
kong目前提供了37個外掛,其中商業收費7個,30個開源免費的外掛,可以設定到api/服務/路由粒度上。
環境設定
外掛功能
類別 | 免費/收費 | name | 外掛名 | 使用場景 |
---|---|---|---|---|
認證 | 免費 | basic-auth | Basic Authentication | 對於服務或者路由提供使用者名稱/密碼基本認證機制 |
認證 | 免費 | key-auth | Key Authentication | 對於服務或者路由提供用關鍵字認證機制 |
認證 | 收費 | - | OpenID Connect | 提供與三方OpenID的整合方式 |
認證 | 免費 | oauth2 | OAuth 2.0 Authentication | 新增OAuth 2.0認證 |
認證 | 收費 | - | OAuth 2.0 Introspection | 提供與三方OAuth 2.0認證伺服器的整合方式 |
認證 | 免費 | hmac-auth | HMAC Authentication | 提供HMAC(Hashed Message Authentication Code)簽名認證方式 |
認證 | 免費 | jwt | JWT | 提供JWT(JSON WEB Token)的認證方式 |
認證 | 免費 | ldap-auth | LDAP Authentication | 提供了與LDAP認證伺服器的整合方式 |
安全 | 免費 | acl | ACL | 通過ACL(Access Control List)的組名稱對服務或者路由進行黑白名單的訪問控制 |
安全 | 免費 | cors | CORS | 對服務或者路由提供CORS支援 |
安全 | 免費 | ip-restriction | IP Restriction | 通過IP地址對服務或者路由進行黑白名單的訪問控制 |
安全 | 免費 | bot-detection | Bot Detection | 對服務或者路由提供機器人檢出並能進行黑白名單的設定 |
安全 | 免費 | ssl | Dynamic SSL | 對路由或者API提供ssl支援 |
流量控制 | 收費 | - | EE Canary Release | 提供金絲雀釋出支援 |
流量控制 | 收費 | - | EE Forward Proxy | 提供企業內網中前向代理的支援 |
流量控制 | 收費 | - | EE Proxy Caching | 提供代理快取的加速功能 |
流量控制 | 免費 | request-size-limiting | Request Size Limiting | 可以對請求的body的size進行限制 |
流量控制 | 免費 | rate-limiting | Rate Limiting | 提供對給定時間段HTTP請求數目的控制功能 |
流量控制 | 收費 | - | EE Rate Limiting | CE版本Rate Limiting的強化 |
流量控制 | 免費 | response-ratelimiting | Response Rate Limiting | 基於使用者響應Header的資訊,提供對HTTP請求數目的控制功能 |
流量控制 | 免費 | request-termination | Request Termination | 根據響應的狀態碼和資訊,可停止對某個服務或者路由的流量 |
Serverless | 免費 | aws-lambda | AWS Lambda | 呼叫和管理AWS Lambda函式 |
Serverless | 免費 | openwhisk | Apache OpenWhisk | 呼叫和管理OpenWhisk |
分析&監控 | 免費 | zipkin | Zipkin | 提供相容zipkin的方式對使用者請求進行追蹤 |
分析&監控 | 免費 | datadog | Datadog | 可將API的指標資訊在datadog上進行視覺化地展示 |
分析&監控 | 免費 | runscope | Runscope | 結合runscope進行API效能測試和監控 |
轉換 | 免費 | request-transformer | Request Transformer | 通過kong對請求進行轉換 |
轉換 | 收費 | - | EE request Transformer | request Transformer的強化版 |
轉換 | 免費 | response-transformer | Response Transformer | 通過kong對響應進行轉換 |
轉換 | 免費 | correlation-id | Correlation ID | 通過kong實現請求和響應之間的關聯 |
日誌 | 免費 | tcp-log | TCP | 傳送請求和響應日誌到TCP伺服器 |
日誌 | 免費 | udp-log | UDP | 傳送請求和響應日誌到UDP伺服器 |
日誌 | 免費 | http-log | HTTP | 傳送請求和響應日誌到HTTP伺服器 |
日誌 | 免費 | file-log | File | 傳送請求和響應日誌到磁碟的檔案中 |
日誌 | 免費 | statsd | StatsD | 傳送請求和響應日誌到StatsD伺服器 |
日誌 | 免費 | syslog | Syslog | 傳送請求和響應日誌到syslog中 |
日誌 | 免費 | syslog | Loggly | 傳送請求和響應日誌到Loggly伺服器 |
api資訊
[root@kong ~]# curl http://localhost:8001/apis
{"total":2,"data":[{"created_at":1527303008505,"strip_uri":true,"id":"28513d56-43d7-49f2-9e64-9a7a0698e24f","hosts":["userhost"],"name":"userapi","http_if_terminated":false,"https_only":false,"retries":5,"preserve_host":false,"upstream_connect_timeout":60000,"upstream_read_timeout":60000,"upstream_send_timeout":60000,"upstream_url":"http:\/\/192.168.163.117:9001\/"},{"created_at":1527303019963,"strip_uri":true,"id":"992b9c3b-889a-4c15-94b5-561ac9fb1cc9","hosts":["orghost"],"name":"orgapi","http_if_terminated":false,"https_only":false,"retries":5,"preserve_host":false,"upstream_connect_timeout":60000,"upstream_read_timeout":60000,"upstream_send_timeout":60000,"upstream_url":"http:\/\/192.168.163.117:9002\/"}]}
[root@kong ~]#
設定ip-restriction外掛
事前外掛資訊確認
[root@kong ~]# curl http://localhost:8001/plugins
{"total":0,"data":[]}
[root@kong ~]#
設定ip-restriction外掛
[root@kong ~]# curl -X POST http://localhost:8001/apis/userapi/plugins \
> --data "name=ip-restriction" \
> --data "config.blacklist=172.17.0.3/16"
{"created_at":1527303305000,"config":{"blacklist":["172.17.0.3\/16"]},"id":"3341a3f8-1d87-4960-9da1-232718d2b89d","name":"ip-restriction","api_id":"28513d56-43d7-49f2-9e64-9a7a0698e24f","enabled":true}
[root@kong ~]#
設定後外掛資訊確認
[root@kong ~]# curl http://localhost:8001/plugins
{"total":1,"data":[{"created_at":1527303305000,"config":{"blacklist":["172.17.0.3\/16"]},"id":"3341a3f8-1d87-4960-9da1-232718d2b89d","name":"ip-restriction","api_id":"28513d56-43d7-49f2-9e64-9a7a0698e24f","enabled":true}]}
[root@kong ~]#
blacklist確認
由於設定blacklist為172.17.0.3/16,所以IP為172.17.0.3和172.17.0.2的兩個容器內都被限制了訪問,從如下的容器中得到了確認
172.17.0.3被限制
[root@kong ~]# docker exec -it trusting_liskov sh
/ # ip ad |grep 172
inet 172.17.0.3/16 scope global eth0
/ #
/ # curl -H 'Host: userhost' http://192.168.163.117:8000
{"message":"Your IP address is not allowed"}
/ #
172.17.0.2被限制
[root@kong ~]# docker exec -it optimistic_stallman sh
/ # ip ad |grep 172
inet 172.17.0.2/16 scope global eth0
/ #
/ # curl -H 'Host: userhost' http://192.168.163.117:8000
{"message":"Your IP address is not allowed"}
/ #
192.168.163.117可正常訪問
[root@kong ~]# curl -H 'Host: userhost' http://192.168.163.117:8000
Hello, Service :User Service
[root@kong ~]# ip ad |grep 192
inet 192.168.163.117/24 brd 192.168.163.255 scope global enp0s3
[root@kong ~]#