鑒權(quán)插件
Basic Authentication
配置信息
屬性 |
描述 |
認(rèn)證方式 |
基本認(rèn)證、用戶名密碼登錄 |
插件作用域 |
服務(wù)、路由小腊、全局 |
適用協(xié)議 |
http阔籽、https |
參數(shù) |
描述 |
name |
插件名稱甫贯、此處為basic-auth
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件涛漂,默認(rèn)是true |
config.hide_credentials |
是否隱藏請(qǐng)求中的憑證信息(如Authorization 頭)篇梭,默認(rèn)是false |
config.anonymous |
在驗(yàn)證失敗后是否啟用匿名消費(fèi)者氢橙,值可以配置為消費(fèi)者Id |
使用詳情
curl -d "username=user123&custom_id=SOME_CUSTOM_ID" http://kong:8001/consumers/
curl -X POST http://kong:8001/consumers/{consumer}/basic-auth --data "username=Aladdin" --data "password=OpenSesame"
curl http://kong:8000/{path matching a configured Route} -H 'Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l'
憑證接口
curl -X GET http://kong:8001/basic-auths
curl -X GET http://kong:8001/consumers/{username or id}/basic-auths
HMAC Authentication
配置信息
屬性 |
描述 |
認(rèn)證方式 |
HMAC 簽名認(rèn)證 |
插件作用域 |
服務(wù)、路由恬偷、全局 |
適用協(xié)議 |
http悍手、https |
參數(shù) |
描述 |
name |
插件名稱、此處為hmac-auth
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件袍患,默認(rèn)是true |
config.hide_credentials |
是否隱藏請(qǐng)求中的憑證信息(如Authorization 頭)坦康,默認(rèn)是false |
config.clock_skew |
時(shí)間偏移量,默認(rèn)是300s |
config.anonymous |
在驗(yàn)證失敗后是否啟用匿名消費(fèi)者诡延,值可以配置為消費(fèi)者Id |
config.validate_request_body |
是否啟用Body體校驗(yàn)滞欠,默認(rèn)是false |
config.enforce_headers |
請(qǐng)求中必須包含的頭部信息 |
config.algorithms |
HMAC 算法支持的列表,默認(rèn)值是hmac-sha1,hmac-sha256,hmac-sha384,hmac-sha512
|
使用詳情
curl -i -X POST http://localhost:8001/services -d "name=example-service" -d "url=http://example.com"
curl -i -f -X POST http://localhost:8001/services/example-service/routes -d "paths[]=/"
curl -i -X POST http://localhost:8001/services/example-service/plugins -d "name=hmac-auth" -d "config.enforce_headers=date, request-line" -d "config.algorithms=hmac-sha1, hmac-sha256"
curl -i -X POST http://localhost:8001/consumers/ -d "username=alice"
curl -i -X POST http://localhost:8001/consumers/alice/hmac-auth -d "username=alice123" -d "secret=secret"
curl -i -X GET http://localhost:8000/requests -H "Host: hmac.com" -H "Date: Thu, 22 Jun 2017 17:15:21 GMT" -H 'Authorization: hmac username="alice123", algorithm="hmac-sha256", headers="date request-line", signature="ujWCGHeec9Xd6UD2zlyxiNMCiXnDOWeVFMu5VeRUxtw="'
憑證接口
curl -X GET http://kong:8001/hmac-auths
curl -X GET http://kong:8001/consumers/{username or id}/hmac-auths
JWT
配置信息
屬性 |
描述 |
認(rèn)證方式 |
JWT(JSON Web Tokens)認(rèn)證 |
插件作用域 |
服務(wù)肆良、路由筛璧、全局 |
適用協(xié)議 |
http逸绎、https |
參數(shù) |
描述 |
name |
插件名稱、此處為jwt
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件夭谤,默認(rèn)是true |
config.uri_param_names |
jwt的參數(shù)列表棺牧,默認(rèn)值是jwt |
config.cookie_names |
cookie中jwt的參數(shù)列表 |
config.claims_to_verify |
JWT聲明中的驗(yàn)證,可接受的值exp沮翔、nbf |
config.key_claim_name |
創(chuàng)建憑證時(shí)響應(yīng)體中的key字段必須需要傳遞陨帆,默認(rèn)參數(shù)值iss |
config.secret_is_base64 |
JWT是否啟用Base64加密曲秉,默認(rèn)是false |
config.anonymous |
在驗(yàn)證失敗后是否啟用匿名消費(fèi)者采蚀,值可以配置為消費(fèi)者Id |
config.run_on_preflight |
是否在 OPTIONS 類型的請(qǐng)求上啟用鑒權(quán) |
config.maximum_expiration |
JWT的最大失效時(shí)間,config.key_claim_name必須設(shè)置為exp承二,0代表永不是失效榆鼠,默認(rèn)值為0 |
使用詳情
curl -d "username=user123&custom_id=SOME_CUSTOM_ID" http://kong:8001/consumers/
curl -X POST http://kong:8001/consumers/{consumer}/jwt -H "Content-Type: application/x-www-form-urlencoded"
參數(shù) |
描述 |
{consumer} |
消費(fèi)者的Id或者用戶名 |
key |
JWT憑證的key名,如果不指定亥鸠,會(huì)自動(dòng)生成 |
algorithm |
校驗(yàn)token的算法妆够,可以是HS256、HS384负蚊、HS512神妹、RS256或者ES256,默認(rèn)是HS256 |
rsa_public_key |
如果算法使用RS256或者ES256家妆,需要上傳公鑰鸵荠,pem格式 |
secret |
如果算法使用RS256或者ES256,JWT簽名的secret伤极,如果不指定蛹找,會(huì)自動(dòng)生成 |
curl http://kong:8000/{route path} -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhMzZjMzA0OWIzNjI0OWEzYzlmODg5MWNiMTI3MjQzYyIsImV4cCI6MTQ0MjQzMDA1NCwibmJmIjoxNDQyNDI2NDU0LCJpYXQiOjE0NDI0MjY0NTR9.AhumfY35GFLuEEjrOXiaADo7Ae6gt_8VLwX7qffhQN4'
或者
curl http://kong:8000/{route path}?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhMzZjMzA0OWIzNjI0OWEzYzlmODg5MWNiMTI3MjQzYyIsImV4cCI6MTQ0MjQzMDA1NCwibmJmIjoxNDQyNDI2NDU0LCJpYXQiOjE0NDI0MjY0NTR9.AhumfY35GFLuEEjrOXiaADo7Ae6gt_8VLwX7qffhQN4
或者
curl --cookie jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhMzZjMzA0OWIzNjI0OWEzYzlmODg5MWNiMTI3MjQzYyIsImV4cCI6MTQ0MjQzMDA1NCwibmJmIjoxNDQyNDI2NDU0LCJpYXQiOjE0NDI0MjY0NTR9.AhumfY35GFLuEEjrOXiaADo7Ae6gt_8VLwX7qffhQN4 http://kong:8000/{route path}
curl -X DELETE http://kong:8001/consumers/{consumer}/jwt/{id}
curl -X GET http://kong:8001/consumers/{consumer}/jwt
加密詳情
# 請(qǐng)求頭
{
"typ": "JWT",
"alg": "HS256"
}
{
"iss": "a36c3049b36249a3c9f8891cb127243c"
}
# token值
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhMzZjMzA0OWIzNjI0OWEzYzlmODg5MWNiMTI3MjQzYyIsImV4cCI6MTQ0MjQzMDA1NCwibmJmIjoxNDQyNDI2NDU0LCJpYXQiOjE0NDI0MjY0NTR9.AhumfY35GFLuEEjrOXiaADo7Ae6gt_8VLwX7qffhQN4
# 創(chuàng)建憑證
curl -X POST http://kong:8001/consumers/{consumer}/jwt -F "rsa_public_key=@/path/to/public_key.pem"
# 請(qǐng)求頭
{
"typ": "JWT",
"alg": "RS256"
}
{
"iss": "a36c3049b36249a3c9f8891cb127243c"
}
# 使用憑證
curl http://kong:8000/{route path} -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIxM2Q1ODE0NTcyZTc0YTIyYjFhOWEwMDJmMmQxN2MzNyJ9.uNPTnDZXVShFYUSiii78Q-IAfhnc2ExjarZr_WVhGrHHBLweOBJxGJlAKZQEKE4rVd7D6hCtWSkvAAOu7BU34OnlxtQqB8ArGX58xhpIqHtFUkj882JQ9QD6_v2S2Ad-EmEx5402ge71VWEJ0-jyH2WvfxZ_pD90n5AG5rAbYNAIlm2Ew78q4w4GVSivpletUhcv31-U3GROsa7dl8rYMqx6gyo9oIIDcGoMh3bu8su5kQc5SQBFp1CcA5H8sHGfYs-Et5rCU2A6yKbyXtpHrd1Y9oMrZpEfQdgpLae0AfWRf6JutA9SPhst9-5rn4o3cdUmto_TBGqHsFmVyob8VQ'
憑證接口
curl -X GET http://kong:8001/jwts
curl -X GET http://kong:8001/consumers/{username or id}/jwts
Key Authentication
配置信息
屬性 |
描述 |
認(rèn)證方式 |
Key認(rèn)證 |
插件作用域 |
服務(wù)、路由技肩、全局 |
適用協(xié)議 |
http悦穿、https |
參數(shù) |
描述 |
name |
插件名稱锦聊、此處為key-auth
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件,默認(rèn)是true |
config.key_names |
key的參數(shù)列表届慈,默認(rèn)值是apikey |
config.key_in_body |
參數(shù)key是否可以存放在請(qǐng)求體中,默認(rèn)是false |
config.hide_credentials |
是否隱藏請(qǐng)求中的憑證信息忿偷,默認(rèn)是false |
config.anonymous |
在驗(yàn)證失敗后是否啟用匿名消費(fèi)者金顿,值可以配置為消費(fèi)者Id |
config.run_on_preflight |
是否在 OPTIONS 類型的請(qǐng)求上啟用鑒權(quán) |
使用詳情
curl -d "username=user123&custom_id=SOME_CUSTOM_ID" http://kong:8001/consumers/
curl -X POST http://kong:8001/consumers/{consumer}/key-auth -d ''
curl http://kong:8000/{proxy path}?apikey=<some_key>
或者
curl http://kong:8000/{proxy path} -H 'apikey: <some_key>'
curl -X DELETE http://kong:8001/consumers/{consumer}/key-auth/{id}
憑證接口
curl -X GET http://kong:8001/key-auths
curl -X GET http://kong:8001/consumers/{username or id}/key-auth
LDAP Authentication
配置信息
屬性 |
描述 |
認(rèn)證方式 |
LDAP認(rèn)證 |
插件作用域 |
路由 |
適用協(xié)議 |
http、https |
參數(shù) |
描述 |
name |
插件名稱牵舱、此處為ldap-auth
|
route_id |
綁定的路由Id |
enabled |
是否啟用該插件串绩,默認(rèn)是true |
config.hide_credentials |
是否隱藏請(qǐng)求中的憑證信息,默認(rèn)是false |
config.ldap_host |
LDAP服務(wù)器的地址 |
config.ldap_port |
LDAP服務(wù)器的端口 |
config.start_tls |
是否啟用TLS芜壁,默認(rèn)是false |
config.base_dn |
基準(zhǔn)DN(Distinguished Name) |
config.verify_ldap_host |
是否啟用LDAP服務(wù)器驗(yàn)證礁凡,默認(rèn)是false |
config.attribute |
搜索用戶使用的屬性 |
config.cache_ttl |
緩存的失效時(shí)間高氮,默認(rèn)是60s |
config.timeout |
連接LDAP服務(wù)器超時(shí)時(shí)間,默認(rèn)10000ms |
config.keepalive |
LDAP服務(wù)器保持連接時(shí)間顷牌,默認(rèn)60000ms |
config.anonymous |
在驗(yàn)證失敗后是否啟用匿名消費(fèi)者剪芍,值可以配置為消費(fèi)者Id |
config.header_type |
請(qǐng)求頭參數(shù)名,默認(rèn)是ldap |
OAuth2.0 Authentication
配置信息
屬性 |
描述 |
認(rèn)證方式 |
HMAC 簽名認(rèn)證 |
插件作用域 |
服務(wù)窟蓝、路由罪裹、全局 |
適用協(xié)議 |
http、https |
參數(shù) |
描述 |
name |
插件名稱运挫、此處為key-auth
|
service_id |
綁定的服務(wù)Id |
enabled |
是否啟用該插件状共,默認(rèn)是true |
config.scopes |
描述終端用戶可以訪問的范圍 |
config.mandatory_scope |
是否告訴插件至少給終端用戶分配一個(gè)訪問范圍,默認(rèn)是false |
config.token_expiration |
token的過期時(shí)間谁帕,默認(rèn)是7200s峡继,設(shè)置為0時(shí)token不過期 |
config.enable_authorization_code |
是否啟用Authorization Code模式,默認(rèn)是false |
config.enable_client_credentials |
是否啟用Client Credentials模式匈挖,默認(rèn)是false |
config.enable_implicit_grant |
是否啟用Implicit Grant模式碾牌,默認(rèn)是false |
config.enable_password_grant |
是否啟用Password Grant模式,默認(rèn)是false |
config.auth_header_name |
攜帶access token的頭信息儡循,默認(rèn)值是authorization |
config.hide_credentials |
是否隱藏請(qǐng)求中的憑證信息舶吗,默認(rèn)是false |
config.accept_http_if_already_terminated |
|
config.anonymous |
在驗(yàn)證失敗后是否啟用匿名消費(fèi)者,值可以配置為消費(fèi)者Id |
config.global_credentials |
是否啟用通用OAuth2.0服務(wù) |
config.refresh_token_ttl |
refresh_token的過期時(shí)間择膝,默認(rèn)為兩周誓琼,設(shè)置為0時(shí)token不過期 |
使用詳情
端點(diǎn) |
描述 |
/oauth2/authorize |
授權(quán)服務(wù)器的端點(diǎn),用于為Authorization Code模式提供authorization code调榄,或?yàn)镮mplicit Grant模式提供access token踊赠,僅支持POST方法 |
/oauth2/token |
令牌服務(wù)器的端點(diǎn),也作為Client Credentials模式和Password Grant模式唯一的端點(diǎn)每庆,僅支持POST方法 |
curl -X POST http://kong:8001/consumers/ --data "username=user123" --data "custom_id=SOME_CUSTOM_ID"
- 創(chuàng)建OAuth2.0應(yīng)用
curl -X POST http://kong:8001/consumers/{consumer_id}/oauth2 --data "name=Test%20Application" --data "client_id=SOME-CLIENT-ID" --data "client_secret=SOME-CLIENT-SECRET" --data "redirect_uris=http://some-domain/endpoint/"
參數(shù) |
描述 |
name |
新建OAuth2.0應(yīng)用的名稱 |
client_id |
clientId筐带,如果不指定,會(huì)自動(dòng)生成 |
client_secret |
clientSecrct缤灵,如果不指定伦籍,會(huì)自動(dòng)生成 |
redirect_uris |
授權(quán)完成之后的跳轉(zhuǎn)地址 |
OAuth2.0 Client Credentials模式
Client Credentials模式開箱即用,無需構(gòu)建任何授權(quán)頁(yè)面腮出,客戶端需要使用 /oauth2/token
端點(diǎn)獲取access token
OAuth2.0 Authorization Code模式
配置完消費(fèi)者并且與OAuth2.0憑證關(guān)聯(lián)之后帖鸦,需要正確了解OAuth2.0的授權(quán)流程,與大多數(shù)Kong插件不同胚嘲,OAuth2.0插件需要一些額外的工作才能運(yùn)轉(zhuǎn)正常
用戶必須在Web應(yīng)用中實(shí)現(xiàn)授權(quán)頁(yè)面作儿,該頁(yè)面與插件的服務(wù)器端通信,用戶需要在網(wǎng)站或文檔中解釋清楚如何使用受OAuth2.0保護(hù)的服務(wù)馋劈,這個(gè)開發(fā)者可以知道如何實(shí)現(xiàn)客戶端程序
構(gòu)建授權(quán)頁(yè)面是插件無法開箱即用的部分攻锰,因?yàn)樗枰獧z查用戶是否已正確登錄晾嘶,此操作與用戶身份校驗(yàn)的實(shí)現(xiàn)密切相關(guān)
授權(quán)頁(yè)面由兩部分組成:
- 用戶看到的前端的頁(yè)面,這將允許他授權(quán)客戶端應(yīng)用程序訪問他的數(shù)據(jù)
- 后端將處理HTML表單中顯示的信息娶吞,這個(gè)表單會(huì)與Kong上的OAuth2.0插件進(jìn)行通信垒迂,并最終將用戶重定向到第三方URL上
完整流程如下:
- 客戶端應(yīng)用會(huì)將終端用戶重定向到用戶實(shí)現(xiàn)的授權(quán)頁(yè)面上,并將
client_id
妒蛇、response_type
和 scope
(如果需要的話) 作為參數(shù)傳遞過來机断;
- 在展示真實(shí)的授權(quán)頁(yè)面前,應(yīng)用程序需要確保該用戶已經(jīng)正常登錄了绣夺;
- 客戶端應(yīng)用會(huì)通過參數(shù)將
client_id
傳遞給應(yīng)用程序吏奸,應(yīng)用程序向Kong發(fā)送指令取回OAuth2.0應(yīng)用名和開發(fā)者名:
curl kong:8001/oauth2?client_id=XXX
- 如果終端用戶授權(quán)了客戶端程序,表單將使用
POST
請(qǐng)求將 client_id
乐导、response_type
和 scope
提交到后端苦丁;
- 后端在發(fā)送
POST
請(qǐng)求時(shí)還需要加入 provision_key
和 authenticated_userid
參數(shù)浸颓,如果客戶端帶入了 Authorization
頭物臂,請(qǐng)求也需要透?jìng)鳎渲?provision_key
是插件添加到服務(wù)時(shí)生成的密鑰产上,authenticated_userid
是授予權(quán)限的用戶的Id棵磷,請(qǐng)求大致如下:
curl https://your.service.com/oauth2/authorize --header "Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW" --data "client_id=XXX" --data "response_type=XXX" --data "scope=XXX" --data "provision_key=XXX" --data "authenticated_userid=XXX"
- Kong響應(yīng)大致如下,
200 OK
或者 400 Bad Request
響應(yīng)碼取決請(qǐng)求是否成功:
{
"redirect_uri": "http://some/url"
}
- 無論哪種情況晋涣,忽略響應(yīng)狀態(tài)碼仪媒,只需將用戶重定向到
redirect_uri
屬性返回的URI上;
- 客戶端應(yīng)用將在此處獲取它谢鹊,并將繼續(xù)使用Kong算吩,而不與用戶的應(yīng)用進(jìn)行其他交互;
- 當(dāng)獲取到access token之后佃扼,客戶端應(yīng)用可以作為用戶偎巢,向上游服務(wù)發(fā)起請(qǐng)求;
- access token是會(huì)過期的兼耀,當(dāng)發(fā)生這種情況時(shí)压昼,客戶端應(yīng)用程序需要使用Kong續(xù)訂訪問令牌
在這些步驟中,用戶需要實(shí)現(xiàn)的步驟有:
- 登錄頁(yè)(步驟2)
- 授權(quán)頁(yè)面瘤运,以及后端簡(jiǎn)單地收集信息窍霞,向Kong發(fā)送請(qǐng)求,并重定向到Kong返回的URL上
Password Grant模式
Password Grant模式是一個(gè)比較簡(jiǎn)單的模式拯坟,但仍需要后端授權(quán)(無前端)才能正常工作:
- 第一個(gè)請(qǐng)求中但金,客戶端應(yīng)用向應(yīng)用程序發(fā)送請(qǐng)求,攜帶一些OAuth2的參數(shù)郁季,包含用戶名和密碼冷溃;
- 應(yīng)用程序的后端會(huì)校驗(yàn)客戶端發(fā)送的用戶名和密碼掠哥,如果校驗(yàn)成功,會(huì)將
provision_key
秃诵、authenticated_userid
和 grant_type
這些參數(shù)添加到客戶端最初發(fā)送的參數(shù)中续搀,并向Kong的 /oauth2/token
端點(diǎn)發(fā)送 POST
請(qǐng)求,其中 provision_key
是插件添加到服務(wù)時(shí)生成的密鑰菠净,authenticated_userid
是授予權(quán)限的用戶的Id禁舷,請(qǐng)求大致如下:
curl https://your.service.com/oauth2/token --header "Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW" --data "client_id=XXX" --data "client_secret=XXX" --data "grant_type=password" --data "scope=XXX" --data "provision_key=XXX" --data "authenticated_userid=XXX" --data "username=XXX" --data "password=XXX"
- Kong返回一個(gè)JSON響應(yīng);
- Kong的響應(yīng)必須原樣發(fā)送回客戶端毅往,如果操作成功牵咙,響應(yīng)中會(huì)包含access token,否則會(huì)包含錯(cuò)誤
在這些步驟中攀唯,用戶需要實(shí)現(xiàn)的步驟有:
后端端點(diǎn)將處理原始請(qǐng)求并驗(yàn)證客戶端發(fā)送的用戶名和密碼洁桌,如果驗(yàn)證成功,則向Kong發(fā)出請(qǐng)求并返回客戶端侯嘀,無論Kong響應(yīng)什么內(nèi)容
刷新Token
當(dāng)用戶的access token過期時(shí)另凌,用戶可以使用refresh token生成新的access token
curl -X POST https://your.service.com/oauth2/token --data "grant_type=refresh_token" --data "client_id=XXX" --data "client_secret=XXX" --data "refresh_token=XXX"
安全插件
CORS
配置信息
屬性 |
描述 |
安全策略 |
跨域資源共享 |
插件作用域 |
服務(wù)、路由戒幔、全局 |
適用協(xié)議 |
http吠谢、https |
參數(shù) |
描述 |
name |
插件名稱、此處為cors
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件诗茎,默認(rèn)是true |
config.origins |
Access-Control-Allow-Origin頭允許的域列表工坊,使用 * 代表允許所有域,值可以是普通值敢订;也可以是正則表達(dá)式 |
config.methods |
Access-Control-Allow-Methods頭的值王污,默認(rèn)為GET, HEAD, PUT, PATCH, POST |
config.headers |
Access-Control-Allow-Headers頭的值,默認(rèn)為Access-Control-Request-Headers頭的值 |
config.exposed_headers |
Access-Control-Expose-Headers的值楚午,如果不指定昭齐,不暴露自定義的頭 |
config.credentials |
是否發(fā)送Access-Control-Allow-Credentials請(qǐng)求頭,默認(rèn)是false |
config.max_age |
預(yù)檢請(qǐng)求的緩存時(shí)間 |
config.preflight_continue |
是否將 OPTIONS 請(qǐng)求代理到上游服務(wù) |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=cors" --data "config.origins=http://mockbin.com" --data "config.methods=GET" --data "config.methods=POST" --data "config.headers=Accept" --data "config.headers=Accept-Version" --data "config.headers=Content-Length" --data "config.headers=Content-MD5" --data "config.headers=Content-Type" --data "config.headers=Date" --data "config.headers=X-Auth-Token" --data "config.exposed_headers=X-Auth-Token" --data "config.credentials=true" --data "config.max_age=3600"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=cors" --data "config.origins=http://mockbin.com" --data "config.methods=GET" --data "config.methods=POST" --data "config.headers=Accept" --data "config.headers=Accept-Version" --data "config.headers=Content-Length" --data "config.headers=Content-MD5" --data "config.headers=Content-Type" --data "config.headers=Date" --data "config.headers=X-Auth-Token" --data "config.exposed_headers=X-Auth-Token" --data "config.credentials=true" --data "config.max_age=3600"
IP Restriction
配置信息
屬性 |
描述 |
安全策略 |
IP限制 |
插件作用域 |
服務(wù)醒叁、路由司浪、消費(fèi)者、全局 |
適用協(xié)議 |
http把沼、https |
參數(shù) |
描述 |
name |
插件名稱啊易、此處為ip-restriction
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
config.whitelist |
白名單列表饮睬,白名單或黑名單必須配置一個(gè) |
config.blacklist |
黑名單列表租谈,白名單或黑名單必須配置一個(gè) |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=ip-restriction" --data "config.whitelist=54.13.21.1" --data "config.whitelist=143.1.0.0/24"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=ip-restriction" --data "config.whitelist=54.13.21.1" --data "config.whitelist=143.1.0.0/24"
curl -X POST http://kong:8001/consumers/{consumer}/plugins --data "name=ip-restriction" --data "config.whitelist=54.13.21.1" --data "config.whitelist=143.1.0.0/24"
注意,白名單
和 黑名單
模型在使用中是互斥的,它們提供了互補(bǔ)的方法割去,也就是說窟却,用戶無法既用白名單
,又用黑名單
配置插件呻逆,白名單提供了積極的安全模型夸赫,只允許配置范圍內(nèi)的ip訪問資源,其他全部拒絕咖城;黑名單提供了被動(dòng)的安全模型茬腿,只拒絕配置范圍內(nèi)的ip訪問資源,其他全部允許
流量控制插件
ACL
配置信息
屬性 |
描述 |
限流策略 |
ACL組限制 |
插件作用域 |
服務(wù)宜雀、路由切平、全局 |
適用協(xié)議 |
http、https |
參數(shù) |
描述 |
name |
插件名稱辐董、此處為acl
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件悴品,默認(rèn)是true |
config.whitelist |
白名單組列表,白名單或黑名單必須配置一個(gè) |
config.blacklist |
黑名單組列表简烘,白名單或黑名單必須配置一個(gè) |
config.hide_groups_header |
是否將X-Consumer-Groups 頭發(fā)送給上游服務(wù) |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=acl" --data "config.whitelist=group1" --data "config.whitelist=group2" --data "config.hide_groups_header=true"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=acl" --data "config.whitelist=group1" --data "config.whitelist=group2" --data "config.hide_groups_header=true"
curl -X POST http://kong:8001/consumers/{consumer}/acls --data "group=group1"
憑證接口
curl -X GET http://kong:8001/acls
curl -X GET http://kong:8001/consumers/{username or id}/acls
- 查詢ACL組Id關(guān)聯(lián)的消費(fèi)者
curl -X GET http://kong:8001/acls/{id}/consumer
Proxy Caching
配置信息
屬性 |
描述 |
限流策略 |
終止請(qǐng)求限制 |
插件作用域 |
服務(wù)苔严、路由、消費(fèi)者夸研、全局 |
適用協(xié)議 |
http邦蜜、https |
參數(shù) |
描述 |
name |
插件名稱、此處為proxy-cache
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件亥至,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
api_id |
綁定的API Id,已經(jīng)廢棄 |
config.response_code |
上游服務(wù)可以作為緩存的狀態(tài)碼贱迟,默認(rèn)值為200, 301, 404 |
config.request_method |
下游服務(wù)可以作為緩存的方法姐扮,默認(rèn)為GET, HEAD |
config.content_type |
可以作為緩存的 content type |
config.vary_headers |
用于生成緩存Key的頭部信息,如果不指定衣吠,所有頭部信息都不作為數(shù)據(jù)源 |
config.vary_query_params |
用于生成緩存Key的查詢參數(shù)茶敏,如果不指定,所有查詢參數(shù)都作為數(shù)據(jù)源 |
config.cache_ttl |
緩存的TTL時(shí)間缚俏,默認(rèn)是300s |
config.cache_control |
是否啟用Cache-Control行為惊搏,默認(rèn)是false |
config.storage_ttl |
數(shù)據(jù)在存儲(chǔ)引擎中的TTL時(shí)間 |
config.strategy |
緩存策略,可以取 memory 或 redis
|
config.memory.dictionary_name |
當(dāng)選用 memory 策略時(shí)忧换,用于保存緩存實(shí)體的 shared dictionary 的名稱恬惯,這個(gè) shared dictionary 必須在Kong的Nginx模板中手動(dòng)定義,默認(rèn)是kong_cache |
config.redis.host |
redis服務(wù)器地址 |
config.redis.port |
redis服務(wù)器端口 |
config.redis.timeout |
連接redis服務(wù)器超時(shí)時(shí)間 |
config.redis.password |
redis服務(wù)器密碼 |
config.redis.database |
redis數(shù)據(jù)庫(kù) |
config.redis.sentinel_master |
redis哨兵模式master節(jié)點(diǎn)地址 |
config.redis.sentinel_role |
redis哨兵模式角色 |
config.redis.sentinel_addresses |
redis哨兵模式哨兵地址 |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=proxy-cache" --data "config.strategy=memory"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=proxy-cache" --data "config.strategy=memory"
curl -X POST http://kong:8001/apis/{api}/plugins --data "name=proxy-cache" --data "config.strategy=memory"
模式 |
描述 |
memory |
內(nèi)存模式亚茬,是一個(gè)lua_shared_dict酪耳,默認(rèn)的kong_cache也被Kong的其他插件用于緩存其他數(shù)據(jù),使用這種方式比較簡(jiǎn)單刹缝,但不推薦在大規(guī)模場(chǎng)景中使用碗暗,因?yàn)榇罅渴褂脮?huì)對(duì)Kong數(shù)據(jù)緩存操作造成壓力 |
redis |
支持redis和redis哨兵模式 |
- 緩存Key
Kong基于請(qǐng)求方法颈将、完整的客戶端請(qǐng)求(路徑和請(qǐng)求參數(shù))和綁定在請(qǐng)求上的API或消費(fèi)者UUID來生成緩存 Key,所以各個(gè)API或消費(fèi)者之間的緩存都是不同的言疗,目前緩存Key的格式是硬編碼的晴圾,無法調(diào)整,緩存的組成方式如下:
key = md5(UUID | method | request)
其中 method
是通過 ngx.req.get_method()
方法獲得的噪奄,request
是根據(jù) Nginx $request
變量獲得的疑务,Kong會(huì)在響應(yīng)的 X-Cache-Key
頭中返回緩存Key值,如上所述梗醇,可以預(yù)先計(jì)算緩存Key
- 緩存控制
當(dāng)啟用 cache_control
時(shí)知允,Kong將遵守 Cache-Control
頭的使用規(guī)范,有一些例外情況:
- 不支持緩存重新驗(yàn)證
- 類似地叙谨,
no-cache
排除緩存存在
- 不支持根據(jù)
Vary
計(jì)算二級(jí)緩存
- 緩存狀態(tài)
Kong通過 X-Cache-Status
頭表示緩存行為的狀態(tài):
狀態(tài) |
描述 |
Miss |
請(qǐng)求可以緩存温鸽,但是緩存中沒有數(shù)據(jù),數(shù)據(jù)從代理的服務(wù)中獲取 |
Hit |
緩存命中手负,數(shù)據(jù)從緩存中獲取 |
Refresh |
緩存命中涤垫,但因?yàn)榫彺婵刂菩袨榛蛘哌_(dá)到了cache_ttl設(shè)置的閾值 |
Bypass |
不能使用緩存 |
- 存儲(chǔ)TTL
Kong可以在存儲(chǔ)引擎中存儲(chǔ)數(shù)據(jù),存儲(chǔ)時(shí)間超過 cache_ttl
或 Cache-Control
閾值所限定的時(shí)間竟终,這使得Kong在資源過期后蝠猬,還能維持一份資源的緩存副本,這樣客戶端在必要時(shí)可以使用 max-age
或 max-stale
頭請(qǐng)求過期的數(shù)據(jù)
緩存API
- 獲取緩存實(shí)體
GET /proxy-cache/:plugin_id/caches/:cache_id
GET /proxy-cache/:cache_id
- 刪除緩存實(shí)體
DELETE /proxy-cache/:plugin_id/caches/:cache_id
DELETE /proxy-cache/:cache_id
- 清除所有緩存
DELETE /proxy-cache/
Rate Limiting
配置信息
屬性 |
描述 |
限流策略 |
ACL組限制 |
插件作用域 |
服務(wù)统捶、路由榆芦、消費(fèi)者、全局 |
適用協(xié)議 |
http喘鸟、https |
參數(shù) |
描述 |
name |
插件名稱匆绣、此處為rate-limiting
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
config.second |
每秒的限流數(shù) |
config.minute |
每分鐘的限流數(shù) |
config.hour |
每小時(shí)的限流數(shù) |
config.day |
每天的限流數(shù) |
config.month |
每月的限流數(shù) |
config.year |
每年的限流數(shù) |
config.limit_by |
限制次數(shù)的衡量標(biāo)準(zhǔn)什黑,可以取consumer 崎淳、credential 或 ip ,如果不能識(shí)別consumer或credential愕把,都按照ip計(jì)數(shù)拣凹,默認(rèn)是consumer |
config.policy |
限流累加器的計(jì)數(shù)策略,可以取local 恨豁、cluster 或 redis 嚣镜,默認(rèn)是cluster |
config.fault_tolerant |
當(dāng)?shù)谌綌?shù)據(jù)源出錯(cuò)時(shí),是否啟用限流功能圣絮,取true時(shí)會(huì)禁用限流功能祈惶,默認(rèn)是true |
config.hide_client_headers |
是否隱藏消息響應(yīng)頭,默認(rèn)是false |
config.redis_host |
redis服務(wù)器地址 |
config.redis_port |
redis服務(wù)器端口 |
config.redis_password |
redis服務(wù)器密碼 |
config.redis_timeout |
連接redis服務(wù)器超時(shí)時(shí)間 |
config.redis_database |
redis數(shù)據(jù)庫(kù) |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=rate-limiting" --data "config.second=5" --data "config.hour=10000"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=rate-limiting" --data "config.second=5" --data "config.hour=10000"
curl -X POST http://kong:8001/consumers/{consumer}/plugins --data "name=rate-limiting" --data "config.second=5" --data "config.hour=10000"
X-RateLimit-Limit-Second: 5
X-RateLimit-Remaining-Second: 4
X-RateLimit-Limit-Minute: 10
X-RateLimit-Remaining-Minute: 9
模式 |
優(yōu)點(diǎn) |
描述 |
cluster |
準(zhǔn)確,不需要依賴其他組件 |
相對(duì)來說性能影響最大的捧请,每個(gè)請(qǐng)求都會(huì)強(qiáng)制對(duì)底層的數(shù)據(jù)源進(jìn)行讀寫操作 |
redis |
準(zhǔn)確凡涩,性能影響比cluster模式小 |
需要額外安裝redis,相比local模式性能影響大 |
local |
性能影響最小 |
不太準(zhǔn)確疹蛉,除非在Kong之前使用Hash一致性負(fù)載均衡器 |
- 事務(wù)粒度
這個(gè)場(chǎng)景中活箕,不能選用 local
策略,應(yīng)該在 cluster
或 redis
策略中考量可款,推薦是先嘗試使用 cluster
策略育韩,如果性能急速下降,則切換成 redis
策略闺鲸,需要注意的是筋讨,指標(biāo)數(shù)據(jù)無法從原有數(shù)據(jù)源切換到redis,通常來說摸恍,短周期指標(biāo)(如秒悉罕、分)不受影響,長(zhǎng)周期指標(biāo)(月)可能會(huì)有影響立镶,所以切換數(shù)據(jù)源時(shí)需要小心
- 后端保護(hù)模式
這種場(chǎng)景中因?yàn)闇?zhǔn)確性不太重要壁袄,可以使用 local
策略,這需要多些嘗試才能找到合適的值媚媒,比如用戶希望配置限流每秒100個(gè)請(qǐng)求嗜逻,總共有5個(gè)Kong節(jié)點(diǎn),設(shè)置 local
策略缭召,每秒30個(gè)請(qǐng)求栈顷,大致可以滿足需求,如果覺得返回的失敗過于頻繁恼琼,可以適當(dāng)增大閾值
需要注意的是妨蛹,當(dāng)增加Kong節(jié)點(diǎn)時(shí),會(huì)增加總請(qǐng)求數(shù)晴竞;同理減少Kong節(jié)點(diǎn)時(shí),會(huì)降低總請(qǐng)求數(shù)狠半,所以調(diào)整節(jié)點(diǎn)數(shù)時(shí)需要同步調(diào)整閾值
在Kong節(jié)點(diǎn)前使用Hash一致性負(fù)載均衡器可以避免上述的問題噩死,因?yàn)樗鼤?huì)保證相同的用戶會(huì)路由到指定的Kong節(jié)點(diǎn),保證數(shù)據(jù)準(zhǔn)確神年,并且不受節(jié)點(diǎn)縮放的影響
通常情況下已维,真實(shí)的請(qǐng)求數(shù)會(huì)大于限流的閾值,但是它還是能有效的防止攻擊已日,并且保持最佳性能
RequestTermination
配置信息
屬性 |
描述 |
限流策略 |
終止請(qǐng)求限制 |
插件作用域 |
服務(wù)垛耳、路由、消費(fèi)者、全局 |
適用協(xié)議 |
http堂鲜、https |
參數(shù) |
描述 |
name |
插件名稱栈雳、此處為request-termination
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
config.status_code |
響應(yīng)碼缔莲,默認(rèn)是503 |
config.message |
響應(yīng)消息 |
config.body |
響應(yīng)消息體哥纫,與響應(yīng)消息互斥 |
config.content_type |
響應(yīng)體的content_type,默認(rèn)值是 application/json; charset=utf-8 |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=request-termination" --data "config.status_code=403" --data "config.message=So long and thanks for all the fish!"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=request-termination" --data "config.status_code=403" --data "config.message=So long and thanks for all the fish!"
curl -X POST http://kong:8001/consumers/{consumer}/plugins --data "name=request-termination" --data "config.status_code=403" --data "config.message=So long and thanks for all the fish!"
- 臨時(shí)禁用某個(gè)服務(wù)(比如正在維護(hù)中)
- 臨時(shí)禁用某個(gè)路由(比如需要禁用某個(gè)端點(diǎn))
- 臨時(shí)禁用某個(gè)消費(fèi)者(比如過度消費(fèi))
- 阻止匿名訪問
Kong Service Virtualization
配置信息
屬性 |
描述 |
限流策略 |
服務(wù)虛擬化 |
插件作用域 |
服務(wù)痴奏、路由蛀骇、全局 |
適用協(xié)議 |
http、https |
參數(shù) |
描述 |
name |
插件名稱读拆、此處為kong-service-virtualization
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件擅憔,默認(rèn)是true |
api_id |
綁定的API Id,已經(jīng)廢棄 |
config.virtual_tests |
JSON字串配置項(xiàng) |
參數(shù) |
描述 |
name |
測(cè)試用例名稱 |
requestHttpMethod |
測(cè)試用例的請(qǐng)求方法 |
requestHash |
測(cè)試用例的請(qǐng)求參數(shù)檐晕,使用Sha256算法加密 |
responseHttpStatus |
測(cè)試用例的響應(yīng)狀態(tài)碼 |
responseContentType |
測(cè)試用例的響應(yīng)ContentType |
response |
測(cè)試用例的響應(yīng)體暑诸,采用Base64編碼 |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=kong-service-virtualization" --data "config.virtual_tests={"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}" --data "config.virtual_tests={"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=kong-service-virtualization" --data "config.virtual_tests={"name"=>"TestCase1", "requestHttpMethod"=>"POST", "requestHash"=>"0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="}" --data "config.virtual_tests={"name"=>"TestCase2", "requestHttpMethod"=>"GET", "requestHash"=>"e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32", "responseHttpStatus"=>"200", "responseContentType"=>"application/json", "response"=>"eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="}"
- 推薦安裝方式
luarocks install kong-service-virtualization
- 其他選擇安裝方式
git clone https://github.com/Optum/kong-service-virtualization
cd /path/to/kong/plugins/kong-service-virtualization
luarocks make *.rockspec
[
{
"name": "TestCase1",
"requestHttpMethod": "POST",
"requestHash": "0296217561490155228da9c17fc555cf9db82d159732f3206638c25f04a285c4",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkEgQmlnIFN1Y2Nlc3MhIn0="
},
{
"name": "TestCase2",
"requestHttpMethod": "GET",
"requestHash": "e2c319e4ef41706e2c0c1b266c62cad607a014c59597ba662bef6d10a0b64a32",
"responseHttpStatus": "200",
"responseContentType": "application/json",
"response": "eyJtZXNzYWdlIjogIkFub3RoZXIgU3VjY2VzcyEifQ=="
}
]
上述的請(qǐng)求相當(dāng)于:
https://gateway.company.com/virtualtest
POST:
{
"virtual": "test"
}
Response : {"message": "A Big Success!"} as base64 encoded in plugin
GET:
hello=world&service=virtualized
Response : {"message": "Another Success!"} as base4 encoded in plugin
日志插件
HTTP Log
配置信息
屬性 |
描述 |
日志策略 |
HTTP日志 |
插件作用域 |
服務(wù)、路由棉姐、消費(fèi)者屠列、全局 |
適用協(xié)議 |
http、https |
參數(shù) |
描述 |
name |
插件名稱伞矩、此處為http-log
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件笛洛,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
config.http_endpoint |
http服務(wù)器端點(diǎn),數(shù)據(jù)會(huì)發(fā)送到該端點(diǎn) |
config.method |
向http服務(wù)器發(fā)送數(shù)據(jù)的方法乃坤,默認(rèn)是POST 方法苛让,其他支持的方法是PUT 和PATCH
|
config.timeout |
發(fā)送數(shù)據(jù)的超時(shí)時(shí)間,默認(rèn)是10000ms |
config.keepalive |
空閑連接的等待時(shí)間湿诊,默認(rèn)是60000ms |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=http-log" --data "config.http_endpoint=http://mockbin.org/bin/:id" --data "config.method=POST" --data "config.timeout=1000" --data "config.keepalive=1000"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=http-log" --data "config.http_endpoint=http://mockbin.org/bin/:id" --data "config.method=POST" --data "config.timeout=1000" --data "config.keepalive=1000"
curl -X POST http://kong:8001/consumers/{consumer}/plugins --data "name=http-log" --data "config.http_endpoint=http://mockbin.org/bin/:id" --data "config.method=POST" --data "config.timeout=1000" --data "config.keepalive=1000"
日志格式
{
"request": {
"method": "GET",
"uri": "/get",
"url": "http://httpbin.org:8000/get",
"size": "75",
"querystring": {},
"headers": {
"accept": "*/*",
"host": "httpbin.org",
"user-agent": "curl/7.37.1"
},
"tls": {
"version": "TLSv1.2",
"cipher": "ECDHE-RSA-AES256-GCM-SHA384",
"supported_client_ciphers": "ECDHE-RSA-AES256-GCM-SHA384",
"client_verify": "NONE"
}
},
"upstream_uri": "/",
"response": {
"status": 200,
"size": "434",
"headers": {
"Content-Length": "197",
"via": "kong/0.3.0",
"Connection": "close",
"access-control-allow-credentials": "true",
"Content-Type": "application/json",
"server": "nginx",
"access-control-allow-origin": "*"
}
},
"tries": [
{
"state": "next",
"code": 502,
"ip": "127.0.0.1",
"port": 8000
},
{
"ip": "127.0.0.1",
"port": 8000
}
],
"authenticated_entity": {
"consumer_id": "80f74eef-31b8-45d5-c525-ae532297ea8e",
"id": "eaa330c0-4cff-47f5-c79e-b2e4f355207e"
},
"route": {
"created_at": 1521555129,
"hosts": null,
"id": "75818c5f-202d-4b82-a553-6a46e7c9a19e",
"methods": null,
"paths": [
"/example-path"
],
"preserve_host": false,
"protocols": [
"http",
"https"
],
"regex_priority": 0,
"service": {
"id": "0590139e-7481-466c-bcdf-929adcaaf804"
},
"strip_path": true,
"updated_at": 1521555129
},
"service": {
"connect_timeout": 60000,
"created_at": 1521554518,
"host": "example.com",
"id": "0590139e-7481-466c-bcdf-929adcaaf804",
"name": "myservice",
"path": "/",
"port": 80,
"protocol": "http",
"read_timeout": 60000,
"retries": 5,
"updated_at": 1521554518,
"write_timeout": 60000
},
"workspaces": [
{
"id":"b7cac81a-05dc-41f5-b6dc-b87e29b6c3a3",
"name": "default"
}
],
"consumer": {
"username": "demo",
"created_at": 1491847011000,
"id": "35b03bfc-7a5b-4a23-a594-aa350c585fa8"
},
"latencies": {
"proxy": 1430,
"kong": 9,
"request": 1921
},
"client_ip": "127.0.0.1",
"started_at": 1433209822425
}
參數(shù) |
描述 |
request |
客戶端發(fā)送的請(qǐng)求信息 |
response |
發(fā)送給客戶端的響應(yīng)信息 |
tries |
請(qǐng)求負(fù)載均衡記錄信息 |
route |
請(qǐng)求對(duì)應(yīng)的路由信息 |
service |
請(qǐng)求對(duì)應(yīng)的服務(wù)信息 |
authenticated_entity |
請(qǐng)求對(duì)應(yīng)的憑證信息 |
workspaces |
企業(yè)版本 >= 0.34 包含的信息狱杰,可以忽略 |
consumer |
請(qǐng)求對(duì)應(yīng)的消費(fèi)者信息 |
latencies.proxy |
從接收到客戶端請(qǐng)求,到查找到指定服務(wù)花的時(shí)間 |
latencies.kong |
Kong所有插件運(yùn)行的時(shí)間 |
latencies.request |
請(qǐng)求的總時(shí)常 |
client_ip |
客戶端的原始IP |
started_at |
請(qǐng)求的時(shí)間戳 |
TCP Log
配置信息
屬性 |
描述 |
日志策略 |
TCP日志 |
插件作用域 |
服務(wù)厅须、路由仿畸、消費(fèi)者、全局 |
適用協(xié)議 |
http朗和、https |
參數(shù) |
描述 |
name |
插件名稱错沽、此處為tcp-log
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
config.host |
服務(wù)器Ip地址 |
config.port |
服務(wù)器端口 |
config.timeout |
發(fā)送數(shù)據(jù)的超時(shí)時(shí)間眶拉,默認(rèn)是10000ms |
config.keepalive |
空閑連接的等待時(shí)間丸逸,默認(rèn)是60000ms |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=tcp-log" --data "config.host=127.0.0.1" --data "config.port=9999"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=tcp-log" --data "config.host=127.0.0.1" --data "config.port=9999"
curl -X POST http://kong:8001/consumers/{consumer}/plugins --data "name=tcp-log" --data "config.host=127.0.0.1" --data "config.port=9999"
日志格式
{
"request": {
"method": "GET",
"uri": "/get",
"url": "http://httpbin.org:8000/get",
"size": "75",
"querystring": {},
"headers": {
"accept": "*/*",
"host": "httpbin.org",
"user-agent": "curl/7.37.1"
},
"tls": {
"version": "TLSv1.2",
"cipher": "ECDHE-RSA-AES256-GCM-SHA384",
"supported_client_ciphers": "ECDHE-RSA-AES256-GCM-SHA384",
"client_verify": "NONE"
}
},
"upstream_uri": "/",
"response": {
"status": 200,
"size": "434",
"headers": {
"Content-Length": "197",
"via": "kong/0.3.0",
"Connection": "close",
"access-control-allow-credentials": "true",
"Content-Type": "application/json",
"server": "nginx",
"access-control-allow-origin": "*"
}
},
"tries": [
{
"state": "next",
"code": 502,
"ip": "127.0.0.1",
"port": 8000
},
{
"ip": "127.0.0.1",
"port": 8000
}
],
"authenticated_entity": {
"consumer_id": "80f74eef-31b8-45d5-c525-ae532297ea8e",
"id": "eaa330c0-4cff-47f5-c79e-b2e4f355207e"
},
"route": {
"created_at": 1521555129,
"hosts": null,
"id": "75818c5f-202d-4b82-a553-6a46e7c9a19e",
"methods": null,
"paths": [
"/example-path"
],
"preserve_host": false,
"protocols": [
"http",
"https"
],
"regex_priority": 0,
"service": {
"id": "0590139e-7481-466c-bcdf-929adcaaf804"
},
"strip_path": true,
"updated_at": 1521555129
},
"service": {
"connect_timeout": 60000,
"created_at": 1521554518,
"host": "example.com",
"id": "0590139e-7481-466c-bcdf-929adcaaf804",
"name": "myservice",
"path": "/",
"port": 80,
"protocol": "http",
"read_timeout": 60000,
"retries": 5,
"updated_at": 1521554518,
"write_timeout": 60000
},
"workspaces": [
{
"id":"b7cac81a-05dc-41f5-b6dc-b87e29b6c3a3",
"name": "default"
}
],
"consumer": {
"username": "demo",
"created_at": 1491847011000,
"id": "35b03bfc-7a5b-4a23-a594-aa350c585fa8"
},
"latencies": {
"proxy": 1430,
"kong": 9,
"request": 1921
},
"client_ip": "127.0.0.1",
"started_at": 1433209822425
}
參數(shù) |
描述 |
request |
客戶端發(fā)送的請(qǐng)求信息 |
response |
發(fā)送給客戶端的響應(yīng)信息 |
tries |
請(qǐng)求負(fù)載均衡記錄信息 |
route |
請(qǐng)求對(duì)應(yīng)的路由信息 |
service |
請(qǐng)求對(duì)應(yīng)的服務(wù)信息 |
authenticated_entity |
請(qǐng)求對(duì)應(yīng)的憑證信息 |
workspaces |
企業(yè)版本 >= 0.34 包含的信息远剩,可以忽略 |
consumer |
請(qǐng)求對(duì)應(yīng)的消費(fèi)者信息 |
latencies.proxy |
從接收到客戶端請(qǐng)求,到查找到指定服務(wù)花的時(shí)間 |
latencies.kong |
Kong所有插件運(yùn)行的時(shí)間 |
latencies.request |
請(qǐng)求的總時(shí)常 |
client_ip |
客戶端的原始IP |
started_at |
請(qǐng)求的時(shí)間戳 |
UDP Log
配置信息
屬性 |
描述 |
日志策略 |
UDP日志 |
插件作用域 |
服務(wù)、路由、消費(fèi)者、全局 |
適用協(xié)議 |
http、https |
參數(shù) |
描述 |
name |
插件名稱、此處為udp-log
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件蜈缤,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
config.host |
服務(wù)器Ip地址 |
config.port |
服務(wù)器端口 |
config.timeout |
發(fā)送數(shù)據(jù)的超時(shí)時(shí)間,默認(rèn)是10000ms |
config.keepalive |
空閑連接的等待時(shí)間备韧,默認(rèn)是60000ms |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=udp-log" --data "config.host=127.0.0.1" --data "config.port=9999" --data "config.timeout=10000"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=udp-log" --data "config.host=127.0.0.1" --data "config.port=9999" --data "config.timeout=10000"
curl -X POST http://kong:8001/consumers/{consumer}/plugins --data "name=udp-log" --data "config.host=127.0.0.1" --data "config.port=9999" --data "config.timeout=10000"
日志格式
{
"request": {
"method": "GET",
"uri": "/get",
"url": "http://httpbin.org:8000/get",
"size": "75",
"querystring": {},
"headers": {
"accept": "*/*",
"host": "httpbin.org",
"user-agent": "curl/7.37.1"
},
"tls": {
"version": "TLSv1.2",
"cipher": "ECDHE-RSA-AES256-GCM-SHA384",
"supported_client_ciphers": "ECDHE-RSA-AES256-GCM-SHA384",
"client_verify": "NONE"
}
},
"upstream_uri": "/",
"response": {
"status": 200,
"size": "434",
"headers": {
"Content-Length": "197",
"via": "kong/0.3.0",
"Connection": "close",
"access-control-allow-credentials": "true",
"Content-Type": "application/json",
"server": "nginx",
"access-control-allow-origin": "*"
}
},
"tries": [
{
"state": "next",
"code": 502,
"ip": "127.0.0.1",
"port": 8000
},
{
"ip": "127.0.0.1",
"port": 8000
}
],
"authenticated_entity": {
"consumer_id": "80f74eef-31b8-45d5-c525-ae532297ea8e",
"id": "eaa330c0-4cff-47f5-c79e-b2e4f355207e"
},
"route": {
"created_at": 1521555129,
"hosts": null,
"id": "75818c5f-202d-4b82-a553-6a46e7c9a19e",
"methods": null,
"paths": [
"/example-path"
],
"preserve_host": false,
"protocols": [
"http",
"https"
],
"regex_priority": 0,
"service": {
"id": "0590139e-7481-466c-bcdf-929adcaaf804"
},
"strip_path": true,
"updated_at": 1521555129
},
"service": {
"connect_timeout": 60000,
"created_at": 1521554518,
"host": "example.com",
"id": "0590139e-7481-466c-bcdf-929adcaaf804",
"name": "myservice",
"path": "/",
"port": 80,
"protocol": "http",
"read_timeout": 60000,
"retries": 5,
"updated_at": 1521554518,
"write_timeout": 60000
},
"workspaces": [
{
"id":"b7cac81a-05dc-41f5-b6dc-b87e29b6c3a3",
"name": "default"
}
],
"consumer": {
"username": "demo",
"created_at": 1491847011000,
"id": "35b03bfc-7a5b-4a23-a594-aa350c585fa8"
},
"latencies": {
"proxy": 1430,
"kong": 9,
"request": 1921
},
"client_ip": "127.0.0.1",
"started_at": 1433209822425
}
參數(shù) |
描述 |
request |
客戶端發(fā)送的請(qǐng)求信息 |
response |
發(fā)送給客戶端的響應(yīng)信息 |
tries |
請(qǐng)求負(fù)載均衡記錄信息 |
route |
請(qǐng)求對(duì)應(yīng)的路由信息 |
service |
請(qǐng)求對(duì)應(yīng)的服務(wù)信息 |
authenticated_entity |
請(qǐng)求對(duì)應(yīng)的憑證信息 |
workspaces |
企業(yè)版本 >= 0.34 包含的信息劫樟,可以忽略 |
consumer |
請(qǐng)求對(duì)應(yīng)的消費(fèi)者信息 |
latencies.proxy |
從接收到客戶端請(qǐng)求,到查找到指定服務(wù)花的時(shí)間 |
latencies.kong |
Kong所有插件運(yùn)行的時(shí)間 |
latencies.request |
請(qǐng)求的總時(shí)常 |
client_ip |
客戶端的原始IP |
started_at |
請(qǐng)求的時(shí)間戳 |
轉(zhuǎn)換插件
Correlation ID
配置信息
屬性 |
描述 |
轉(zhuǎn)換策略 |
關(guān)聯(lián)ID |
插件作用域 |
服務(wù)织堂、路由叠艳、消費(fèi)者、全局 |
適用協(xié)議 |
http易阳、https |
參數(shù) |
描述 |
name |
插件名稱附较、此處為correlation-id
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
config.header_name |
用來關(guān)聯(lián)Id的Http頭潦俺,默認(rèn)是 Kong-Request-ID
|
config.generator |
關(guān)聯(lián)Id的生成器拒课,可以取 uuid 、uuid#counter 或 tracker 事示,默認(rèn)是 uuid#counter
|
config.echo_downstream |
是否將 Kong-Request-ID 返回給客戶端早像,默認(rèn)是false |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=correlation-id" --data "config.header_name=Kong-Request-ID" --data "config.generator=uuid#counter" --data "config.echo_downstream=false"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=correlation-id" --data "config.header_name=Kong-Request-ID" --data "config.generator=uuid#counter" --data "config.echo_downstream=false"
curl -X POST http://kong:8001/consumers/{consumer}/plugins --data "name=correlation-id" --data "config.header_name=Kong-Request-ID" --data "config.generator=uuid#counter" --data "config.echo_downstream=false"
ID生成器
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
使用這種格式時(shí),每個(gè)請(qǐng)求以十六進(jìn)制形式生成UUID
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx#counter
使用這種格式時(shí)肖爵,基于每個(gè) worker 生成UUID卢鹦,之后的請(qǐng)求會(huì)在UUID之后添加一個(gè)累加器,每個(gè)計(jì)數(shù)器從0開始計(jì)數(shù)劝堪,每個(gè) worker 之間都是獨(dú)立的冀自,這種格式性能比較高,但是比較難以存儲(chǔ)并進(jìn)一步進(jìn)行處理
ip-port-pid-connection-connection_requests-timestamp
使用這種格式時(shí)秒啦,關(guān)聯(lián)Id更有實(shí)際意義:
參數(shù) |
描述 |
ip |
處理請(qǐng)求的服務(wù)器地址 |
port |
處理請(qǐng)求的服務(wù)器端口 |
pid |
nginx worker進(jìn)程 |
connection |
連接序列號(hào) |
connection_requests |
連接的請(qǐng)求序列號(hào) |
timestamp |
時(shí)間戳 |
Request Transformer
配置信息
屬性 |
描述 |
轉(zhuǎn)換策略 |
請(qǐng)求轉(zhuǎn)換 |
插件作用域 |
服務(wù)熬粗、路由、消費(fèi)者余境、全局 |
適用協(xié)議 |
http驻呐、https |
參數(shù) |
描述 |
name |
插件名稱、此處為request-transformer
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件芳来,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
config.http_method |
更改請(qǐng)求的Http方法 |
config.remove.headers |
刪除請(qǐng)求頭 |
config.remove.querystring |
刪除請(qǐng)求參數(shù) |
config.remove.body |
刪除請(qǐng)求體中的參數(shù)暴氏,content-type為 application/json 、 multipart/form-data 或 application/x-www-form-urlencoded 中的一個(gè) |
config.replace.headers |
替換請(qǐng)求頭绣张,形式為鍵值對(duì) |
config.replace.querystring |
替換請(qǐng)求參數(shù),形式為鍵值對(duì) |
config.replace.body |
替換請(qǐng)求體中的參數(shù)关带,形式為鍵值對(duì)侥涵,content-type為 application/json 沼撕、 multipart/form-data 或 application/x-www-form-urlencoded 中的一個(gè) |
config.rename.headers |
重命名請(qǐng)求頭,形式為鍵值對(duì) |
config.rename.querystring |
重命名請(qǐng)求參數(shù)芜飘,形式為鍵值對(duì) |
config.rename.body |
重命名請(qǐng)求體中的參數(shù)务豺,形式為鍵值對(duì),content-type為 application/json 嗦明、 multipart/form-data 或 application/x-www-form-urlencoded 中的一個(gè) |
config.add.headers |
添加請(qǐng)求頭笼沥,形式為鍵值對(duì),如果原請(qǐng)求中沒有該參數(shù)娶牌,添加一個(gè)奔浅;如果原請(qǐng)求中已經(jīng)有了,直接忽略 |
config.add.querystring |
添加請(qǐng)求參數(shù)诗良,形式為鍵值對(duì)汹桦,如果原請(qǐng)求中沒有該參數(shù),添加一個(gè)鉴裹;如果原請(qǐng)求中已經(jīng)有了舞骆,直接忽略 |
config.add.body |
添加請(qǐng)求體中的參數(shù),形式為鍵值對(duì)径荔,content-type為 application/json 督禽、 multipart/form-data 或 application/x-www-form-urlencoded 中的一個(gè),如果原請(qǐng)求中沒有該參數(shù)总处,添加一個(gè)狈惫;如果原請(qǐng)求中已經(jīng)有了,直接忽略 |
config.append.headers |
追加請(qǐng)求頭辨泳,形式為鍵值對(duì)虱岂,如果原請(qǐng)求中沒有該參數(shù),添加一個(gè)菠红;如果原請(qǐng)求中已經(jīng)有了第岖,再原樣添加一個(gè) |
config.append.querystring |
追加請(qǐng)求參數(shù),形式為鍵值對(duì)试溯,如果原請(qǐng)求中沒有該參數(shù)蔑滓,添加一個(gè);如果原請(qǐng)求中已經(jīng)有了遇绞,直接忽略 |
config.append.body |
追加請(qǐng)求體中的參數(shù)键袱,形式為鍵值對(duì),content-type為 application/json 摹闽、 multipart/form-data 或 application/x-www-form-urlencoded 中的一個(gè)蹄咖,如果原請(qǐng)求中沒有該參數(shù),添加一個(gè)付鹿;如果原請(qǐng)求中已經(jīng)有了澜汤,直接忽略 |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=request-transformer" --data "config.remove.headers=x-toremove" --data "config.remove.headers=x-another-one" --data "config.remove.querystring=qs-old-name:qs-new-name" --data "config.remove.querystring=qs2-old-name:qs2-new-name" --data "config.remove.body=formparam-toremove" --data "config.remove.body=formparam-another-one" --data "config.rename.headers=header-old-name:header-new-name" --data "config.rename.headers=another-old-name:another-new-name" --data "config.rename.querystring=qs-old-name:qs-new-name" --data "config.rename.querystring=qs2-old-name:qs2-new-name" --data "config.rename.body=param-old:param-new" --data "config.rename.body=param2-old:param2-new" --data "config.add.headers=x-new-header:value" --data "config.add.headers=x-another-header:something" --data "config.add.querystring=new-param:some_value" --data "config.add.querystring=another-param:some_value" --data "config.add.body=new-form-param:some_value" --data "config.add.body=another-form-param:some_value"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=request-transformer" --data "config.remove.headers=x-toremove" --data "config.remove.headers=x-another-one" --data "config.remove.querystring=qs-old-name:qs-new-name" --data "config.remove.querystring=qs2-old-name:qs2-new-name" --data "config.remove.body=formparam-toremove" --data "config.remove.body=formparam-another-one" --data "config.rename.headers=header-old-name:header-new-name" --data "config.rename.headers=another-old-name:another-new-name" --data "config.rename.querystring=qs-old-name:qs-new-name" --data "config.rename.querystring=qs2-old-name:qs2-new-name" --data "config.rename.body=param-old:param-new" --data "config.rename.body=param2-old:param2-new" --data "config.add.headers=x-new-header:value" --data "config.add.headers=x-another-header:something" --data "config.add.querystring=new-param:some_value" --data "config.add.querystring=another-param:some_value" --data "config.add.body=new-form-param:some_value" --data "config.add.body=another-form-param:some_value"
curl -X POST http://kong:8001/consumers/{consumer}/plugins --data "name=request-transformer" --data "config.remove.headers=x-toremove" --data "config.remove.headers=x-another-one" --data "config.remove.querystring=qs-old-name:qs-new-name" --data "config.remove.querystring=qs2-old-name:qs2-new-name" --data "config.remove.body=formparam-toremove" --data "config.remove.body=formparam-another-one" --data "config.rename.headers=header-old-name:header-new-name" --data "config.rename.headers=another-old-name:another-new-name" --data "config.rename.querystring=qs-old-name:qs-new-name" --data "config.rename.querystring=qs2-old-name:qs2-new-name" --data "config.rename.body=param-old:param-new" --data "config.rename.body=param2-old:param2-new" --data "config.add.headers=x-new-header:value" --data "config.add.headers=x-another-header:something" --data "config.add.querystring=new-param:some_value" --data "config.add.querystring=another-param:some_value" --data "config.add.body=new-form-param:some_value" --data "config.add.body=another-form-param:some_value"
- 參數(shù)執(zhí)行順序
刪除 -> 重命名 -> 替換 -> 添加 -> 追加
Response Transformer
配置信息
屬性 |
描述 |
轉(zhuǎn)換策略 |
響應(yīng)轉(zhuǎn)換 |
插件作用域 |
服務(wù)蚜迅、路由、消費(fèi)者俊抵、全局 |
適用協(xié)議 |
http谁不、https |
參數(shù) |
描述 |
name |
插件名稱、此處為response-transformer
|
service_id |
綁定的服務(wù)Id |
route_id |
綁定的路由Id |
enabled |
是否啟用該插件徽诲,默認(rèn)是true |
consumer_id |
綁定的消費(fèi)者Id |
config.remove.headers |
刪除響應(yīng)頭 |
config.remove.json |
刪除響應(yīng)體 |
config.replace.headers |
替換響應(yīng)頭刹帕,形式為鍵值對(duì) |
config.replace.json |
替換響應(yīng)體,形式為鍵值對(duì) |
config.add.headers |
添加響應(yīng)頭谎替,形式為鍵值對(duì)偷溺,如果原響應(yīng)中沒有該參數(shù),添加一個(gè)院喜;如果原響應(yīng)中已經(jīng)有了亡蓉,直接忽略 |
config.add.json |
添加響應(yīng)體,形式為鍵值對(duì)喷舀,如果原響應(yīng)中沒有該參數(shù)砍濒,添加一個(gè);如果原響應(yīng)中已經(jīng)有了硫麻,直接忽略 |
config.append.headers |
追加響應(yīng)頭爸邢,形式為鍵值對(duì),如果原響應(yīng)中沒有該參數(shù)拿愧,添加一個(gè)杠河;如果原響應(yīng)中已經(jīng)有了,再原樣添加一個(gè) |
config.append.json |
追加響應(yīng)體浇辜,形式為鍵值對(duì)券敌,如果原響應(yīng)中沒有該參數(shù),添加一個(gè)柳洋;如果原響應(yīng)中已經(jīng)有了待诅,再原樣添加一個(gè) |
使用詳情
curl -X POST http://kong:8001/services/{service}/plugins --data "name=response-transformer" --data "config.remove.headers=x-toremove, x-another-one" --data "config.remove.json=json-key-toremove, another-json-key" --data "config.add.headers=x-new-header:value,x-another-header:something" --data "config.add.json=new-json-key:some_value, another-json-key:some_value" --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value"
curl -X POST http://kong:8001/routes/{route}/plugins --data "name=response-transformer" --data "config.remove.headers=x-toremove, x-another-one" --data "config.remove.json=json-key-toremove, another-json-key" --data "config.add.headers=x-new-header:value,x-another-header:something" --data "config.add.json=new-json-key:some_value, another-json-key:some_value" --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value"
curl -X POST http://kong:8001/consumers/{consumer}/plugins --data "name=response-transformer" --data "config.remove.headers=x-toremove, x-another-one" --data "config.remove.json=json-key-toremove, another-json-key" --data "config.add.headers=x-new-header:value,x-another-header:something" --data "config.add.json=new-json-key:some_value, another-json-key:some_value" --data "config.append.headers=x-existing-header:some_value, x-another-header:some_value"
- 參數(shù)執(zhí)行順序
刪除 -> 替換 -> 添加 -> 追加