ctl_v3_auth_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. "github.com/coreos/etcd/clientv3"
  19. )
  20. func TestCtlV3AuthEnable(t *testing.T) { testCtl(t, authEnableTest) }
  21. func TestCtlV3AuthDisable(t *testing.T) { testCtl(t, authDisableTest) }
  22. func TestCtlV3AuthWriteKey(t *testing.T) { testCtl(t, authCredWriteKeyTest) }
  23. func TestCtlV3AuthRoleUpdate(t *testing.T) { testCtl(t, authRoleUpdateTest) }
  24. func TestCtlV3AuthUserDeleteDuringOps(t *testing.T) { testCtl(t, authUserDeleteDuringOpsTest) }
  25. func TestCtlV3AuthRoleRevokeDuringOps(t *testing.T) { testCtl(t, authRoleRevokeDuringOpsTest) }
  26. func TestCtlV3AuthTxn(t *testing.T) { testCtl(t, authTestTxn) }
  27. func TestCtlV3AuthPerfixPerm(t *testing.T) { testCtl(t, authTestPrefixPerm) }
  28. func TestCtlV3AuthMemberAdd(t *testing.T) { testCtl(t, authTestMemberAdd) }
  29. func TestCtlV3AuthMemberRemove(t *testing.T) {
  30. testCtl(t, authTestMemberRemove, withQuorum(), withNoStrictReconfig())
  31. }
  32. func TestCtlV3AuthMemberUpdate(t *testing.T) { testCtl(t, authTestMemberUpdate) }
  33. func authEnableTest(cx ctlCtx) {
  34. if err := authEnable(cx); err != nil {
  35. cx.t.Fatal(err)
  36. }
  37. }
  38. func authEnable(cx ctlCtx) error {
  39. // create root user with root role
  40. if err := ctlV3User(cx, []string{"add", "root", "--interactive=false"}, "User root created", []string{"root"}); err != nil {
  41. return fmt.Errorf("failed to create root user %v", err)
  42. }
  43. if err := ctlV3User(cx, []string{"grant-role", "root", "root"}, "Role root is granted to user root", nil); err != nil {
  44. return fmt.Errorf("failed to grant root user root role %v", err)
  45. }
  46. if err := ctlV3AuthEnable(cx); err != nil {
  47. return fmt.Errorf("authEnableTest ctlV3AuthEnable error (%v)", err)
  48. }
  49. return nil
  50. }
  51. func ctlV3AuthEnable(cx ctlCtx) error {
  52. cmdArgs := append(cx.PrefixArgs(), "auth", "enable")
  53. return spawnWithExpect(cmdArgs, "Authentication Enabled")
  54. }
  55. func authDisableTest(cx ctlCtx) {
  56. // a key that isn't granted to test-user
  57. if err := ctlV3Put(cx, "hoo", "a", ""); err != nil {
  58. cx.t.Fatal(err)
  59. }
  60. if err := authEnable(cx); err != nil {
  61. cx.t.Fatal(err)
  62. }
  63. cx.user, cx.pass = "root", "root"
  64. authSetupTestUser(cx)
  65. // test-user doesn't have the permission, it must fail
  66. cx.user, cx.pass = "test-user", "pass"
  67. if err := ctlV3PutFailPerm(cx, "hoo", "bar"); err != nil {
  68. cx.t.Fatal(err)
  69. }
  70. cx.user, cx.pass = "root", "root"
  71. if err := ctlV3AuthDisable(cx); err != nil {
  72. cx.t.Fatalf("authDisableTest ctlV3AuthDisable error (%v)", err)
  73. }
  74. // now auth fails unconditionally, note that failed RPC is Authenticate(), not Put()
  75. cx.user, cx.pass = "test-user", "pass"
  76. if err := ctlV3PutFailAuthDisabled(cx, "hoo", "bar"); err != nil {
  77. cx.t.Fatal(err)
  78. }
  79. // now the key can be accessed
  80. cx.user, cx.pass = "", ""
  81. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  82. cx.t.Fatal(err)
  83. }
  84. // confirm put succeeded
  85. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar"}}...); err != nil {
  86. cx.t.Fatal(err)
  87. }
  88. }
  89. func ctlV3AuthDisable(cx ctlCtx) error {
  90. cmdArgs := append(cx.PrefixArgs(), "auth", "disable")
  91. return spawnWithExpect(cmdArgs, "Authentication Disabled")
  92. }
  93. func authCredWriteKeyTest(cx ctlCtx) {
  94. // baseline key to check for failed puts
  95. if err := ctlV3Put(cx, "foo", "a", ""); err != nil {
  96. cx.t.Fatal(err)
  97. }
  98. if err := authEnable(cx); err != nil {
  99. cx.t.Fatal(err)
  100. }
  101. cx.user, cx.pass = "root", "root"
  102. authSetupTestUser(cx)
  103. // confirm root role can access to all keys
  104. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  105. cx.t.Fatal(err)
  106. }
  107. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  108. cx.t.Fatal(err)
  109. }
  110. // try invalid user
  111. cx.user, cx.pass = "a", "b"
  112. if err := ctlV3PutFailAuth(cx, "foo", "bar"); err != nil {
  113. cx.t.Fatal(err)
  114. }
  115. // confirm put failed
  116. cx.user, cx.pass = "test-user", "pass"
  117. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  118. cx.t.Fatal(err)
  119. }
  120. // try good user
  121. cx.user, cx.pass = "test-user", "pass"
  122. if err := ctlV3Put(cx, "foo", "bar2", ""); err != nil {
  123. cx.t.Fatal(err)
  124. }
  125. // confirm put succeeded
  126. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar2"}}...); err != nil {
  127. cx.t.Fatal(err)
  128. }
  129. // try bad password
  130. cx.user, cx.pass = "test-user", "badpass"
  131. if err := ctlV3PutFailAuth(cx, "foo", "baz"); err != nil {
  132. cx.t.Fatal(err)
  133. }
  134. // confirm put failed
  135. cx.user, cx.pass = "test-user", "pass"
  136. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar2"}}...); err != nil {
  137. cx.t.Fatal(err)
  138. }
  139. }
  140. func authRoleUpdateTest(cx ctlCtx) {
  141. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  142. cx.t.Fatal(err)
  143. }
  144. if err := authEnable(cx); err != nil {
  145. cx.t.Fatal(err)
  146. }
  147. cx.user, cx.pass = "root", "root"
  148. authSetupTestUser(cx)
  149. // try put to not granted key
  150. cx.user, cx.pass = "test-user", "pass"
  151. if err := ctlV3PutFailPerm(cx, "hoo", "bar"); err != nil {
  152. cx.t.Fatal(err)
  153. }
  154. // grant a new key
  155. cx.user, cx.pass = "root", "root"
  156. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "hoo", "", false}); err != nil {
  157. cx.t.Fatal(err)
  158. }
  159. // try a newly granted key
  160. cx.user, cx.pass = "test-user", "pass"
  161. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  162. cx.t.Fatal(err)
  163. }
  164. // confirm put succeeded
  165. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar"}}...); err != nil {
  166. cx.t.Fatal(err)
  167. }
  168. // revoke the newly granted key
  169. cx.user, cx.pass = "root", "root"
  170. if err := ctlV3RoleRevokePermission(cx, "test-role", "hoo", ""); err != nil {
  171. cx.t.Fatal(err)
  172. }
  173. // try put to the revoked key
  174. cx.user, cx.pass = "test-user", "pass"
  175. if err := ctlV3PutFailPerm(cx, "hoo", "bar"); err != nil {
  176. cx.t.Fatal(err)
  177. }
  178. // confirm a key still granted can be accessed
  179. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  180. cx.t.Fatal(err)
  181. }
  182. }
  183. func authUserDeleteDuringOpsTest(cx ctlCtx) {
  184. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  185. cx.t.Fatal(err)
  186. }
  187. if err := authEnable(cx); err != nil {
  188. cx.t.Fatal(err)
  189. }
  190. cx.user, cx.pass = "root", "root"
  191. authSetupTestUser(cx)
  192. // create a key
  193. cx.user, cx.pass = "test-user", "pass"
  194. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  195. cx.t.Fatal(err)
  196. }
  197. // confirm put succeeded
  198. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  199. cx.t.Fatal(err)
  200. }
  201. // delete the user
  202. cx.user, cx.pass = "root", "root"
  203. err := ctlV3User(cx, []string{"delete", "test-user"}, "User test-user deleted", []string{})
  204. if err != nil {
  205. cx.t.Fatal(err)
  206. }
  207. // check the user is deleted
  208. cx.user, cx.pass = "test-user", "pass"
  209. if err := ctlV3PutFailAuth(cx, "foo", "baz"); err != nil {
  210. cx.t.Fatal(err)
  211. }
  212. }
  213. func authRoleRevokeDuringOpsTest(cx ctlCtx) {
  214. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  215. cx.t.Fatal(err)
  216. }
  217. if err := authEnable(cx); err != nil {
  218. cx.t.Fatal(err)
  219. }
  220. cx.user, cx.pass = "root", "root"
  221. authSetupTestUser(cx)
  222. // create a key
  223. cx.user, cx.pass = "test-user", "pass"
  224. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  225. cx.t.Fatal(err)
  226. }
  227. // confirm put succeeded
  228. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  229. cx.t.Fatal(err)
  230. }
  231. // create a new role
  232. cx.user, cx.pass = "root", "root"
  233. if err := ctlV3Role(cx, []string{"add", "test-role2"}, "Role test-role2 created"); err != nil {
  234. cx.t.Fatal(err)
  235. }
  236. // grant a new key to the new role
  237. if err := ctlV3RoleGrantPermission(cx, "test-role2", grantingPerm{true, true, "hoo", "", false}); err != nil {
  238. cx.t.Fatal(err)
  239. }
  240. // grant the new role to the user
  241. if err := ctlV3User(cx, []string{"grant-role", "test-user", "test-role2"}, "Role test-role2 is granted to user test-user", nil); err != nil {
  242. cx.t.Fatal(err)
  243. }
  244. // try a newly granted key
  245. cx.user, cx.pass = "test-user", "pass"
  246. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  247. cx.t.Fatal(err)
  248. }
  249. // confirm put succeeded
  250. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar"}}...); err != nil {
  251. cx.t.Fatal(err)
  252. }
  253. // revoke a role from the user
  254. cx.user, cx.pass = "root", "root"
  255. err := ctlV3User(cx, []string{"revoke-role", "test-user", "test-role"}, "Role test-role is revoked from user test-user", []string{})
  256. if err != nil {
  257. cx.t.Fatal(err)
  258. }
  259. // check the role is revoked and permission is lost from the user
  260. cx.user, cx.pass = "test-user", "pass"
  261. if err := ctlV3PutFailPerm(cx, "foo", "baz"); err != nil {
  262. cx.t.Fatal(err)
  263. }
  264. // try a key that can be accessed from the remaining role
  265. cx.user, cx.pass = "test-user", "pass"
  266. if err := ctlV3Put(cx, "hoo", "bar2", ""); err != nil {
  267. cx.t.Fatal(err)
  268. }
  269. // confirm put succeeded
  270. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar2"}}...); err != nil {
  271. cx.t.Fatal(err)
  272. }
  273. }
  274. func ctlV3PutFailAuth(cx ctlCtx, key, val string) error {
  275. return spawnWithExpect(append(cx.PrefixArgs(), "put", key, val), "authentication failed")
  276. }
  277. func ctlV3PutFailPerm(cx ctlCtx, key, val string) error {
  278. return spawnWithExpect(append(cx.PrefixArgs(), "put", key, val), "permission denied")
  279. }
  280. func ctlV3PutFailAuthDisabled(cx ctlCtx, key, val string) error {
  281. return spawnWithExpect(append(cx.PrefixArgs(), "put", key, val), "authentication is not enabled")
  282. }
  283. func authSetupTestUser(cx ctlCtx) {
  284. if err := ctlV3User(cx, []string{"add", "test-user", "--interactive=false"}, "User test-user created", []string{"pass"}); err != nil {
  285. cx.t.Fatal(err)
  286. }
  287. if err := spawnWithExpect(append(cx.PrefixArgs(), "role", "add", "test-role"), "Role test-role created"); err != nil {
  288. cx.t.Fatal(err)
  289. }
  290. if err := ctlV3User(cx, []string{"grant-role", "test-user", "test-role"}, "Role test-role is granted to user test-user", nil); err != nil {
  291. cx.t.Fatal(err)
  292. }
  293. cmd := append(cx.PrefixArgs(), "role", "grant-permission", "test-role", "readwrite", "foo")
  294. if err := spawnWithExpect(cmd, "Role test-role updated"); err != nil {
  295. cx.t.Fatal(err)
  296. }
  297. }
  298. func authTestTxn(cx ctlCtx) {
  299. // keys with 1 suffix aren't granted to test-user
  300. // keys with 2 suffix are granted to test-user
  301. keys := []string{"c1", "s1", "f1"}
  302. grantedKeys := []string{"c2", "s2", "f2"}
  303. for _, key := range keys {
  304. if err := ctlV3Put(cx, key, "v", ""); err != nil {
  305. cx.t.Fatal(err)
  306. }
  307. }
  308. for _, key := range grantedKeys {
  309. if err := ctlV3Put(cx, key, "v", ""); err != nil {
  310. cx.t.Fatal(err)
  311. }
  312. }
  313. if err := authEnable(cx); err != nil {
  314. cx.t.Fatal(err)
  315. }
  316. cx.user, cx.pass = "root", "root"
  317. authSetupTestUser(cx)
  318. // grant keys to test-user
  319. cx.user, cx.pass = "root", "root"
  320. for _, key := range grantedKeys {
  321. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, key, "", false}); err != nil {
  322. cx.t.Fatal(err)
  323. }
  324. }
  325. // now test txn
  326. cx.interactive = true
  327. cx.user, cx.pass = "test-user", "pass"
  328. rqs := txnRequests{
  329. compare: []string{`version("c2") = "1"`},
  330. ifSucess: []string{"get s2"},
  331. ifFail: []string{"get f2"},
  332. results: []string{"SUCCESS", "s2", "v"},
  333. }
  334. if err := ctlV3Txn(cx, rqs); err != nil {
  335. cx.t.Fatal(err)
  336. }
  337. // a key of compare case isn't granted
  338. rqs = txnRequests{
  339. compare: []string{`version("c1") = "1"`},
  340. ifSucess: []string{"get s2"},
  341. ifFail: []string{"get f2"},
  342. results: []string{"Error: etcdserver: permission denied"},
  343. }
  344. if err := ctlV3Txn(cx, rqs); err != nil {
  345. cx.t.Fatal(err)
  346. }
  347. // a key of success case isn't granted
  348. rqs = txnRequests{
  349. compare: []string{`version("c2") = "1"`},
  350. ifSucess: []string{"get s1"},
  351. ifFail: []string{"get f2"},
  352. results: []string{"Error: etcdserver: permission denied"},
  353. }
  354. if err := ctlV3Txn(cx, rqs); err != nil {
  355. cx.t.Fatal(err)
  356. }
  357. // a key of failure case isn't granted
  358. rqs = txnRequests{
  359. compare: []string{`version("c2") = "1"`},
  360. ifSucess: []string{"get s2"},
  361. ifFail: []string{"get f1"},
  362. results: []string{"Error: etcdserver: permission denied"},
  363. }
  364. if err := ctlV3Txn(cx, rqs); err != nil {
  365. cx.t.Fatal(err)
  366. }
  367. }
  368. func authTestPrefixPerm(cx ctlCtx) {
  369. if err := authEnable(cx); err != nil {
  370. cx.t.Fatal(err)
  371. }
  372. cx.user, cx.pass = "root", "root"
  373. authSetupTestUser(cx)
  374. prefix := "/prefix/" // directory like prefix
  375. // grant keys to test-user
  376. cx.user, cx.pass = "root", "root"
  377. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, prefix, "", true}); err != nil {
  378. cx.t.Fatal(err)
  379. }
  380. // try a prefix granted permission
  381. cx.user, cx.pass = "test-user", "pass"
  382. for i := 0; i < 10; i++ {
  383. key := fmt.Sprintf("%s%d", prefix, i)
  384. if err := ctlV3Put(cx, key, "val", ""); err != nil {
  385. cx.t.Fatal(err)
  386. }
  387. }
  388. if err := ctlV3PutFailPerm(cx, clientv3.GetPrefixRangeEnd(prefix), "baz"); err != nil {
  389. cx.t.Fatal(err)
  390. }
  391. }
  392. func authTestMemberAdd(cx ctlCtx) {
  393. if err := authEnable(cx); err != nil {
  394. cx.t.Fatal(err)
  395. }
  396. cx.user, cx.pass = "root", "root"
  397. authSetupTestUser(cx)
  398. peerURL := fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+11)
  399. // ordinal user cannot add a new member
  400. cx.user, cx.pass = "test-user", "pass"
  401. if err := ctlV3MemberAdd(cx, peerURL); err == nil {
  402. cx.t.Fatalf("ordinal user must not be allowed to add a member")
  403. }
  404. // root can add a new member
  405. cx.user, cx.pass = "root", "root"
  406. if err := ctlV3MemberAdd(cx, peerURL); err != nil {
  407. cx.t.Fatal(err)
  408. }
  409. }
  410. func authTestMemberRemove(cx ctlCtx) {
  411. if err := authEnable(cx); err != nil {
  412. cx.t.Fatal(err)
  413. }
  414. cx.user, cx.pass = "root", "root"
  415. authSetupTestUser(cx)
  416. n1 := cx.cfg.clusterSize
  417. if n1 < 2 {
  418. cx.t.Fatalf("%d-node is too small to test 'member remove'", n1)
  419. }
  420. resp, err := getMemberList(cx)
  421. if err != nil {
  422. cx.t.Fatal(err)
  423. }
  424. if n1 != len(resp.Members) {
  425. cx.t.Fatalf("expected %d, got %d", n1, len(resp.Members))
  426. }
  427. var (
  428. memIDToRemove = fmt.Sprintf("%x", resp.Header.MemberId)
  429. clusterID = fmt.Sprintf("%x", resp.Header.ClusterId)
  430. )
  431. // ordinal user cannot remove a member
  432. cx.user, cx.pass = "test-user", "pass"
  433. if err = ctlV3MemberRemove(cx, memIDToRemove, clusterID); err == nil {
  434. cx.t.Fatalf("ordinal user must not be allowed to remove a member")
  435. }
  436. // root can remove a member
  437. cx.user, cx.pass = "root", "root"
  438. if err = ctlV3MemberRemove(cx, memIDToRemove, clusterID); err != nil {
  439. cx.t.Fatal(err)
  440. }
  441. }
  442. func authTestMemberUpdate(cx ctlCtx) {
  443. if err := authEnable(cx); err != nil {
  444. cx.t.Fatal(err)
  445. }
  446. cx.user, cx.pass = "root", "root"
  447. authSetupTestUser(cx)
  448. mr, err := getMemberList(cx)
  449. if err != nil {
  450. cx.t.Fatal(err)
  451. }
  452. // ordinal user cannot update a member
  453. cx.user, cx.pass = "test-user", "pass"
  454. peerURL := fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+11)
  455. memberID := fmt.Sprintf("%x", mr.Members[0].ID)
  456. if err = ctlV3MemberUpdate(cx, memberID, peerURL); err == nil {
  457. cx.t.Fatalf("ordinal user must not be allowed to update a member")
  458. }
  459. // root can update a member
  460. cx.user, cx.pass = "root", "root"
  461. if err = ctlV3MemberUpdate(cx, memberID, peerURL); err != nil {
  462. cx.t.Fatal(err)
  463. }
  464. }