client_auth_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 etcdhttp
  15. import (
  16. "errors"
  17. "net/http"
  18. "net/http/httptest"
  19. "path"
  20. "strings"
  21. "testing"
  22. "github.com/coreos/etcd/etcdserver/auth"
  23. )
  24. const goodPassword = "$2a$10$VYdJecHfm6WNodzv8XhmYeIG4n2SsQefdo5V2t6xIq/aWDHNqSUQW"
  25. func mustJSONRequest(t *testing.T, method string, p string, body string) *http.Request {
  26. req, err := http.NewRequest(method, path.Join(authPrefix, p), strings.NewReader(body))
  27. if err != nil {
  28. t.Fatalf("Error making JSON request: %s %s %s\n", method, p, body)
  29. }
  30. req.Header.Set("Content-Type", "application/json")
  31. return req
  32. }
  33. type mockAuthStore struct {
  34. user *auth.User
  35. roles map[string]*auth.Role
  36. err error
  37. enabled bool
  38. }
  39. func (s *mockAuthStore) AllUsers() ([]string, error) { return []string{"alice", "bob", "root"}, s.err }
  40. func (s *mockAuthStore) GetUser(name string) (auth.User, error) { return *s.user, s.err }
  41. func (s *mockAuthStore) CreateOrUpdateUser(user auth.User) (out auth.User, created bool, err error) {
  42. if s.user == nil {
  43. u, err := s.CreateUser(user)
  44. return u, true, err
  45. }
  46. u, err := s.UpdateUser(user)
  47. return u, false, err
  48. }
  49. func (s *mockAuthStore) CreateUser(user auth.User) (auth.User, error) { return user, s.err }
  50. func (s *mockAuthStore) DeleteUser(name string) error { return s.err }
  51. func (s *mockAuthStore) UpdateUser(user auth.User) (auth.User, error) { return *s.user, s.err }
  52. func (s *mockAuthStore) AllRoles() ([]string, error) {
  53. return []string{"awesome", "guest", "root"}, s.err
  54. }
  55. func (s *mockAuthStore) GetRole(name string) (auth.Role, error) { return *s.roles[name], s.err }
  56. func (s *mockAuthStore) CreateRole(role auth.Role) error { return s.err }
  57. func (s *mockAuthStore) DeleteRole(name string) error { return s.err }
  58. func (s *mockAuthStore) UpdateRole(role auth.Role) (auth.Role, error) {
  59. return *s.roles[role.Role], s.err
  60. }
  61. func (s *mockAuthStore) AuthEnabled() bool { return s.enabled }
  62. func (s *mockAuthStore) EnableAuth() error { return s.err }
  63. func (s *mockAuthStore) DisableAuth() error { return s.err }
  64. func TestAuthFlow(t *testing.T) {
  65. enableMapMu.Lock()
  66. enabledMap = make(map[capability]bool)
  67. enabledMap[authCapability] = true
  68. enableMapMu.Unlock()
  69. var testCases = []struct {
  70. req *http.Request
  71. store mockAuthStore
  72. wcode int
  73. wbody string
  74. }{
  75. {
  76. req: mustJSONRequest(t, "PUT", "users/alice", `{{{{{{{`),
  77. store: mockAuthStore{},
  78. wcode: http.StatusBadRequest,
  79. wbody: `{"message":"Invalid JSON in request body."}`,
  80. },
  81. {
  82. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "password": "goodpassword"}`),
  83. store: mockAuthStore{enabled: true},
  84. wcode: http.StatusUnauthorized,
  85. wbody: `{"message":"Insufficient credentials"}`,
  86. },
  87. // Users
  88. {
  89. req: mustJSONRequest(t, "GET", "users", ""),
  90. store: mockAuthStore{},
  91. wcode: http.StatusOK,
  92. wbody: `{"users":["alice","bob","root"]}`,
  93. },
  94. {
  95. req: mustJSONRequest(t, "GET", "users/alice", ""),
  96. store: mockAuthStore{
  97. user: &auth.User{
  98. User: "alice",
  99. Roles: []string{"alicerole", "guest"},
  100. Password: "wheeee",
  101. },
  102. },
  103. wcode: http.StatusOK,
  104. wbody: `{"user":"alice","roles":["alicerole","guest"]}`,
  105. },
  106. {
  107. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "password": "goodpassword"}`),
  108. store: mockAuthStore{},
  109. wcode: http.StatusCreated,
  110. wbody: `{"user":"alice","roles":null}`,
  111. },
  112. {
  113. req: mustJSONRequest(t, "DELETE", "users/alice", ``),
  114. store: mockAuthStore{},
  115. wcode: http.StatusOK,
  116. wbody: ``,
  117. },
  118. {
  119. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "password": "goodpassword"}`),
  120. store: mockAuthStore{
  121. user: &auth.User{
  122. User: "alice",
  123. Roles: []string{"alicerole", "guest"},
  124. Password: "wheeee",
  125. },
  126. },
  127. wcode: http.StatusOK,
  128. wbody: `{"user":"alice","roles":["alicerole","guest"]}`,
  129. },
  130. {
  131. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "grant": ["alicerole"]}`),
  132. store: mockAuthStore{
  133. user: &auth.User{
  134. User: "alice",
  135. Roles: []string{"alicerole", "guest"},
  136. Password: "wheeee",
  137. },
  138. },
  139. wcode: http.StatusOK,
  140. wbody: `{"user":"alice","roles":["alicerole","guest"]}`,
  141. },
  142. {
  143. req: mustJSONRequest(t, "GET", "users/alice", ``),
  144. store: mockAuthStore{
  145. user: &auth.User{},
  146. err: auth.Error{Status: http.StatusNotFound, Errmsg: "auth: User alice doesn't exist."},
  147. },
  148. wcode: http.StatusNotFound,
  149. wbody: `{"message":"auth: User alice doesn't exist."}`,
  150. },
  151. // Roles
  152. {
  153. req: mustJSONRequest(t, "GET", "roles/manager", ""),
  154. store: mockAuthStore{
  155. roles: map[string]*auth.Role{
  156. "manager": {
  157. Role: "manager",
  158. },
  159. },
  160. },
  161. wcode: http.StatusOK,
  162. wbody: `{"role":"manager","permissions":{"kv":{"read":null,"write":null}}}`,
  163. },
  164. {
  165. req: mustJSONRequest(t, "DELETE", "roles/manager", ``),
  166. store: mockAuthStore{},
  167. wcode: http.StatusOK,
  168. wbody: ``,
  169. },
  170. {
  171. req: mustJSONRequest(t, "PUT", "roles/manager", `{"role":"manager","permissions":{"kv":{"read":[],"write":[]}}}`),
  172. store: mockAuthStore{},
  173. wcode: http.StatusCreated,
  174. wbody: `{"role":"manager","permissions":{"kv":{"read":[],"write":[]}}}`,
  175. },
  176. {
  177. req: mustJSONRequest(t, "PUT", "roles/manager", `{"role":"manager","revoke":{"kv":{"read":["foo"],"write":[]}}}`),
  178. store: mockAuthStore{
  179. roles: map[string]*auth.Role{
  180. "manager": {
  181. Role: "manager",
  182. },
  183. },
  184. },
  185. wcode: http.StatusOK,
  186. wbody: `{"role":"manager","permissions":{"kv":{"read":null,"write":null}}}`,
  187. },
  188. {
  189. req: mustJSONRequest(t, "GET", "roles", ""),
  190. store: mockAuthStore{},
  191. wcode: http.StatusOK,
  192. wbody: `{"roles":["awesome","guest","root"]}`,
  193. },
  194. {
  195. req: mustJSONRequest(t, "GET", "enable", ""),
  196. store: mockAuthStore{
  197. enabled: true,
  198. },
  199. wcode: http.StatusOK,
  200. wbody: `{"enabled":true}`,
  201. },
  202. {
  203. req: mustJSONRequest(t, "PUT", "enable", ""),
  204. store: mockAuthStore{
  205. enabled: false,
  206. },
  207. wcode: http.StatusOK,
  208. wbody: ``,
  209. },
  210. {
  211. req: (func() *http.Request {
  212. req := mustJSONRequest(t, "DELETE", "enable", "")
  213. req.SetBasicAuth("root", "good")
  214. return req
  215. })(),
  216. store: mockAuthStore{
  217. enabled: true,
  218. user: &auth.User{
  219. User: "root",
  220. Password: goodPassword,
  221. Roles: []string{"root"},
  222. },
  223. roles: map[string]*auth.Role{
  224. "root": {
  225. Role: "root",
  226. },
  227. },
  228. },
  229. wcode: http.StatusOK,
  230. wbody: ``,
  231. },
  232. {
  233. req: (func() *http.Request {
  234. req := mustJSONRequest(t, "DELETE", "enable", "")
  235. req.SetBasicAuth("root", "bad")
  236. return req
  237. })(),
  238. store: mockAuthStore{
  239. enabled: true,
  240. user: &auth.User{
  241. User: "root",
  242. Password: goodPassword,
  243. Roles: []string{"root"},
  244. },
  245. roles: map[string]*auth.Role{
  246. "root": {
  247. Role: "guest",
  248. },
  249. },
  250. },
  251. wcode: http.StatusUnauthorized,
  252. wbody: `{"message":"Insufficient credentials"}`,
  253. },
  254. }
  255. for i, tt := range testCases {
  256. mux := http.NewServeMux()
  257. h := &authHandler{
  258. sec: &tt.store,
  259. cluster: &fakeCluster{id: 1},
  260. }
  261. handleAuth(mux, h)
  262. rw := httptest.NewRecorder()
  263. mux.ServeHTTP(rw, tt.req)
  264. if rw.Code != tt.wcode {
  265. t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode)
  266. }
  267. g := rw.Body.String()
  268. g = strings.TrimSpace(g)
  269. if g != tt.wbody {
  270. t.Errorf("#%d: got body=%s, want %s", i, g, tt.wbody)
  271. }
  272. }
  273. }
  274. func mustAuthRequest(method, username, password string) *http.Request {
  275. req, err := http.NewRequest(method, "path", strings.NewReader(""))
  276. if err != nil {
  277. panic("Cannot make auth request: " + err.Error())
  278. }
  279. req.SetBasicAuth(username, password)
  280. return req
  281. }
  282. func TestPrefixAccess(t *testing.T) {
  283. var table = []struct {
  284. key string
  285. req *http.Request
  286. store *mockAuthStore
  287. hasRoot bool
  288. hasKeyPrefixAccess bool
  289. hasRecursiveAccess bool
  290. }{
  291. {
  292. key: "/foo",
  293. req: mustAuthRequest("GET", "root", "good"),
  294. store: &mockAuthStore{
  295. user: &auth.User{
  296. User: "root",
  297. Password: goodPassword,
  298. Roles: []string{"root"},
  299. },
  300. roles: map[string]*auth.Role{
  301. "root": {
  302. Role: "root",
  303. },
  304. },
  305. enabled: true,
  306. },
  307. hasRoot: true,
  308. hasKeyPrefixAccess: true,
  309. hasRecursiveAccess: true,
  310. },
  311. {
  312. key: "/foo",
  313. req: mustAuthRequest("GET", "user", "good"),
  314. store: &mockAuthStore{
  315. user: &auth.User{
  316. User: "user",
  317. Password: goodPassword,
  318. Roles: []string{"foorole"},
  319. },
  320. roles: map[string]*auth.Role{
  321. "foorole": {
  322. Role: "foorole",
  323. Permissions: auth.Permissions{
  324. KV: auth.RWPermission{
  325. Read: []string{"/foo"},
  326. Write: []string{"/foo"},
  327. },
  328. },
  329. },
  330. },
  331. enabled: true,
  332. },
  333. hasRoot: false,
  334. hasKeyPrefixAccess: true,
  335. hasRecursiveAccess: false,
  336. },
  337. {
  338. key: "/foo",
  339. req: mustAuthRequest("GET", "user", "good"),
  340. store: &mockAuthStore{
  341. user: &auth.User{
  342. User: "user",
  343. Password: goodPassword,
  344. Roles: []string{"foorole"},
  345. },
  346. roles: map[string]*auth.Role{
  347. "foorole": {
  348. Role: "foorole",
  349. Permissions: auth.Permissions{
  350. KV: auth.RWPermission{
  351. Read: []string{"/foo*"},
  352. Write: []string{"/foo*"},
  353. },
  354. },
  355. },
  356. },
  357. enabled: true,
  358. },
  359. hasRoot: false,
  360. hasKeyPrefixAccess: true,
  361. hasRecursiveAccess: true,
  362. },
  363. {
  364. key: "/foo",
  365. req: mustAuthRequest("GET", "user", "bad"),
  366. store: &mockAuthStore{
  367. user: &auth.User{
  368. User: "user",
  369. Password: goodPassword,
  370. Roles: []string{"foorole"},
  371. },
  372. roles: map[string]*auth.Role{
  373. "foorole": {
  374. Role: "foorole",
  375. Permissions: auth.Permissions{
  376. KV: auth.RWPermission{
  377. Read: []string{"/foo*"},
  378. Write: []string{"/foo*"},
  379. },
  380. },
  381. },
  382. },
  383. enabled: true,
  384. },
  385. hasRoot: false,
  386. hasKeyPrefixAccess: false,
  387. hasRecursiveAccess: false,
  388. },
  389. {
  390. key: "/foo",
  391. req: mustAuthRequest("GET", "user", "good"),
  392. store: &mockAuthStore{
  393. user: &auth.User{},
  394. err: errors.New("Not the user"),
  395. enabled: true,
  396. },
  397. hasRoot: false,
  398. hasKeyPrefixAccess: false,
  399. hasRecursiveAccess: false,
  400. },
  401. {
  402. key: "/foo",
  403. req: mustJSONRequest(t, "GET", "somepath", ""),
  404. store: &mockAuthStore{
  405. user: &auth.User{
  406. User: "user",
  407. Password: goodPassword,
  408. Roles: []string{"foorole"},
  409. },
  410. roles: map[string]*auth.Role{
  411. "guest": {
  412. Role: "guest",
  413. Permissions: auth.Permissions{
  414. KV: auth.RWPermission{
  415. Read: []string{"/foo*"},
  416. Write: []string{"/foo*"},
  417. },
  418. },
  419. },
  420. },
  421. enabled: true,
  422. },
  423. hasRoot: false,
  424. hasKeyPrefixAccess: true,
  425. hasRecursiveAccess: true,
  426. },
  427. {
  428. key: "/bar",
  429. req: mustJSONRequest(t, "GET", "somepath", ""),
  430. store: &mockAuthStore{
  431. user: &auth.User{
  432. User: "user",
  433. Password: goodPassword,
  434. Roles: []string{"foorole"},
  435. },
  436. roles: map[string]*auth.Role{
  437. "guest": {
  438. Role: "guest",
  439. Permissions: auth.Permissions{
  440. KV: auth.RWPermission{
  441. Read: []string{"/foo*"},
  442. Write: []string{"/foo*"},
  443. },
  444. },
  445. },
  446. },
  447. enabled: true,
  448. },
  449. hasRoot: false,
  450. hasKeyPrefixAccess: false,
  451. hasRecursiveAccess: false,
  452. },
  453. // check access for multiple roles
  454. {
  455. key: "/foo",
  456. req: mustAuthRequest("GET", "user", "good"),
  457. store: &mockAuthStore{
  458. user: &auth.User{
  459. User: "user",
  460. Password: goodPassword,
  461. Roles: []string{"role1", "role2"},
  462. },
  463. roles: map[string]*auth.Role{
  464. "role1": {
  465. Role: "role1",
  466. },
  467. "role2": {
  468. Role: "role2",
  469. Permissions: auth.Permissions{
  470. KV: auth.RWPermission{
  471. Read: []string{"/foo"},
  472. Write: []string{"/foo"},
  473. },
  474. },
  475. },
  476. },
  477. enabled: true,
  478. },
  479. hasRoot: false,
  480. hasKeyPrefixAccess: true,
  481. hasRecursiveAccess: false,
  482. },
  483. {
  484. key: "/foo",
  485. req: (func() *http.Request {
  486. req := mustJSONRequest(t, "GET", "somepath", "")
  487. req.Header.Set("Authorization", "malformedencoding")
  488. return req
  489. })(),
  490. store: &mockAuthStore{
  491. enabled: true,
  492. user: &auth.User{
  493. User: "root",
  494. Password: goodPassword,
  495. Roles: []string{"root"},
  496. },
  497. roles: map[string]*auth.Role{
  498. "guest": {
  499. Role: "guest",
  500. Permissions: auth.Permissions{
  501. KV: auth.RWPermission{
  502. Read: []string{"/foo*"},
  503. Write: []string{"/foo*"},
  504. },
  505. },
  506. },
  507. },
  508. },
  509. hasRoot: false,
  510. hasKeyPrefixAccess: false,
  511. hasRecursiveAccess: false,
  512. },
  513. }
  514. for i, tt := range table {
  515. if tt.hasRoot != hasRootAccess(tt.store, tt.req) {
  516. t.Errorf("#%d: hasRoot doesn't match (expected %v)", i, tt.hasRoot)
  517. }
  518. if tt.hasKeyPrefixAccess != hasKeyPrefixAccess(tt.store, tt.req, tt.key, false) {
  519. t.Errorf("#%d: hasKeyPrefixAccess doesn't match (expected %v)", i, tt.hasRoot)
  520. }
  521. if tt.hasRecursiveAccess != hasKeyPrefixAccess(tt.store, tt.req, tt.key, true) {
  522. t.Errorf("#%d: hasRecursiveAccess doesn't match (expected %v)", i, tt.hasRoot)
  523. }
  524. }
  525. }