Skip to content

Commit 5f7ce4f

Browse files
committed
e2e: add a test case for --peer-cert-allowed-cn
1 parent 1d28a7a commit 5f7ce4f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

e2e/etcd_config_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,80 @@ func TestEtcdUnixPeers(t *testing.T) {
113113
t.Fatal(err)
114114
}
115115
}
116+
117+
// TestEtcdPeerCNAuth checks that the inter peer auth based on CN of cert is working correctly.
118+
func TestEtcdPeerCNAuth(t *testing.T) {
119+
peers, tmpdirs := make([]string, 3), make([]string, 3)
120+
for i := range peers {
121+
peers[i] = fmt.Sprintf("e%d=https://127.0.0.1:%d", i, etcdProcessBasePort+i)
122+
d, err := ioutil.TempDir("", fmt.Sprintf("e%d.etcd", i))
123+
if err != nil {
124+
t.Fatal(err)
125+
}
126+
tmpdirs[i] = d
127+
}
128+
ic := strings.Join(peers, ",")
129+
130+
procs := make([]*expect.ExpectProcess, len(peers))
131+
defer func() {
132+
for i := range procs {
133+
if procs[i] != nil {
134+
procs[i].Stop()
135+
}
136+
os.RemoveAll(tmpdirs[i])
137+
}
138+
}()
139+
140+
// node 0 and 1 have a cert with the correct CN, node 2 doesn't
141+
for i := range procs {
142+
commonArgs := []string{
143+
binDir + "/etcd",
144+
"--name", fmt.Sprintf("e%d", i),
145+
"--listen-client-urls", "http://0.0.0.0:0",
146+
"--data-dir", tmpdirs[i],
147+
"--advertise-client-urls", "http://0.0.0.0:0",
148+
"--listen-peer-urls", fmt.Sprintf("https://127.0.0.1:%d,https://127.0.0.1:%d", etcdProcessBasePort+i, etcdProcessBasePort+len(peers)+i),
149+
"--initial-advertise-peer-urls", fmt.Sprintf("https://127.0.0.1:%d", etcdProcessBasePort+i),
150+
"--initial-cluster", ic,
151+
}
152+
153+
var args []string
154+
if i <= 1 {
155+
args = []string{
156+
"--peer-cert-file", certPath,
157+
"--peer-key-file", privateKeyPath,
158+
"--peer-trusted-ca-file", caPath,
159+
"--peer-client-cert-auth",
160+
"--peer-cert-allowed-cn", "example.com",
161+
}
162+
} else {
163+
args = []string{
164+
"--peer-cert-file", certPath2,
165+
"--peer-key-file", privateKeyPath2,
166+
"--peer-trusted-ca-file", caPath,
167+
"--peer-client-cert-auth",
168+
"--peer-cert-allowed-cn", "example2.com",
169+
}
170+
}
171+
172+
commonArgs = append(commonArgs, args...)
173+
174+
p, err := spawnCmd(commonArgs)
175+
if err != nil {
176+
t.Fatal(err)
177+
}
178+
procs[i] = p
179+
}
180+
181+
for i, p := range procs {
182+
var expect []string
183+
if i <= 1 {
184+
expect = etcdServerReadyLines
185+
} else {
186+
expect = []string{"(remote error: tls: bad certificate)"}
187+
}
188+
if err := waitReadyExpectProc(p, expect); err != nil {
189+
t.Fatal(err)
190+
}
191+
}
192+
}

e2e/main_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ var (
2121
privateKeyPath string
2222
caPath string
2323

24+
certPath2 string
25+
privateKeyPath2 string
26+
2427
crlPath string
2528
revokedCertPath string
2629
revokedPrivateKeyPath string
@@ -43,6 +46,9 @@ func TestMain(m *testing.M) {
4346
revokedPrivateKeyPath = certDir + "/server-revoked.key.insecure"
4447
crlPath = certDir + "/revoke.crl"
4548

49+
certPath2 = certDir + "/server2.crt"
50+
privateKeyPath2 = certDir + "/server2.key.insecure"
51+
4652
v := m.Run()
4753
if v == 0 && testutil.CheckLeakedGoroutine() {
4854
os.Exit(1)

0 commit comments

Comments
 (0)