Skip to content

Commit cfa2a5a

Browse files
authored
PYTHON-5195 Allow OCSP server to be run as a daemon (#626)
1 parent 14cc285 commit cfa2a5a

File tree

6 files changed

+85
-12
lines changed

6 files changed

+85
-12
lines changed

.evergreen/config.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,13 @@ functions:
559559
binary: bash
560560
args: [src/.evergreen/tests/test-cli.sh]
561561

562+
"run ocsp test":
563+
- command: subprocess.exec
564+
type: test
565+
params:
566+
binary: bash
567+
args: [src/.evergreen/tests/test-ocsp.sh]
568+
562569
"teardown assets":
563570
- command: subprocess.exec
564571
params:
@@ -1035,6 +1042,10 @@ tasks:
10351042
commands:
10361043
- func: "run cli test full"
10371044

1045+
- name: "test-ocsp"
1046+
commands:
1047+
- func: "run ocsp test"
1048+
10381049
- name: "test-cli-partial"
10391050
tags: ["pr"]
10401051
commands:
@@ -1485,6 +1496,7 @@ buildvariants:
14851496
- "test-install-binaries"
14861497
- "test-csfle"
14871498
- "test-cli-full"
1499+
- "test-ocsp"
14881500
- "test-8.0-standalone-require-api"
14891501

14901502
- matrix_name: "tests-os-requires-50"

.evergreen/ocsp/mock-ocsp-responder-requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ flask==2.2.5
44
itsdangerous==2.1.2
55
Jinja2==3.1.5
66
MarkupSafe==2.1.4
7-
git+https://github.com/wbond/oscrypto.git@d5f3437
7+
oscrypto==1.3.0
8+
waitress==3.0.2
89
Werkzeug==3.0.6

.evergreen/ocsp/ocsp_mock.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
sys.path.append(os.path.join(os.getcwd(), "src", "third_party", "mock_ocsp_responder"))
1212

1313
import mock_ocsp_responder
14+
from waitress import serve
1415

1516

1617
def main():
@@ -22,7 +23,7 @@ def main():
2223
)
2324

2425
parser.add_argument(
25-
"-b", "--bind_ip", type=str, default=None, help="IP to listen on"
26+
"-b", "--bind_ip", type=str, default="127.0.0.1", help="IP to listen on"
2627
)
2728

2829
parser.add_argument(
@@ -76,13 +77,7 @@ def main():
7677
next_update_seconds=args.next_update_seconds,
7778
)
7879

79-
mock_ocsp_responder.init(port=args.port, debug=args.verbose, host=args.bind_ip)
80-
81-
# Write the pid file.
82-
with open(os.path.join(os.getcwd(), "ocsp.pid"), "w") as fid:
83-
fid.write(str(os.getpid()))
84-
85-
print("Mock OCSP Responder is running on port %s" % (str(args.port)))
80+
serve(mock_ocsp_responder.app, host=args.bind_ip, port=args.port)
8681

8782

8883
if __name__ == "__main__":

.evergreen/ocsp/setup.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ for VARNAME in "${VARLIST[@]}"; do
1818
[[ -z "${!VARNAME:-}" ]] && echo "ERROR: $VARNAME not set" && exit 1;
1919
done
2020

21+
bash teardown.sh
22+
2123
. ./activate-ocspvenv.sh
2224

25+
echo "Starting OCSP server ${OCSP_ALGORITHM}-${SERVER_TYPE}..."
26+
2327
CA_FILE="${OCSP_ALGORITHM}/ca.pem"
2428
ARGS="-p 8100 -v"
2529

@@ -48,8 +52,22 @@ case $SERVER_TYPE in
4852
;;
4953
esac
5054

51-
python ocsp_mock.py \
55+
COMMAND="python -u"
56+
if [ "$(uname -s)" != "Darwin" ]; then
57+
# On linux and windows host, we need to use nohup to daemonize the process
58+
# and prevent the task from hanging.
59+
# The macos hosts do not support nohup.
60+
COMMAND="nohup $COMMAND"
61+
fi
62+
63+
$COMMAND ocsp_mock.py \
5264
--ca_file $CA_FILE \
5365
--ocsp_responder_cert $CERT \
5466
--ocsp_responder_key $KEY \
55-
$ARGS
67+
$ARGS > ocsp_mock_server.log 2>&1 &
68+
echo "$!" > ocsp.pid
69+
70+
sleep 1
71+
cat ocsp_mock_server.log
72+
73+
echo "Starting OCSP server ${OCSP_ALGORITHM}-${SERVER_TYPE}... done."

.evergreen/ocsp/teardown.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
77
. $SCRIPT_DIR/../handle-paths.sh
88
pushd $SCRIPT_DIR
99
if [ -f "ocsp.pid" ]; then
10-
< ocsp.pid xargs kill -9 || true
10+
echo "Killing ocsp server..."
11+
< ocsp.pid xargs kill -15 || true
1112
rm ocsp.pid
13+
echo "Killing ocsp server...done."
1214
fi
1315
popd

.evergreen/tests/test-ocsp.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
# Test aws setup function for different inputs.
4+
set -eu
5+
6+
SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
7+
. $SCRIPT_DIR/../handle-paths.sh
8+
9+
if [[ $(uname -s) = "Linux" ]]; then
10+
ORCHESTRATION_FILE="ecdsa-basic-tls-ocsp-mustStaple.json"
11+
OCSP_ALGORITHM="ecdsa"
12+
SERVER_TYPE="valid-delegate"
13+
else
14+
ORCHESTRATION_FILE="rsa-basic-tls-ocsp-disableStapling.json"
15+
OCSP_ALGORITHM="rsa"
16+
SERVER_TYPE="valid"
17+
fi
18+
19+
export ORCHESTRATION_FILE
20+
export OCSP_ALGORITHM
21+
export SERVER_TYPE
22+
23+
# # Start a MongoDB server with ocsp enabled.
24+
SSL="ssl" make -C ${DRIVERS_TOOLS} run-server
25+
26+
pushd $SCRIPT_DIR/../ocsp
27+
28+
# # Start the ocsp server.
29+
bash ./setup.sh
30+
31+
# Connect to the MongoDB server.
32+
echo "Connecting to server..."
33+
TLS_OPTS=("--tls --tlsCertificateKeyFile \"${DRIVERS_TOOLS}/.evergreen/ocsp/${OCSP_ALGORITHM}/server.pem\"")
34+
TLS_OPTS+=("--tlsCAFile \"${DRIVERS_TOOLS}/.evergreen/ocsp/${OCSP_ALGORITHM}/ca.pem\"")
35+
URI="mongodb://localhost/?serverSelectionTimeoutMS=10000"
36+
# shellcheck disable=SC2068
37+
$MONGODB_BINARIES/mongosh $URI ${TLS_OPTS[@]} --eval "db.runCommand({\"ping\":1})"
38+
echo "Connecting to server... done."
39+
40+
bash ./teardown.sh
41+
42+
popd
43+
44+
make -C ${DRIVERS_TOOLS} stop-server
45+
make -C ${DRIVERS_TOOLS} test

0 commit comments

Comments
 (0)