store_test.go 5.8 KB

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