Skip to content

Commit d81405c

Browse files
committed
Add reading key file name, minify docker image size
1 parent a457b98 commit d81405c

File tree

3 files changed

+94
-30
lines changed

3 files changed

+94
-30
lines changed

Dockerfile

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.n
1616
yum -y install nginx-$NGINX_VERSION
1717

1818
# for compiling for rh-nginx110
19-
# yum -y install libxml2 libxslt libxml2-devel libxslt-devel gd gd-devel perl-ExtUtils-Embed
19+
# RUN yum -y install libxml2 libxslt libxml2-devel libxslt-devel gd gd-devel perl-ExtUtils-Embed
2020

2121
# for compiling for epel7
2222
RUN yum -y install libxml2 libxslt libxml2-devel libxslt-devel gd gd-devel perl-ExtUtils-Embed geoip geoip-devel google-perftools google-perftools-devel
2323

2424
# Jansson requires new cmake
25-
RUN yum -y install cmake3 && \
26-
alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake 10 \
27-
--slave /usr/local/bin/ctest ctest /usr/bin/ctest \
28-
--slave /usr/local/bin/cpack cpack /usr/bin/cpack \
29-
--slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake \
30-
--family cmake && \
31-
alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake3 20 \
32-
--slave /usr/local/bin/ctest ctest /usr/bin/ctest3 \
33-
--slave /usr/local/bin/cpack cpack /usr/bin/cpack3 \
34-
--slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake3 \
35-
--family cmake
25+
# RUN yum -y install cmake3 && \
26+
# alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake 10 \
27+
# --slave /usr/local/bin/ctest ctest /usr/bin/ctest \
28+
# --slave /usr/local/bin/cpack cpack /usr/bin/cpack \
29+
# --slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake \
30+
# --family cmake && \
31+
# alternatives --install /usr/local/bin/cmake cmake /usr/bin/cmake3 20 \
32+
# --slave /usr/local/bin/ctest ctest /usr/bin/ctest3 \
33+
# --slave /usr/local/bin/cpack cpack /usr/bin/cpack3 \
34+
# --slave /usr/local/bin/ccmake ccmake /usr/bin/ccmake3 \
35+
# --family cmake
3636

3737
RUN mkdir -p /root/dl
3838
WORKDIR /root/dl
@@ -85,19 +85,19 @@ RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && \
8585
rm nginx-$NGINX_VERSION.tar.gz && \
8686
ln -sf nginx-$NGINX_VERSION nginx && \
8787
cd /root/dl/nginx && \
88-
./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-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_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-http_auth_request_module --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' && \
88+
./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-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_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-http_auth_request_module --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 -Werror=unused-variable -Wno-unused-variable -Wno-error=unused-but-set-variable -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' && \
8989
make modules && \
9090
cp /root/dl/nginx/objs/ngx_http_auth_jwt_module.so /usr/lib64/nginx/modules/.
9191

9292
# Get nginx ready to run
9393
COPY resources/nginx.conf /etc/nginx/nginx.conf
9494
COPY resources/test-jwt-nginx.conf /etc/nginx/conf.d/test-jwt-nginx.conf
95-
RUN rm -rf /usr/share/nginx/html
96-
RUN cp -r /root/dl/nginx/html /usr/share/nginx
97-
RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure
98-
RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure-rs256
99-
RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure-auth-header
100-
RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure-no-redirect
95+
# RUN rm -rf /usr/share/nginx/html
96+
# RUN cp -r /root/dl/nginx/html /usr/share/nginx
97+
# RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure
98+
# RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure-rs256
99+
# RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure-auth-header
100+
# RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure-no-redirect
101101

102102
ENTRYPOINT ["/usr/sbin/nginx"]
103103

resources/test-jwt-nginx.conf

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ server {
22
auth_jwt_key "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF";
33
auth_jwt_loginurl "https://teslagov.com";
44
auth_jwt_enabled off;
5-
auth_jwt_redirect on;
5+
auth_jwt_redirect off;
66

77
listen 8000;
88
server_name localhost;
@@ -29,21 +29,30 @@ server {
2929

3030
___location ~ ^/secure-rs256/ {
3131
auth_jwt_enabled on;
32-
auth_jwt_validation_type COOKIE=rampartjwt;
32+
auth_jwt_validation_type AUTHORIZATION;
3333
auth_jwt_algorithm RS256;
3434
auth_jwt_key "-----BEGIN PUBLIC KEY-----
35-
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwtpMAM4l1H995oqlqdMh
36-
uqNuffp4+4aUCwuFE9B5s9MJr63gyf8jW0oDr7Mb1Xb8y9iGkWfhouZqNJbMFry+
37-
iBs+z2TtJF06vbHQZzajDsdux3XVfXv9v6dDIImyU24MsGNkpNt0GISaaiqv51NM
38-
ZQX0miOXXWdkQvWTZFXhmsFCmJLE67oQFSar4hzfAaCulaMD+b3Mcsjlh0yvSq7g
39-
6swiIasEU3qNLKaJAZEzfywroVYr3BwM1IiVbQeKgIkyPS/85M4Y6Ss/T+OWi1Oe
40-
K49NdYBvFP+hNVEoeZzJz5K/nd6C35IX0t2bN5CVXchUFmaUMYk2iPdhXdsC720t
41-
BwIDAQAB
35+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoDssqQi9TL79UPYaWqEr
36+
W11InyFvIUjhOYlTv4N/1AqOBRSCOLyGrm3MI/ngqxD2MEcBYwko7SWX0TR2WJsE
37+
kFv0V+107lmALMLBBrBzBIkmxUsBUwgwbn5639k8p/zXELsBOqTQ4bdLK+cpsEUE
38+
ECZU4kNcWm7c/VNEhf0SbgYoco6IGPBR7SrhWfbFSwCbbzdYMQE39kAOExSZgEav
39+
7oMbjK82ciDWrxtl+adNIPCDAA8tv0//YuxARSDnXXUKybagooYFwLs8RhIH4WRo
40+
ZhHrF0zHJrYCR/QoHF5Q96iDHJ/gBAUAhB76w+65IrzPQHbsCC/9VJAm+DPmEcZq
41+
jwIDAQAB
4242
-----END PUBLIC KEY-----";
4343
root /usr/share/nginx;
4444
index index.html index.htm;
4545
}
4646

47+
___location ~ ^/secure-rs256-file/ {
48+
auth_jwt_enabled on;
49+
auth_jwt_validation_type AUTHORIZATION;
50+
auth_jwt_algorithm RS256;
51+
auth_jwt_filekey on;
52+
root /usr/share/nginx;
53+
index index.html index.htm;
54+
}
55+
4756
___location / {
4857
root /usr/share/nginx/html;
4958
index index.html index.htm;

src/ngx_http_auth_jwt_module.c

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
#include "ngx_http_auth_jwt_binary_converters.h"
1919
#include "ngx_http_auth_jwt_string.h"
2020

21+
#include <stdio.h>
22+
23+
const char* KEY_FILE_PATH = "/app/pub_key";
24+
2125
typedef struct {
2226
ngx_str_t auth_jwt_loginurl;
2327
ngx_str_t auth_jwt_key;
2428
ngx_flag_t auth_jwt_enabled;
2529
ngx_flag_t auth_jwt_redirect;
2630
ngx_str_t auth_jwt_validation_type;
2731
ngx_str_t auth_jwt_algorithm;
32+
ngx_flag_t auth_jwt_filekey;
2833
ngx_flag_t auth_jwt_validate_email;
2934

3035
} ngx_http_auth_jwt_loc_conf_t;
@@ -58,6 +63,13 @@ static ngx_command_t ngx_http_auth_jwt_commands[] = {
5863
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_enabled),
5964
NULL },
6065

66+
{ ngx_string("auth_jwt_filekey"),
67+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
68+
ngx_conf_set_flag_slot,
69+
NGX_HTTP_LOC_CONF_OFFSET,
70+
offsetof(ngx_http_auth_jwt_loc_conf_t, auth_jwt_filekey),
71+
NULL },
72+
6173
{ ngx_string("auth_jwt_redirect"),
6274
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
6375
ngx_conf_set_flag_slot,
@@ -177,8 +189,45 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
177189
else if ( auth_jwt_algorithm.len == sizeof("RS256") - 1 && ngx_strncmp(auth_jwt_algorithm.data, "RS256", sizeof("RS256") - 1) == 0 )
178190
{
179191
// in this case, 'Binary' is a misnomer, as it is the public key string itself
180-
keyBinary = jwtcf->auth_jwt_key.data;
181-
keylen = jwtcf->auth_jwt_key.len;
192+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to find a jwt");
193+
if (jwtcf->auth_jwt_filekey == 1)
194+
{
195+
FILE *file = fopen(KEY_FILE_PATH, "rb");
196+
197+
// Check if file exists or is correctly opened
198+
if (file == NULL)
199+
{
200+
char err[100];
201+
strcpy(err, "failed to open pub key file: ");
202+
strcat(err, KEY_FILE_PATH);
203+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, err);
204+
goto redirect;
205+
}
206+
207+
// Read file length
208+
fseek(file, 0, SEEK_END);
209+
long key_size = ftell(file);
210+
fseek(file, 0, SEEK_SET);
211+
212+
if (key_size == 0)
213+
{
214+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "invalid key file size, check the key file");
215+
goto redirect;
216+
}
217+
218+
// Read pub key
219+
char *pub_key = malloc(key_size + 1);
220+
size_t bytes_read = fread(pub_key, 1, key_size, file);
221+
fclose(file);
222+
223+
keyBinary = (u_char*)pub_key;
224+
keylen = (int)key_size;
225+
}
226+
else
227+
{
228+
keyBinary = jwtcf->auth_jwt_key.data;
229+
keylen = jwtcf->auth_jwt_key.len;
230+
}
182231
}
183232
else
184233
{
@@ -374,6 +423,7 @@ ngx_http_auth_jwt_create_loc_conf(ngx_conf_t *cf)
374423
conf->auth_jwt_enabled = (ngx_flag_t) -1;
375424
conf->auth_jwt_redirect = (ngx_flag_t) -1;
376425
conf->auth_jwt_validate_email = (ngx_flag_t) -1;
426+
conf->auth_jwt_filekey = (ngx_flag_t) -1;
377427

378428
ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, "Created Location Configuration");
379429

@@ -403,6 +453,11 @@ ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
403453
conf->auth_jwt_redirect = (prev->auth_jwt_redirect == ((ngx_flag_t) -1)) ? 0 : prev->auth_jwt_redirect;
404454
}
405455

456+
if (conf->auth_jwt_filekey == ((ngx_flag_t) -1))
457+
{
458+
conf->auth_jwt_filekey = (prev->auth_jwt_filekey == ((ngx_flag_t) -1)) ? 0 : prev->auth_jwt_filekey;
459+
}
460+
406461
return NGX_CONF_OK;
407462
}
408463

0 commit comments

Comments
 (0)