認證說明
- Etcd v2以上的版本才支援權限認證,且僅支援Basic Auth
- Etcd通過使用者(user)-角色(role)-權限的方式來控制通路,使用者關聯角色,角色擁有權限,進而使用者也就擁有了相應的權限
- Etcd預設沒有啟用認證機制,隻要能連接配接etcd服務就擁有所有的權限,還是非常危險的,另一種安全的通路方式是開啟ssl,隻有使用受信任的證書才能通路資料
- Etcd開啟Basic Auth之後,預設會啟用兩個角色root和guest,root角色擁有所有權限,guest擁有隻讀權限,這兩個角色都不要删除,否則你可能會遇到意想不到的Bug
- Etcd的權限分為隻讀、隻寫、可讀寫,可以對etcd的詳細key進行授權,例如:/conf/project/dev/nginx.conf,也可以授權key字首(目錄),例如:/conf/project/,授權規則應以最小滿足需求為準則
權限設計
權限設計應先考慮我們對權限的需求,從需求出發設計權限
- 需求
- 為了友善後續管理,規定配置中心所有key都應已/conf/開頭
- 需要兩個賬号,一個賬号用在Kerrigan(WebUI)擁有讀取、寫入、修改、删除key的權限,一個賬号用在confd,隻有隻讀的權限,能夠讀取配置就可以了
- 設計
- 需求很簡單,我們需要建立兩個賬号,分别對應兩個角色,兩個角色都是對/conf/開頭的Key進行控制,一個讀寫權限,一個隻讀權限
- 定義隻讀賬号名為readx,隻讀角色名為readConf,定義讀寫權限賬号名為authz,讀寫權限為rootConf,可操作的key都為/conf/開頭
詳細步驟
1.添加root使用者
# etcdctl user add root
New password: 12345
User root created
2.建立root賬号後,root預設有root角色,對所有KV有讀寫權限
# etcdctl user get root
User: root
Roles: root
# etcdctl role get root
Role: root
KV Read:
/*
KV Write:
/*
3.開啟auth認證
# etcdctl auth enable
Authentication Enabled
開啟權限認證後預設會多一個guest的角色
# etcdctl --username root:12345 role list
guest
root
4.添加非root賬号,一個authz的賬号,一個readx的賬号
# etcdctl --username root:12345 user add authz
New password:
User authz created
# etcdctl --username root:12345 user add readx
New password:
User readx created
5.添加角色,一個rootConf的角色,一個readConf的角色
# etcdctl --username root:12345 role add rootConf
Role rootConf created
# etcdctl --username root:12345 role add readConf
Role readConf created
6.為角色授權,readConf角色對/conf有隻讀權限,rootConf角色對/conf有讀寫權限
# etcdctl --username root:12345 role grant --read --path /conf/* readConf
Role readConf updated
# etcdctl --username root:12345 role grant --readwrite --path /conf/* rootConf
Role rootConf updated
7.給使用者配置設定角色,authz賬号配置設定rootConf角色,readx賬号配置設定readConf角色
# etcdctl --username root:12345 user grant --roles rootConf authz
User authz updated
# etcdctl --username root:12345 user grant --roles readConf readx
User readx updated
8.檢視使用者所擁有的角色
# etcdctl --username root:12345 user get authz
User: authz
Roles: rootConf
# etcdctl --username root:12345 user get readx
User: readx
Roles: readConf
這樣readx賬号就對/conf下的所有檔案有了隻讀權限,authz對/conf下的所有檔案有了讀寫權限
常用指令
有一些指令上邊沒有介紹到,會用得到的如下:
1.關閉認證
# etcdctl --username root:12345 auth disable
2.删除使用者
# etcdctl --username root:12345 user remove userx
3.使用者撤銷角色
# etcdctl --username root:12345 user revoke rolex
4.修改使用者密碼
# etcdctl --username root:12345 user passwd
同時還有删除角色、撤銷角色權限可參看上邊使用者相關操作
踩坑記錄
在開啟認證後發現系統預設給添加了guest角色,覺得guest角色沒用就給删除了,于是再連接配接etcd叢集時就報如下錯誤:
報錯:
The request requires user authentication (Insufficient credentials)
解決:重新添加guest角色
原文釋出時間為:2018-09-12
本文作者:37丫37
本文來自雲栖社群合作夥伴“運維咖啡吧”,了解相關資訊可以關注“運維咖啡吧”。