Skip to content

Commit 38c6b89

Browse files
committed
Let's do regular https and mtls on the same port
1 parent f97df82 commit 38c6b89

File tree

5 files changed

+16
-29
lines changed

5 files changed

+16
-29
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ RUN set -ex \
2020
FROM node:16-alpine AS final
2121
WORKDIR /app
2222
COPY --from=build /app /app
23-
ENV HTTP_PORT=8080 HTTPS_PORT=8443 HTTPS_MTLS_PORT=8444
24-
EXPOSE $HTTP_PORT $HTTPS_PORT $HTTPS_MTLS_PORT
23+
ENV HTTP_PORT=8080 HTTPS_PORT=8443
24+
EXPOSE $HTTP_PORT $HTTPS_PORT
2525
USER 1000
2626
CMD ["node", "./index.js"]

README.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,27 +203,17 @@ The output will be 'cauliflower'.
203203

204204
## Client certificate details (mTLS)
205205

206-
There's also an HTTPS server that requests client certificates (also known as mTLS), listening on port 8444 by default.
206+
If you pass a client certificate, then the details about that certificate can be echoed back in the response body.
207207

208-
```bash
209-
docker run -p 8444:8444 --rm -t mendhak/http-https-echo:25
210-
```
211-
212-
You can then call it with curl passing a certificate and key. The client certificate will not be validated.
208+
For example, invoke using curl, passing a certificate and key. The client certificate will not be validated.
213209

214210
```bash
215-
curl -k --cert cert.pem --key privkey.pem https://localhost:8444/
211+
curl -k --cert cert.pem --key privkey.pem https://localhost:8443/
216212
```
217213

218214
The response body will contain details about the client certificate passed in.
219215

220-
You can change the port by using the `HTTPS_MTLS_PORT` environment variable.
221-
222-
```bash
223-
docker run -e HTTPS_MTLS_PORT=3333 -p 3333:3333 --rm -t mendhak/http-https-echo:25
224-
```
225-
226-
If you browse to https://localhost:8444/ in Firefox, you should get prompted to supply a client certificate as long as you have [an imported certificate by the same issuer as the server](https://superuser.com/questions/1043415/firefox-doesnt-ask-me-for-a-certificate-when-visiting-a-site-that-needs-one). If you need browser prompting to work, you'll need to follow the 'use your own certificates' section.
216+
If you browse to https://localhost:8443/ in Firefox, you won't get prompted to supply a client certificate unless you have [an imported certificate by the same issuer as the server](https://superuser.com/questions/1043415/firefox-doesnt-ask-me-for-a-certificate-when-visiting-a-site-that-needs-one). If you need browser prompting to work, you'll need to follow the 'use your own certificates' section.
227217

228218

229219
## Output

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ services:
66
environment:
77
- HTTP_PORT=8888
88
- HTTPS_PORT=9999
9-
- HTTPS_MTLS_PORT=10101
109
ports:
1110
- "8080:8888"
1211
- "8443:9999"
13-
- "8444:10101"
1412
# volumes:
1513
# - /etc/ssl/certs/ssl-cert-snakeoil.pem:/app/fullchain.pem
1614
# - /etc/ssl/private/ssl-cert-snakeoil.key:/app/privkey.pem

index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,13 @@ app.all('*', (req, res) => {
105105
const sslOpts = {
106106
key: require('fs').readFileSync('privkey.pem'),
107107
cert: require('fs').readFileSync('fullchain.pem'),
108+
requestCert: true,
109+
rejectUnauthorized: false
108110
};
109111

110112
var httpServer = http.createServer(app).listen(process.env.HTTP_PORT || 8080);
111113
var httpsServer = https.createServer(sslOpts,app).listen(process.env.HTTPS_PORT || 8443);
112-
var httpsMTlsServer = https.createServer( { requestCert: true, rejectUnauthorized: false, ...sslOpts }, app).listen(process.env.HTTPS_MTLS_PORT || 8444);
113-
console.log(`Listening on ports ${process.env.HTTP_PORT || 8080} for http, and ${process.env.HTTPS_PORT || 8443} for https, and ${process.env.HTTPS_MTLS_PORT || 8444} for https_mtls`);
114+
console.log(`Listening on ports ${process.env.HTTP_PORT || 8080} for http, and ${process.env.HTTPS_PORT || 8443} for https.`);
114115

115116
let calledClose = false;
116117

@@ -130,10 +131,8 @@ function shutDown(){
130131
calledClose = true;
131132
httpServer.close(function() {
132133
httpsServer.close(function() {
133-
httpsMTlsServer.close(function(){
134-
console.log("HTTP and HTTPS servers closed. Asking process to exit.");
135-
process.exit()
136-
})
134+
console.log("HTTP and HTTPS servers closed. Asking process to exit.");
135+
process.exit()
137136
});
138137
});
139138
}

tests.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ message " Check that mTLS server responds with client certificate details"
328328
# Might as well just reuse any cert
329329
cp ../generate-cert.sh .
330330
bash generate-cert.sh
331-
docker run -d --rm --name http-echo-tests -p 8080:8080 -p 8443:8443 -p 8444:8444 -t mendhak/http-https-echo
331+
docker run -d --rm --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
332332
sleep 5
333-
COMMON_NAME="$(curl -sk --cert cert.pem --key privkey.pem https://localhost:8444/ | jq -r '.clientCertificate.subject.CN')"
333+
COMMON_NAME="$(curl -sk --cert cert.pem --key privkey.pem https://localhost:8443/ | jq -r '.clientCertificate.subject.CN')"
334334
if [ "$COMMON_NAME" == "my.example.com" ]
335335
then
336336
passed "Client certificate details are present in the output"
@@ -339,11 +339,11 @@ else
339339
exit 1
340340
fi
341341

342-
message " Check that HTTPS server (non-MTLS) does not have any client certificate details"
343-
CLIENT_CERT="$(curl -sk --cert cert.pem --key privkey.pem https://localhost:8443/ | jq -r '.clientCertificate')"
342+
message " Check if certificate is not passed, then client certificate details are empty"
343+
CLIENT_CERT="$(curl -sk https://localhost:8443/ | jq -r '.clientCertificate')"
344344
if [ "$CLIENT_CERT" == "{}" ]
345345
then
346-
passed "Client certificate details are not present in regular HTTPS server"
346+
passed "Client certificate details are not present in the response"
347347
else
348348
failed "Client certificate details found in output? ${CLIENT_CERT}"
349349
exit 1

0 commit comments

Comments
 (0)