v3_auth_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2017 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. "testing"
  17. "time"
  18. "golang.org/x/net/context"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  21. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. )
  24. // TestV3AuthEmptyUserGet ensures that a get with an empty user will return an empty user error.
  25. func TestV3AuthEmptyUserGet(t *testing.T) {
  26. defer testutil.AfterTest(t)
  27. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  28. defer clus.Terminate(t)
  29. ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
  30. defer cancel()
  31. api := toGRPC(clus.Client(0))
  32. authSetupRoot(t, api.Auth)
  33. _, err := api.KV.Range(ctx, &pb.RangeRequest{Key: []byte("abc")})
  34. if !eqErrGRPC(err, rpctypes.ErrUserEmpty) {
  35. t.Fatalf("got %v, expected %v", err, rpctypes.ErrUserEmpty)
  36. }
  37. }
  38. // TestV3AuthTokenWithDisable tests that auth won't crash if
  39. // given a valid token when authentication is disabled
  40. func TestV3AuthTokenWithDisable(t *testing.T) {
  41. defer testutil.AfterTest(t)
  42. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  43. defer clus.Terminate(t)
  44. authSetupRoot(t, toGRPC(clus.Client(0)).Auth)
  45. c, cerr := clientv3.New(clientv3.Config{Endpoints: clus.Client(0).Endpoints(), Username: "root", Password: "123"})
  46. if cerr != nil {
  47. t.Fatal(cerr)
  48. }
  49. defer c.Close()
  50. rctx, cancel := context.WithCancel(context.TODO())
  51. donec := make(chan struct{})
  52. go func() {
  53. defer close(donec)
  54. for rctx.Err() == nil {
  55. c.Put(rctx, "abc", "def")
  56. }
  57. }()
  58. time.Sleep(10 * time.Millisecond)
  59. if _, err := c.AuthDisable(context.TODO()); err != nil {
  60. t.Fatal(err)
  61. }
  62. time.Sleep(10 * time.Millisecond)
  63. cancel()
  64. <-donec
  65. }
  66. func TestV3AuthRevision(t *testing.T) {
  67. defer testutil.AfterTest(t)
  68. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  69. defer clus.Terminate(t)
  70. api := toGRPC(clus.Client(0))
  71. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  72. presp, perr := api.KV.Put(ctx, &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
  73. cancel()
  74. if perr != nil {
  75. t.Fatal(perr)
  76. }
  77. rev := presp.Header.Revision
  78. ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
  79. aresp, aerr := api.Auth.UserAdd(ctx, &pb.AuthUserAddRequest{Name: "root", Password: "123"})
  80. cancel()
  81. if aerr != nil {
  82. t.Fatal(aerr)
  83. }
  84. if aresp.Header.Revision != rev {
  85. t.Fatalf("revision expected %d, got %d", rev, aresp.Header.Revision)
  86. }
  87. }
  88. func authSetupRoot(t *testing.T, auth pb.AuthClient) {
  89. if _, err := auth.UserAdd(context.TODO(), &pb.AuthUserAddRequest{Name: "root", Password: "123"}); err != nil {
  90. t.Fatal(err)
  91. }
  92. if _, err := auth.RoleAdd(context.TODO(), &pb.AuthRoleAddRequest{Name: "root"}); err != nil {
  93. t.Fatal(err)
  94. }
  95. if _, err := auth.UserGrantRole(context.TODO(), &pb.AuthUserGrantRoleRequest{User: "root", Role: "root"}); err != nil {
  96. t.Fatal(err)
  97. }
  98. if _, err := auth.AuthEnable(context.TODO(), &pb.AuthEnableRequest{}); err != nil {
  99. t.Fatal(err)
  100. }
  101. }