client_auth_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. // Copyright 2015 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 v2http
  15. import (
  16. "crypto/tls"
  17. "crypto/x509"
  18. "encoding/json"
  19. "encoding/pem"
  20. "errors"
  21. "fmt"
  22. "io/ioutil"
  23. "net/http"
  24. "net/http/httptest"
  25. "net/url"
  26. "path"
  27. "sort"
  28. "strings"
  29. "testing"
  30. "github.com/coreos/etcd/etcdserver/api"
  31. "github.com/coreos/etcd/etcdserver/auth"
  32. )
  33. const goodPassword = "good"
  34. func mustJSONRequest(t *testing.T, method string, p string, body string) *http.Request {
  35. req, err := http.NewRequest(method, path.Join(authPrefix, p), strings.NewReader(body))
  36. if err != nil {
  37. t.Fatalf("Error making JSON request: %s %s %s\n", method, p, body)
  38. }
  39. req.Header.Set("Content-Type", "application/json")
  40. return req
  41. }
  42. type mockAuthStore struct {
  43. users map[string]*auth.User
  44. roles map[string]*auth.Role
  45. err error
  46. enabled bool
  47. }
  48. func (s *mockAuthStore) AllUsers() ([]string, error) {
  49. var us []string
  50. for u := range s.users {
  51. us = append(us, u)
  52. }
  53. sort.Strings(us)
  54. return us, s.err
  55. }
  56. func (s *mockAuthStore) GetUser(name string) (auth.User, error) {
  57. u, ok := s.users[name]
  58. if !ok {
  59. return auth.User{}, s.err
  60. }
  61. return *u, s.err
  62. }
  63. func (s *mockAuthStore) CreateOrUpdateUser(user auth.User) (out auth.User, created bool, err error) {
  64. if s.users == nil {
  65. out, err = s.CreateUser(user)
  66. return out, true, err
  67. }
  68. out, err = s.UpdateUser(user)
  69. return out, false, err
  70. }
  71. func (s *mockAuthStore) CreateUser(user auth.User) (auth.User, error) { return user, s.err }
  72. func (s *mockAuthStore) DeleteUser(name string) error { return s.err }
  73. func (s *mockAuthStore) UpdateUser(user auth.User) (auth.User, error) {
  74. return *s.users[user.User], s.err
  75. }
  76. func (s *mockAuthStore) AllRoles() ([]string, error) {
  77. return []string{"awesome", "guest", "root"}, s.err
  78. }
  79. func (s *mockAuthStore) GetRole(name string) (auth.Role, error) {
  80. r, ok := s.roles[name]
  81. if ok {
  82. return *r, s.err
  83. }
  84. return auth.Role{}, fmt.Errorf("%q does not exist (%v)", name, s.err)
  85. }
  86. func (s *mockAuthStore) CreateRole(role auth.Role) error { return s.err }
  87. func (s *mockAuthStore) DeleteRole(name string) error { return s.err }
  88. func (s *mockAuthStore) UpdateRole(role auth.Role) (auth.Role, error) {
  89. return *s.roles[role.Role], s.err
  90. }
  91. func (s *mockAuthStore) AuthEnabled() bool { return s.enabled }
  92. func (s *mockAuthStore) EnableAuth() error { return s.err }
  93. func (s *mockAuthStore) DisableAuth() error { return s.err }
  94. func (s *mockAuthStore) CheckPassword(user auth.User, password string) bool {
  95. return user.Password == password
  96. }
  97. func (s *mockAuthStore) HashPassword(password string) (string, error) {
  98. return password, nil
  99. }
  100. func TestAuthFlow(t *testing.T) {
  101. api.EnableCapability(api.AuthCapability)
  102. var testCases = []struct {
  103. req *http.Request
  104. store mockAuthStore
  105. wcode int
  106. wbody string
  107. }{
  108. {
  109. req: mustJSONRequest(t, "PUT", "users/alice", `{{{{{{{`),
  110. store: mockAuthStore{},
  111. wcode: http.StatusBadRequest,
  112. wbody: `{"message":"Invalid JSON in request body."}`,
  113. },
  114. {
  115. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "password": "goodpassword"}`),
  116. store: mockAuthStore{enabled: true},
  117. wcode: http.StatusUnauthorized,
  118. wbody: `{"message":"Insufficient credentials"}`,
  119. },
  120. // Users
  121. {
  122. req: mustJSONRequest(t, "GET", "users", ""),
  123. store: mockAuthStore{
  124. users: map[string]*auth.User{
  125. "alice": {
  126. User: "alice",
  127. Roles: []string{"alicerole", "guest"},
  128. Password: "wheeee",
  129. },
  130. "bob": {
  131. User: "bob",
  132. Roles: []string{"guest"},
  133. Password: "wheeee",
  134. },
  135. "root": {
  136. User: "root",
  137. Roles: []string{"root"},
  138. Password: "wheeee",
  139. },
  140. },
  141. roles: map[string]*auth.Role{
  142. "alicerole": {
  143. Role: "alicerole",
  144. },
  145. "guest": {
  146. Role: "guest",
  147. },
  148. "root": {
  149. Role: "root",
  150. },
  151. },
  152. },
  153. wcode: http.StatusOK,
  154. wbody: `{"users":[` +
  155. `{"user":"alice","roles":[` +
  156. `{"role":"alicerole","permissions":{"kv":{"read":null,"write":null}}},` +
  157. `{"role":"guest","permissions":{"kv":{"read":null,"write":null}}}` +
  158. `]},` +
  159. `{"user":"bob","roles":[{"role":"guest","permissions":{"kv":{"read":null,"write":null}}}]},` +
  160. `{"user":"root","roles":[{"role":"root","permissions":{"kv":{"read":null,"write":null}}}]}]}`,
  161. },
  162. {
  163. req: mustJSONRequest(t, "GET", "users/alice", ""),
  164. store: mockAuthStore{
  165. users: map[string]*auth.User{
  166. "alice": {
  167. User: "alice",
  168. Roles: []string{"alicerole"},
  169. Password: "wheeee",
  170. },
  171. },
  172. roles: map[string]*auth.Role{
  173. "alicerole": {
  174. Role: "alicerole",
  175. },
  176. },
  177. },
  178. wcode: http.StatusOK,
  179. wbody: `{"user":"alice","roles":[{"role":"alicerole","permissions":{"kv":{"read":null,"write":null}}}]}`,
  180. },
  181. {
  182. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "password": "goodpassword"}`),
  183. store: mockAuthStore{},
  184. wcode: http.StatusCreated,
  185. wbody: `{"user":"alice","roles":null}`,
  186. },
  187. {
  188. req: mustJSONRequest(t, "DELETE", "users/alice", ``),
  189. store: mockAuthStore{},
  190. wcode: http.StatusOK,
  191. wbody: ``,
  192. },
  193. {
  194. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "password": "goodpassword"}`),
  195. store: mockAuthStore{
  196. users: map[string]*auth.User{
  197. "alice": {
  198. User: "alice",
  199. Roles: []string{"alicerole", "guest"},
  200. Password: "wheeee",
  201. },
  202. },
  203. },
  204. wcode: http.StatusOK,
  205. wbody: `{"user":"alice","roles":["alicerole","guest"]}`,
  206. },
  207. {
  208. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "grant": ["alicerole"]}`),
  209. store: mockAuthStore{
  210. users: map[string]*auth.User{
  211. "alice": {
  212. User: "alice",
  213. Roles: []string{"alicerole", "guest"},
  214. Password: "wheeee",
  215. },
  216. },
  217. },
  218. wcode: http.StatusOK,
  219. wbody: `{"user":"alice","roles":["alicerole","guest"]}`,
  220. },
  221. {
  222. req: mustJSONRequest(t, "GET", "users/alice", ``),
  223. store: mockAuthStore{
  224. users: map[string]*auth.User{},
  225. err: auth.Error{Status: http.StatusNotFound, Errmsg: "auth: User alice doesn't exist."},
  226. },
  227. wcode: http.StatusNotFound,
  228. wbody: `{"message":"auth: User alice doesn't exist."}`,
  229. },
  230. {
  231. req: mustJSONRequest(t, "GET", "roles/manager", ""),
  232. store: mockAuthStore{
  233. roles: map[string]*auth.Role{
  234. "manager": {
  235. Role: "manager",
  236. },
  237. },
  238. },
  239. wcode: http.StatusOK,
  240. wbody: `{"role":"manager","permissions":{"kv":{"read":null,"write":null}}}`,
  241. },
  242. {
  243. req: mustJSONRequest(t, "DELETE", "roles/manager", ``),
  244. store: mockAuthStore{},
  245. wcode: http.StatusOK,
  246. wbody: ``,
  247. },
  248. {
  249. req: mustJSONRequest(t, "PUT", "roles/manager", `{"role":"manager","permissions":{"kv":{"read":[],"write":[]}}}`),
  250. store: mockAuthStore{},
  251. wcode: http.StatusCreated,
  252. wbody: `{"role":"manager","permissions":{"kv":{"read":[],"write":[]}}}`,
  253. },
  254. {
  255. req: mustJSONRequest(t, "PUT", "roles/manager", `{"role":"manager","revoke":{"kv":{"read":["foo"],"write":[]}}}`),
  256. store: mockAuthStore{
  257. roles: map[string]*auth.Role{
  258. "manager": {
  259. Role: "manager",
  260. },
  261. },
  262. },
  263. wcode: http.StatusOK,
  264. wbody: `{"role":"manager","permissions":{"kv":{"read":null,"write":null}}}`,
  265. },
  266. {
  267. req: mustJSONRequest(t, "GET", "roles", ""),
  268. store: mockAuthStore{
  269. roles: map[string]*auth.Role{
  270. "awesome": {
  271. Role: "awesome",
  272. },
  273. "guest": {
  274. Role: "guest",
  275. },
  276. "root": {
  277. Role: "root",
  278. },
  279. },
  280. },
  281. wcode: http.StatusOK,
  282. wbody: `{"roles":[{"role":"awesome","permissions":{"kv":{"read":null,"write":null}}},` +
  283. `{"role":"guest","permissions":{"kv":{"read":null,"write":null}}},` +
  284. `{"role":"root","permissions":{"kv":{"read":null,"write":null}}}]}`,
  285. },
  286. {
  287. req: mustJSONRequest(t, "GET", "enable", ""),
  288. store: mockAuthStore{
  289. enabled: true,
  290. },
  291. wcode: http.StatusOK,
  292. wbody: `{"enabled":true}`,
  293. },
  294. {
  295. req: mustJSONRequest(t, "PUT", "enable", ""),
  296. store: mockAuthStore{
  297. enabled: false,
  298. },
  299. wcode: http.StatusOK,
  300. wbody: ``,
  301. },
  302. {
  303. req: (func() *http.Request {
  304. req := mustJSONRequest(t, "DELETE", "enable", "")
  305. req.SetBasicAuth("root", "good")
  306. return req
  307. })(),
  308. store: mockAuthStore{
  309. enabled: true,
  310. users: map[string]*auth.User{
  311. "root": {
  312. User: "root",
  313. Password: goodPassword,
  314. Roles: []string{"root"},
  315. },
  316. },
  317. roles: map[string]*auth.Role{
  318. "root": {
  319. Role: "root",
  320. },
  321. },
  322. },
  323. wcode: http.StatusOK,
  324. wbody: ``,
  325. },
  326. {
  327. req: (func() *http.Request {
  328. req := mustJSONRequest(t, "DELETE", "enable", "")
  329. req.SetBasicAuth("root", "bad")
  330. return req
  331. })(),
  332. store: mockAuthStore{
  333. enabled: true,
  334. users: map[string]*auth.User{
  335. "root": {
  336. User: "root",
  337. Password: goodPassword,
  338. Roles: []string{"root"},
  339. },
  340. },
  341. roles: map[string]*auth.Role{
  342. "root": {
  343. Role: "guest",
  344. },
  345. },
  346. },
  347. wcode: http.StatusUnauthorized,
  348. wbody: `{"message":"Insufficient credentials"}`,
  349. },
  350. }
  351. for i, tt := range testCases {
  352. mux := http.NewServeMux()
  353. h := &authHandler{
  354. sec: &tt.store,
  355. cluster: &fakeCluster{id: 1},
  356. }
  357. handleAuth(mux, h)
  358. rw := httptest.NewRecorder()
  359. mux.ServeHTTP(rw, tt.req)
  360. if rw.Code != tt.wcode {
  361. t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode)
  362. }
  363. g := rw.Body.String()
  364. g = strings.TrimSpace(g)
  365. if g != tt.wbody {
  366. t.Errorf("#%d: got body=%s, want %s", i, g, tt.wbody)
  367. }
  368. }
  369. }
  370. func TestGetUserGrantedWithNonexistingRole(t *testing.T) {
  371. sh := &authHandler{
  372. sec: &mockAuthStore{
  373. users: map[string]*auth.User{
  374. "root": {
  375. User: "root",
  376. Roles: []string{"root", "foo"},
  377. },
  378. },
  379. roles: map[string]*auth.Role{
  380. "root": {
  381. Role: "root",
  382. },
  383. },
  384. },
  385. cluster: &fakeCluster{id: 1},
  386. }
  387. srv := httptest.NewServer(http.HandlerFunc(sh.baseUsers))
  388. defer srv.Close()
  389. req, err := http.NewRequest("GET", "", nil)
  390. if err != nil {
  391. t.Fatal(err)
  392. }
  393. req.URL, err = url.Parse(srv.URL)
  394. if err != nil {
  395. t.Fatal(err)
  396. }
  397. req.Header.Set("Content-Type", "application/json")
  398. cli := http.DefaultClient
  399. resp, err := cli.Do(req)
  400. if err != nil {
  401. t.Fatal(err)
  402. }
  403. defer resp.Body.Close()
  404. var uc usersCollections
  405. if err := json.NewDecoder(resp.Body).Decode(&uc); err != nil {
  406. t.Fatal(err)
  407. }
  408. if len(uc.Users) != 1 {
  409. t.Fatalf("expected 1 user, got %+v", uc.Users)
  410. }
  411. if uc.Users[0].User != "root" {
  412. t.Fatalf("expected 'root', got %q", uc.Users[0].User)
  413. }
  414. if len(uc.Users[0].Roles) != 1 {
  415. t.Fatalf("expected 1 role, got %+v", uc.Users[0].Roles)
  416. }
  417. if uc.Users[0].Roles[0].Role != "root" {
  418. t.Fatalf("expected 'root', got %q", uc.Users[0].Roles[0].Role)
  419. }
  420. }
  421. func mustAuthRequest(method, username, password string) *http.Request {
  422. req, err := http.NewRequest(method, "path", strings.NewReader(""))
  423. if err != nil {
  424. panic("Cannot make auth request: " + err.Error())
  425. }
  426. req.SetBasicAuth(username, password)
  427. return req
  428. }
  429. func unauthedRequest(method string) *http.Request {
  430. req, err := http.NewRequest(method, "path", strings.NewReader(""))
  431. if err != nil {
  432. panic("Cannot make request: " + err.Error())
  433. }
  434. return req
  435. }
  436. func tlsAuthedRequest(req *http.Request, certname string) *http.Request {
  437. bytes, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.pem", certname))
  438. if err != nil {
  439. panic(err)
  440. }
  441. block, _ := pem.Decode(bytes)
  442. cert, err := x509.ParseCertificate(block.Bytes)
  443. if err != nil {
  444. panic(err)
  445. }
  446. req.TLS = &tls.ConnectionState{
  447. VerifiedChains: [][]*x509.Certificate{{cert}},
  448. }
  449. return req
  450. }
  451. func TestPrefixAccess(t *testing.T) {
  452. var table = []struct {
  453. key string
  454. req *http.Request
  455. store *mockAuthStore
  456. hasRoot bool
  457. hasKeyPrefixAccess bool
  458. hasRecursiveAccess bool
  459. }{
  460. {
  461. key: "/foo",
  462. req: mustAuthRequest("GET", "root", "good"),
  463. store: &mockAuthStore{
  464. users: map[string]*auth.User{
  465. "root": {
  466. User: "root",
  467. Password: goodPassword,
  468. Roles: []string{"root"},
  469. },
  470. },
  471. roles: map[string]*auth.Role{
  472. "root": {
  473. Role: "root",
  474. },
  475. },
  476. enabled: true,
  477. },
  478. hasRoot: true,
  479. hasKeyPrefixAccess: true,
  480. hasRecursiveAccess: true,
  481. },
  482. {
  483. key: "/foo",
  484. req: mustAuthRequest("GET", "user", "good"),
  485. store: &mockAuthStore{
  486. users: map[string]*auth.User{
  487. "user": {
  488. User: "user",
  489. Password: goodPassword,
  490. Roles: []string{"foorole"},
  491. },
  492. },
  493. roles: map[string]*auth.Role{
  494. "foorole": {
  495. Role: "foorole",
  496. Permissions: auth.Permissions{
  497. KV: auth.RWPermission{
  498. Read: []string{"/foo"},
  499. Write: []string{"/foo"},
  500. },
  501. },
  502. },
  503. },
  504. enabled: true,
  505. },
  506. hasRoot: false,
  507. hasKeyPrefixAccess: true,
  508. hasRecursiveAccess: false,
  509. },
  510. {
  511. key: "/foo",
  512. req: mustAuthRequest("GET", "user", "good"),
  513. store: &mockAuthStore{
  514. users: map[string]*auth.User{
  515. "user": {
  516. User: "user",
  517. Password: goodPassword,
  518. Roles: []string{"foorole"},
  519. },
  520. },
  521. roles: map[string]*auth.Role{
  522. "foorole": {
  523. Role: "foorole",
  524. Permissions: auth.Permissions{
  525. KV: auth.RWPermission{
  526. Read: []string{"/foo*"},
  527. Write: []string{"/foo*"},
  528. },
  529. },
  530. },
  531. },
  532. enabled: true,
  533. },
  534. hasRoot: false,
  535. hasKeyPrefixAccess: true,
  536. hasRecursiveAccess: true,
  537. },
  538. {
  539. key: "/foo",
  540. req: mustAuthRequest("GET", "user", "bad"),
  541. store: &mockAuthStore{
  542. users: map[string]*auth.User{
  543. "user": {
  544. User: "user",
  545. Password: goodPassword,
  546. Roles: []string{"foorole"},
  547. },
  548. },
  549. roles: map[string]*auth.Role{
  550. "foorole": {
  551. Role: "foorole",
  552. Permissions: auth.Permissions{
  553. KV: auth.RWPermission{
  554. Read: []string{"/foo*"},
  555. Write: []string{"/foo*"},
  556. },
  557. },
  558. },
  559. },
  560. enabled: true,
  561. },
  562. hasRoot: false,
  563. hasKeyPrefixAccess: false,
  564. hasRecursiveAccess: false,
  565. },
  566. {
  567. key: "/foo",
  568. req: mustAuthRequest("GET", "user", "good"),
  569. store: &mockAuthStore{
  570. users: map[string]*auth.User{},
  571. err: errors.New("Not the user"),
  572. enabled: true,
  573. },
  574. hasRoot: false,
  575. hasKeyPrefixAccess: false,
  576. hasRecursiveAccess: false,
  577. },
  578. {
  579. key: "/foo",
  580. req: mustJSONRequest(t, "GET", "somepath", ""),
  581. store: &mockAuthStore{
  582. users: map[string]*auth.User{
  583. "user": {
  584. User: "user",
  585. Password: goodPassword,
  586. Roles: []string{"foorole"},
  587. },
  588. },
  589. roles: map[string]*auth.Role{
  590. "guest": {
  591. Role: "guest",
  592. Permissions: auth.Permissions{
  593. KV: auth.RWPermission{
  594. Read: []string{"/foo*"},
  595. Write: []string{"/foo*"},
  596. },
  597. },
  598. },
  599. },
  600. enabled: true,
  601. },
  602. hasRoot: false,
  603. hasKeyPrefixAccess: true,
  604. hasRecursiveAccess: true,
  605. },
  606. {
  607. key: "/bar",
  608. req: mustJSONRequest(t, "GET", "somepath", ""),
  609. store: &mockAuthStore{
  610. users: map[string]*auth.User{
  611. "user": {
  612. User: "user",
  613. Password: goodPassword,
  614. Roles: []string{"foorole"},
  615. },
  616. },
  617. roles: map[string]*auth.Role{
  618. "guest": {
  619. Role: "guest",
  620. Permissions: auth.Permissions{
  621. KV: auth.RWPermission{
  622. Read: []string{"/foo*"},
  623. Write: []string{"/foo*"},
  624. },
  625. },
  626. },
  627. },
  628. enabled: true,
  629. },
  630. hasRoot: false,
  631. hasKeyPrefixAccess: false,
  632. hasRecursiveAccess: false,
  633. },
  634. // check access for multiple roles
  635. {
  636. key: "/foo",
  637. req: mustAuthRequest("GET", "user", "good"),
  638. store: &mockAuthStore{
  639. users: map[string]*auth.User{
  640. "user": {
  641. User: "user",
  642. Password: goodPassword,
  643. Roles: []string{"role1", "role2"},
  644. },
  645. },
  646. roles: map[string]*auth.Role{
  647. "role1": {
  648. Role: "role1",
  649. },
  650. "role2": {
  651. Role: "role2",
  652. Permissions: auth.Permissions{
  653. KV: auth.RWPermission{
  654. Read: []string{"/foo"},
  655. Write: []string{"/foo"},
  656. },
  657. },
  658. },
  659. },
  660. enabled: true,
  661. },
  662. hasRoot: false,
  663. hasKeyPrefixAccess: true,
  664. hasRecursiveAccess: false,
  665. },
  666. {
  667. key: "/foo",
  668. req: (func() *http.Request {
  669. req := mustJSONRequest(t, "GET", "somepath", "")
  670. req.Header.Set("Authorization", "malformedencoding")
  671. return req
  672. })(),
  673. store: &mockAuthStore{
  674. enabled: true,
  675. users: map[string]*auth.User{
  676. "root": {
  677. User: "root",
  678. Password: goodPassword,
  679. Roles: []string{"root"},
  680. },
  681. },
  682. roles: map[string]*auth.Role{
  683. "guest": {
  684. Role: "guest",
  685. Permissions: auth.Permissions{
  686. KV: auth.RWPermission{
  687. Read: []string{"/foo*"},
  688. Write: []string{"/foo*"},
  689. },
  690. },
  691. },
  692. },
  693. },
  694. hasRoot: false,
  695. hasKeyPrefixAccess: false,
  696. hasRecursiveAccess: false,
  697. },
  698. { // guest access in non-TLS mode
  699. key: "/foo",
  700. req: (func() *http.Request {
  701. return mustJSONRequest(t, "GET", "somepath", "")
  702. })(),
  703. store: &mockAuthStore{
  704. enabled: true,
  705. users: map[string]*auth.User{
  706. "root": {
  707. User: "root",
  708. Password: goodPassword,
  709. Roles: []string{"root"},
  710. },
  711. },
  712. roles: map[string]*auth.Role{
  713. "guest": {
  714. Role: "guest",
  715. Permissions: auth.Permissions{
  716. KV: auth.RWPermission{
  717. Read: []string{"/foo*"},
  718. Write: []string{"/foo*"},
  719. },
  720. },
  721. },
  722. },
  723. },
  724. hasRoot: false,
  725. hasKeyPrefixAccess: true,
  726. hasRecursiveAccess: true,
  727. },
  728. }
  729. for i, tt := range table {
  730. if tt.hasRoot != hasRootAccess(tt.store, tt.req, true) {
  731. t.Errorf("#%d: hasRoot doesn't match (expected %v)", i, tt.hasRoot)
  732. }
  733. if tt.hasKeyPrefixAccess != hasKeyPrefixAccess(tt.store, tt.req, tt.key, false, true) {
  734. t.Errorf("#%d: hasKeyPrefixAccess doesn't match (expected %v)", i, tt.hasRoot)
  735. }
  736. if tt.hasRecursiveAccess != hasKeyPrefixAccess(tt.store, tt.req, tt.key, true, true) {
  737. t.Errorf("#%d: hasRecursiveAccess doesn't match (expected %v)", i, tt.hasRoot)
  738. }
  739. }
  740. }
  741. func TestUserFromClientCertificate(t *testing.T) {
  742. witherror := &mockAuthStore{
  743. users: map[string]*auth.User{
  744. "user": {
  745. User: "user",
  746. Roles: []string{"root"},
  747. Password: "password",
  748. },
  749. "basicauth": {
  750. User: "basicauth",
  751. Roles: []string{"root"},
  752. Password: "password",
  753. },
  754. },
  755. roles: map[string]*auth.Role{
  756. "root": {
  757. Role: "root",
  758. },
  759. },
  760. err: errors.New(""),
  761. }
  762. noerror := &mockAuthStore{
  763. users: map[string]*auth.User{
  764. "user": {
  765. User: "user",
  766. Roles: []string{"root"},
  767. Password: "password",
  768. },
  769. "basicauth": {
  770. User: "basicauth",
  771. Roles: []string{"root"},
  772. Password: "password",
  773. },
  774. },
  775. roles: map[string]*auth.Role{
  776. "root": {
  777. Role: "root",
  778. },
  779. },
  780. }
  781. var table = []struct {
  782. req *http.Request
  783. userExists bool
  784. store auth.Store
  785. username string
  786. }{
  787. {
  788. // non tls request
  789. req: unauthedRequest("GET"),
  790. userExists: false,
  791. store: witherror,
  792. },
  793. {
  794. // cert with cn of existing user
  795. req: tlsAuthedRequest(unauthedRequest("GET"), "user"),
  796. userExists: true,
  797. username: "user",
  798. store: noerror,
  799. },
  800. {
  801. // cert with cn of non-existing user
  802. req: tlsAuthedRequest(unauthedRequest("GET"), "otheruser"),
  803. userExists: false,
  804. store: witherror,
  805. },
  806. }
  807. for i, tt := range table {
  808. user := userFromClientCertificate(tt.store, tt.req)
  809. userExists := user != nil
  810. if tt.userExists != userExists {
  811. t.Errorf("#%d: userFromClientCertificate doesn't match (expected %v)", i, tt.userExists)
  812. }
  813. if user != nil && (tt.username != user.User) {
  814. t.Errorf("#%d: userFromClientCertificate username doesn't match (expected %s, got %s)", i, tt.username, user.User)
  815. }
  816. }
  817. }
  818. func TestUserFromBasicAuth(t *testing.T) {
  819. sec := &mockAuthStore{
  820. users: map[string]*auth.User{
  821. "user": {
  822. User: "user",
  823. Roles: []string{"root"},
  824. Password: "password",
  825. },
  826. },
  827. roles: map[string]*auth.Role{
  828. "root": {
  829. Role: "root",
  830. },
  831. },
  832. }
  833. var table = []struct {
  834. username string
  835. req *http.Request
  836. userExists bool
  837. }{
  838. {
  839. // valid user, valid pass
  840. username: "user",
  841. req: mustAuthRequest("GET", "user", "password"),
  842. userExists: true,
  843. },
  844. {
  845. // valid user, bad pass
  846. username: "user",
  847. req: mustAuthRequest("GET", "user", "badpass"),
  848. userExists: false,
  849. },
  850. {
  851. // valid user, no pass
  852. username: "user",
  853. req: mustAuthRequest("GET", "user", ""),
  854. userExists: false,
  855. },
  856. {
  857. // missing user
  858. username: "missing",
  859. req: mustAuthRequest("GET", "missing", "badpass"),
  860. userExists: false,
  861. },
  862. {
  863. // no basic auth
  864. req: unauthedRequest("GET"),
  865. userExists: false,
  866. },
  867. }
  868. for i, tt := range table {
  869. user := userFromBasicAuth(sec, tt.req)
  870. userExists := user != nil
  871. if tt.userExists != userExists {
  872. t.Errorf("#%d: userFromBasicAuth doesn't match (expected %v)", i, tt.userExists)
  873. }
  874. if user != nil && (tt.username != user.User) {
  875. t.Errorf("#%d: userFromBasicAuth username doesn't match (expected %s, got %s)", i, tt.username, user.User)
  876. }
  877. }
  878. }