ctl_v3_auth_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package e2e
  15. import (
  16. "fmt"
  17. "testing"
  18. )
  19. func TestCtlV3AuthEnable(t *testing.T) { testCtl(t, authEnableTest) }
  20. func TestCtlV3AuthDisable(t *testing.T) { testCtl(t, authDisableTest) }
  21. func TestCtlV3AuthWriteKey(t *testing.T) { testCtl(t, authCredWriteKeyTest) }
  22. func TestCtlV3AuthRoleUpdate(t *testing.T) { testCtl(t, authRoleUpdateTest) }
  23. func TestCtlV3AuthUserDeleteDuringOps(t *testing.T) { testCtl(t, authUserDeleteDuringOpsTest) }
  24. func TestCtlV3AuthRoleRevokeDuringOps(t *testing.T) { testCtl(t, authRoleRevokeDuringOpsTest) }
  25. func authEnableTest(cx ctlCtx) {
  26. if err := authEnable(cx); err != nil {
  27. cx.t.Fatal(err)
  28. }
  29. }
  30. func authEnable(cx ctlCtx) error {
  31. // create root user with root role
  32. if err := ctlV3User(cx, []string{"add", "root", "--interactive=false"}, "User root created", []string{"root"}); err != nil {
  33. return fmt.Errorf("failed to create root user %v", err)
  34. }
  35. if err := ctlV3User(cx, []string{"grant-role", "root", "root"}, "Role root is granted to user root", nil); err != nil {
  36. return fmt.Errorf("failed to grant root user root role %v", err)
  37. }
  38. if err := ctlV3AuthEnable(cx); err != nil {
  39. return fmt.Errorf("authEnableTest ctlV3AuthEnable error (%v)", err)
  40. }
  41. return nil
  42. }
  43. func ctlV3AuthEnable(cx ctlCtx) error {
  44. cmdArgs := append(cx.PrefixArgs(), "auth", "enable")
  45. return spawnWithExpect(cmdArgs, "Authentication Enabled")
  46. }
  47. func authDisableTest(cx ctlCtx) {
  48. if err := ctlV3AuthDisable(cx); err != nil {
  49. cx.t.Fatalf("authDisableTest ctlV3AuthDisable error (%v)", err)
  50. }
  51. }
  52. func ctlV3AuthDisable(cx ctlCtx) error {
  53. cmdArgs := append(cx.PrefixArgs(), "auth", "disable")
  54. return spawnWithExpect(cmdArgs, "Authentication Disabled")
  55. }
  56. func authCredWriteKeyTest(cx ctlCtx) {
  57. // baseline key to check for failed puts
  58. if err := ctlV3Put(cx, "foo", "a", ""); err != nil {
  59. cx.t.Fatal(err)
  60. }
  61. if err := authEnable(cx); err != nil {
  62. cx.t.Fatal(err)
  63. }
  64. cx.user, cx.pass = "root", "root"
  65. authSetupTestUser(cx)
  66. // confirm root role can access to all keys
  67. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  68. cx.t.Fatal(err)
  69. }
  70. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  71. cx.t.Fatal(err)
  72. }
  73. // try invalid user
  74. cx.user, cx.pass = "a", "b"
  75. if err := ctlV3PutFailAuth(cx, "foo", "bar"); err != nil {
  76. cx.t.Fatal(err)
  77. }
  78. // confirm put failed
  79. cx.user, cx.pass = "test-user", "pass"
  80. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  81. cx.t.Fatal(err)
  82. }
  83. // try good user
  84. cx.user, cx.pass = "test-user", "pass"
  85. if err := ctlV3Put(cx, "foo", "bar2", ""); err != nil {
  86. cx.t.Fatal(err)
  87. }
  88. // confirm put succeeded
  89. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar2"}}...); err != nil {
  90. cx.t.Fatal(err)
  91. }
  92. // try bad password
  93. cx.user, cx.pass = "test-user", "badpass"
  94. if err := ctlV3PutFailAuth(cx, "foo", "baz"); err != nil {
  95. cx.t.Fatal(err)
  96. }
  97. // confirm put failed
  98. cx.user, cx.pass = "test-user", "pass"
  99. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar2"}}...); err != nil {
  100. cx.t.Fatal(err)
  101. }
  102. }
  103. func authRoleUpdateTest(cx ctlCtx) {
  104. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  105. cx.t.Fatal(err)
  106. }
  107. if err := authEnable(cx); err != nil {
  108. cx.t.Fatal(err)
  109. }
  110. cx.user, cx.pass = "root", "root"
  111. authSetupTestUser(cx)
  112. // try put to not granted key
  113. cx.user, cx.pass = "test-user", "pass"
  114. if err := ctlV3PutFailPerm(cx, "hoo", "bar"); err != nil {
  115. cx.t.Fatal(err)
  116. }
  117. // grant a new key
  118. cx.user, cx.pass = "root", "root"
  119. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "hoo", ""}); err != nil {
  120. cx.t.Fatal(err)
  121. }
  122. // try a newly granted key
  123. cx.user, cx.pass = "test-user", "pass"
  124. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  125. cx.t.Fatal(err)
  126. }
  127. // confirm put succeeded
  128. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar"}}...); err != nil {
  129. cx.t.Fatal(err)
  130. }
  131. // revoke the newly granted key
  132. cx.user, cx.pass = "root", "root"
  133. if err := ctlV3RoleRevokePermission(cx, "test-role", "hoo", ""); err != nil {
  134. cx.t.Fatal(err)
  135. }
  136. // try put to the revoked key
  137. cx.user, cx.pass = "test-user", "pass"
  138. if err := ctlV3PutFailPerm(cx, "hoo", "bar"); err != nil {
  139. cx.t.Fatal(err)
  140. }
  141. // confirm a key still granted can be accessed
  142. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  143. cx.t.Fatal(err)
  144. }
  145. }
  146. func authUserDeleteDuringOpsTest(cx ctlCtx) {
  147. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  148. cx.t.Fatal(err)
  149. }
  150. if err := authEnable(cx); err != nil {
  151. cx.t.Fatal(err)
  152. }
  153. cx.user, cx.pass = "root", "root"
  154. authSetupTestUser(cx)
  155. // create a key
  156. cx.user, cx.pass = "test-user", "pass"
  157. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  158. cx.t.Fatal(err)
  159. }
  160. // confirm put succeeded
  161. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  162. cx.t.Fatal(err)
  163. }
  164. // delete the user
  165. cx.user, cx.pass = "root", "root"
  166. err := ctlV3User(cx, []string{"delete", "test-user"}, "User test-user deleted", []string{})
  167. if err != nil {
  168. cx.t.Fatal(err)
  169. }
  170. // check the user is deleted
  171. cx.user, cx.pass = "test-user", "pass"
  172. if err := ctlV3PutFailAuth(cx, "foo", "baz"); err != nil {
  173. cx.t.Fatal(err)
  174. }
  175. }
  176. func authRoleRevokeDuringOpsTest(cx ctlCtx) {
  177. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  178. cx.t.Fatal(err)
  179. }
  180. if err := authEnable(cx); err != nil {
  181. cx.t.Fatal(err)
  182. }
  183. cx.user, cx.pass = "root", "root"
  184. authSetupTestUser(cx)
  185. // create a key
  186. cx.user, cx.pass = "test-user", "pass"
  187. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  188. cx.t.Fatal(err)
  189. }
  190. // confirm put succeeded
  191. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  192. cx.t.Fatal(err)
  193. }
  194. // create a new role
  195. cx.user, cx.pass = "root", "root"
  196. if err := ctlV3Role(cx, []string{"add", "test-role2"}, "Role test-role2 created"); err != nil {
  197. cx.t.Fatal(err)
  198. }
  199. // grant a new key to the new role
  200. if err := ctlV3RoleGrantPermission(cx, "test-role2", grantingPerm{true, true, "hoo", ""}); err != nil {
  201. cx.t.Fatal(err)
  202. }
  203. // grant the new role to the user
  204. if err := ctlV3User(cx, []string{"grant-role", "test-user", "test-role2"}, "Role test-role2 is granted to user test-user", nil); err != nil {
  205. cx.t.Fatal(err)
  206. }
  207. // try a newly granted key
  208. cx.user, cx.pass = "test-user", "pass"
  209. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  210. cx.t.Fatal(err)
  211. }
  212. // confirm put succeeded
  213. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar"}}...); err != nil {
  214. cx.t.Fatal(err)
  215. }
  216. // revoke a role from the user
  217. cx.user, cx.pass = "root", "root"
  218. err := ctlV3User(cx, []string{"revoke-role", "test-user", "test-role"}, "Role test-role is revoked from user test-user", []string{})
  219. if err != nil {
  220. cx.t.Fatal(err)
  221. }
  222. // check the role is revoked and permission is lost from the user
  223. cx.user, cx.pass = "test-user", "pass"
  224. if err := ctlV3PutFailPerm(cx, "foo", "baz"); err != nil {
  225. cx.t.Fatal(err)
  226. }
  227. // try a key that can be accessed from the remaining role
  228. cx.user, cx.pass = "test-user", "pass"
  229. if err := ctlV3Put(cx, "hoo", "bar2", ""); err != nil {
  230. cx.t.Fatal(err)
  231. }
  232. // confirm put succeeded
  233. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar2"}}...); err != nil {
  234. cx.t.Fatal(err)
  235. }
  236. }
  237. func ctlV3PutFailAuth(cx ctlCtx, key, val string) error {
  238. return spawnWithExpect(append(cx.PrefixArgs(), "put", key, val), "authentication failed")
  239. }
  240. func ctlV3PutFailPerm(cx ctlCtx, key, val string) error {
  241. return spawnWithExpect(append(cx.PrefixArgs(), "put", key, val), "permission denied")
  242. }
  243. func authSetupTestUser(cx ctlCtx) {
  244. if err := ctlV3User(cx, []string{"add", "test-user", "--interactive=false"}, "User test-user created", []string{"pass"}); err != nil {
  245. cx.t.Fatal(err)
  246. }
  247. if err := spawnWithExpect(append(cx.PrefixArgs(), "role", "add", "test-role"), "Role test-role created"); err != nil {
  248. cx.t.Fatal(err)
  249. }
  250. if err := ctlV3User(cx, []string{"grant-role", "test-user", "test-role"}, "Role test-role is granted to user test-user", nil); err != nil {
  251. cx.t.Fatal(err)
  252. }
  253. cmd := append(cx.PrefixArgs(), "role", "grant-permission", "test-role", "readwrite", "foo")
  254. if err := spawnWithExpect(cmd, "Role test-role updated"); err != nil {
  255. cx.t.Fatal(err)
  256. }
  257. }