使用java實現keystone v3相關功能與概念:html
(圖1)
java
Registerapi
服務中介與權限管理.緩存
Provider架構
服務提供者.dom
Consumercurl
服務消費者.分佈式
(圖2)
ide
關鍵測試
系統管理用戶: ADMIN域ADMIN項目ADMIN角色的用戶. 容許:
系統管理用戶不是超級管理用戶. 系統用戶不能干擾域的平常管理, 例如建立用戶, 建立項目, 加減用戶角色等.
域管理用戶: 特定域ADMIN項目ADMIN角色的用戶. 容許:
項目管理用戶: 特定域特定項目ADMIN角色的用戶. 容許操做由各個域發佈的策略(policy)決定.
項目普通用戶: 除系統管理用戶, 域管理用戶, 項目管理用戶外的其餘用戶.
項目管理用戶與項目普通用戶的容許行爲 由域自己定義.
curl -XPOST 'https://oauth.huya.com/v1/domain/createDomain' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))"' -d '{"domain":"my_domain","user":"my_admin","pass":"123","enabled":true}' response: 200 OK {"errno":0,"data":{"domain":"my_domain"}} 結果: - 建立my_domain域 - 在my_domain域建立ADMIN項目 - 在my_domain域建立my_admin管理用戶,其密碼爲123.
curl -XDELETE 'https://oauth.huya.com/v1/domain/destroyDomain?domain=my_domain2' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0} 注意: - 無用域必須沒有項目或僅僅含有ADMIN項目. 刪除域會清除該域下全部用戶,項目,服務,策略等數據.
curl -XPOST 'https://oauth.huya.com/v1/domain/createRole' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))"' -d '{"role":"SERVICE","remark":"服務角色"}' response: 200 OK {"errno":0,"data":{"role":"SERVICE","remark":"服務角色"}}
curl -XDELETE 'https://oauth.huya.com/v1/domain/destroyRole?role=SERVICE2' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0} 注意: - 無用角色必須沒有policy或principal引用.
curl -XPUT 'https://oauth.huya.com/v1/system/updateAllowHosts' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0}
curl -XPUT 'https://oauth.huya.com/v1/system/updateDomainCache' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0}
curl -XPOST 'https://oauth.huya.com/v1/domain/createUser' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))"' -d '{"user":"my_user","pass":"456","remark":"this is a test user","enabled":true}' response: 200 OK {"errno":0,"data":{"domain":"my_domain","user":"my_user","remark":"this is a test user","enabled":true}}
curl -XPUT 'https://oauth.huya.com/v1/domain/enableUser' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))"' -d '{"user":"my_user","enabled":true}' response: 200 OK {"errno":0}
curl -XDELETE 'https://oauth.huya.com/v1/domain/destroyUser?user=my_user2' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0}
curl -XPOST 'https://oauth.huya.com/v1/domain/createProject' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))"' -d '{"project":"my_project","remark":"這是個人測試項目!","enabled":true}' response: 200 OK {"errno":0,"data":{"domain":"my_domain","project":"my_project","remark":"這是個人測試項目!","enabled":true}}
curl -XPUT 'https://oauth.huya.com/v1/domain/enableProject' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))"' -d '{"project":"my_project2","enabled":true}' response: 200 OK {"errno":0}
curl -XDELETE 'https://oauth.huya.com/v1/domain/destroyProject?project=my_project2' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0}
curl -XPOST 'https://oauth.huya.com/v1/domain/addUserRole' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))"' -d '{"user":"my_user","project":"my_project","role":"SERVICE"}' response: 200 OK {"errno":0,"data":{"domain":"my_domain","user":"my_user","project":"my_project","role":"SERVICE"}}
curl -XGET 'https://oauth.huya.com/v1/domain/getUserRoles?user=my_user&project=my_project' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0,"data":["ADMIN","SERVICE"]}
curl -XDELETE 'https://oauth.huya.com/v1/domain/delUserRole?user=my_user&project=my_project&role=ADMIN' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0}
curl -XGET 'https://oauth.huya.com/v1/domain/getDomainUser' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0,"data":[{"domain":"my_domain","user":"my_admin","enabled":true},{"domain":"my_domain","user":"my_user","remark":"this is a test user","enabled":true}]}
curl -XGET 'https://oauth.huya.com/v1/domain/getDomainProject' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0,"data":[{"domain":"my_domain","project":"ADMIN","enabled":true},{"domain":"my_domain","project":"my_project","remark":"這是個人測試項目!","enabled":true}]}
curl -XPUT 'https://oauth.huya.com/v1/domain/publishService' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))"' -d '{"endpoint":"https://cdn.game.yy.com/v1","apis":[{"api":"api_name_0","method":"GET","path":"/service/action0","category":"test"},{"api":"api_name_1","method":"GET","path":"/service/action1","category":"test"},{"api":"api_name_2","method":"GET","path":"/service/action2","category":"test"},{"api":"api_name_3","method":"GET","path":"/service/action3","category":"test"},{"api":"api_name_4","method":"GET","path":"/service/action4","category":"test"},{"api":"api_name_5","method":"GET","path":"/service/action5","category":"test"},{"api":"api_name_6","method":"GET","path":"/service/action6","category":"test"},{"api":"api_name_7","method":"GET","path":"/service/action7","category":"test"},{"api":"api_name_8","method":"GET","path":"/service/action8","category":"test"},{"api":"api_name_9","method":"GET","path":"/service/action9","category":"test"}],"policies":[{"role":"SERVICE","rules":"test,test:*"}]}' response: 200 OK {"errno":0} 注意: - 發佈服務能夠指定endpoint, apis, policies. 每次發佈這些信息都是全量覆蓋.
curl -XPOST 'https://oauth.huya.com/v1/domain/verifyRequest' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))"' -d '{"domain":"my_domain","user":"my_user","project":"my_project","expires":"1598b5b3eb7","nonce":"74a465fddab8b","signature":"56f8519d7f31460821e4722de0c77c5f","api":"api_name_0"}' response: 200 OK {"errno":0,"data":{"domain":"my_domain","user":"my_user","project":"my_project","expires":"1598b5b3eb7","nonce":"74a465fddab8b","signature":"56f8519d7f31460821e4722de0c77c5f","api":"api_name_0","roles":["SERVICE"]}} - 若是指定api, 則根據policy規則校驗 - 若是不指定api, 則僅僅驗證簽名
curl -XGET 'https://oauth.huya.com/v1/domain/lookupService?service=my_domain' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0,"data":{"endpoint":"https://cdn.game.yy.com/v1","apis":[{"api":"api_name_4","method":"GET","path":"/service/action4","category":"test"},{"api":"api_name_3","method":"GET","path":"/service/action3","category":"test"},{"api":"api_name_6","method":"GET","path":"/service/action6","category":"test"},{"api":"api_name_5","method":"GET","path":"/service/action5","category":"test"},{"api":"api_name_0","method":"GET","path":"/service/action0","category":"test"},{"api":"api_name_2","method":"GET","path":"/service/action2","category":"test"},{"api":"api_name_1","method":"GET","path":"/service/action1","category":"test"},{"api":"api_name_8","method":"GET","path":"/service/action8","category":"test"},{"api":"api_name_7","method":"GET","path":"/service/action7","category":"test"},{"api":"api_name_9","method":"GET","path":"/service/action9","category":"test"}]}}
curl -XGET 'https://oauth.huya.com/v1/domain/getAllRole' -H "X-AUTH-DOMAIN:${domain}" #required -H "X-AUTH-USER:${user}" #required -H "X-AUTH-PROJECT:${project}" #optional, maybe null -H "X-AUTH-EXPIRES:${HEX(expires_millis)}" #required, hex format of expires millliseonds -H "X-AUTH-NONCE:${HEX(nonce_nanos)}" #required, hex format of nonce nanoseonds -H "X-AUTH-SIGNATURE:${HEX(signature)} #required, hex format of signatue=MD5(domain,user,project(maybe null),SHA1(pass),HEX(expires_millis),HEX(nonce_nanos))" response: 200 OK {"errno":0,"data":[{"role":"ADMIN","remark":"全局管理角色"},{"role":"SERVICE","remark":"服務角色"},{"role":"ut_role_d6a62c98_c243_4fcc_9a61_b732185ffb3d"}]}
curl -XGET 'https://oauth.huya.com/v1/domain/lookupService?service=my_domain' \ -H "X-AUTH-DOMAIN:${DOMAIN}" \ -H "X-AUTH-USER:${USER}" \ -H "X-AUTH-PASS:${PASS}" response: 200 OK {"errno":0,"data":{"endpoint":"","apis":[{"api":"api_name_4","method":"GET","path":"/service/action4"},{"api":"api_name_3","method":"GET","path":"/service/action3"},{"api":"api_name_6","method":"GET","path":"/service/action6"},{"api":"api_name_5","method":"GET","path":"/service/action5"},{"api":"api_name_0","method":"GET","path":"/service/action0"},{"api":"api_name_2","method":"GET","path":"/service/action2"},{"api":"api_name_1","method":"GET","path":"/service/action1"},{"api":"api_name_8","method":"GET","path":"/service/action8"},{"api":"api_name_7","method":"GET","path":"/service/action7"},{"api":"api_name_9","method":"GET","path":"/service/action9"}]}}
請求頭:
X-AUTH-DOMAIN: 域 X-AUTH-USER: 用戶 X-AUTH-PROJECT: 項目,可選 X-AUTH-EXPIRES: 有效時間點毫秒時間戳的16進制 X-AUTH-NONCE: 唯一隨機數值, 通常使用當前納秒時間戳的16進制 X-AUTH-SIGNATURE: 用戶簽名, 規則見下
規則:
-帶項目: signature=md5sum(domain,user,project,sha1sum(pass),hex(expires_millis),hex(current_nanos)) -不帶項目: signature=md5sum(domain,user,sha1sum(pass),hex(expires_millis),hex(current_nanos))
例子:
假設my_domain的my_user的密碼爲456, 其要訪問my_project的數據. 則相應腳本: expires_millis_hex=$(printf '%x' $(($(date +%s)*1000+5000))) nonce_nanos_hex=$(printf '%x' $(date +%N)) pass_sha1=$(printf 456 | openssl sha1 | awk '{print $2}') signature=$(printf '%s%s%s%s%s%s' my_domain my_user $pass_sha1 my_project $expires_millis_hex $nonce_nanos_hex | openssl md5 | awk '{print $2}') curl -XGET 'https://test.huya.com/v1/api_name_0' \ -H "X-AUTH-DOMAIN:my_domain" \ -H "X-AUTH-USER:my_user" \ -H "X-AUTH-PROJECT:my_project" \ -H "X-AUTH-EXPIRES:${HEX(expires_millis_hex)}" \ -H "X-AUTH-NONCE:nonce_nanos_hex" \ -H "X-AUTH-SIGNATURE:signature" \
提取http request中的X-AUTH-*頭部,發往ikeystone驗證, 成功返回對應用戶的角色等信息:
假設test服務管理員爲test, 密碼也爲456, 驗證請求腳本(與ikeystone交互不須要項目) expires_millis_hex=$(printf '%x' $(($(date +%s)*1000+5000))) nonce_nanos_hex=$(printf '%x' $(date +%N)) pass_sha1=$(printf 456 | openssl sha1 | awk '{print $2}') signature=$(printf '%s%s%s%s%s' test test $pass_sha1 $expires_millis_hex $nonce_nanos_hex | openssl md5 | awk '{print $2}') curl -XPOST 'https://oauth.huya.com/v1/domain/verifyRequest' \ -H "X-AUTH-DOMAIN:my_domain" \ -H "X-AUTH-USER:my_user" \ -H "X-AUTH-EXPIRES:${HEX(expires_millis_hex)}" \ -H "X-AUTH-NONCE:nonce_nanos_hex" \ -H "X-AUTH-SIGNATURE:signature" \ -d '{"domain":"my_domain","user":"my_user","project":"my_project","expires":"1598b7efac5","nonce":"74c67a48ebe23","signature":"bd99837ae32dcda3f21c91b7f95671cf","api":"api_name_0"}' response: 200 OK {"errno":0,"data":{"domain":"my_domain","user":"my_user","project":"my_project","expires":"1598b7efac5","nonce":"74c67a48ebe23","signature":"bd99837ae32dcda3f21c91b7f95671cf","api":"api_name_0","roles":["SERVICE"]}}