v3_auth_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "fmt"
  17. "sync"
  18. "testing"
  19. "time"
  20. "golang.org/x/net/context"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  23. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. "github.com/coreos/etcd/pkg/testutil"
  25. )
  26. // TestV3AuthEmptyUserGet ensures that a get with an empty user will return an empty user error.
  27. func TestV3AuthEmptyUserGet(t *testing.T) {
  28. defer testutil.AfterTest(t)
  29. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  30. defer clus.Terminate(t)
  31. ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
  32. defer cancel()
  33. api := toGRPC(clus.Client(0))
  34. authSetupRoot(t, api.Auth)
  35. _, err := api.KV.Range(ctx, &pb.RangeRequest{Key: []byte("abc")})
  36. if !eqErrGRPC(err, rpctypes.ErrUserEmpty) {
  37. t.Fatalf("got %v, expected %v", err, rpctypes.ErrUserEmpty)
  38. }
  39. }
  40. // TestV3AuthTokenWithDisable tests that auth won't crash if
  41. // given a valid token when authentication is disabled
  42. func TestV3AuthTokenWithDisable(t *testing.T) {
  43. defer testutil.AfterTest(t)
  44. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  45. defer clus.Terminate(t)
  46. authSetupRoot(t, toGRPC(clus.Client(0)).Auth)
  47. c, cerr := clientv3.New(clientv3.Config{Endpoints: clus.Client(0).Endpoints(), Username: "root", Password: "123"})
  48. if cerr != nil {
  49. t.Fatal(cerr)
  50. }
  51. defer c.Close()
  52. rctx, cancel := context.WithCancel(context.TODO())
  53. donec := make(chan struct{})
  54. go func() {
  55. defer close(donec)
  56. for rctx.Err() == nil {
  57. c.Put(rctx, "abc", "def")
  58. }
  59. }()
  60. time.Sleep(10 * time.Millisecond)
  61. if _, err := c.AuthDisable(context.TODO()); err != nil {
  62. t.Fatal(err)
  63. }
  64. time.Sleep(10 * time.Millisecond)
  65. cancel()
  66. <-donec
  67. }
  68. func TestV3AuthRevision(t *testing.T) {
  69. defer testutil.AfterTest(t)
  70. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  71. defer clus.Terminate(t)
  72. api := toGRPC(clus.Client(0))
  73. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  74. presp, perr := api.KV.Put(ctx, &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
  75. cancel()
  76. if perr != nil {
  77. t.Fatal(perr)
  78. }
  79. rev := presp.Header.Revision
  80. ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
  81. aresp, aerr := api.Auth.UserAdd(ctx, &pb.AuthUserAddRequest{Name: "root", Password: "123"})
  82. cancel()
  83. if aerr != nil {
  84. t.Fatal(aerr)
  85. }
  86. if aresp.Header.Revision != rev {
  87. t.Fatalf("revision expected %d, got %d", rev, aresp.Header.Revision)
  88. }
  89. }
  90. func authSetupRoot(t *testing.T, auth pb.AuthClient) {
  91. if _, err := auth.UserAdd(context.TODO(), &pb.AuthUserAddRequest{Name: "root", Password: "123"}); err != nil {
  92. t.Fatal(err)
  93. }
  94. if _, err := auth.RoleAdd(context.TODO(), &pb.AuthRoleAddRequest{Name: "root"}); err != nil {
  95. t.Fatal(err)
  96. }
  97. if _, err := auth.UserGrantRole(context.TODO(), &pb.AuthUserGrantRoleRequest{User: "root", Role: "root"}); err != nil {
  98. t.Fatal(err)
  99. }
  100. if _, err := auth.AuthEnable(context.TODO(), &pb.AuthEnableRequest{}); err != nil {
  101. t.Fatal(err)
  102. }
  103. }
  104. func TestV3AuthOldRevConcurrent(t *testing.T) {
  105. defer testutil.AfterTest(t)
  106. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  107. defer clus.Terminate(t)
  108. authSetupRoot(t, toGRPC(clus.Client(0)).Auth)
  109. c, cerr := clientv3.New(clientv3.Config{
  110. Endpoints: clus.Client(0).Endpoints(),
  111. DialTimeout: 5 * time.Second,
  112. Username: "root",
  113. Password: "123",
  114. })
  115. testutil.AssertNil(t, cerr)
  116. defer c.Close()
  117. var wg sync.WaitGroup
  118. f := func(i int) {
  119. defer wg.Done()
  120. role, user := fmt.Sprintf("test-role-%d", i), fmt.Sprintf("test-user-%d", i)
  121. _, err := c.RoleAdd(context.TODO(), role)
  122. testutil.AssertNil(t, err)
  123. _, err = c.RoleGrantPermission(context.TODO(), role, "", clientv3.GetPrefixRangeEnd(""), clientv3.PermissionType(clientv3.PermReadWrite))
  124. testutil.AssertNil(t, err)
  125. _, err = c.UserAdd(context.TODO(), user, "123")
  126. testutil.AssertNil(t, err)
  127. _, err = c.Put(context.TODO(), "a", "b")
  128. testutil.AssertNil(t, err)
  129. }
  130. // needs concurrency to trigger
  131. numRoles := 2
  132. wg.Add(numRoles)
  133. for i := 0; i < numRoles; i++ {
  134. go f(i)
  135. }
  136. wg.Wait()
  137. }
  138. // TestV3AuthWithLeaseRevokeWithRoot ensures that granted leases
  139. // with root user be revoked after TTL.
  140. func TestV3AuthWithLeaseRevokeWithRoot(t *testing.T) {
  141. defer testutil.AfterTest(t)
  142. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  143. defer clus.Terminate(t)
  144. api := toGRPC(clus.Client(0))
  145. authSetupRoot(t, api.Auth)
  146. rootc, cerr := clientv3.New(clientv3.Config{
  147. Endpoints: clus.Client(0).Endpoints(),
  148. Username: "root",
  149. Password: "123",
  150. })
  151. if cerr != nil {
  152. t.Fatal(cerr)
  153. }
  154. defer rootc.Close()
  155. leaseResp, err := rootc.Grant(context.TODO(), 2)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. leaseID := leaseResp.ID
  160. if _, err = rootc.Put(context.TODO(), "foo", "bar", clientv3.WithLease(leaseID)); err != nil {
  161. t.Fatal(err)
  162. }
  163. // wait for lease expire
  164. time.Sleep(3 * time.Second)
  165. tresp, terr := api.Lease.LeaseTimeToLive(
  166. context.TODO(),
  167. &pb.LeaseTimeToLiveRequest{
  168. ID: int64(leaseID),
  169. Keys: true,
  170. },
  171. )
  172. if terr != nil {
  173. t.Error(terr)
  174. }
  175. if len(tresp.Keys) > 0 || tresp.GrantedTTL != 0 {
  176. t.Errorf("lease %016x should have been revoked, got %+v", leaseID, tresp)
  177. }
  178. if tresp.TTL != -1 {
  179. t.Errorf("lease %016x should have been expired, got %+v", leaseID, tresp)
  180. }
  181. }