[Kong] basic-auth基本認證及ACL鑒權



在上篇文章《key-auth實現對API請求的密鑰認證》,簡單學習了如何對API做一個訪問認證。

本文將通過配置basic-auth插件,繼續對Kong的學習。

basic-auth

Basic插件和key-auth插件的啟用基本相同, 都可以安裝配置在Service或者Route上。

所有插件都可以使用 http://kong_ip:8001/plugins/ 進行通用配置。這是與任何服務、路由或消費者都無關的插件,它是“公共的”,並且將在每個請求上運行
下面是basic-auth插件可配置參數列表:

形式參數 默認值 描述
name 插件名稱, 這里是 basic-auth
service_id 插件針對的服務ID
route_id 插件針對的路由ID
enabled true 插件是否可用
api_id 插件針對的API模塊
config.hide_credentials(可選) false 可選的布爾值,告訴插件從上游服務中顯示或隱藏憑證. 如果為"true",插件將在代理之前將憑據從請求中剝離(例如:Authorization 頭部)
config.anonymous(可選) 可選字符串,如果身份驗證失敗,該值(消費者uuid)用作“匿名”使用者。 如果空(默認),請求失敗將提示認證失敗“4xx”。請注意,這個值必須是指向消費者 id屬性, 而不是 它的 custom_id.

任何具有有效憑證的用戶都可以訪問Kong API。要限制只有部分經過身份驗證的用戶,還可以添加ACL插件,並創建白名單或黑名單用戶組(下節知識)。

本文我們將使用 之前添加的route 進行測試,另外現有的認證插件請先禁用,防止影響測試。route及其對應service信息如下:

{
  "updated_at": 1577246121,
  "created_at": 1577246121,
  "strip_path": false,
  "snis": null,
  "hosts": [
    "example.com"
  ],
  "name": "test-plugin-basic_auth",
  "methods": [
    "GET",
    "POST"
  ],
  "sources": null,
  "preserve_host": false,
  "regex_priority": 0,
  "service": {
    "host": "mockbin.org",
    "created_at": 1575882610,
    "connect_timeout": 60000,
    "id": "faa7761e-8883-4762-a7bf-d8df80768db1",
    "protocol": "http",
    "name": "example-service",
    "read_timeout": 60000,
    "port": 80,
    "path": null,
    "updated_at": 1575882610,
    "retries": 5,
    "write_timeout": 60000,
    "tags": null,
    "extras": {}
  },
  "paths": [
    "/request"
  ],
  "destinations": null,
  "id": "6dc2a722-3e71-4e17-8917-6271f3271d96",
  "protocols": [
    "http",
    "https"
  ],
  "tags": null
}

1. Route上啟用插件

$ curl -X POST http://localhost:8001/routes/6dc2a722-3e71-4e17-8917-6271f3271d96/plugins \
    --data "name=basic-auth" \
    --data "config.hide_credentials=true"

2. 創建一個Consumer

$ curl -i -X POST \
  --url http://localhost:8001/consumers/ \
  --data "username=basic-user" \
  --data "custom_id=basic-01"

3. 為Consumer創建憑證

$ curl -X POST http://localhost:8001/consumers/basic-user/basic-auth \
  --data "username=zhangsan" \
  --data "password=123abc"

4. 驗證憑證

授權頭必須時由Base64編碼加密的。

例如,憑證用戶為“zhangsan,密碼為“123abc”,那么加密憑證就是zhangsan:123abc的Base64編碼,即 QWxhZGRpbjpPcGVuU2VzYW1l。

Curl

$ curl -i -X GET \
  --url http://localhost:8000/request/ \
  --header "Host: example.com" \
  --header 'Authorization: Basic emhhbmdzYW46MTIzYWJj'

響應請求:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 1042
Connection: keep-alive
Server: Cowboy
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: host,connection,x-forwarded-for,x-forwarded-proto,x-forwarded-host,x-forwarded-port,x-real-ip,user-agent,accept,x-consumer-id,x-consumer-custom-id,x-consumer-username,x-credential-username,x-request-id,via,connect-time,x-request-start,total-route-time
Access-Control-Allow-Credentials: true
X-Powered-By: mockbin
Vary: Accept, Accept-Encoding
Etag: W/"412-ENv9agFDtEk++eM3OQT6sXpcdNs"
Date: Thu, 26 Dec 2019 06:42:49 GMT
Via: kong/1.1.2
X-Kong-Upstream-Status: 200
X-Kong-Upstream-Latency: 505
X-Kong-Proxy-Latency: 295
Kong-Cloud-Request-ID: b810a616ec95ee60df8fe690d3575f25

{
  "startedDateTime": "2019-12-26T06:42:49.546Z",
  "clientIPAddress": "127.0.0.1",
  "method": "GET",
  "url": "http://mockbin.org/request/",
  "httpVersion": "HTTP/1.1",
  "cookies": {},
  "headers": {
    "host": "mockbin.org",
    "connection": "close",
    "x-forwarded-for": "127.0.0.1, 146.xxx.xxx.254, 18.xxx.xx.183",
    "x-forwarded-proto": "http",
    "x-forwarded-host": "mockbin.org",
    "x-forwarded-port": "80",
    "x-real-ip": "146.xxx.xxx.254",
    "user-agent": "curl/7.29.0",
    "accept": "*/*",
    "x-consumer-id": "95a87530-1c64-49a2-8e48-cff854356408",
    "x-consumer-custom-id": "basic-01",
    "x-consumer-username": "basic-user",
    "x-credential-username": "zhangsan",
    "x-request-id": "3d8aee46-2bab-43b8-a02c-cee35901d777",
    "via": "1.1 vegur",
    "connect-time": "1",
    "x-request-start": "1577342569543",
    "total-route-time": "0"
  },
  "queryString": {},
  "postData": {
    "mimeType": "application/octet-stream",
    "text": "",
    "params": []
  },
  "headersSize": 573,
  "bodySize": 0
}

瀏覽器
當使用瀏覽器訪問時,需要提供正確的用戶憑證,否則將被拒絕訪問:401 message "Invalid authentication credentials"

ACL 用戶鑒權

上節提到,認證插件啟用后,任何具有有效憑證的用戶都可以訪問Kong API。

但實際環境中,我只想要指定的用戶可以訪問API,這時候就需要借助ACL插件進行訪問控制。
通過下圖,可以直白的了解ACL插件的功能,即:控制哪些Comsumers可以訪問API。

1. 在route上啟用ACL鑒權插件

為了方便測試,我們還是在route上啟用插件。(測試route_id 6dc2a722-3e71-4e17-8917-6271f3271d96)

$ curl -X POST http://localhost:8001/routes/{route_id}/plugins \
    --data "name=acl" \
    --data "config.whitelist=group1" \
    --data "config.hide_groups_header=true"

可以看到,我們在上面的route上添加ACL插件,並設置了"config.whitelist=group1, group2",意思就是說:在啟用了ACL插件后,只有在白名單里的group1, group2組的comsuner,才可以訪問這個route。

我們繼續往下看,在上節文章中,我們在route上啟用了basic-auth,並且已經實現訪問認證,但是在同一route上又添加了ACL后,我們不能訪問這個服務了,這時就是ACL鑒權白名單起了作用,所有consumers都被禁止訪問了,它們都沒有再ACL鑒權白名單中。

下面是acl參數列表:

形式參數 默認值 描述
service_id 服務ID
route_id 路由ID
enabled true 是否可用.
api_id API ID
config.whitelist semi-optional 白名單,逗號分隔的任意組名的列表
config.blacklist semi-optional 黑名單,逗號分隔的任意組名的列表
config.hide_groups_header optional false 如果啟用了標記(“true”),可以防止 請求將X-Consumer-Groups頭部發送到上游服務的請求。

2. 關聯comsumer

如下圖所示,假如,我們希望只讓消費者basic-user可以訪問Kong API,

$ curl -X POST http://localhost:8001/consumers/basic-user/acls \
    --data "group=group1"

我們再次訪問添加了basic-auth的API,提示我們輸入用戶憑證,這說明消費者basic-user通過了ACL鑒權。

輸入正確的用戶憑證,就可以正常訪問Kong API了

Basic auth 與ACL 的總結

利用basic-auth以及acl插件,我們能夠對API接口訪問進行有效控制

  • ACL:首先需要通過ACL鑒權的用戶,才有資格進行API訪問認證
  • Basic auth:通過了ACL鑒權的用戶,需要攜帶正確的用戶憑證,才能正常訪問API

 
[sleepy↓]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM