store_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 TestAuthenticate(t *testing.T) {
  43. b, tPath := backend.NewDefaultTmpBackend()
  44. defer func() {
  45. b.Close()
  46. os.Remove(tPath)
  47. }()
  48. as := NewAuthStore(b)
  49. ua := &pb.AuthUserAddRequest{Name: "foo", Password: "bar"}
  50. _, err := as.UserAdd(ua)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. // auth a non-existing user
  55. ctx1 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(1)), "simpleToken", "dummy")
  56. _, err = as.Authenticate(ctx1, "foo-test", "bar")
  57. if err == nil {
  58. t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
  59. }
  60. if err != ErrAuthFailed {
  61. t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
  62. }
  63. // auth an existing user with correct password
  64. ctx2 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(2)), "simpleToken", "dummy")
  65. _, err = as.Authenticate(ctx2, "foo", "bar")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. // auth an existing user but with wrong password
  70. ctx3 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(3)), "simpleToken", "dummy")
  71. _, err = as.Authenticate(ctx3, "foo", "")
  72. if err == nil {
  73. t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
  74. }
  75. if err != ErrAuthFailed {
  76. t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
  77. }
  78. }
  79. func TestUserDelete(t *testing.T) {
  80. b, tPath := backend.NewDefaultTmpBackend()
  81. defer func() {
  82. b.Close()
  83. os.Remove(tPath)
  84. }()
  85. as := NewAuthStore(b)
  86. ua := &pb.AuthUserAddRequest{Name: "foo"}
  87. _, err := as.UserAdd(ua)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. // delete an existing user
  92. ud := &pb.AuthUserDeleteRequest{Name: "foo"}
  93. _, err = as.UserDelete(ud)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. // delete a non-existing user
  98. _, err = as.UserDelete(ud)
  99. if err == nil {
  100. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  101. }
  102. if err != ErrUserNotFound {
  103. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  104. }
  105. }
  106. func TestUserChangePassword(t *testing.T) {
  107. b, tPath := backend.NewDefaultTmpBackend()
  108. defer func() {
  109. b.Close()
  110. os.Remove(tPath)
  111. }()
  112. as := NewAuthStore(b)
  113. _, err := as.UserAdd(&pb.AuthUserAddRequest{Name: "foo"})
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. ctx1 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(1)), "simpleToken", "dummy")
  118. _, err = as.Authenticate(ctx1, "foo", "")
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. _, err = as.UserChangePassword(&pb.AuthUserChangePasswordRequest{Name: "foo", Password: "bar"})
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. ctx2 := context.WithValue(context.WithValue(context.TODO(), "index", uint64(2)), "simpleToken", "dummy")
  127. _, err = as.Authenticate(ctx2, "foo", "bar")
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. // change a non-existing user
  132. _, err = as.UserChangePassword(&pb.AuthUserChangePasswordRequest{Name: "foo-test", Password: "bar"})
  133. if err == nil {
  134. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  135. }
  136. if err != ErrUserNotFound {
  137. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  138. }
  139. }
  140. func TestRoleAdd(t *testing.T) {
  141. b, tPath := backend.NewDefaultTmpBackend()
  142. defer func() {
  143. b.Close()
  144. os.Remove(tPath)
  145. }()
  146. as := NewAuthStore(b)
  147. // adds a new role
  148. _, err := as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test"})
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. }
  153. func TestUserGrant(t *testing.T) {
  154. b, tPath := backend.NewDefaultTmpBackend()
  155. defer func() {
  156. b.Close()
  157. os.Remove(tPath)
  158. }()
  159. as := NewAuthStore(b)
  160. _, err := as.UserAdd(&pb.AuthUserAddRequest{Name: "foo"})
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. // adds a new role
  165. _, err = as.RoleAdd(&pb.AuthRoleAddRequest{Name: "role-test"})
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. // grants a role to the user
  170. _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo", Role: "role-test"})
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. // grants a role to a non-existing user
  175. _, err = as.UserGrantRole(&pb.AuthUserGrantRoleRequest{User: "foo-test", Role: "role-test"})
  176. if err == nil {
  177. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  178. }
  179. if err != ErrUserNotFound {
  180. t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
  181. }
  182. }