auth_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. // Copyright 2015 CoreOS, Inc.
  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 auth
  15. import (
  16. "reflect"
  17. "testing"
  18. "time"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  20. etcderr "github.com/coreos/etcd/error"
  21. "github.com/coreos/etcd/etcdserver"
  22. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. etcdstore "github.com/coreos/etcd/store"
  24. )
  25. const testTimeout = time.Millisecond
  26. func TestMergeUser(t *testing.T) {
  27. tbl := []struct {
  28. input User
  29. merge User
  30. expect User
  31. iserr bool
  32. }{
  33. {
  34. User{User: "foo"},
  35. User{User: "bar"},
  36. User{},
  37. true,
  38. },
  39. {
  40. User{User: "foo"},
  41. User{User: "foo"},
  42. User{User: "foo", Roles: []string{}},
  43. false,
  44. },
  45. {
  46. User{User: "foo"},
  47. User{User: "foo", Grant: []string{"role1"}},
  48. User{User: "foo", Roles: []string{"role1"}},
  49. false,
  50. },
  51. {
  52. User{User: "foo", Roles: []string{"role1"}},
  53. User{User: "foo", Grant: []string{"role1"}},
  54. User{},
  55. true,
  56. },
  57. {
  58. User{User: "foo", Roles: []string{"role1"}},
  59. User{User: "foo", Revoke: []string{"role2"}},
  60. User{},
  61. true,
  62. },
  63. {
  64. User{User: "foo", Roles: []string{"role1"}},
  65. User{User: "foo", Grant: []string{"role2"}},
  66. User{User: "foo", Roles: []string{"role1", "role2"}},
  67. false,
  68. },
  69. {
  70. User{User: "foo"},
  71. User{User: "foo", Password: "$2a$10$aUPOdbOGNawaVSusg3g2wuC3AH6XxIr9/Ms4VgDvzrAVOJPYzZILa"},
  72. User{User: "foo", Roles: []string{}, Password: "$2a$10$aUPOdbOGNawaVSusg3g2wuC3AH6XxIr9/Ms4VgDvzrAVOJPYzZILa"},
  73. false,
  74. },
  75. }
  76. for i, tt := range tbl {
  77. out, err := tt.input.merge(tt.merge)
  78. if err != nil && !tt.iserr {
  79. t.Fatalf("Got unexpected error on item %d", i)
  80. }
  81. if !tt.iserr {
  82. if !reflect.DeepEqual(out, tt.expect) {
  83. t.Errorf("Unequal merge expectation on item %d: got: %#v, expect: %#v", i, out, tt.expect)
  84. }
  85. }
  86. }
  87. }
  88. func TestMergeRole(t *testing.T) {
  89. tbl := []struct {
  90. input Role
  91. merge Role
  92. expect Role
  93. iserr bool
  94. }{
  95. {
  96. Role{Role: "foo"},
  97. Role{Role: "bar"},
  98. Role{},
  99. true,
  100. },
  101. {
  102. Role{Role: "foo"},
  103. Role{Role: "foo", Grant: &Permissions{KV: RWPermission{Read: []string{"/foodir"}, Write: []string{"/foodir"}}}},
  104. Role{Role: "foo", Permissions: Permissions{KV: RWPermission{Read: []string{"/foodir"}, Write: []string{"/foodir"}}}},
  105. false,
  106. },
  107. {
  108. Role{Role: "foo", Permissions: Permissions{KV: RWPermission{Read: []string{"/foodir"}, Write: []string{"/foodir"}}}},
  109. Role{Role: "foo", Revoke: &Permissions{KV: RWPermission{Read: []string{"/foodir"}, Write: []string{"/foodir"}}}},
  110. Role{Role: "foo", Permissions: Permissions{KV: RWPermission{Read: []string{}, Write: []string{}}}},
  111. false,
  112. },
  113. {
  114. Role{Role: "foo", Permissions: Permissions{KV: RWPermission{Read: []string{"/bardir"}}}},
  115. Role{Role: "foo", Revoke: &Permissions{KV: RWPermission{Read: []string{"/foodir"}}}},
  116. Role{},
  117. true,
  118. },
  119. }
  120. for i, tt := range tbl {
  121. out, err := tt.input.merge(tt.merge)
  122. if err != nil && !tt.iserr {
  123. t.Fatalf("Got unexpected error on item %d", i)
  124. }
  125. if !tt.iserr {
  126. if !reflect.DeepEqual(out, tt.expect) {
  127. t.Errorf("Unequal merge expectation on item %d: got: %#v, expect: %#v", i, out, tt.expect)
  128. }
  129. }
  130. }
  131. }
  132. type testDoer struct {
  133. get []etcdserver.Response
  134. put []etcdserver.Response
  135. getindex int
  136. putindex int
  137. explicitlyEnabled bool
  138. }
  139. func (td *testDoer) Do(_ context.Context, req etcdserverpb.Request) (etcdserver.Response, error) {
  140. if td.explicitlyEnabled && (req.Path == StorePermsPrefix+"/enabled") {
  141. t := "true"
  142. return etcdserver.Response{
  143. Event: &etcdstore.Event{
  144. Action: etcdstore.Get,
  145. Node: &etcdstore.NodeExtern{
  146. Key: StorePermsPrefix + "/users/cat",
  147. Value: &t,
  148. },
  149. },
  150. }, nil
  151. }
  152. if req.Method == "GET" && td.get != nil {
  153. res := td.get[td.getindex]
  154. if res.Event == nil {
  155. td.getindex++
  156. return etcdserver.Response{}, &etcderr.Error{
  157. ErrorCode: etcderr.EcodeKeyNotFound,
  158. }
  159. }
  160. td.getindex++
  161. return res, nil
  162. }
  163. if req.Method == "PUT" && td.put != nil {
  164. res := td.put[td.putindex]
  165. if res.Event == nil {
  166. td.putindex++
  167. return etcdserver.Response{}, &etcderr.Error{
  168. ErrorCode: etcderr.EcodeNodeExist,
  169. }
  170. }
  171. td.putindex++
  172. return res, nil
  173. }
  174. return etcdserver.Response{}, nil
  175. }
  176. func TestAllUsers(t *testing.T) {
  177. d := &testDoer{
  178. get: []etcdserver.Response{
  179. {
  180. Event: &etcdstore.Event{
  181. Action: etcdstore.Get,
  182. Node: &etcdstore.NodeExtern{
  183. Nodes: etcdstore.NodeExterns([]*etcdstore.NodeExtern{
  184. {
  185. Key: StorePermsPrefix + "/users/cat",
  186. },
  187. {
  188. Key: StorePermsPrefix + "/users/dog",
  189. },
  190. }),
  191. },
  192. },
  193. },
  194. },
  195. }
  196. expected := []string{"cat", "dog"}
  197. s := store{server: d, timeout: testTimeout, ensuredOnce: false}
  198. users, err := s.AllUsers()
  199. if err != nil {
  200. t.Error("Unexpected error", err)
  201. }
  202. if !reflect.DeepEqual(users, expected) {
  203. t.Error("AllUsers doesn't match given store. Got", users, "expected", expected)
  204. }
  205. }
  206. func TestGetAndDeleteUser(t *testing.T) {
  207. data := `{"user": "cat", "roles" : ["animal"]}`
  208. d := &testDoer{
  209. get: []etcdserver.Response{
  210. {
  211. Event: &etcdstore.Event{
  212. Action: etcdstore.Get,
  213. Node: &etcdstore.NodeExtern{
  214. Key: StorePermsPrefix + "/users/cat",
  215. Value: &data,
  216. },
  217. },
  218. },
  219. },
  220. explicitlyEnabled: true,
  221. }
  222. expected := User{User: "cat", Roles: []string{"animal"}}
  223. s := store{server: d, timeout: testTimeout, ensuredOnce: false}
  224. out, err := s.GetUser("cat")
  225. if err != nil {
  226. t.Error("Unexpected error", err)
  227. }
  228. if !reflect.DeepEqual(out, expected) {
  229. t.Error("GetUser doesn't match given store. Got", out, "expected", expected)
  230. }
  231. err = s.DeleteUser("cat")
  232. if err != nil {
  233. t.Error("Unexpected error", err)
  234. }
  235. }
  236. func TestAllRoles(t *testing.T) {
  237. d := &testDoer{
  238. get: []etcdserver.Response{
  239. {
  240. Event: &etcdstore.Event{
  241. Action: etcdstore.Get,
  242. Node: &etcdstore.NodeExtern{
  243. Nodes: etcdstore.NodeExterns([]*etcdstore.NodeExtern{
  244. {
  245. Key: StorePermsPrefix + "/roles/animal",
  246. },
  247. {
  248. Key: StorePermsPrefix + "/roles/human",
  249. },
  250. }),
  251. },
  252. },
  253. },
  254. },
  255. explicitlyEnabled: true,
  256. }
  257. expected := []string{"animal", "human", "root"}
  258. s := store{server: d, timeout: testTimeout, ensuredOnce: false}
  259. out, err := s.AllRoles()
  260. if err != nil {
  261. t.Error("Unexpected error", err)
  262. }
  263. if !reflect.DeepEqual(out, expected) {
  264. t.Error("AllRoles doesn't match given store. Got", out, "expected", expected)
  265. }
  266. }
  267. func TestGetAndDeleteRole(t *testing.T) {
  268. data := `{"role": "animal"}`
  269. d := &testDoer{
  270. get: []etcdserver.Response{
  271. {
  272. Event: &etcdstore.Event{
  273. Action: etcdstore.Get,
  274. Node: &etcdstore.NodeExtern{
  275. Key: StorePermsPrefix + "/roles/animal",
  276. Value: &data,
  277. },
  278. },
  279. },
  280. },
  281. explicitlyEnabled: true,
  282. }
  283. expected := Role{Role: "animal"}
  284. s := store{server: d, timeout: testTimeout, ensuredOnce: false}
  285. out, err := s.GetRole("animal")
  286. if err != nil {
  287. t.Error("Unexpected error", err)
  288. }
  289. if !reflect.DeepEqual(out, expected) {
  290. t.Error("GetRole doesn't match given store. Got", out, "expected", expected)
  291. }
  292. err = s.DeleteRole("animal")
  293. if err != nil {
  294. t.Error("Unexpected error", err)
  295. }
  296. }
  297. func TestEnsure(t *testing.T) {
  298. d := &testDoer{
  299. get: []etcdserver.Response{
  300. {
  301. Event: &etcdstore.Event{
  302. Action: etcdstore.Set,
  303. Node: &etcdstore.NodeExtern{
  304. Key: StorePermsPrefix,
  305. Dir: true,
  306. },
  307. },
  308. },
  309. {
  310. Event: &etcdstore.Event{
  311. Action: etcdstore.Set,
  312. Node: &etcdstore.NodeExtern{
  313. Key: StorePermsPrefix + "/users/",
  314. Dir: true,
  315. },
  316. },
  317. },
  318. {
  319. Event: &etcdstore.Event{
  320. Action: etcdstore.Set,
  321. Node: &etcdstore.NodeExtern{
  322. Key: StorePermsPrefix + "/roles/",
  323. Dir: true,
  324. },
  325. },
  326. },
  327. },
  328. }
  329. s := store{server: d, timeout: testTimeout, ensuredOnce: false}
  330. err := s.ensureAuthDirectories()
  331. if err != nil {
  332. t.Error("Unexpected error", err)
  333. }
  334. }
  335. type fastPasswordStore struct {
  336. }
  337. func (_ fastPasswordStore) CheckPassword(user User, password string) bool {
  338. return user.Password == password
  339. }
  340. func (_ fastPasswordStore) HashPassword(password string) (string, error) { return password, nil }
  341. func TestCreateAndUpdateUser(t *testing.T) {
  342. olduser := `{"user": "cat", "roles" : ["animal"]}`
  343. newuser := `{"user": "cat", "roles" : ["animal", "pet"]}`
  344. d := &testDoer{
  345. get: []etcdserver.Response{
  346. {
  347. Event: nil,
  348. },
  349. {
  350. Event: &etcdstore.Event{
  351. Action: etcdstore.Get,
  352. Node: &etcdstore.NodeExtern{
  353. Key: StorePermsPrefix + "/users/cat",
  354. Value: &olduser,
  355. },
  356. },
  357. },
  358. {
  359. Event: &etcdstore.Event{
  360. Action: etcdstore.Get,
  361. Node: &etcdstore.NodeExtern{
  362. Key: StorePermsPrefix + "/users/cat",
  363. Value: &olduser,
  364. },
  365. },
  366. },
  367. },
  368. put: []etcdserver.Response{
  369. {
  370. Event: &etcdstore.Event{
  371. Action: etcdstore.Update,
  372. Node: &etcdstore.NodeExtern{
  373. Key: StorePermsPrefix + "/users/cat",
  374. Value: &olduser,
  375. },
  376. },
  377. },
  378. {
  379. Event: &etcdstore.Event{
  380. Action: etcdstore.Update,
  381. Node: &etcdstore.NodeExtern{
  382. Key: StorePermsPrefix + "/users/cat",
  383. Value: &newuser,
  384. },
  385. },
  386. },
  387. },
  388. explicitlyEnabled: true,
  389. }
  390. user := User{User: "cat", Password: "meow", Roles: []string{"animal"}}
  391. update := User{User: "cat", Grant: []string{"pet"}}
  392. expected := User{User: "cat", Roles: []string{"animal", "pet"}}
  393. s := store{server: d, timeout: testTimeout, ensuredOnce: true, PasswordStore: fastPasswordStore{}}
  394. out, created, err := s.CreateOrUpdateUser(user)
  395. if created == false {
  396. t.Error("Should have created user, instead updated?")
  397. }
  398. if err != nil {
  399. t.Error("Unexpected error", err)
  400. }
  401. out.Password = "meow"
  402. if !reflect.DeepEqual(out, user) {
  403. t.Error("UpdateUser doesn't match given update. Got", out, "expected", expected)
  404. }
  405. out, created, err = s.CreateOrUpdateUser(update)
  406. if created == true {
  407. t.Error("Should have updated user, instead created?")
  408. }
  409. if err != nil {
  410. t.Error("Unexpected error", err)
  411. }
  412. if !reflect.DeepEqual(out, expected) {
  413. t.Error("UpdateUser doesn't match given update. Got", out, "expected", expected)
  414. }
  415. }
  416. func TestUpdateRole(t *testing.T) {
  417. oldrole := `{"role": "animal", "permissions" : {"kv": {"read": ["/animal"], "write": []}}}`
  418. newrole := `{"role": "animal", "permissions" : {"kv": {"read": ["/animal"], "write": ["/animal"]}}}`
  419. d := &testDoer{
  420. get: []etcdserver.Response{
  421. {
  422. Event: &etcdstore.Event{
  423. Action: etcdstore.Get,
  424. Node: &etcdstore.NodeExtern{
  425. Key: StorePermsPrefix + "/roles/animal",
  426. Value: &oldrole,
  427. },
  428. },
  429. },
  430. },
  431. put: []etcdserver.Response{
  432. {
  433. Event: &etcdstore.Event{
  434. Action: etcdstore.Update,
  435. Node: &etcdstore.NodeExtern{
  436. Key: StorePermsPrefix + "/roles/animal",
  437. Value: &newrole,
  438. },
  439. },
  440. },
  441. },
  442. explicitlyEnabled: true,
  443. }
  444. update := Role{Role: "animal", Grant: &Permissions{KV: RWPermission{Read: []string{}, Write: []string{"/animal"}}}}
  445. expected := Role{Role: "animal", Permissions: Permissions{KV: RWPermission{Read: []string{"/animal"}, Write: []string{"/animal"}}}}
  446. s := store{server: d, timeout: testTimeout, ensuredOnce: true}
  447. out, err := s.UpdateRole(update)
  448. if err != nil {
  449. t.Error("Unexpected error", err)
  450. }
  451. if !reflect.DeepEqual(out, expected) {
  452. t.Error("UpdateRole doesn't match given update. Got", out, "expected", expected)
  453. }
  454. }
  455. func TestCreateRole(t *testing.T) {
  456. role := `{"role": "animal", "permissions" : {"kv": {"read": ["/animal"], "write": []}}}`
  457. d := &testDoer{
  458. put: []etcdserver.Response{
  459. {
  460. Event: &etcdstore.Event{
  461. Action: etcdstore.Create,
  462. Node: &etcdstore.NodeExtern{
  463. Key: StorePermsPrefix + "/roles/animal",
  464. Value: &role,
  465. },
  466. },
  467. },
  468. {
  469. Event: nil,
  470. },
  471. },
  472. explicitlyEnabled: true,
  473. }
  474. r := Role{Role: "animal", Permissions: Permissions{KV: RWPermission{Read: []string{"/animal"}, Write: []string{}}}}
  475. s := store{server: d, timeout: testTimeout, ensuredOnce: true}
  476. err := s.CreateRole(Role{Role: "root"})
  477. if err == nil {
  478. t.Error("Should error creating root role")
  479. }
  480. err = s.CreateRole(r)
  481. if err != nil {
  482. t.Error("Unexpected error", err)
  483. }
  484. err = s.CreateRole(r)
  485. if err == nil {
  486. t.Error("Creating duplicate role, should error")
  487. }
  488. }
  489. func TestEnableAuth(t *testing.T) {
  490. rootUser := `{"user": "root", "password": ""}`
  491. guestRole := `{"role": "guest", "permissions" : {"kv": {"read": ["*"], "write": ["*"]}}}`
  492. trueval := "true"
  493. falseval := "false"
  494. d := &testDoer{
  495. get: []etcdserver.Response{
  496. {
  497. Event: &etcdstore.Event{
  498. Action: etcdstore.Get,
  499. Node: &etcdstore.NodeExtern{
  500. Key: StorePermsPrefix + "/enabled",
  501. Value: &falseval,
  502. },
  503. },
  504. },
  505. {
  506. Event: &etcdstore.Event{
  507. Action: etcdstore.Get,
  508. Node: &etcdstore.NodeExtern{
  509. Key: StorePermsPrefix + "/user/root",
  510. Value: &rootUser,
  511. },
  512. },
  513. },
  514. {
  515. Event: nil,
  516. },
  517. },
  518. put: []etcdserver.Response{
  519. {
  520. Event: &etcdstore.Event{
  521. Action: etcdstore.Create,
  522. Node: &etcdstore.NodeExtern{
  523. Key: StorePermsPrefix + "/roles/guest",
  524. Value: &guestRole,
  525. },
  526. },
  527. },
  528. {
  529. Event: &etcdstore.Event{
  530. Action: etcdstore.Update,
  531. Node: &etcdstore.NodeExtern{
  532. Key: StorePermsPrefix + "/enabled",
  533. Value: &trueval,
  534. },
  535. },
  536. },
  537. },
  538. explicitlyEnabled: false,
  539. }
  540. s := store{server: d, timeout: testTimeout, ensuredOnce: true}
  541. err := s.EnableAuth()
  542. if err != nil {
  543. t.Error("Unexpected error", err)
  544. }
  545. }
  546. func TestDisableAuth(t *testing.T) {
  547. trueval := "true"
  548. falseval := "false"
  549. d := &testDoer{
  550. get: []etcdserver.Response{
  551. {
  552. Event: &etcdstore.Event{
  553. Action: etcdstore.Get,
  554. Node: &etcdstore.NodeExtern{
  555. Key: StorePermsPrefix + "/enabled",
  556. Value: &falseval,
  557. },
  558. },
  559. },
  560. {
  561. Event: &etcdstore.Event{
  562. Action: etcdstore.Get,
  563. Node: &etcdstore.NodeExtern{
  564. Key: StorePermsPrefix + "/enabled",
  565. Value: &trueval,
  566. },
  567. },
  568. },
  569. },
  570. put: []etcdserver.Response{
  571. {
  572. Event: &etcdstore.Event{
  573. Action: etcdstore.Update,
  574. Node: &etcdstore.NodeExtern{
  575. Key: StorePermsPrefix + "/enabled",
  576. Value: &falseval,
  577. },
  578. },
  579. },
  580. },
  581. explicitlyEnabled: false,
  582. }
  583. s := store{server: d, timeout: testTimeout, ensuredOnce: true}
  584. err := s.DisableAuth()
  585. if err == nil {
  586. t.Error("Expected error; already disabled")
  587. }
  588. err = s.DisableAuth()
  589. if err != nil {
  590. t.Error("Unexpected error", err)
  591. }
  592. }
  593. func TestSimpleMatch(t *testing.T) {
  594. role := Role{Role: "foo", Permissions: Permissions{KV: RWPermission{Read: []string{"/foodir/*", "/fookey"}, Write: []string{"/bardir/*", "/barkey"}}}}
  595. if !role.HasKeyAccess("/foodir/foo/bar", false) {
  596. t.Fatal("role lacks expected access")
  597. }
  598. if !role.HasKeyAccess("/fookey", false) {
  599. t.Fatal("role lacks expected access")
  600. }
  601. if !role.HasRecursiveAccess("/foodir/*", false) {
  602. t.Fatal("role lacks expected access")
  603. }
  604. if !role.HasRecursiveAccess("/foodir/foo*", false) {
  605. t.Fatal("role lacks expected access")
  606. }
  607. if !role.HasRecursiveAccess("/bardir/*", true) {
  608. t.Fatal("role lacks expected access")
  609. }
  610. if !role.HasKeyAccess("/bardir/bar/foo", true) {
  611. t.Fatal("role lacks expected access")
  612. }
  613. if !role.HasKeyAccess("/barkey", true) {
  614. t.Fatal("role lacks expected access")
  615. }
  616. if role.HasKeyAccess("/bardir/bar/foo", false) {
  617. t.Fatal("role has unexpected access")
  618. }
  619. if role.HasKeyAccess("/barkey", false) {
  620. t.Fatal("role has unexpected access")
  621. }
  622. if role.HasKeyAccess("/foodir/foo/bar", true) {
  623. t.Fatal("role has unexpected access")
  624. }
  625. if role.HasKeyAccess("/fookey", true) {
  626. t.Fatal("role has unexpected access")
  627. }
  628. }