1. 程式人生 > >etcd入門系列三:身份驗證訪問控制

etcd入門系列三:身份驗證訪問控制

etcd入門系列

一. etcd在docker中的安裝與使用
二. etcd 開啟 https

1. 簡介

etcd 預設是沒有開啟訪問控制的,如果我們開啟外網訪問的話就需要考慮訪問控制的問題,etcd 提供了兩種訪問控制的方式:

  1. 基於身份驗證的訪問控制
  2. 基於證書的訪問控制

這節主要是選擇第一種方式,進行的講解,由於之前文章中是採用http介面的方式通訊,為了更全面的瞭解 etcd 的使用,本節使用官方提供的 etcdctl 工具進行與伺服器之間的通訊。

2. root 使用者

rootetcd 的超級管理員,擁有 etcd 的所有許可權,在開啟角色認證之前為們必須要先建立好 root

使用者。還需要注意的是 root 使用者必須擁有 root 的角色,允許在 etcd 的所有操作.

3. root 角色

root 角色可以賦予任何使用者,擁有 root 角色的使用者有全域性讀寫許可權和叢集身份驗證配置許可權,此外,還具有修改叢集成員身份,碎片整理,建立快照等許可權。

4. 使用者操作

檢視使用者列表:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user list

建立使用者:

 $ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user add user1

使用者可以被賦予角色,也可以被撤銷角色:

#賦予許可權
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user grant --roles root user1
# 撤銷許可權
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user revoke --roles root user1

修改使用者密碼:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user passwd user1

5. 角色操作

角色列表:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role list

建立角色:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role add myrolename
  • 角色沒有密碼,僅僅是定義的一組訪問許可權
  • 角色的訪問許可權可以被賦予read(讀),write(寫),readwrite(讀和寫)許可權

賦予訪問許可權範例:

# 給 role1 角色賦予鍵 /foo 的讀操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role grant role1 --path /foo --read
# 給 role1 角色賦予鍵 /foo 的寫操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role grant role1 --path /foo --write
# 給 role1 角色賦予鍵 /foo 讀寫操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role grant role1 --path /foo --rw
# 給 role1 角色賦予鍵 /foo 目錄讀寫操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role grant role1 --path /foo/* --rw

收回訪問許可權範例:

# 收回 role1 角色對 /foo 的讀操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role revoke role1 --path /foo --read
# 收回 role1 角色對 /foo 的寫操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role revoke role1 --path /foo --write
# 收回 role1 角色對 /foo 的讀寫操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role revoke role1 --path /foo --rw
# 收回 role1 角色對 /foo 目錄的讀寫操作
$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role revoke role1 --path /foo/* --rw

檢視角色訪問許可權:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role get role1

刪除角色 :

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 role remove role1

6. 開啟身份驗證

1.增加root使用者:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 user add root
Password of root:

2.開啟身份驗證:

$ etcdctl --ca-file /root/cfssl/ca.pem --endpoints https://192.168.3.3:2379 etcdctl auth enable

至此,身份驗證已經開啟,執行下面命令

etcdctl --ca-file /root/cfssl/ca.pem  --endpoints https://192.168.3.3:2379 set /foo bar
//返回bar

這裡出現了一個問題,我們已經開啟了身份訪問驗證,卻仍然可以不通過任何身份進行操作,這是什麼原因呢?其實是因為在 Etcd 開啟 Basic Auth 之後,預設會啟用兩個角色 rootguestrootguest 角色都擁有所有許可權,當我們未指定身份的時候其實是通過 guest 角色進行的操作,這裡需要注意的是兩個角色都不要刪除,否則你可能會遇到意想不到的Bug,既然無法刪除,那麼為們可以通過收回許可權的方式對 guest 的許可權進行限制,執行下面程式碼:

etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 ro
le revoke guest --path '/*' --rw
//返回:Role guest updated

//檢視guest最新的許可權資訊
etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 ro
le get guest


//返回 :
 //       Role: guest
//        KV Read:
//        KV Write:
// ok 收回許可權成功

繼續執行之前的set程式碼 :

etcdctl --ca-file /root/cfssl/ca.pem  --endpoints https://192.168.3.3:2379 set /foo bar
//Error:  110: The request requires user authentication (Insufficient credentials) [0]

如我們所願的已經不可以對 etcd 進行操作了,下面我們建立一個使用者並賦予一個新建的角色試試:

# 建立user2使用者
etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 user add user2

New password: 
//返回 :User user2 created

# 建立role2角色
etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 role add role2

Role role2 created

# 賦予role2 角色許可權

etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 ro
le grant role2 --path /foo --rw
Role role2 updated


# 將使用者user2賦予角色role2
etcdctl --ca-file /root/cfssl/ca.pem --username root:passwod --endpoints https://192.168.3.3:2379 us
er  grant --roles role2 user2

User user2 updated

# 設定 foo 值
etcdctl --ca-file /root/cfssl/ca.pem --username user2:passwod --endpoints https://192.168.3.3:2379:2379 set foo bar

bar

到此我們想要實現的功能都已實現,個人理解難免有些疏漏和不足,歡迎大神斧正。

7. 參考連線

Role-based access control
Etcd安全配置之Basic Auth認證

原文連線:http://www.artacode.com/posts/etcd/auth/