store_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2016 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 auth
  15. import (
  16. "os"
  17. "testing"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "github.com/coreos/etcd/mvcc/backend"
  20. "golang.org/x/net/context"
  21. )
  22. func TestUserAdd(t *testing.T) {
  23. b, tPath := backend.NewDefaultTmpBackend()
  24. defer func() {
  25. b.Close()
  26. os.Remove(tPath)
  27. }()
  28. as := NewAuthStore(b)
  29. ua := &pb.AuthUserAddRequest{Name: "foo"}
  30. _, err := as.UserAdd(ua) // add a non-existing user
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. _, err = as.UserAdd(ua) // add an existing user
  35. if err == nil {
  36. t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
  37. }
  38. if err != ErrUserAlreadyExist {
  39. t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
  40. }
  41. }
  42. func enableAuthAndCreateRoot(as *authStore) error {
  43. _, err := as.UserAdd(&pb.AuthUserAddRequest{Name: "root", Password: "root"})
  44. if err != nil {
  45. return err
  46. }
  47. _, err = as.RoleAdd(&pb.AuthRoleAddRequest{Name: "root"})
  48. if err != nil {
  49. return err
  50. }
  51. _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "root", Role: "root"})
  52. if err != nil {
  53. return err
  54. }
  55. return as.AuthEnable()
  56. }
  57. func TestAuthenticate(t *testing.T) {
  58. b, tPath := backend.NewDefaultTmpBackend()
  59. defer func() {
  60. b.Close()
  61. os.Remove(tPath)
  62. }()
  63. as := NewAuthStore(b)
  64. err := enableAuthAndCreateRoot(as)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. ua := &pb.AuthUserAddRequest{Name: "foo", Password: "bar"}
  69. _, err = as.UserAdd(ua)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. // auth a non-existing user
  74. ctx1 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(1)), "simpleToken", "dummy")
  75. _, err = as.Authenticate(ctx1, "foo-test", "bar")
  76. if err == nil {
  77. t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
  78. }
  79. if err != ErrAuthFailed {
  80. t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
  81. }
  82. // auth an existing user with correct password
  83. ctx2 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(2)), "simpleToken", "dummy")
  84. _, err = as.Authenticate(ctx2, "foo", "bar")
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. // auth an existing user but with wrong password
  89. ctx3 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(3)), "simpleToken", "dummy")
  90. _, err = as.Authenticate(ctx3, "foo", "")
  91. if err == nil {
  92. t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
  93. }
  94. if err != ErrAuthFailed {
  95. t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
  96. }
  97. }
  98. func TestUserDelete(t *testing.T) {
  99. b, tPath := backend.NewDefaultTmpBackend()
  100. defer func() {
  101. b.Close()
  102. os.Remove(tPath)
  103. }()
  104. as := NewAuthStore(b)
  105. err := enableAuthAndCreateRoot(as)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. ua := &pb.AuthUserAddRequest{Name: "foo"}
  110. _, err = as.UserAdd(ua)
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. // delete an existing user
  115. ud := &pb.AuthUserDeleteRequest{Name: "foo"}
  116. _, err = as.UserDelete(ud)
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. // delete a non-existing user
  121. _, err = as.UserDelete(ud)
  122. if err == nil {
  123. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  124. }
  125. if err != ErrUserNotFound {
  126. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  127. }
  128. }
  129. func TestUserChangePassword(t *testing.T) {
  130. b, tPath := backend.NewDefaultTmpBackend()
  131. defer func() {
  132. b.Close()
  133. os.Remove(tPath)
  134. }()
  135. as := NewAuthStore(b)
  136. err := enableAuthAndCreateRoot(as)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. _, err = as.UserAdd(&pb.AuthUserAddRequest{Name: "foo"})
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. ctx1 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(1)), "simpleToken", "dummy")
  145. _, err = as.Authenticate(ctx1, "foo", "")
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. _, err = as.UserChangePassword(&pb.AuthUserChangePasswordRequest{Name: "foo", Password: "bar"})
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. ctx2 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(2)), "simpleToken", "dummy")
  154. _, err = as.Authenticate(ctx2, "foo", "bar")
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. // change a non-existing user
  159. _, err = as.UserChangePassword(&pb.AuthUserChangePasswordRequest{Name: "foo-test", Password: "bar"})
  160. if err == nil {
  161. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  162. }
  163. if err != ErrUserNotFound {
  164. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  165. }
  166. }
  167. func TestRoleAdd(t *testing.T) {
  168. b, tPath := backend.NewDefaultTmpBackend()
  169. defer func() {
  170. b.Close()
  171. os.Remove(tPath)
  172. }()
  173. as := NewAuthStore(b)
  174. err := enableAuthAndCreateRoot(as)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. // adds a new role
  179. _, err = as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test"})
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. }
  184. func TestUserGrant(t *testing.T) {
  185. b, tPath := backend.NewDefaultTmpBackend()
  186. defer func() {
  187. b.Close()
  188. os.Remove(tPath)
  189. }()
  190. as := NewAuthStore(b)
  191. err := enableAuthAndCreateRoot(as)
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. _, err = as.UserAdd(&pb.AuthUserAddRequest{Name: "foo"})
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. // adds a new role
  200. _, err = as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test"})
  201. if err != nil {
  202. t.Fatal(err)
  203. }
  204. // grants a role to the user
  205. _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo", Role: "role-test"})
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. // grants a role to a non-existing user
  210. _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo-test", Role: "role-test"})
  211. if err == nil {
  212. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  213. }
  214. if err != ErrUserNotFound {
  215. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  216. }
  217. }