-
Notifications
You must be signed in to change notification settings - Fork 9.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[3.4] backport #13359 Fix http2 authority header in single endpoint scenario #16988
Merged
ahrtr
merged 1 commit into
etcd-io:release-3.4
from
chaochn47:release-3.4-backport-authority-header-fix
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
// Copyright 2021 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build !cluster_proxy | ||
// +build !cluster_proxy | ||
|
||
package e2e | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.etcd.io/etcd/pkg/testutil" | ||
) | ||
|
||
func TestAuthority(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
useTLS bool | ||
useInsecureTLS bool | ||
// Pattern used to generate endpoints for client. Fields filled | ||
// %d - will be filled with member grpc port | ||
clientURLPattern string | ||
|
||
// Pattern used to validate authority received by server. Fields filled: | ||
// %d - will be filled with first member grpc port | ||
expectAuthorityPattern string | ||
}{ | ||
{ | ||
name: "http://domain[:port]", | ||
clientURLPattern: "http://localhost:%d", | ||
expectAuthorityPattern: "localhost:%d", | ||
}, | ||
{ | ||
name: "http://address[:port]", | ||
clientURLPattern: "http://127.0.0.1:%d", | ||
expectAuthorityPattern: "127.0.0.1:%d", | ||
}, | ||
{ | ||
name: "https://domain[:port] insecure", | ||
useTLS: true, | ||
useInsecureTLS: true, | ||
clientURLPattern: "https://localhost:%d", | ||
expectAuthorityPattern: "localhost:%d", | ||
}, | ||
{ | ||
name: "https://address[:port] insecure", | ||
useTLS: true, | ||
useInsecureTLS: true, | ||
clientURLPattern: "https://127.0.0.1:%d", | ||
expectAuthorityPattern: "127.0.0.1:%d", | ||
}, | ||
{ | ||
name: "https://domain[:port]", | ||
useTLS: true, | ||
clientURLPattern: "https://localhost:%d", | ||
expectAuthorityPattern: "localhost:%d", | ||
}, | ||
{ | ||
name: "https://address[:port]", | ||
useTLS: true, | ||
clientURLPattern: "https://127.0.0.1:%d", | ||
expectAuthorityPattern: "127.0.0.1:%d", | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
for _, clusterSize := range []int{1, 3} { | ||
t.Run(fmt.Sprintf("Size: %d, Scenario: %q", clusterSize, tc.name), func(t *testing.T) { | ||
defer testutil.AfterTest(t) | ||
|
||
cfg := configNoTLS | ||
cfg.clusterSize = clusterSize | ||
if tc.useTLS { | ||
cfg.clientTLS = clientTLS | ||
} | ||
cfg.isClientAutoTLS = tc.useInsecureTLS | ||
// Enable debug mode to get logs with http2 headers (including authority) | ||
cfg.envVars = map[string]string{"GODEBUG": "http2debug=2"} | ||
|
||
epc, err := newEtcdProcessCluster(&cfg) | ||
if err != nil { | ||
t.Fatalf("could not start etcd process cluster (%v)", err) | ||
} | ||
defer epc.Close() | ||
endpoints := templateEndpoints(t, tc.clientURLPattern, epc) | ||
|
||
client := clusterEtcdctlV3(&cfg, endpoints) | ||
err = client.Put("foo", "bar") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
executeWithTimeout(t, 5*time.Second, func() { | ||
assertAuthority(t, fmt.Sprintf(tc.expectAuthorityPattern, 20000), epc) | ||
}) | ||
}) | ||
|
||
} | ||
} | ||
} | ||
|
||
func templateEndpoints(t *testing.T, pattern string, clus *etcdProcessCluster) []string { | ||
t.Helper() | ||
endpoints := []string{} | ||
for i := 0; i < clus.cfg.clusterSize; i++ { | ||
ent := pattern | ||
if strings.Contains(ent, "%d") { | ||
ent = fmt.Sprintf(ent, etcdProcessBasePort+i*5) | ||
} | ||
if strings.Contains(ent, "%") { | ||
t.Fatalf("Failed to template pattern, %% symbol left %q", ent) | ||
} | ||
endpoints = append(endpoints, ent) | ||
} | ||
return endpoints | ||
} | ||
|
||
func assertAuthority(t *testing.T, expectAurhority string, clus *etcdProcessCluster) { | ||
logs := []logsExpect{} | ||
for _, proc := range clus.procs { | ||
logs = append(logs, proc.Logs()) | ||
} | ||
line := firstMatch(t, `http2: decoded hpack field header field ":authority"`, logs...) | ||
line = strings.TrimSuffix(line, "\n") | ||
line = strings.TrimSuffix(line, "\r") | ||
expectLine := fmt.Sprintf(`http2: decoded hpack field header field ":authority" = %q`, expectAurhority) | ||
assert.True(t, strings.HasSuffix(line, expectLine), fmt.Sprintf("Got %q expected suffix %q", line, expectLine)) | ||
} | ||
|
||
func firstMatch(t *testing.T, expectLine string, logs ...logsExpect) string { | ||
t.Helper() | ||
match := make(chan string, len(logs)) | ||
for i := range logs { | ||
go func(l logsExpect) { | ||
line, _ := l.Expect(expectLine) | ||
match <- line | ||
}(logs[i]) | ||
} | ||
return <-match | ||
} | ||
|
||
func executeWithTimeout(t *testing.T, timeout time.Duration, f func()) { | ||
donec := make(chan struct{}) | ||
go func() { | ||
defer close(donec) | ||
f() | ||
}() | ||
|
||
select { | ||
case <-time.After(timeout): | ||
testutil.FatalStack(t, fmt.Sprintf("test timed out after %v", timeout)) | ||
case <-donec: | ||
} | ||
} | ||
|
||
type etcdctlV3 struct { | ||
cfg *etcdProcessClusterConfig | ||
endpoints []string | ||
} | ||
|
||
func clusterEtcdctlV3(cfg *etcdProcessClusterConfig, endpoints []string) *etcdctlV3 { | ||
return &etcdctlV3{ | ||
cfg: cfg, | ||
endpoints: endpoints, | ||
} | ||
} | ||
|
||
func (ctl *etcdctlV3) Put(key, value string) error { | ||
return ctl.runCmd("put", key, value) | ||
} | ||
|
||
func (ctl *etcdctlV3) runCmd(args ...string) error { | ||
cmdArgs := []string{ctlBinPath + "3"} | ||
for k, v := range ctl.flags() { | ||
cmdArgs = append(cmdArgs, fmt.Sprintf("--%s=%s", k, v)) | ||
} | ||
cmdArgs = append(cmdArgs, args...) | ||
return spawnWithExpect(cmdArgs, "OK") | ||
} | ||
|
||
func (ctl *etcdctlV3) flags() map[string]string { | ||
fmap := make(map[string]string) | ||
if ctl.cfg.clientTLS == clientTLS { | ||
if ctl.cfg.isClientAutoTLS { | ||
fmap["insecure-transport"] = "false" | ||
fmap["insecure-skip-tls-verify"] = "true" | ||
} else if ctl.cfg.isClientCRL { | ||
fmap["cacert"] = caPath | ||
fmap["cert"] = revokedCertPath | ||
fmap["key"] = revokedPrivateKeyPath | ||
} else { | ||
fmap["cacert"] = caPath | ||
fmap["cert"] = certPath | ||
fmap["key"] = privateKeyPath | ||
} | ||
} | ||
fmap["endpoints"] = strings.Join(ctl.endpoints, ",") | ||
return fmap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it from main branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YES.
There is no function
SpawnCmdWithLogger
inreleasae-3.4
, and a new functionspawnCmdWithEnv
is added in this PR. It is a minor difference from the original PR. It should be OK.