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

Commit ae57c6b

Browse files
kevinmichaelchenfitzyjoe
authored andcommitted
Dockerize tests
1 parent 5ed9041 commit ae57c6b

File tree

7 files changed

+201
-103
lines changed

7 files changed

+201
-103
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
.idea
12
ngx_http_auth_jwt_module.so

Dockerfile

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ ENV LD_LIBRARY_PATH=/usr/local/lib
88

99
RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && \
1010
yum -y update && \
11-
yum -y groupinstall 'Development Tools' && \
12-
yum -y install pcre-devel pcre zlib-devel openssl-devel wget cmake check-devel check && \
13-
yum -y install nginx-$NGINX_VERSION
11+
yum -y groupinstall 'Development Tools' && \
12+
yum -y install pcre-devel pcre zlib-devel openssl-devel wget cmake check-devel check && \
13+
yum -y install nginx-$NGINX_VERSION
1414

1515
# for compiling for rh-nginx110
1616
# yum -y install libxml2 libxslt libxml2-devel libxslt-devel gd gd-devel perl-ExtUtils-Embed
@@ -24,40 +24,26 @@ WORKDIR /root/dl
2424
# build jansson
2525
ARG JANSSON_VERSION=2.10
2626
RUN wget https://github.com/akheron/jansson/archive/v$JANSSON_VERSION.zip && \
27-
unzip v$JANSSON_VERSION.zip && \
28-
rm v$JANSSON_VERSION.zip && \
29-
ln -sf jansson-$JANSSON_VERSION jansson && \
30-
cd /root/dl/jansson && \
31-
cmake . -DJANSSON_BUILD_SHARED_LIBS=1 -DJANSSON_BUILD_DOCS=OFF && \
32-
make && \
33-
make check && \
34-
make install
27+
unzip v$JANSSON_VERSION.zip && \
28+
rm v$JANSSON_VERSION.zip && \
29+
ln -sf jansson-$JANSSON_VERSION jansson && \
30+
cd /root/dl/jansson && \
31+
cmake . -DJANSSON_BUILD_SHARED_LIBS=1 -DJANSSON_BUILD_DOCS=OFF && \
32+
make && \
33+
make check && \
34+
make install
3535

3636
# build libjwt
3737
ARG LIBJWT_VERSION=1.9.0
3838
RUN wget https://github.com/benmcollins/libjwt/archive/v$LIBJWT_VERSION.zip && \
39-
unzip v$LIBJWT_VERSION.zip && \
40-
rm v$LIBJWT_VERSION.zip && \
41-
ln -sf libjwt-$LIBJWT_VERSION libjwt && \
42-
cd /root/dl/libjwt && \
43-
autoreconf -i && \
44-
./configure JANSSON_CFLAGS=/usr/local/include JANSSON_LIBS=/usr/local/lib && \
45-
make all && \
46-
make install
47-
48-
# get our JWT module
49-
# change this to get a specific version?
50-
#ARG TESLA_REPO_NAME=ngx-http-auth-jwt-module
51-
# ARG TESLA_REPO_URL_PREFIX=joefitz/
52-
# ARG TESLA_REPO_FILE_PREFIX=joefitz-
53-
# ARG TESLA_REPO_FILENAME=validate-authorization-header
54-
#ARG TESLA_REPO_URL_PREFIX=
55-
#ARG TESLA_REPO_FILE_PREFIX=
56-
#ARG TESLA_REPO_FILENAME=master
57-
#ADD https://github.com/TeslaGov/$TESLA_REPO_NAME/archive/${TESLA_REPO_URL_PREFIX}${TESLA_REPO_FILENAME}.zip .
58-
#RUN unzip ${TESLA_REPO_FILENAME}.zip && \
59-
# rm ${TESLA_REPO_FILENAME}.zip && \
60-
# ln -sf ${TESLA_REPO_NAME}-${TESLA_REPO_FILE_PREFIX}${TESLA_REPO_FILENAME} ${TESLA_REPO_NAME}
39+
unzip v$LIBJWT_VERSION.zip && \
40+
rm v$LIBJWT_VERSION.zip && \
41+
ln -sf libjwt-$LIBJWT_VERSION libjwt && \
42+
cd /root/dl/libjwt && \
43+
autoreconf -i && \
44+
./configure JANSSON_CFLAGS=/usr/local/include JANSSON_LIBS=/usr/local/lib && \
45+
make all && \
46+
make install
6147

6248
ADD . /root/dl/ngx-http-auth-jwt-module
6349

@@ -76,24 +62,73 @@ ADD . /root/dl/ngx-http-auth-jwt-module
7662
# ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -std=gnu99' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
7763
#
7864
#RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && \
79-
# tar -xzf nginx-$NGINX_VERSION.tar.gz && \
80-
# rm nginx-$NGINX_VERSION.tar.gz && \
81-
# ln -sf nginx-$NGINX_VERSION nginx && \
82-
# cd /root/dl/nginx && \
65+
# tar -xzf nginx-$NGINX_VERSION.tar.gz && \
66+
# rm nginx-$NGINX_VERSION.tar.gz && \
67+
# ln -sf nginx-$NGINX_VERSION nginx && \
68+
# cd /root/dl/nginx && \
8369
# ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -std=gnu99' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' && \
84-
# make modules && \
85-
# cp /root/dl/nginx/objs/ngx_http_auth_jwt_module.so /usr/lib64/nginx/modules/.
70+
# make modules && \
71+
# cp /root/dl/nginx/objs/ngx_http_auth_jwt_module.so /usr/lib64/nginx/modules/.
8672

8773
# ARG CACHEBUST=1
8874

8975
RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && \
90-
tar -xzf nginx-$NGINX_VERSION.tar.gz && \
91-
rm nginx-$NGINX_VERSION.tar.gz && \
92-
ln -sf nginx-$NGINX_VERSION nginx && \
76+
tar -xzf nginx-$NGINX_VERSION.tar.gz && \
77+
rm nginx-$NGINX_VERSION.tar.gz && \
78+
ln -sf nginx-$NGINX_VERSION nginx && \
9379
cd /root/dl/nginx && \
94-
./configure --add-dynamic-module=../ngx-http-auth-jwt-module --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -std=gnu99' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' && \
80+
./configure \
81+
--add-dynamic-module=../ngx-http-auth-jwt-module \
82+
--prefix=/usr/share/nginx \
83+
--sbin-path=/usr/sbin/nginx \
84+
--modules-path=/usr/lib64/nginx/modules \
85+
--conf-path=/etc/nginx/nginx.conf \
86+
--error-log-path=/var/log/nginx/error.log \
87+
--http-log-path=/var/log/nginx/access.log \
88+
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
89+
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
90+
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
91+
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
92+
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
93+
--pid-path=/run/nginx.pid \
94+
--lock-path=/run/lock/subsys/nginx \
95+
--user=nginx \
96+
--group=nginx \
97+
--with-file-aio \
98+
--with-ipv6 \
99+
--with-http_ssl_module \
100+
--with-http_v2_module \
101+
--with-http_realip_module \
102+
--with-http_addition_module \
103+
--with-http_xslt_module=dynamic \
104+
--with-http_image_filter_module=dynamic \
105+
--with-http_geoip_module=dynamic \
106+
--with-http_sub_module \
107+
--with-http_dav_module \
108+
--with-http_flv_module \
109+
--with-http_mp4_module \
110+
--with-http_gunzip_module \
111+
--with-http_gzip_static_module \
112+
--with-http_random_index_module \
113+
--with-http_secure_link_module \
114+
--with-http_degradation_module \
115+
--with-http_slice_module \
116+
--with-http_stub_status_module \
117+
--with-http_perl_module=dynamic \
118+
--with-mail=dynamic \
119+
--with-mail_ssl_module \
120+
--with-pcre \
121+
--with-pcre-jit \
122+
--with-stream=dynamic \
123+
--with-stream_ssl_module \
124+
--with-google_perftools_module \
125+
--with-debug \
126+
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -std=gnu99' \
127+
--with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' && \
95128
make modules && \
96-
cp /root/dl/nginx/objs/ngx_http_auth_jwt_module.so /usr/lib64/nginx/modules/.
129+
cp /root/dl/nginx/objs/ngx_http_auth_jwt_module.so /usr/lib64/nginx/modules/. && \
130+
mkdir /build && \
131+
cp /root/dl/nginx/objs/ngx_http_auth_jwt_module.so /build.
97132

98133
# Get nginx ready to run
99134
COPY resources/nginx.conf /etc/nginx/nginx.conf

Dockerfile-test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM alpine:3.7
2+
RUN apk add --no-cache bash curl
3+
COPY test.sh .
4+
CMD ["./test.sh"]

Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
SHELL += -eu
2+
3+
BLUE := \033[0;34m
4+
GREEN := \033[0;32m
5+
RED := \033[0;31m
6+
NC := \033[0m
7+
8+
DOCKER_ORG_NAME = teslagov
9+
DOCKER_IMAGE_NAME = jwt-nginx
10+
11+
.PHONY: all
12+
all:
13+
@$(MAKE) build-nginx
14+
@$(MAKE) build-test-runner
15+
@$(MAKE) start-nginx
16+
@$(MAKE) test
17+
18+
.PHONY: build-nginx
19+
build-nginx:
20+
@echo "${BLUE} Building...${NC}"
21+
@docker image build -t $(DOCKER_ORG_NAME)/$(DOCKER_IMAGE_NAME) . ; \
22+
if [ $$? -ne 0 ] ; \
23+
then echo "${RED} Build failed :(${NC}" ; \
24+
else echo "${GREEN}✓ Successfully built NGINX module ${NC}" ; fi
25+
26+
.PHONY: rebuild-nginx
27+
rebuild-nginx:
28+
@echo "${BLUE} Rebuilding...${NC}"
29+
@docker image build -t $(DOCKER_ORG_NAME)/$(DOCKER_IMAGE_NAME) . --no-cache ; \
30+
if [ $$? -ne 0 ] ; \
31+
then echo "${RED} Build failed :(${NC}" ; \
32+
else echo "${GREEN}✓ Successfully rebuilt NGINX module ${NC}" ; fi
33+
34+
.PHONY: stop-nginx
35+
stop-nginx:
36+
docker stop $(shell docker inspect --format="{{.Id}}" "$(DOCKER_IMAGE_NAME)-cont") ||:
37+
38+
.PHONY: start-nginx
39+
start-nginx:
40+
docker run --rm --name "$(DOCKER_IMAGE_NAME)-cont" -d -p 8000:8000 $(DOCKER_ORG_NAME)/$(DOCKER_IMAGE_NAME)
41+
docker cp $(DOCKER_IMAGE_NAME)-cont:/usr/lib64/nginx/modules/ngx_http_auth_jwt_module.so .
42+
43+
.PHONY: build-test-runner
44+
build-test-runner:
45+
docker image build -f Dockerfile-test -t $(DOCKER_ORG_NAME)/jwt-nginx-test-runner .
46+
47+
.PHONY: frebuild-test-runner
48+
rebuild-test-runner:
49+
docker image build -f Dockerfile-test -t $(DOCKER_ORG_NAME)/jwt-nginx-test-runner . --no-cache
50+
51+
.PHONY: test
52+
test:
53+
docker run --rm $(DOCKER_ORG_NAME)/jwt-nginx-test-runner

README.md

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
# Intro
22
This is an NGINX module to check for a valid JWT and proxy to an upstream server or redirect to a login page.
33

4-
# Build Requirements
4+
## Building and testing
5+
To build the Docker image, start NGINX, and run our Bash test against it, run
6+
```bash
7+
make
8+
```
9+
10+
When you make a change to the module, run `make rebuild-nginx`.
11+
12+
When you make a change to `test.sh`, run `make rebuild-test-runner`.
13+
14+
| Command | Description |
15+
| -------------------------- |:-------------------------------------------:|
16+
| `make build-nginx` | Builds the NGINX image |
17+
| `make rebuild-nginx` | Re-builds the NGINX image |
18+
| `make build-test-runner` | Builds the image that will run `test.sh` |
19+
| `make rebuild-test-runner` | Re-builds the image that will run `test.sh` |
20+
| `make start-nginx` | Starts the NGINX container |
21+
| `make stop-nginx` | Stops the NGINX container |
22+
| `make test` | Runs `test.sh` against the NGINX container |
23+
24+
You can re-run tests as many times as you like while NGINX is up.
25+
When you're done running tests, make sure to stop the NGINX container.
26+
27+
The Dockerfile builds all of the dependencies as well as the module,
28+
downloads a binary version of NGINX, and runs the module as a dynamic module.
29+
30+
Tests get executed in containers. This project is 100% Docker-ized.
31+
32+
## Dependencies
533
This module depends on the [JWT C Library](https://github.com/benmcollins/libjwt)
634

7-
Transitively, that library depends on a JSON Parser called [Jansson](https://github.com/akheron/jansson) as well as the OpenSSL library.
35+
Transitively, that library depends on a JSON Parser called
36+
[Jansson](https://github.com/akheron/jansson) as well as the OpenSSL library.
837

9-
# NGINX Directives
10-
This module requires several new nginx.conf directives, which can be specified in on the `main` `server` or `___location` level.
38+
## NGINX Directives
39+
This module requires several new `nginx.conf` directives,
40+
which can be specified in on the `main` `server` or `___location` level.
1141

1242
```
1343
auth_jwt_key "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF";
@@ -17,7 +47,9 @@ auth_jwt_algorithm HS256; # or RS256
1747
auth_jwt_validate_email on; # or off
1848
```
1949

20-
So, a typical use would be to specify the key and loginurl on the main level and then only turn on the locations that you want to secure (not the login page). Unauthorized requests are given 302 "Moved Temporarily" responses with a ___location of the specified loginurl.
50+
So, a typical use would be to specify the key and loginurl on the main level
51+
and then only turn on the locations that you want to secure (not the login page).
52+
Unauthorized requests are given 302 "Moved Temporarily" responses with a ___location of the specified loginurl.
2153

2254
```
2355
auth_jwt_redirect off;
@@ -28,13 +60,16 @@ If you prefer to return 401 Unauthorized, you may turn `auth_jwt_redirect` off.
2860
auth_jwt_validation_type AUTHORIZATION;
2961
auth_jwt_validation_type COOKIE=rampartjwt;
3062
```
31-
By default the authorization header is used to provide a JWT for validation. However, you may use the `auth_jwt_validation_type` configuration to specify the name of a cookie that provides the JWT.
63+
By default the authorization header is used to provide a JWT for validation.
64+
However, you may use the `auth_jwt_validation_type` configuration to specify the name of a cookie that provides the JWT.
3265

3366

3467

35-
The default algorithm is 'HS256', for symmetric key validation. Also supported is 'RS256', for RSA 256-bit public key validation.
68+
The default algorithm is 'HS256', for symmetric key validation.
69+
Also supported is 'RS256', for RSA 256-bit public key validation.
3670

37-
If using "auth_jwt_algorithm RS256;", then the 'auth_jwt_key' field must be set to your public key. That is the public key, rather than a PEM certificate. I.e.:
71+
If using "auth_jwt_algorithm RS256;", then the 'auth_jwt_key' field must be set to your public key.
72+
That is the public key, rather than a PEM certificate. I.e.:
3873

3974
```
4075
auth_jwt_key "-----BEGIN PUBLIC KEY-----
@@ -48,16 +83,10 @@ oQIDAQAB
4883
-----END PUBLIC KEY-----";
4984
```
5085

51-
52-
53-
By default, the module will attempt to validate the email address field of the JWT, then set the x-email header of the session, and will log an error if it isn't found. To disable this behavior, for instance if you are using a different user identifier property such as 'sub', set:
86+
By default, the module will attempt to validate the email address field of the JWT, then set the x-email header of the
87+
session, and will log an error if it isn't found. To disable this behavior, for instance if you are using a different
88+
user identifier property such as 'sub', set:
5489

5590
```
5691
auth_jwt_validate_email off;
5792
```
58-
59-
60-
61-
The Dockerfile builds all of the dependencies as well as the module, downloads a binary version of nginx, and runs the module as a dynamic module.
62-
63-
Have a look at build.sh, which creates the docker image and container and executes some test requests to illustrate that some pages are secured by the module and requre a valid JWT.

build.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)