Skip to content

Commit 9350d1b

Browse files
izumo27hoguni
authored andcommitted
[fix] Fix hostname verification (#126)
### Motivation If `ValidateHostName` is set to true, handshake always fails. ``` INFO [] ClientConnection:375 | [ -> ] Connected to broker ERROR [] ClientConnection:463 | [ -> ] Handshake failed: certificate verify failed INFO [] ClientConnection:1560 | [ -> ] Connection closed ``` ### Modifications - Verify `serviceUrl.host()`, not `physicalAddress`. - `physicalAddress` is serviceUrl, which contains protocol (e.g. pulsar+ssl) and port number. - Use `ssl::stream::set_verify_callback` instead of `ssl::context::set_verify_callback`. - Verification should work with `ssl::context::set_verify_callback`, but somehow it doesn't work. Co-authored-by: hoguni <hoguni@yahoo-corp.jp>
1 parent e6a8a64 commit 9350d1b

10 files changed

+624
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
#
20+
21+
set -e -x
22+
23+
if [ $# -ne 2 ]; then
24+
echo "Usage: $0 \$CONTAINER_ID \$START_TEST_SERVICE_INSIDE_CONTAINER"
25+
exit 1
26+
fi
27+
28+
CONTAINER_ID=$1
29+
START_TEST_SERVICE_INSIDE_CONTAINER=$2
30+
31+
echo $CONTAINER_ID >> .tests-container-id.txt
32+
33+
docker cp test-conf $CONTAINER_ID:/pulsar/test-conf
34+
docker cp build-support/$START_TEST_SERVICE_INSIDE_CONTAINER $CONTAINER_ID:$START_TEST_SERVICE_INSIDE_CONTAINER
35+
36+
docker exec -i $CONTAINER_ID /$START_TEST_SERVICE_INSIDE_CONTAINER
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
#
20+
21+
set -e -x
22+
23+
export PULSAR_EXTRA_OPTS=-Dpulsar.auth.basic.conf=test-conf/.htpasswd
24+
25+
# Generate secret key and token
26+
mkdir -p data/tokens
27+
bin/pulsar tokens create-secret-key --output data/tokens/secret.key
28+
29+
bin/pulsar tokens create \
30+
--subject token-principal \
31+
--secret-key file:///pulsar/data/tokens/secret.key \
32+
> /pulsar/data/tokens/token.txt
33+
34+
export PULSAR_STANDALONE_CONF=test-conf/standalone-ssl-mim.conf
35+
export PULSAR_PID_DIR=/tmp
36+
bin/pulsar-daemon start standalone \
37+
--no-functions-worker --no-stream-storage \
38+
--bookkeeper-dir data/bookkeeper
39+
40+
echo "-- Wait for Pulsar service to be ready"
41+
until curl http://localhost:8081/metrics > /dev/null 2>&1 ; do sleep 1; done
42+
43+
echo "-- Pulsar service is ready -- Configure permissions"
44+
45+
export PULSAR_CLIENT_CONF=test-conf/client-ssl-mim.conf
46+
47+
# Create "standalone" cluster if it does not exist
48+
bin/pulsar-admin clusters list | grep -q '^standalone$' ||
49+
bin/pulsar-admin clusters create \
50+
standalone \
51+
--url http://localhost:8081/ \
52+
--url-secure https://localhost:8444/ \
53+
--broker-url pulsar://localhost:6652/ \
54+
--broker-url-secure pulsar+ssl://localhost:6653/
55+
56+
# Create "private" tenant
57+
bin/pulsar-admin tenants create private -r "" -c "standalone"
58+
59+
# Create "private/auth" with required authentication
60+
bin/pulsar-admin namespaces create private/auth --clusters standalone
61+
62+
bin/pulsar-admin namespaces grant-permission private/auth \
63+
--actions produce,consume \
64+
--role "token-principal"
65+
66+
echo "-- Ready to start tests"

lib/ClientConnection.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,6 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
201201
} else {
202202
ctx.set_verify_mode(boost::asio::ssl::context::verify_peer);
203203

204-
if (clientConfiguration.isValidateHostName()) {
205-
LOG_DEBUG("Validating hostname for " << serviceUrl.host() << ":" << serviceUrl.port());
206-
ctx.set_verify_callback(boost::asio::ssl::rfc2818_verification(physicalAddress));
207-
}
208-
209204
std::string trustCertFilePath = clientConfiguration.getTlsTrustCertsFilePath();
210205
if (!trustCertFilePath.empty()) {
211206
if (file_exists(trustCertFilePath)) {
@@ -254,6 +249,11 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
254249

255250
tlsSocket_ = ExecutorService::createTlsSocket(socket_, ctx);
256251

252+
if (!clientConfiguration.isTlsAllowInsecureConnection() && clientConfiguration.isValidateHostName()) {
253+
LOG_DEBUG("Validating hostname for " << serviceUrl.host() << ":" << serviceUrl.port());
254+
tlsSocket_->set_verify_callback(boost::asio::ssl::rfc2818_verification(serviceUrl.host()));
255+
}
256+
257257
LOG_DEBUG("TLS SNI Host: " << serviceUrl.host());
258258
if (!SSL_set_tlsext_host_name(tlsSocket_->native_handle(), serviceUrl.host().c_str())) {
259259
boost::system::error_code ec{static_cast<int>(::ERR_get_error()),

pulsar-test-service-start.sh

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ cd $SRC_DIR
2626
./pulsar-test-service-stop.sh
2727

2828
CONTAINER_ID=$(docker run -i -p 8080:8080 -p 6650:6650 -p 8443:8443 -p 6651:6651 --rm --detach apachepulsar/pulsar:latest sleep 3600)
29-
echo $CONTAINER_ID > .tests-container-id.txt
30-
31-
docker cp test-conf $CONTAINER_ID:/pulsar/test-conf
32-
docker cp build-support/start-test-service-inside-container.sh $CONTAINER_ID:start-test-service-inside-container.sh
33-
34-
docker exec -i $CONTAINER_ID /start-test-service-inside-container.sh
29+
build-support/setup-test-service-container.sh $CONTAINER_ID start-test-service-inside-container.sh
3530

3631
docker cp $CONTAINER_ID:/pulsar/data/tokens/token.txt .test-token.txt
3732

33+
CONTAINER_ID=$(docker run -i -p 8081:8081 -p 6652:6652 -p 8444:8444 -p 6653:6653 --rm --detach apachepulsar/pulsar:latest sleep 3600)
34+
build-support/setup-test-service-container.sh $CONTAINER_ID start-mim-test-service-inside-container.sh
35+
3836
echo "-- Ready to start tests"

test-conf/client-ssl-mim.conf

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
# Pulsar Client configuration
21+
webServiceUrl=https://localhost:8444/
22+
brokerServiceUrl=pulsar+ssl://localhost:6653/
23+
tlsAllowInsecureConnection=false
24+
tlsTrustCertsFilePath=test-conf/hn-verification/cacert.pem
25+
authPlugin=org.apache.pulsar.client.impl.auth.AuthenticationTls
26+
authParams=tlsCertFile:test-conf/client-cert.pem,tlsKeyFile:test-conf/client-key.pem
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIEkDCCAnigAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwETEPMA0GA1UEAwwGZm9v
3+
YmFyMCAXDTE4MDYyMjA4NTUzMloYDzIyOTIwNDA2MDg1NTMyWjAjMSEwHwYDVQQD
4+
DBhicm9rZXIucHVsc2FyLmFwYWNoZS5vcmcwggEiMA0GCSqGSIb3DQEBAQUAA4IB
5+
DwAwggEKAoIBAQDQouKhZah4hMCqmg4aS5RhQG/Y1gA+yP9DGF9mlw35tfhfWs63
6+
EvNjEK4L/ZWSEV45L/wc6YV14RmM6bJ0V/0vXo4xmISbqptND/2kRIspkLZQ5F0O
7+
OQXVicqZLOc6igZQhRg8ANDYdTJUTF65DqauX4OJt3YMhF2FSt7jQtlj06IQBa01
8+
+ARO9OotMJtBY+vIU5bV6JydfgkhQH9rIDI7AMeY5j02gGkJJrelfm+WoOsUez+X
9+
aqTN3/tF8+MBcFB3G04s1qc2CJPJM3YGxvxEtHqTGI14t9J8p5O7X9JHpcY8X00s
10+
bxa4FGbKgfDobbkJ+GgblWCkAcLN95sKTqtHAgMBAAGjgd0wgdowCQYDVR0TBAIw
11+
ADARBglghkgBhvhCAQEEBAMCBkAwMwYJYIZIAYb4QgENBCYWJE9wZW5TU0wgR2Vu
12+
ZXJhdGVkIFNlcnZlciBDZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQUaxFvJrkEGqk8azTA
13+
DyVyTyTbJAIwQQYDVR0jBDowOIAUVwvpyyPov0c+UHo/RX6hGEOdFSehFaQTMBEx
14+
DzANBgNVBAMMBmZvb2JhcoIJANfih0+geeIMMA4GA1UdDwEB/wQEAwIFoDATBgNV
15+
HSUEDDAKBggrBgEFBQcDATANBgkqhkiG9w0BAQsFAAOCAgEA35QDGclHzQtHs3yQ
16+
ZzNOSKisg5srTiIoQgRzfHrXfkthNFCnBzhKjBxqk3EIasVtvyGuk0ThneC1ai3y
17+
ZK3BivnMZfm1SfyvieFoqWetsxohWfcpOSVkpvO37P6v/NmmaTIGkBN3gxKCx0QN
18+
zqApLQyNTM++X3wxetYH/afAGUrRmBGWZuJheQpB9yZ+FB6BRp8YuYIYBzANJyW9
19+
spvXW03TpqX2AIoRBoGMLzK72vbhAbLWiCIfEYREhbZVRkP+yvD338cWrILlOEur
20+
x/n8L/FTmbf7mXzHg4xaQ3zg/5+0OCPMDPUBE4xWDBAbZ82hgOcTqfVjwoPgo2V0
21+
fbbx6redq44J3Vn5d9Xhi59fkpqEjHpX4xebr5iMikZsNTJMeLh0h3uf7DstuO9d
22+
mfnF5j+yDXCKb9XzCsTSvGCN+spmUh6RfSrbkw8/LrRvBUpKVEM0GfKSnaFpOaSS
23+
efM4UEi72FRjszzHEkdvpiLhYvihINLJmDXszhc3fCi42be/DGmUhuhTZWynOPmp
24+
0N0V/8/sGT5gh4fGEtGzS/8xEvZwO9uDlccJiG8Pi+aO0/K9urB9nppd/xKWXv3C
25+
cib/QrW0Qow4TADWC1fnGYCpFzzaZ2esPL2MvzOYXnW4/AbEqmb6Weatluai64ZK
26+
3N2cGJWRyvpvvmbP2hKCa4eLgEc=
27+
-----END CERTIFICATE-----
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDQouKhZah4hMCq
3+
mg4aS5RhQG/Y1gA+yP9DGF9mlw35tfhfWs63EvNjEK4L/ZWSEV45L/wc6YV14RmM
4+
6bJ0V/0vXo4xmISbqptND/2kRIspkLZQ5F0OOQXVicqZLOc6igZQhRg8ANDYdTJU
5+
TF65DqauX4OJt3YMhF2FSt7jQtlj06IQBa01+ARO9OotMJtBY+vIU5bV6Jydfgkh
6+
QH9rIDI7AMeY5j02gGkJJrelfm+WoOsUez+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vdTAisSMDKIOCylRrNb5f5baIP2gR3zkeyxiqPtm
23+
3htsHfSy67EtXpP50wQW4Dft2eLi7ZweJXMEWFfomfEjBeeWYAGNHHe5DFIauuVZ
24+
2WexDEGqNpAlIm0s7aSjVPrn1DHbouOkNyenlMqN+QKBgQDVYVhk9widShSnCmUA
25+
G30moXDgj3eRqCf5T7NEr9GXD1QBD/rQSPh5agnDV7IYLpV7/wkYLI7l9x7mDwu+
26+
I9mRXkyAmTVEctLTdXQHt0jdJa5SfUaVEDUzQbr0fUjkmythTvqZ809+d3ELPeLI
27+
5qJ7jxgksHWji4lYfL4r4J6Zaw==
28+
-----END PRIVATE KEY-----

test-conf/hn-verification/cacert.pem

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIFCDCCAvCgAwIBAgIJANfih0+geeIMMA0GCSqGSIb3DQEBCwUAMBExDzANBgNV
3+
BAMMBmZvb2JhcjAeFw0xODA2MjIwODQ2MjFaFw0zODA2MTcwODQ2MjFaMBExDzAN
4+
BgNVBAMMBmZvb2JhcjCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAOVU
5+
UpTPeXCeyfUiQS824l9s9krZd4R6TA4D97eQ9EWm2D7ppV4gPApHO8j5f+joo/b6
6+
Iso4aFlHpJ8VV2a5Ol7rjQw43MJHaBgwDxB1XWgsNdfoI7ebtp/BWg2nM3r8wm+Z
7+
gKenf9d1/1Ol+6yFUehkLkIXUvldiVegmmje8FnwhcDNE1eTrh66XqSJXEXqgBKu
8+
NqsoYcVak72OyOO1/N8CESoSdyBkbSiH5vJyo0AUCjn7tULga7fxojmqBZDog9Pg
9+
e5Fi/hbCrdinbxBrMgIxQ7wqXw2sw6iOWu4FU8Ih/CuF4xaQy2YP7MEk4Ff0LCY0
10+
KMhFMWU7550r/fz/C2l7fKhREyCQPa/bVE+dfxgZ/gCZ+p7vQ154hCCjpd+5bECv
11+
SN1bcVIPG6ngQu4vM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