ctl_v3_auth_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 TestCtlV3AuthCertCN(t *testing.T) { testCtl(t, authTestCertCN, withCfg(configClientTLSCertAuth)) }
  34. func TestCtlV3AuthRevokeWithDelete(t *testing.T) { testCtl(t, authTestRevokeWithDelete) }
  35. func TestCtlV3AuthInvalidMgmt(t *testing.T) { testCtl(t, authTestInvalidMgmt) }
  36. func TestCtlV3AuthFromKeyPerm(t *testing.T) { testCtl(t, authTestFromKeyPerm) }
  37. func TestCtlV3AuthAndWatch(t *testing.T) { testCtl(t, authTestWatch) }
  38. func authEnableTest(cx ctlCtx) {
  39. if err := authEnable(cx); err != nil {
  40. cx.t.Fatal(err)
  41. }
  42. }
  43. func authEnable(cx ctlCtx) error {
  44. // create root user with root role
  45. if err := ctlV3User(cx, []string{"add", "root", "--interactive=false"}, "User root created", []string{"root"}); err != nil {
  46. return fmt.Errorf("failed to create root user %v", err)
  47. }
  48. if err := ctlV3User(cx, []string{"grant-role", "root", "root"}, "Role root is granted to user root", nil); err != nil {
  49. return fmt.Errorf("failed to grant root user root role %v", err)
  50. }
  51. if err := ctlV3AuthEnable(cx); err != nil {
  52. return fmt.Errorf("authEnableTest ctlV3AuthEnable error (%v)", err)
  53. }
  54. return nil
  55. }
  56. func ctlV3AuthEnable(cx ctlCtx) error {
  57. cmdArgs := append(cx.PrefixArgs(), "auth", "enable")
  58. return spawnWithExpect(cmdArgs, "Authentication Enabled")
  59. }
  60. func authDisableTest(cx ctlCtx) {
  61. // a key that isn't granted to test-user
  62. if err := ctlV3Put(cx, "hoo", "a", ""); err != nil {
  63. cx.t.Fatal(err)
  64. }
  65. if err := authEnable(cx); err != nil {
  66. cx.t.Fatal(err)
  67. }
  68. cx.user, cx.pass = "root", "root"
  69. authSetupTestUser(cx)
  70. // test-user doesn't have the permission, it must fail
  71. cx.user, cx.pass = "test-user", "pass"
  72. if err := ctlV3PutFailPerm(cx, "hoo", "bar"); err != nil {
  73. cx.t.Fatal(err)
  74. }
  75. cx.user, cx.pass = "root", "root"
  76. if err := ctlV3AuthDisable(cx); err != nil {
  77. cx.t.Fatalf("authDisableTest ctlV3AuthDisable error (%v)", err)
  78. }
  79. // now ErrAuthNotEnabled of Authenticate() is simply ignored
  80. cx.user, cx.pass = "test-user", "pass"
  81. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  82. cx.t.Fatal(err)
  83. }
  84. // now the key can be accessed
  85. cx.user, cx.pass = "", ""
  86. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  87. cx.t.Fatal(err)
  88. }
  89. // confirm put succeeded
  90. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar"}}...); err != nil {
  91. cx.t.Fatal(err)
  92. }
  93. }
  94. func ctlV3AuthDisable(cx ctlCtx) error {
  95. cmdArgs := append(cx.PrefixArgs(), "auth", "disable")
  96. return spawnWithExpect(cmdArgs, "Authentication Disabled")
  97. }
  98. func authCredWriteKeyTest(cx ctlCtx) {
  99. // baseline key to check for failed puts
  100. if err := ctlV3Put(cx, "foo", "a", ""); err != nil {
  101. cx.t.Fatal(err)
  102. }
  103. if err := authEnable(cx); err != nil {
  104. cx.t.Fatal(err)
  105. }
  106. cx.user, cx.pass = "root", "root"
  107. authSetupTestUser(cx)
  108. // confirm root role can access to all keys
  109. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  110. cx.t.Fatal(err)
  111. }
  112. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  113. cx.t.Fatal(err)
  114. }
  115. // try invalid user
  116. cx.user, cx.pass = "a", "b"
  117. if err := ctlV3PutFailAuth(cx, "foo", "bar"); err != nil {
  118. cx.t.Fatal(err)
  119. }
  120. // confirm put failed
  121. cx.user, cx.pass = "test-user", "pass"
  122. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  123. cx.t.Fatal(err)
  124. }
  125. // try good user
  126. cx.user, cx.pass = "test-user", "pass"
  127. if err := ctlV3Put(cx, "foo", "bar2", ""); err != nil {
  128. cx.t.Fatal(err)
  129. }
  130. // confirm put succeeded
  131. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar2"}}...); err != nil {
  132. cx.t.Fatal(err)
  133. }
  134. // try bad password
  135. cx.user, cx.pass = "test-user", "badpass"
  136. if err := ctlV3PutFailAuth(cx, "foo", "baz"); err != nil {
  137. cx.t.Fatal(err)
  138. }
  139. // confirm put failed
  140. cx.user, cx.pass = "test-user", "pass"
  141. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar2"}}...); err != nil {
  142. cx.t.Fatal(err)
  143. }
  144. }
  145. func authRoleUpdateTest(cx ctlCtx) {
  146. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  147. cx.t.Fatal(err)
  148. }
  149. if err := authEnable(cx); err != nil {
  150. cx.t.Fatal(err)
  151. }
  152. cx.user, cx.pass = "root", "root"
  153. authSetupTestUser(cx)
  154. // try put to not granted key
  155. cx.user, cx.pass = "test-user", "pass"
  156. if err := ctlV3PutFailPerm(cx, "hoo", "bar"); err != nil {
  157. cx.t.Fatal(err)
  158. }
  159. // grant a new key
  160. cx.user, cx.pass = "root", "root"
  161. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "hoo", "", false}); err != nil {
  162. cx.t.Fatal(err)
  163. }
  164. // try a newly granted key
  165. cx.user, cx.pass = "test-user", "pass"
  166. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  167. cx.t.Fatal(err)
  168. }
  169. // confirm put succeeded
  170. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar"}}...); err != nil {
  171. cx.t.Fatal(err)
  172. }
  173. // revoke the newly granted key
  174. cx.user, cx.pass = "root", "root"
  175. if err := ctlV3RoleRevokePermission(cx, "test-role", "hoo", "", false); err != nil {
  176. cx.t.Fatal(err)
  177. }
  178. // try put to the revoked key
  179. cx.user, cx.pass = "test-user", "pass"
  180. if err := ctlV3PutFailPerm(cx, "hoo", "bar"); err != nil {
  181. cx.t.Fatal(err)
  182. }
  183. // confirm a key still granted can be accessed
  184. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  185. cx.t.Fatal(err)
  186. }
  187. }
  188. func authUserDeleteDuringOpsTest(cx ctlCtx) {
  189. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  190. cx.t.Fatal(err)
  191. }
  192. if err := authEnable(cx); err != nil {
  193. cx.t.Fatal(err)
  194. }
  195. cx.user, cx.pass = "root", "root"
  196. authSetupTestUser(cx)
  197. // create a key
  198. cx.user, cx.pass = "test-user", "pass"
  199. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  200. cx.t.Fatal(err)
  201. }
  202. // confirm put succeeded
  203. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  204. cx.t.Fatal(err)
  205. }
  206. // delete the user
  207. cx.user, cx.pass = "root", "root"
  208. err := ctlV3User(cx, []string{"delete", "test-user"}, "User test-user deleted", []string{})
  209. if err != nil {
  210. cx.t.Fatal(err)
  211. }
  212. // check the user is deleted
  213. cx.user, cx.pass = "test-user", "pass"
  214. if err := ctlV3PutFailAuth(cx, "foo", "baz"); err != nil {
  215. cx.t.Fatal(err)
  216. }
  217. }
  218. func authRoleRevokeDuringOpsTest(cx ctlCtx) {
  219. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  220. cx.t.Fatal(err)
  221. }
  222. if err := authEnable(cx); err != nil {
  223. cx.t.Fatal(err)
  224. }
  225. cx.user, cx.pass = "root", "root"
  226. authSetupTestUser(cx)
  227. // create a key
  228. cx.user, cx.pass = "test-user", "pass"
  229. if err := ctlV3Put(cx, "foo", "bar", ""); err != nil {
  230. cx.t.Fatal(err)
  231. }
  232. // confirm put succeeded
  233. if err := ctlV3Get(cx, []string{"foo"}, []kv{{"foo", "bar"}}...); err != nil {
  234. cx.t.Fatal(err)
  235. }
  236. // create a new role
  237. cx.user, cx.pass = "root", "root"
  238. if err := ctlV3Role(cx, []string{"add", "test-role2"}, "Role test-role2 created"); err != nil {
  239. cx.t.Fatal(err)
  240. }
  241. // grant a new key to the new role
  242. if err := ctlV3RoleGrantPermission(cx, "test-role2", grantingPerm{true, true, "hoo", "", false}); err != nil {
  243. cx.t.Fatal(err)
  244. }
  245. // grant the new role to the user
  246. if err := ctlV3User(cx, []string{"grant-role", "test-user", "test-role2"}, "Role test-role2 is granted to user test-user", nil); err != nil {
  247. cx.t.Fatal(err)
  248. }
  249. // try a newly granted key
  250. cx.user, cx.pass = "test-user", "pass"
  251. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  252. cx.t.Fatal(err)
  253. }
  254. // confirm put succeeded
  255. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar"}}...); err != nil {
  256. cx.t.Fatal(err)
  257. }
  258. // revoke a role from the user
  259. cx.user, cx.pass = "root", "root"
  260. err := ctlV3User(cx, []string{"revoke-role", "test-user", "test-role"}, "Role test-role is revoked from user test-user", []string{})
  261. if err != nil {
  262. cx.t.Fatal(err)
  263. }
  264. // check the role is revoked and permission is lost from the user
  265. cx.user, cx.pass = "test-user", "pass"
  266. if err := ctlV3PutFailPerm(cx, "foo", "baz"); err != nil {
  267. cx.t.Fatal(err)
  268. }
  269. // try a key that can be accessed from the remaining role
  270. cx.user, cx.pass = "test-user", "pass"
  271. if err := ctlV3Put(cx, "hoo", "bar2", ""); err != nil {
  272. cx.t.Fatal(err)
  273. }
  274. // confirm put succeeded
  275. if err := ctlV3Get(cx, []string{"hoo"}, []kv{{"hoo", "bar2"}}...); err != nil {
  276. cx.t.Fatal(err)
  277. }
  278. }
  279. func ctlV3PutFailAuth(cx ctlCtx, key, val string) error {
  280. return spawnWithExpect(append(cx.PrefixArgs(), "put", key, val), "authentication failed")
  281. }
  282. func ctlV3PutFailPerm(cx ctlCtx, key, val string) error {
  283. return spawnWithExpect(append(cx.PrefixArgs(), "put", key, val), "permission denied")
  284. }
  285. func authSetupTestUser(cx ctlCtx) {
  286. if err := ctlV3User(cx, []string{"add", "test-user", "--interactive=false"}, "User test-user created", []string{"pass"}); err != nil {
  287. cx.t.Fatal(err)
  288. }
  289. if err := spawnWithExpect(append(cx.PrefixArgs(), "role", "add", "test-role"), "Role test-role created"); err != nil {
  290. cx.t.Fatal(err)
  291. }
  292. if err := ctlV3User(cx, []string{"grant-role", "test-user", "test-role"}, "Role test-role is granted to user test-user", nil); err != nil {
  293. cx.t.Fatal(err)
  294. }
  295. cmd := append(cx.PrefixArgs(), "role", "grant-permission", "test-role", "readwrite", "foo")
  296. if err := spawnWithExpect(cmd, "Role test-role updated"); err != nil {
  297. cx.t.Fatal(err)
  298. }
  299. }
  300. func authTestTxn(cx ctlCtx) {
  301. // keys with 1 suffix aren't granted to test-user
  302. // keys with 2 suffix are granted to test-user
  303. keys := []string{"c1", "s1", "f1"}
  304. grantedKeys := []string{"c2", "s2", "f2"}
  305. for _, key := range keys {
  306. if err := ctlV3Put(cx, key, "v", ""); err != nil {
  307. cx.t.Fatal(err)
  308. }
  309. }
  310. for _, key := range grantedKeys {
  311. if err := ctlV3Put(cx, key, "v", ""); err != nil {
  312. cx.t.Fatal(err)
  313. }
  314. }
  315. if err := authEnable(cx); err != nil {
  316. cx.t.Fatal(err)
  317. }
  318. cx.user, cx.pass = "root", "root"
  319. authSetupTestUser(cx)
  320. // grant keys to test-user
  321. cx.user, cx.pass = "root", "root"
  322. for _, key := range grantedKeys {
  323. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, key, "", false}); err != nil {
  324. cx.t.Fatal(err)
  325. }
  326. }
  327. // now test txn
  328. cx.interactive = true
  329. cx.user, cx.pass = "test-user", "pass"
  330. rqs := txnRequests{
  331. compare: []string{`version("c2") = "1"`},
  332. ifSucess: []string{"get s2"},
  333. ifFail: []string{"get f2"},
  334. results: []string{"SUCCESS", "s2", "v"},
  335. }
  336. if err := ctlV3Txn(cx, rqs); err != nil {
  337. cx.t.Fatal(err)
  338. }
  339. // a key of compare case isn't granted
  340. rqs = txnRequests{
  341. compare: []string{`version("c1") = "1"`},
  342. ifSucess: []string{"get s2"},
  343. ifFail: []string{"get f2"},
  344. results: []string{"Error: etcdserver: permission denied"},
  345. }
  346. if err := ctlV3Txn(cx, rqs); err != nil {
  347. cx.t.Fatal(err)
  348. }
  349. // a key of success case isn't granted
  350. rqs = txnRequests{
  351. compare: []string{`version("c2") = "1"`},
  352. ifSucess: []string{"get s1"},
  353. ifFail: []string{"get f2"},
  354. results: []string{"Error: etcdserver: permission denied"},
  355. }
  356. if err := ctlV3Txn(cx, rqs); err != nil {
  357. cx.t.Fatal(err)
  358. }
  359. // a key of failure case isn't granted
  360. rqs = txnRequests{
  361. compare: []string{`version("c2") = "1"`},
  362. ifSucess: []string{"get s2"},
  363. ifFail: []string{"get f1"},
  364. results: []string{"Error: etcdserver: permission denied"},
  365. }
  366. if err := ctlV3Txn(cx, rqs); err != nil {
  367. cx.t.Fatal(err)
  368. }
  369. }
  370. func authTestPrefixPerm(cx ctlCtx) {
  371. if err := authEnable(cx); err != nil {
  372. cx.t.Fatal(err)
  373. }
  374. cx.user, cx.pass = "root", "root"
  375. authSetupTestUser(cx)
  376. prefix := "/prefix/" // directory like prefix
  377. // grant keys to test-user
  378. cx.user, cx.pass = "root", "root"
  379. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, prefix, "", true}); err != nil {
  380. cx.t.Fatal(err)
  381. }
  382. // try a prefix granted permission
  383. cx.user, cx.pass = "test-user", "pass"
  384. for i := 0; i < 10; i++ {
  385. key := fmt.Sprintf("%s%d", prefix, i)
  386. if err := ctlV3Put(cx, key, "val", ""); err != nil {
  387. cx.t.Fatal(err)
  388. }
  389. }
  390. if err := ctlV3PutFailPerm(cx, clientv3.GetPrefixRangeEnd(prefix), "baz"); err != nil {
  391. cx.t.Fatal(err)
  392. }
  393. }
  394. func authTestMemberAdd(cx ctlCtx) {
  395. if err := authEnable(cx); err != nil {
  396. cx.t.Fatal(err)
  397. }
  398. cx.user, cx.pass = "root", "root"
  399. authSetupTestUser(cx)
  400. peerURL := fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+11)
  401. // ordinary user cannot add a new member
  402. cx.user, cx.pass = "test-user", "pass"
  403. if err := ctlV3MemberAdd(cx, peerURL); err == nil {
  404. cx.t.Fatalf("ordinary user must not be allowed to add a member")
  405. }
  406. // root can add a new member
  407. cx.user, cx.pass = "root", "root"
  408. if err := ctlV3MemberAdd(cx, peerURL); err != nil {
  409. cx.t.Fatal(err)
  410. }
  411. }
  412. func authTestMemberRemove(cx ctlCtx) {
  413. if err := authEnable(cx); err != nil {
  414. cx.t.Fatal(err)
  415. }
  416. cx.user, cx.pass = "root", "root"
  417. authSetupTestUser(cx)
  418. ep, memIDToRemove, clusterID := cx.memberToRemove()
  419. // ordinary user cannot remove a member
  420. cx.user, cx.pass = "test-user", "pass"
  421. if err := ctlV3MemberRemove(cx, ep, memIDToRemove, clusterID); err == nil {
  422. cx.t.Fatalf("ordinary user must not be allowed to remove a member")
  423. }
  424. // root can remove a member
  425. cx.user, cx.pass = "root", "root"
  426. if err := ctlV3MemberRemove(cx, ep, memIDToRemove, clusterID); err != nil {
  427. cx.t.Fatal(err)
  428. }
  429. }
  430. func authTestMemberUpdate(cx ctlCtx) {
  431. if err := authEnable(cx); err != nil {
  432. cx.t.Fatal(err)
  433. }
  434. cx.user, cx.pass = "root", "root"
  435. authSetupTestUser(cx)
  436. mr, err := getMemberList(cx)
  437. if err != nil {
  438. cx.t.Fatal(err)
  439. }
  440. // ordinary user cannot update a member
  441. cx.user, cx.pass = "test-user", "pass"
  442. peerURL := fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+11)
  443. memberID := fmt.Sprintf("%x", mr.Members[0].ID)
  444. if err = ctlV3MemberUpdate(cx, memberID, peerURL); err == nil {
  445. cx.t.Fatalf("ordinary user must not be allowed to update a member")
  446. }
  447. // root can update a member
  448. cx.user, cx.pass = "root", "root"
  449. if err = ctlV3MemberUpdate(cx, memberID, peerURL); err != nil {
  450. cx.t.Fatal(err)
  451. }
  452. }
  453. func authTestCertCN(cx ctlCtx) {
  454. if err := ctlV3User(cx, []string{"add", "etcd", "--interactive=false"}, "User etcd created", []string{""}); err != nil {
  455. cx.t.Fatal(err)
  456. }
  457. if err := spawnWithExpect(append(cx.PrefixArgs(), "role", "add", "test-role"), "Role test-role created"); err != nil {
  458. cx.t.Fatal(err)
  459. }
  460. if err := ctlV3User(cx, []string{"grant-role", "etcd", "test-role"}, "Role test-role is granted to user etcd", nil); err != nil {
  461. cx.t.Fatal(err)
  462. }
  463. cmd := append(cx.PrefixArgs(), "role", "grant-permission", "test-role", "readwrite", "foo")
  464. if err := spawnWithExpect(cmd, "Role test-role updated"); err != nil {
  465. cx.t.Fatal(err)
  466. }
  467. // grant a new key
  468. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "hoo", "", false}); err != nil {
  469. cx.t.Fatal(err)
  470. }
  471. // try a granted key
  472. cx.user, cx.pass = "", ""
  473. if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
  474. cx.t.Fatal(err)
  475. }
  476. // try a non granted key
  477. cx.user, cx.pass = "", ""
  478. if err := ctlV3PutFailPerm(cx, "baz", "bar"); err == nil {
  479. cx.t.Fatal(err)
  480. }
  481. }
  482. func authTestRevokeWithDelete(cx ctlCtx) {
  483. if err := authEnable(cx); err != nil {
  484. cx.t.Fatal(err)
  485. }
  486. cx.user, cx.pass = "root", "root"
  487. authSetupTestUser(cx)
  488. // create a new role
  489. cx.user, cx.pass = "root", "root"
  490. if err := ctlV3Role(cx, []string{"add", "test-role2"}, "Role test-role2 created"); err != nil {
  491. cx.t.Fatal(err)
  492. }
  493. // grant the new role to the user
  494. if err := ctlV3User(cx, []string{"grant-role", "test-user", "test-role2"}, "Role test-role2 is granted to user test-user", nil); err != nil {
  495. cx.t.Fatal(err)
  496. }
  497. // check the result
  498. if err := ctlV3User(cx, []string{"get", "test-user"}, "Roles: test-role test-role2", nil); err != nil {
  499. cx.t.Fatal(err)
  500. }
  501. // delete the role, test-role2 must be revoked from test-user
  502. if err := ctlV3Role(cx, []string{"delete", "test-role2"}, "Role test-role2 deleted"); err != nil {
  503. cx.t.Fatal(err)
  504. }
  505. // check the result
  506. if err := ctlV3User(cx, []string{"get", "test-user"}, "Roles: test-role", nil); err != nil {
  507. cx.t.Fatal(err)
  508. }
  509. }
  510. func authTestInvalidMgmt(cx ctlCtx) {
  511. if err := authEnable(cx); err != nil {
  512. cx.t.Fatal(err)
  513. }
  514. if err := ctlV3Role(cx, []string{"delete", "root"}, "Error: etcdserver: invalid auth management"); err == nil {
  515. cx.t.Fatal("deleting the role root must not be allowed")
  516. }
  517. if err := ctlV3User(cx, []string{"revoke-role", "root", "root"}, "Error: etcdserver: invalid auth management", []string{}); err == nil {
  518. cx.t.Fatal("revoking the role root from the user root must not be allowed")
  519. }
  520. }
  521. func authTestFromKeyPerm(cx ctlCtx) {
  522. if err := authEnable(cx); err != nil {
  523. cx.t.Fatal(err)
  524. }
  525. cx.user, cx.pass = "root", "root"
  526. authSetupTestUser(cx)
  527. // grant keys after z to test-user
  528. cx.user, cx.pass = "root", "root"
  529. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "z", "\x00", false}); err != nil {
  530. cx.t.Fatal(err)
  531. }
  532. // try the granted open ended permission
  533. cx.user, cx.pass = "test-user", "pass"
  534. for i := 0; i < 10; i++ {
  535. key := fmt.Sprintf("z%d", i)
  536. if err := ctlV3Put(cx, key, "val", ""); err != nil {
  537. cx.t.Fatal(err)
  538. }
  539. }
  540. largeKey := ""
  541. for i := 0; i < 10; i++ {
  542. largeKey += "\xff"
  543. if err := ctlV3Put(cx, largeKey, "val", ""); err != nil {
  544. cx.t.Fatal(err)
  545. }
  546. }
  547. // try a non granted key
  548. if err := ctlV3PutFailPerm(cx, "x", "baz"); err != nil {
  549. cx.t.Fatal(err)
  550. }
  551. // revoke the open ended permission
  552. cx.user, cx.pass = "root", "root"
  553. if err := ctlV3RoleRevokePermission(cx, "test-role", "z", "", true); err != nil {
  554. cx.t.Fatal(err)
  555. }
  556. // try the revoked open ended permission
  557. cx.user, cx.pass = "test-user", "pass"
  558. for i := 0; i < 10; i++ {
  559. key := fmt.Sprintf("z%d", i)
  560. if err := ctlV3PutFailPerm(cx, key, "val"); err != nil {
  561. cx.t.Fatal(err)
  562. }
  563. }
  564. }
  565. func authTestWatch(cx ctlCtx) {
  566. if err := authEnable(cx); err != nil {
  567. cx.t.Fatal(err)
  568. }
  569. cx.user, cx.pass = "root", "root"
  570. authSetupTestUser(cx)
  571. // grant a key range
  572. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "key", "key4", false}); err != nil {
  573. cx.t.Fatal(err)
  574. }
  575. tests := []struct {
  576. puts []kv
  577. args []string
  578. wkv []kv
  579. want bool
  580. }{
  581. { // watch 1 key, should be successful
  582. []kv{{"key", "value"}},
  583. []string{"key", "--rev", "1"},
  584. []kv{{"key", "value"}},
  585. true,
  586. },
  587. { // watch 3 keys by range, should be successful
  588. []kv{{"key1", "val1"}, {"key3", "val3"}, {"key2", "val2"}},
  589. []string{"key", "key3", "--rev", "1"},
  590. []kv{{"key1", "val1"}, {"key2", "val2"}},
  591. true,
  592. },
  593. { // watch 1 key, should not be successful
  594. []kv{},
  595. []string{"key5", "--rev", "1"},
  596. []kv{},
  597. false,
  598. },
  599. { // watch 3 keys by range, should not be successful
  600. []kv{},
  601. []string{"key", "key6", "--rev", "1"},
  602. []kv{},
  603. false,
  604. },
  605. }
  606. cx.user, cx.pass = "test-user", "pass"
  607. for i, tt := range tests {
  608. donec := make(chan struct{})
  609. go func(i int, puts []kv) {
  610. defer close(donec)
  611. for j := range puts {
  612. if err := ctlV3Put(cx, puts[j].key, puts[j].val, ""); err != nil {
  613. cx.t.Fatalf("watchTest #%d-%d: ctlV3Put error (%v)", i, j, err)
  614. }
  615. }
  616. }(i, tt.puts)
  617. var err error
  618. if tt.want {
  619. err = ctlV3Watch(cx, tt.args, tt.wkv...)
  620. } else {
  621. err = ctlV3WatchFailPerm(cx, tt.args)
  622. }
  623. if err != nil {
  624. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  625. cx.t.Errorf("watchTest #%d: ctlV3Watch error (%v)", i, err)
  626. }
  627. }
  628. <-donec
  629. }
  630. }