store_test.go 6.1 KB

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