client_auth_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. role *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.role, 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) { return *s.role, s.err }
  59. func (s *mockAuthStore) AuthEnabled() bool { return s.enabled }
  60. func (s *mockAuthStore) EnableAuth() error { return s.err }
  61. func (s *mockAuthStore) DisableAuth() error { return s.err }
  62. func TestAuthFlow(t *testing.T) {
  63. enableMapMu.Lock()
  64. enabledMap = make(map[capability]bool)
  65. enabledMap[authCapability] = true
  66. enableMapMu.Unlock()
  67. var testCases = []struct {
  68. req *http.Request
  69. store mockAuthStore
  70. wcode int
  71. wbody string
  72. }{
  73. {
  74. req: mustJSONRequest(t, "PUT", "users/alice", `{{{{{{{`),
  75. store: mockAuthStore{},
  76. wcode: http.StatusBadRequest,
  77. wbody: `{"message":"Invalid JSON in request body."}`,
  78. },
  79. {
  80. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "password": "goodpassword"}`),
  81. store: mockAuthStore{enabled: true},
  82. wcode: http.StatusUnauthorized,
  83. wbody: `{"message":"Insufficient credentials"}`,
  84. },
  85. // Users
  86. {
  87. req: mustJSONRequest(t, "GET", "users", ""),
  88. store: mockAuthStore{},
  89. wcode: http.StatusOK,
  90. wbody: `{"users":["alice","bob","root"]}`,
  91. },
  92. {
  93. req: mustJSONRequest(t, "GET", "users/alice", ""),
  94. store: mockAuthStore{
  95. user: &auth.User{
  96. User: "alice",
  97. Roles: []string{"alicerole", "guest"},
  98. Password: "wheeee",
  99. },
  100. },
  101. wcode: http.StatusOK,
  102. wbody: `{"user":"alice","roles":["alicerole","guest"]}`,
  103. },
  104. {
  105. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "password": "goodpassword"}`),
  106. store: mockAuthStore{},
  107. wcode: http.StatusCreated,
  108. wbody: `{"user":"alice","roles":null}`,
  109. },
  110. {
  111. req: mustJSONRequest(t, "DELETE", "users/alice", ``),
  112. store: mockAuthStore{},
  113. wcode: http.StatusOK,
  114. wbody: ``,
  115. },
  116. {
  117. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "password": "goodpassword"}`),
  118. store: mockAuthStore{
  119. user: &auth.User{
  120. User: "alice",
  121. Roles: []string{"alicerole", "guest"},
  122. Password: "wheeee",
  123. },
  124. },
  125. wcode: http.StatusOK,
  126. wbody: `{"user":"alice","roles":["alicerole","guest"]}`,
  127. },
  128. {
  129. req: mustJSONRequest(t, "PUT", "users/alice", `{"user": "alice", "grant": ["alicerole"]}`),
  130. store: mockAuthStore{
  131. user: &auth.User{
  132. User: "alice",
  133. Roles: []string{"alicerole", "guest"},
  134. Password: "wheeee",
  135. },
  136. },
  137. wcode: http.StatusOK,
  138. wbody: `{"user":"alice","roles":["alicerole","guest"]}`,
  139. },
  140. {
  141. req: mustJSONRequest(t, "GET", "users/alice", ``),
  142. store: mockAuthStore{
  143. user: &auth.User{},
  144. err: auth.Error{Status: http.StatusNotFound, Errmsg: "auth: User alice doesn't exist."},
  145. },
  146. wcode: http.StatusNotFound,
  147. wbody: `{"message":"auth: User alice doesn't exist."}`,
  148. },
  149. // Roles
  150. {
  151. req: mustJSONRequest(t, "GET", "roles/manager", ""),
  152. store: mockAuthStore{
  153. role: &auth.Role{
  154. Role: "manager",
  155. },
  156. },
  157. wcode: http.StatusOK,
  158. wbody: `{"role":"manager","permissions":{"kv":{"read":null,"write":null}}}`,
  159. },
  160. {
  161. req: mustJSONRequest(t, "DELETE", "roles/manager", ``),
  162. store: mockAuthStore{},
  163. wcode: http.StatusOK,
  164. wbody: ``,
  165. },
  166. {
  167. req: mustJSONRequest(t, "PUT", "roles/manager", `{"role":"manager","permissions":{"kv":{"read":[],"write":[]}}}`),
  168. store: mockAuthStore{},
  169. wcode: http.StatusCreated,
  170. wbody: `{"role":"manager","permissions":{"kv":{"read":[],"write":[]}}}`,
  171. },
  172. {
  173. req: mustJSONRequest(t, "PUT", "roles/manager", `{"role":"manager","revoke":{"kv":{"read":["foo"],"write":[]}}}`),
  174. store: mockAuthStore{
  175. role: &auth.Role{
  176. Role: "manager",
  177. },
  178. },
  179. wcode: http.StatusOK,
  180. wbody: `{"role":"manager","permissions":{"kv":{"read":null,"write":null}}}`,
  181. },
  182. {
  183. req: mustJSONRequest(t, "GET", "roles", ""),
  184. store: mockAuthStore{},
  185. wcode: http.StatusOK,
  186. wbody: `{"roles":["awesome","guest","root"]}`,
  187. },
  188. {
  189. req: mustJSONRequest(t, "GET", "enable", ""),
  190. store: mockAuthStore{
  191. enabled: true,
  192. },
  193. wcode: http.StatusOK,
  194. wbody: `{"enabled":true}`,
  195. },
  196. {
  197. req: mustJSONRequest(t, "PUT", "enable", ""),
  198. store: mockAuthStore{
  199. enabled: false,
  200. },
  201. wcode: http.StatusOK,
  202. wbody: ``,
  203. },
  204. {
  205. req: (func() *http.Request {
  206. req := mustJSONRequest(t, "DELETE", "enable", "")
  207. req.SetBasicAuth("root", "good")
  208. return req
  209. })(),
  210. store: mockAuthStore{
  211. enabled: true,
  212. user: &auth.User{
  213. User: "root",
  214. Password: goodPassword,
  215. Roles: []string{"root"},
  216. },
  217. role: &auth.Role{
  218. Role: "root",
  219. },
  220. },
  221. wcode: http.StatusOK,
  222. wbody: ``,
  223. },
  224. }
  225. for i, tt := range testCases {
  226. mux := http.NewServeMux()
  227. h := &authHandler{
  228. sec: &tt.store,
  229. cluster: &fakeCluster{id: 1},
  230. }
  231. handleAuth(mux, h)
  232. rw := httptest.NewRecorder()
  233. mux.ServeHTTP(rw, tt.req)
  234. if rw.Code != tt.wcode {
  235. t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode)
  236. }
  237. g := rw.Body.String()
  238. g = strings.TrimSpace(g)
  239. if g != tt.wbody {
  240. t.Errorf("#%d: got body=%s, want %s", i, g, tt.wbody)
  241. }
  242. }
  243. }
  244. func mustAuthRequest(method, username, password string) *http.Request {
  245. req, err := http.NewRequest(method, "path", strings.NewReader(""))
  246. if err != nil {
  247. panic("Cannot make auth request: " + err.Error())
  248. }
  249. req.SetBasicAuth(username, password)
  250. return req
  251. }
  252. func TestPrefixAccess(t *testing.T) {
  253. var table = []struct {
  254. key string
  255. req *http.Request
  256. store *mockAuthStore
  257. hasRoot bool
  258. hasKeyPrefixAccess bool
  259. hasRecursiveAccess bool
  260. }{
  261. {
  262. key: "/foo",
  263. req: mustAuthRequest("GET", "root", "good"),
  264. store: &mockAuthStore{
  265. user: &auth.User{
  266. User: "root",
  267. Password: goodPassword,
  268. Roles: []string{"root"},
  269. },
  270. role: &auth.Role{
  271. Role: "root",
  272. },
  273. enabled: true,
  274. },
  275. hasRoot: true,
  276. hasKeyPrefixAccess: true,
  277. hasRecursiveAccess: true,
  278. },
  279. {
  280. key: "/foo",
  281. req: mustAuthRequest("GET", "user", "good"),
  282. store: &mockAuthStore{
  283. user: &auth.User{
  284. User: "user",
  285. Password: goodPassword,
  286. Roles: []string{"foorole"},
  287. },
  288. role: &auth.Role{
  289. Role: "foorole",
  290. Permissions: auth.Permissions{
  291. KV: auth.RWPermission{
  292. Read: []string{"/foo"},
  293. Write: []string{"/foo"},
  294. },
  295. },
  296. },
  297. enabled: true,
  298. },
  299. hasRoot: false,
  300. hasKeyPrefixAccess: true,
  301. hasRecursiveAccess: false,
  302. },
  303. {
  304. key: "/foo",
  305. req: mustAuthRequest("GET", "user", "good"),
  306. store: &mockAuthStore{
  307. user: &auth.User{
  308. User: "user",
  309. Password: goodPassword,
  310. Roles: []string{"foorole"},
  311. },
  312. role: &auth.Role{
  313. Role: "foorole",
  314. Permissions: auth.Permissions{
  315. KV: auth.RWPermission{
  316. Read: []string{"/foo*"},
  317. Write: []string{"/foo*"},
  318. },
  319. },
  320. },
  321. enabled: true,
  322. },
  323. hasRoot: false,
  324. hasKeyPrefixAccess: true,
  325. hasRecursiveAccess: true,
  326. },
  327. {
  328. key: "/foo",
  329. req: mustAuthRequest("GET", "user", "bad"),
  330. store: &mockAuthStore{
  331. user: &auth.User{
  332. User: "user",
  333. Password: goodPassword,
  334. Roles: []string{"foorole"},
  335. },
  336. role: &auth.Role{
  337. Role: "foorole",
  338. Permissions: auth.Permissions{
  339. KV: auth.RWPermission{
  340. Read: []string{"/foo*"},
  341. Write: []string{"/foo*"},
  342. },
  343. },
  344. },
  345. enabled: true,
  346. },
  347. hasRoot: false,
  348. hasKeyPrefixAccess: false,
  349. hasRecursiveAccess: false,
  350. },
  351. {
  352. key: "/foo",
  353. req: mustAuthRequest("GET", "user", "good"),
  354. store: &mockAuthStore{
  355. user: &auth.User{},
  356. err: errors.New("Not the user"),
  357. enabled: true,
  358. },
  359. hasRoot: false,
  360. hasKeyPrefixAccess: false,
  361. hasRecursiveAccess: false,
  362. },
  363. {
  364. key: "/foo",
  365. req: mustJSONRequest(t, "GET", "somepath", ""),
  366. store: &mockAuthStore{
  367. user: &auth.User{
  368. User: "user",
  369. Password: goodPassword,
  370. Roles: []string{"foorole"},
  371. },
  372. role: &auth.Role{
  373. Role: "guest",
  374. Permissions: auth.Permissions{
  375. KV: auth.RWPermission{
  376. Read: []string{"/foo*"},
  377. Write: []string{"/foo*"},
  378. },
  379. },
  380. },
  381. enabled: true,
  382. },
  383. hasRoot: false,
  384. hasKeyPrefixAccess: true,
  385. hasRecursiveAccess: true,
  386. },
  387. {
  388. key: "/bar",
  389. req: mustJSONRequest(t, "GET", "somepath", ""),
  390. store: &mockAuthStore{
  391. user: &auth.User{
  392. User: "user",
  393. Password: goodPassword,
  394. Roles: []string{"foorole"},
  395. },
  396. role: &auth.Role{
  397. Role: "guest",
  398. Permissions: auth.Permissions{
  399. KV: auth.RWPermission{
  400. Read: []string{"/foo*"},
  401. Write: []string{"/foo*"},
  402. },
  403. },
  404. },
  405. enabled: true,
  406. },
  407. hasRoot: false,
  408. hasKeyPrefixAccess: false,
  409. hasRecursiveAccess: false,
  410. },
  411. }
  412. for i, tt := range table {
  413. if tt.hasRoot != hasRootAccess(tt.store, tt.req) {
  414. t.Errorf("#%d: hasRoot doesn't match (expected %v)", i, tt.hasRoot)
  415. }
  416. if tt.hasKeyPrefixAccess != hasKeyPrefixAccess(tt.store, tt.req, tt.key, false) {
  417. t.Errorf("#%d: hasKeyPrefixAccess doesn't match (expected %v)", i, tt.hasRoot)
  418. }
  419. if tt.hasRecursiveAccess != hasKeyPrefixAccess(tt.store, tt.req, tt.key, true) {
  420. t.Errorf("#%d: hasRecursiveAccess doesn't match (expected %v)", i, tt.hasRoot)
  421. }
  422. }
  423. }