From 528dd82be969d251c11c281b06ec3c37c6c9033d Mon Sep 17 00:00:00 2001 From: Hitoshi Mitake Date: Sat, 10 Sep 2022 00:49:37 +0900 Subject: [PATCH] tests: a test case for watch with auth token expiration Signed-off-by: Hitoshi Mitake --- tests/integration/cluster.go | 8 +++++++- tests/integration/v3_auth_test.go | 33 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/integration/cluster.go b/tests/integration/cluster.go index 0d7b47ce4..a99c554ba 100644 --- a/tests/integration/cluster.go +++ b/tests/integration/cluster.go @@ -136,7 +136,8 @@ type ClusterConfig struct { DiscoveryURL string - AuthToken string + AuthToken string + AuthTokenTTL uint UseGRPC bool @@ -314,6 +315,7 @@ func (c *cluster) mustNewMember(t testutil.TB, memberNumber int64) *member { name: c.generateMemberName(), memberNumber: memberNumber, authToken: c.cfg.AuthToken, + authTokenTTL: c.cfg.AuthTokenTTL, peerTLS: c.cfg.PeerTLS, clientTLS: c.cfg.ClientTLS, quotaBackendBytes: c.cfg.QuotaBackendBytes, @@ -624,6 +626,7 @@ type memberConfig struct { peerTLS *transport.TLSInfo clientTLS *transport.TLSInfo authToken string + authTokenTTL uint quotaBackendBytes int64 maxTxnOps uint maxRequestBytes uint @@ -715,6 +718,9 @@ func mustNewMember(t testutil.TB, mcfg memberConfig) *member { if mcfg.authToken != "" { m.AuthToken = mcfg.authToken } + if mcfg.authTokenTTL != 0 { + m.TokenTTL = mcfg.authTokenTTL + } m.BcryptCost = uint(bcrypt.MinCost) // use min bcrypt cost to speedy up integration testing diff --git a/tests/integration/v3_auth_test.go b/tests/integration/v3_auth_test.go index 89a1377b5..21c87a4f9 100644 --- a/tests/integration/v3_auth_test.go +++ b/tests/integration/v3_auth_test.go @@ -498,3 +498,36 @@ func TestV3AuthRestartMember(t *testing.T) { _, err = c2.Put(context.TODO(), "foo", "bar2") testutil.AssertNil(t, err) } + +func TestV3AuthWatchAndTokenExpire(t *testing.T) { + BeforeTest(t) + clus := NewClusterV3(t, &ClusterConfig{Size: 1, AuthTokenTTL: 3}) + defer clus.Terminate(t) + + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + + authSetupRoot(t, toGRPC(clus.Client(0)).Auth) + + c, cerr := NewClient(t, clientv3.Config{Endpoints: clus.Client(0).Endpoints(), Username: "root", Password: "123"}) + if cerr != nil { + t.Fatal(cerr) + } + defer c.Close() + + _, err := c.Put(ctx, "key", "val") + if err != nil { + t.Fatalf("Unexpected error from Put: %v", err) + } + + // The first watch gets a valid auth token through watcher.newWatcherGrpcStream() + // We should discard the first one by waiting TTL after the first watch. + wChan := c.Watch(ctx, "key", clientv3.WithRev(1)) + watchResponse := <-wChan + + time.Sleep(5 * time.Second) + + wChan = c.Watch(ctx, "key", clientv3.WithRev(1)) + watchResponse = <-wChan + testutil.AssertNil(t, watchResponse.Err()) +}