Skip to content

Commit

Permalink
client: Use first endpoint as http2 authority header
Browse files Browse the repository at this point in the history
This doesn't fully fix authority in multiple endpoint scenario, but
should at least fixes single endpoint scenario.
  • Loading branch information
serathius committed Sep 17, 2021
1 parent 8a74d7d commit 8ce6100
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
11 changes: 9 additions & 2 deletions client/v3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,22 @@ func (c *Client) dial(creds grpccredentials.TransportCredentials, dopts ...grpc.
defer cancel() // TODO: Is this right for cases where grpc.WithBlock() is not set on the dial options?
}

initialEndpoints := strings.Join(c.Endpoints(), ";")
target := fmt.Sprintf("%s://%p/#initially=[%s]", resolver.Schema, c, initialEndpoints)
target := fmt.Sprintf("%s://%p/%s", resolver.Schema, c, authority(c.endpoints[0]))
conn, err := grpc.DialContext(dctx, target, opts...)
if err != nil {
return nil, err
}
return conn, nil
}

func authority(endpoint string) string {
spl := strings.SplitN(endpoint, "://", 2)
if len(spl) < 2 {
return endpoint
}
return spl[1]
}

func (c *Client) credentialsForEndpoint(ep string) grpccredentials.TransportCredentials {
r := endpoint.RequiresCredentials(ep)
switch r {
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/v3rpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func logUnaryRequestStats(ctx context.Context, lg *zap.Logger, warnLatency time.
reqCount = 1
reqSize = _req.Size()
reqContent = pb.NewLoggablePutRequest(_req).String()
// redact value field from request content, see PR #9821
// redact value field from RequestInfo content, see PR #9821
}
if _resp != nil {
respCount = 0
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/ctl_v3_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestCtlV3AuthoritySingleEndpoint(t *testing.T) {
}

executeWithTimeout(t, 5*time.Second, func() {
assertAuthority(t, "#initially=[http://localhost:20000]", epc.procs[0].Logs())
assertAuthority(t, "localhost:20000", epc.procs[0].Logs())
})
assert.NoError(t, epc.Close())
}
Expand All @@ -65,7 +65,7 @@ func TestCtlV3AuthorityMultipleEndpoints(t *testing.T) {
}

executeWithTimeout(t, 10*time.Second, func() {
assertAuthority(t, "#initially=[http://localhost:20000;http://localhost:20005;http://localhost:20010]", epc.procs[0].Logs(), epc.procs[1].Logs(), epc.procs[2].Logs())
assertAuthority(t, "localhost:20000", epc.procs[0].Logs(), epc.procs[1].Logs(), epc.procs[2].Logs())
})
assert.NoError(t, epc.Close())
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/clientv3/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestAuthoritySingleEndpoint(t *testing.T) {
}

reqs := clus.Members[0].RecordedRequests()
assert.ElementsMatch(t, []v3rpc.RequestInfo{{FullMethod: "/etcdserverpb.KV/Put", Authority: "#initially=[unix://localhost:m00]"}}, reqs)
assert.ElementsMatch(t, []v3rpc.RequestInfo{{FullMethod: "/etcdserverpb.KV/Put", Authority: "localhost:m00"}}, reqs)
}

func TestAuthorityMultipleEndpoints(t *testing.T) {
Expand All @@ -62,5 +62,5 @@ func TestAuthorityMultipleEndpoints(t *testing.T) {
for _, m := range clus.Members {
reqs = append(reqs, m.RecordedRequests()...)
}
assert.ElementsMatch(t, []v3rpc.RequestInfo{{FullMethod: "/etcdserverpb.KV/Put", Authority: "#initially=[unix://localhost:m00;unix://localhost:m10;unix://localhost:m20]"}}, reqs)
assert.ElementsMatch(t, []v3rpc.RequestInfo{{FullMethod: "/etcdserverpb.KV/Put", Authority: "localhost:m00"}}, reqs)
}

0 comments on commit 8ce6100

Please sign in to comment.