user_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "time"
  19. "go.etcd.io/etcd/clientv3"
  20. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  21. "go.etcd.io/etcd/integration"
  22. "go.etcd.io/etcd/pkg/testutil"
  23. "google.golang.org/grpc"
  24. )
  25. func TestUserError(t *testing.T) {
  26. defer testutil.AfterTest(t)
  27. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  28. defer clus.Terminate(t)
  29. authapi := clus.RandClient()
  30. _, err := authapi.UserAdd(context.TODO(), "foo", "bar")
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. _, err = authapi.UserAdd(context.TODO(), "foo", "bar")
  35. if err != rpctypes.ErrUserAlreadyExist {
  36. t.Fatalf("expected %v, got %v", rpctypes.ErrUserAlreadyExist, err)
  37. }
  38. _, err = authapi.UserDelete(context.TODO(), "not-exist-user")
  39. if err != rpctypes.ErrUserNotFound {
  40. t.Fatalf("expected %v, got %v", rpctypes.ErrUserNotFound, err)
  41. }
  42. _, err = authapi.UserGrantRole(context.TODO(), "foo", "test-role-does-not-exist")
  43. if err != rpctypes.ErrRoleNotFound {
  44. t.Fatalf("expected %v, got %v", rpctypes.ErrRoleNotFound, err)
  45. }
  46. }
  47. func TestUserErrorAuth(t *testing.T) {
  48. defer testutil.AfterTest(t)
  49. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  50. defer clus.Terminate(t)
  51. authapi := clus.RandClient()
  52. authSetupRoot(t, authapi.Auth)
  53. // unauthenticated client
  54. if _, err := authapi.UserAdd(context.TODO(), "foo", "bar"); err != rpctypes.ErrUserNotFound {
  55. t.Fatalf("expected %v, got %v", rpctypes.ErrUserNotFound, err)
  56. }
  57. // wrong id or password
  58. cfg := clientv3.Config{
  59. Endpoints: authapi.Endpoints(),
  60. DialTimeout: 5 * time.Second,
  61. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  62. }
  63. cfg.Username, cfg.Password = "wrong-id", "123"
  64. if _, err := clientv3.New(cfg); err != rpctypes.ErrAuthFailed {
  65. t.Fatalf("expected %v, got %v", rpctypes.ErrAuthFailed, err)
  66. }
  67. cfg.Username, cfg.Password = "root", "wrong-pass"
  68. if _, err := clientv3.New(cfg); err != rpctypes.ErrAuthFailed {
  69. t.Fatalf("expected %v, got %v", rpctypes.ErrAuthFailed, err)
  70. }
  71. cfg.Username, cfg.Password = "root", "123"
  72. authed, err := clientv3.New(cfg)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. defer authed.Close()
  77. if _, err := authed.UserList(context.TODO()); err != nil {
  78. t.Fatal(err)
  79. }
  80. }
  81. func authSetupRoot(t *testing.T, auth clientv3.Auth) {
  82. if _, err := auth.UserAdd(context.TODO(), "root", "123"); err != nil {
  83. t.Fatal(err)
  84. }
  85. if _, err := auth.RoleAdd(context.TODO(), "root"); err != nil {
  86. t.Fatal(err)
  87. }
  88. if _, err := auth.UserGrantRole(context.TODO(), "root", "root"); err != nil {
  89. t.Fatal(err)
  90. }
  91. if _, err := auth.AuthEnable(context.TODO()); err != nil {
  92. t.Fatal(err)
  93. }
  94. }
  95. func TestGetTokenWithoutAuth(t *testing.T) {
  96. defer testutil.AfterTest(t)
  97. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 10})
  98. defer clus.Terminate(t)
  99. authapi := clus.RandClient()
  100. var err error
  101. var client *clientv3.Client
  102. // make sure "auth" was disabled
  103. if _, err = authapi.AuthDisable(context.TODO()); err != nil {
  104. t.Fatal(err)
  105. }
  106. // "Username" and "Password" must be used
  107. cfg := clientv3.Config{
  108. Endpoints: authapi.Endpoints(),
  109. DialTimeout: 1 * time.Second, // make sure all connection time of connect all endpoint must be more DialTimeout
  110. Username: "root",
  111. Password: "123",
  112. }
  113. client, err = clientv3.New(cfg)
  114. if err == nil {
  115. defer client.Close()
  116. }
  117. switch err {
  118. case nil:
  119. t.Log("passes as expected, but may be connection time less than DialTimeout")
  120. case context.DeadlineExceeded:
  121. t.Errorf("not expected result:%v with endpoint:%s", err, authapi.Endpoints())
  122. case rpctypes.ErrAuthNotEnabled:
  123. t.Logf("passes as expected:%v", err)
  124. default:
  125. t.Errorf("other errors:%v", err)
  126. }
  127. }