client_auth_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. for i, tt := range testCases {
  234. mux := http.NewServeMux()
  235. h := &authHandler{
  236. sec: &tt.store,
  237. cluster: &fakeCluster{id: 1},
  238. }
  239. handleAuth(mux, h)
  240. rw := httptest.NewRecorder()
  241. mux.ServeHTTP(rw, tt.req)
  242. if rw.Code != tt.wcode {
  243. t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode)
  244. }
  245. g := rw.Body.String()
  246. g = strings.TrimSpace(g)
  247. if g != tt.wbody {
  248. t.Errorf("#%d: got body=%s, want %s", i, g, tt.wbody)
  249. }
  250. }
  251. }
  252. func mustAuthRequest(method, username, password string) *http.Request {
  253. req, err := http.NewRequest(method, "path", strings.NewReader(""))
  254. if err != nil {
  255. panic("Cannot make auth request: " + err.Error())
  256. }
  257. req.SetBasicAuth(username, password)
  258. return req
  259. }
  260. func TestPrefixAccess(t *testing.T) {
  261. var table = []struct {
  262. key string
  263. req *http.Request
  264. store *mockAuthStore
  265. hasRoot bool
  266. hasKeyPrefixAccess bool
  267. hasRecursiveAccess bool
  268. }{
  269. {
  270. key: "/foo",
  271. req: mustAuthRequest("GET", "root", "good"),
  272. store: &mockAuthStore{
  273. user: &auth.User{
  274. User: "root",
  275. Password: goodPassword,
  276. Roles: []string{"root"},
  277. },
  278. roles: map[string]*auth.Role{
  279. "root": {
  280. Role: "root",
  281. },
  282. },
  283. enabled: true,
  284. },
  285. hasRoot: true,
  286. hasKeyPrefixAccess: true,
  287. hasRecursiveAccess: true,
  288. },
  289. {
  290. key: "/foo",
  291. req: mustAuthRequest("GET", "user", "good"),
  292. store: &mockAuthStore{
  293. user: &auth.User{
  294. User: "user",
  295. Password: goodPassword,
  296. Roles: []string{"foorole"},
  297. },
  298. roles: map[string]*auth.Role{
  299. "foorole": {
  300. Role: "foorole",
  301. Permissions: auth.Permissions{
  302. KV: auth.RWPermission{
  303. Read: []string{"/foo"},
  304. Write: []string{"/foo"},
  305. },
  306. },
  307. },
  308. },
  309. enabled: true,
  310. },
  311. hasRoot: false,
  312. hasKeyPrefixAccess: true,
  313. hasRecursiveAccess: false,
  314. },
  315. {
  316. key: "/foo",
  317. req: mustAuthRequest("GET", "user", "good"),
  318. store: &mockAuthStore{
  319. user: &auth.User{
  320. User: "user",
  321. Password: goodPassword,
  322. Roles: []string{"foorole"},
  323. },
  324. roles: map[string]*auth.Role{
  325. "foorole": {
  326. Role: "foorole",
  327. Permissions: auth.Permissions{
  328. KV: auth.RWPermission{
  329. Read: []string{"/foo*"},
  330. Write: []string{"/foo*"},
  331. },
  332. },
  333. },
  334. },
  335. enabled: true,
  336. },
  337. hasRoot: false,
  338. hasKeyPrefixAccess: true,
  339. hasRecursiveAccess: true,
  340. },
  341. {
  342. key: "/foo",
  343. req: mustAuthRequest("GET", "user", "bad"),
  344. store: &mockAuthStore{
  345. user: &auth.User{
  346. User: "user",
  347. Password: goodPassword,
  348. Roles: []string{"foorole"},
  349. },
  350. roles: map[string]*auth.Role{
  351. "foorole": {
  352. Role: "foorole",
  353. Permissions: auth.Permissions{
  354. KV: auth.RWPermission{
  355. Read: []string{"/foo*"},
  356. Write: []string{"/foo*"},
  357. },
  358. },
  359. },
  360. },
  361. enabled: true,
  362. },
  363. hasRoot: false,
  364. hasKeyPrefixAccess: false,
  365. hasRecursiveAccess: false,
  366. },
  367. {
  368. key: "/foo",
  369. req: mustAuthRequest("GET", "user", "good"),
  370. store: &mockAuthStore{
  371. user: &auth.User{},
  372. err: errors.New("Not the user"),
  373. enabled: true,
  374. },
  375. hasRoot: false,
  376. hasKeyPrefixAccess: false,
  377. hasRecursiveAccess: false,
  378. },
  379. {
  380. key: "/foo",
  381. req: mustJSONRequest(t, "GET", "somepath", ""),
  382. store: &mockAuthStore{
  383. user: &auth.User{
  384. User: "user",
  385. Password: goodPassword,
  386. Roles: []string{"foorole"},
  387. },
  388. roles: map[string]*auth.Role{
  389. "guest": {
  390. Role: "guest",
  391. Permissions: auth.Permissions{
  392. KV: auth.RWPermission{
  393. Read: []string{"/foo*"},
  394. Write: []string{"/foo*"},
  395. },
  396. },
  397. },
  398. },
  399. enabled: true,
  400. },
  401. hasRoot: false,
  402. hasKeyPrefixAccess: true,
  403. hasRecursiveAccess: true,
  404. },
  405. {
  406. key: "/bar",
  407. req: mustJSONRequest(t, "GET", "somepath", ""),
  408. store: &mockAuthStore{
  409. user: &auth.User{
  410. User: "user",
  411. Password: goodPassword,
  412. Roles: []string{"foorole"},
  413. },
  414. roles: map[string]*auth.Role{
  415. "guest": {
  416. Role: "guest",
  417. Permissions: auth.Permissions{
  418. KV: auth.RWPermission{
  419. Read: []string{"/foo*"},
  420. Write: []string{"/foo*"},
  421. },
  422. },
  423. },
  424. },
  425. enabled: true,
  426. },
  427. hasRoot: false,
  428. hasKeyPrefixAccess: false,
  429. hasRecursiveAccess: false,
  430. },
  431. // check access for multiple roles
  432. {
  433. key: "/foo",
  434. req: mustAuthRequest("GET", "user", "good"),
  435. store: &mockAuthStore{
  436. user: &auth.User{
  437. User: "user",
  438. Password: goodPassword,
  439. Roles: []string{"role1", "role2"},
  440. },
  441. roles: map[string]*auth.Role{
  442. "role1": {
  443. Role: "role1",
  444. },
  445. "role2": {
  446. Role: "role2",
  447. Permissions: auth.Permissions{
  448. KV: auth.RWPermission{
  449. Read: []string{"/foo"},
  450. Write: []string{"/foo"},
  451. },
  452. },
  453. },
  454. },
  455. enabled: true,
  456. },
  457. hasRoot: false,
  458. hasKeyPrefixAccess: true,
  459. hasRecursiveAccess: false,
  460. },
  461. }
  462. for i, tt := range table {
  463. if tt.hasRoot != hasRootAccess(tt.store, tt.req) {
  464. t.Errorf("#%d: hasRoot doesn't match (expected %v)", i, tt.hasRoot)
  465. }
  466. if tt.hasKeyPrefixAccess != hasKeyPrefixAccess(tt.store, tt.req, tt.key, false) {
  467. t.Errorf("#%d: hasKeyPrefixAccess doesn't match (expected %v)", i, tt.hasRoot)
  468. }
  469. if tt.hasRecursiveAccess != hasKeyPrefixAccess(tt.store, tt.req, tt.key, true) {
  470. t.Errorf("#%d: hasRecursiveAccess doesn't match (expected %v)", i, tt.hasRoot)
  471. }
  472. }
  473. }