Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.

Commit 8b65452

Browse files
authored
Adding docker examples (#2)
* examples: adding docker example
1 parent 8252852 commit 8b65452

File tree

6 files changed

+159
-5
lines changed

6 files changed

+159
-5
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.idea
1+
.idea
2+
example/certs
3+
example/cloudflare_mock

Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
FROM haproxy:1.9
2+
3+
WORKDIR /root
4+
5+
RUN apt update && \
6+
apt install lua5.3 liblua5.3-dev wget make libssl-dev -y && \
7+
8+
mkdir -p /usr/local/share/lua/5.3 && \
9+
10+
wget https://github.com/haproxytech/haproxy-lua-http/archive/master.tar.gz && \
11+
tar -xf master.tar.gz -C /usr/local/share/lua/5.3 && \
12+
ln -s /usr/local/share/lua/5.3/haproxy-lua-http-master/http.lua /usr/local/share/lua/5.3/http.lua && \
13+
rm /root/master.tar.gz && \
14+
15+
wget https://github.com/rxi/json.lua/archive/v0.1.2.tar.gz && \
16+
tar -xf v0.1.2.tar.gz -C /usr/local/share/lua/5.3 && \
17+
ln -s /usr/local/share/lua/5.3/json.lua-0.1.2/json.lua /usr/local/share/lua/5.3/json.lua && \
18+
rm /root/v0.1.2.tar.gz && \
19+
20+
wget https://github.com/diegonehab/luasocket/archive/master.tar.gz && \
21+
tar -xf master.tar.gz -C /usr/local/share/lua/5.3 && \
22+
cd /usr/local/share/lua/5.3/luasocket-master && \
23+
make clean all install-both LUAINC=/usr/include/lua5.3 && \
24+
rm /root/master.tar.gz && \
25+
26+
cd /root && \
27+
wget https://github.com/wahern/luaossl/archive/rel-20190731.tar.gz && \
28+
tar -xf rel-20190731.tar.gz -C /usr/local/share/lua/5.3 && \
29+
cd /usr/local/share/lua/5.3/luaossl-rel-20190731 && \
30+
make install && \
31+
rm /root/rel-20190731.tar.gz
32+
33+
COPY ./src/base64.lua /usr/local/share/lua/5.3
34+
COPY ./src/jwtverify.lua /usr/local/share/lua/5.3

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Install the following dependencies:
1414
* [haproxy-lua-http](https://github.com/haproxytech/haproxy-lua-http)
1515
* [rxi/json](https://github.com/rxi/json.lua)
1616
* [wahern/luaossl](https://github.com/wahern/luaossl)
17+
* [diegonehab/luasocket](https://github.com/diegonehab/luasocket)
1718

1819
Extract base64.lua & jwtverify.lua to the same directory like so:
1920

@@ -37,13 +38,14 @@ Define a HAProxy backend, DNS Resolver, and ENV variables with the following nam
3738
```
3839
global
3940
lua-load /usr/local/share/lua/5.3/jwtverify.lua
40-
setenv OAUTH_JWKS_URL https://|cloudflare_jwt|/cdn-cgi/access/certs
41-
setenv OAUTH_ISSUER https://test.cloudflareaccess.com
41+
setenv OAUTH_HOST test.cloudflareaccess.com
42+
setenv OAUTH_JWKS_URL https://|cloudflare_jwt|/cdn-cgi/access/certs
43+
setenv OAUTH_ISSUER https://"${OAUTH_HOST}"
4244
4345
backend cloudflare_jwt
4446
mode http
4547
default-server inter 10s rise 2 fall 2
46-
server test.cloudflareaccess.com test.cloudflareaccess.com:443 check ssl verify required ca-file /etc/ssl/certs/ca-bundle.crt resolvers dnsresolver resolve-prefer ipv4
48+
server "${OAUTH_HOST}" "${OAUTH_HOST}":443 check resolvers dnsresolver resolve-prefer ipv4
4749
4850
resolvers dnsresolver
4951
nameserver dns1 1.1.1.1:53
@@ -60,7 +62,7 @@ Obtain your Application Audience (AUD) Tag from Cloudflare and define your backe
6062
backend my_jwt_validated_app
6163
mode http
6264
http-request deny unless { req.hdr(Cf-Access-Jwt-Assertion) -m found }
63-
http-request set-var(txn.audience) str("4714c1358e65fe4b408ad6d432a5f878f08194bdb4752441fd56faefa9b2b6f2")
65+
http-request set-var(txn.audience) str("1234567890abcde1234567890abcde1234567890abcde")
6466
http-request lua.jwtverify
6567
http-request deny unless { var(txn.authorized) -m bool }
6668
server haproxy 127.0.0.1:8080

example/docker-compose.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '3'
2+
3+
services:
4+
haproxy_cloudflare_jwt_validator:
5+
build: ../
6+
image: haproxy_cloudflare_jwt_validator:latest
7+
ports:
8+
- "8080:8080"
9+
volumes:
10+
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
11+
- ../src/jwtverify.lua:/usr/local/share/lua/5.3/jwtverify.lua
12+
- ../src/base64.lua:/usr/local/share/lua/5.3/base64.lua
13+
depends_on:
14+
- debug_http_listener
15+
- cloudflare_mock
16+
17+
debug_http_listener:
18+
image: mendhak/http-https-echo
19+
20+
cloudflare_mock:
21+
image: python:2.7
22+
volumes:
23+
- ./cloudflare_mock/cdn-cgi:/cdn-cgi
24+
expose:
25+
- "80"
26+
ports:
27+
- "8081:80"
28+
command: python -m SimpleHTTPServer 80

example/haproxy/haproxy.cfg

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This file managed by Puppet
2+
global
3+
log stdout format raw local0 debug
4+
maxconn 4096
5+
daemon
6+
lua-load /usr/local/share/lua/5.3/jwtverify.lua
7+
setenv OAUTH_HOST cloudflare_mock
8+
setenv OAUTH_JWKS_URL http://|cloudflare_jwt|/cdn-cgi/access/certs
9+
setenv OAUTH_ISSUER http://"${OAUTH_HOST}"
10+
11+
defaults
12+
log global
13+
mode http
14+
option httplog
15+
option dontlognull
16+
option forwardfor
17+
option http-server-close
18+
stats enable
19+
stats uri /haproxyStats
20+
timeout http-request 10s
21+
timeout queue 1m
22+
timeout connect 10s
23+
timeout client 1m
24+
timeout server 1m
25+
timeout check 10s
26+
27+
frontend http-in
28+
bind *:8080
29+
mode http
30+
use_backend http-backend
31+
32+
backend http-backend
33+
mode http
34+
http-request deny unless { req.hdr(Cf-Access-Jwt-Assertion) -m found }
35+
http-request set-var(txn.audience) str("1234567890abcde1234567890abcde1234567890abcde")
36+
http-request lua.jwtverify
37+
http-request deny unless { var(txn.authorized) -m bool }
38+
server debug_http_listener debug_http_listener:80 check
39+
40+
backend cloudflare_jwt
41+
mode http
42+
default-server inter 10s rise 2 fall 2
43+
server "${OAUTH_HOST}" "${OAUTH_HOST}":80 check

example/jwt_test.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+
mkdir -p cloudflare_mock/cdn-cgi/access
4+
5+
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
6+
-subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" \
7+
-keyout certs/private.key -out certs/certificate.pem
8+
9+
CERT=$(cat certs/certificate.pem)
10+
11+
jq -n --arg cert "$CERT" '{public_certs: [{kid: "1", cert: $cert}, {kid: "2", cert: $cert}]}' \
12+
> cloudflare_mock/cdn-cgi/access/certs
13+
14+
docker-compose stop
15+
docker-compose up -d
16+
17+
CLAIM='{
18+
"aud": [
19+
"1234567890abcde1234567890abcde1234567890abcde"
20+
],
21+
"email": "[email protected]",
22+
"sub": "1234567890",
23+
"name": "John Doe",
24+
"admin": true,
25+
"iss": "http://cloudflare_mock",
26+
"iat": 1593204858,
27+
"nbf": 1593204858,
28+
"exp": 3993204858,
29+
"type": "app",
30+
"identity_nonce": "11111111111",
31+
"custom": {}
32+
}'
33+
34+
while ! nc -z localhost 8080; do
35+
sleep 0.1
36+
done
37+
38+
#wait a couple of seconds for the backends to start for haproxy
39+
sleep 3
40+
41+
JWT_TOKEN=$(jwtgen -a RS256 -p certs/private.key --claims "$CLAIM")
42+
43+
curl -H "Cf-Access-Jwt-Assertion: ${JWT_TOKEN}" localhost:8080
44+
45+
docker-compose stop

0 commit comments

Comments
 (0)