user_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 integration
  15. import (
  16. "context"
  17. "testing"
  18. "github.com/coreos/etcd/clientv3"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. "github.com/coreos/etcd/integration"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. func TestUserError(t *testing.T) {
  24. defer testutil.AfterTest(t)
  25. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  26. defer clus.Terminate(t)
  27. authapi := clus.RandClient()
  28. _, err := authapi.UserAdd(context.TODO(), "foo", "bar")
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. _, err = authapi.UserAdd(context.TODO(), "foo", "bar")
  33. if err != rpctypes.ErrUserAlreadyExist {
  34. t.Fatalf("expected %v, got %v", rpctypes.ErrUserAlreadyExist, err)
  35. }
  36. _, err = authapi.UserDelete(context.TODO(), "not-exist-user")
  37. if err != rpctypes.ErrUserNotFound {
  38. t.Fatalf("expected %v, got %v", rpctypes.ErrUserNotFound, err)
  39. }
  40. _, err = authapi.UserGrantRole(context.TODO(), "foo", "test-role-does-not-exist")
  41. if err != rpctypes.ErrRoleNotFound {
  42. t.Fatalf("expected %v, got %v", rpctypes.ErrRoleNotFound, err)
  43. }
  44. }
  45. func TestUserErrorAuth(t *testing.T) {
  46. defer testutil.AfterTest(t)
  47. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  48. defer clus.Terminate(t)
  49. authapi := clus.RandClient()
  50. authSetupRoot(t, authapi.Auth)
  51. // unauthenticated client
  52. if _, err := authapi.UserAdd(context.TODO(), "foo", "bar"); err != rpctypes.ErrUserNotFound {
  53. t.Fatalf("expected %v, got %v", rpctypes.ErrUserNotFound, err)
  54. }
  55. // wrong id or password
  56. cfg := clientv3.Config{Endpoints: authapi.Endpoints()}
  57. cfg.Username, cfg.Password = "wrong-id", "123"
  58. if _, err := clientv3.New(cfg); err != rpctypes.ErrAuthFailed {
  59. t.Fatalf("expected %v, got %v", rpctypes.ErrAuthFailed, err)
  60. }
  61. cfg.Username, cfg.Password = "root", "wrong-pass"
  62. if _, err := clientv3.New(cfg); err != rpctypes.ErrAuthFailed {
  63. t.Fatalf("expected %v, got %v", rpctypes.ErrAuthFailed, err)
  64. }
  65. cfg.Username, cfg.Password = "root", "123"
  66. authed, err := clientv3.New(cfg)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. defer authed.Close()
  71. if _, err := authed.UserList(context.TODO()); err != nil {
  72. t.Fatal(err)
  73. }
  74. }
  75. func authSetupRoot(t *testing.T, auth clientv3.Auth) {
  76. if _, err := auth.UserAdd(context.TODO(), "root", "123"); err != nil {
  77. t.Fatal(err)
  78. }
  79. if _, err := auth.RoleAdd(context.TODO(), "root"); err != nil {
  80. t.Fatal(err)
  81. }
  82. if _, err := auth.UserGrantRole(context.TODO(), "root", "root"); err != nil {
  83. t.Fatal(err)
  84. }
  85. if _, err := auth.AuthEnable(context.TODO()); err != nil {
  86. t.Fatal(err)
  87. }
  88. }