Skip to content

Commit 382edac

Browse files
authored
Merge pull request TeslaGov#9 from TeslaGov/joefitz/match-rh-nginx110-version
Joefitz/match rh nginx110 version
2 parents 59bd2c8 + 8a76f12 commit 382edac

File tree

3 files changed

+66
-39
lines changed

3 files changed

+66
-39
lines changed

Dockerfile

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,75 @@ FROM centos:7
22

33
LABEL maintainer="TeslaGov" email="[email protected]"
44

5+
ARG NGINX_VERSION=1.12.0
6+
57
COPY resources/nginx.repo /etc/yum.repos.d/nginx.repo
68

79
ENV LD_LIBRARY_PATH=/usr/local/lib
810

911
RUN yum -y update && \
1012
yum -y groupinstall 'Development Tools' && \
1113
yum -y install pcre-devel pcre zlib-devel openssl-devel wget cmake check-devel check && \
12-
yum -y install nginx-1.12.0
14+
yum -y install nginx-$NGINX_VERSION
15+
16+
# for compiling for rh-nginx110
17+
# yum -y install libxml2 libxslt libxml2-devel libxslt-devel gd gd-devel perl-ExtUtils-Embed
1318

1419
RUN mkdir -p /root/dl
1520
WORKDIR /root/dl
1621

17-
# get our JWT module
18-
# change this to get a specific version?
19-
RUN wget https://github.com/TeslaGov/ngx-http-auth-jwt-module/archive/master.zip && \
20-
unzip master.zip && \
21-
rm master.zip && \
22-
ln -sf ngx-http-auth-jwt-module-master ngx-http-auth-jwt-module
23-
2422
# build jansson
25-
RUN wget https://github.com/akheron/jansson/archive/v2.10.zip && \
26-
unzip v2.10.zip && \
27-
rm v2.10.zip && \
28-
ln -sf jansson-2.10 jansson && \
23+
ARG JANSSON_VERSION=2.10
24+
RUN wget https://github.com/akheron/jansson/archive/v$JANSSON_VERSION.zip && \
25+
unzip v$JANSSON_VERSION.zip && \
26+
rm v$JANSSON_VERSION.zip && \
27+
ln -sf jansson-$JANSSON_VERSION jansson && \
2928
cd /root/dl/jansson && \
3029
cmake . -DJANSSON_BUILD_SHARED_LIBS=1 -DJANSSON_BUILD_DOCS=OFF && \
3130
make && \
3231
make check && \
3332
make install
3433

3534
# build libjwt
36-
RUN wget https://github.com/benmcollins/libjwt/archive/v1.8.0.zip && \
37-
unzip v1.8.0.zip && \
38-
rm v1.8.0.zip && \
39-
ln -sf libjwt-1.8.0 libjwt && \
35+
ARG LIBJWT_VERSION=1.8.0
36+
RUN wget https://github.com/benmcollins/libjwt/archive/v$LIBJWT_VERSION.zip && \
37+
unzip v$LIBJWT_VERSION.zip && \
38+
rm v$LIBJWT_VERSION.zip && \
39+
ln -sf libjwt-$LIBJWT_VERSION libjwt && \
4040
cd /root/dl/libjwt && \
4141
autoreconf -i && \
4242
./configure JANSSON_CFLAGS=/usr/local/include JANSSON_LIBS=/usr/local/lib && \
4343
make all && \
4444
make install
4545

46+
# get our JWT module
47+
# change this to get a specific version?
48+
ARG TESLA_REPO_NAME=ngx-http-auth-jwt-module
49+
# ARG TESLA_REPO_URL_PREFIX=joefitz/
50+
# ARG TESLA_REPO_FILE_PREFIX=joefitz-
51+
# ARG TESLA_REPO_FILENAME=match-rh-nginx110-version
52+
ARG TESLA_REPO_URL_PREFIX=
53+
ARG TESLA_REPO_FILE_PREFIX=
54+
ARG TESLA_REPO_FILENAME=master
55+
ADD https://github.com/TeslaGov/$TESLA_REPO_NAME/archive/${TESLA_REPO_URL_PREFIX}${TESLA_REPO_FILENAME}.zip .
56+
RUN unzip ${TESLA_REPO_FILENAME}.zip && \
57+
rm ${TESLA_REPO_FILENAME}.zip && \
58+
ln -sf ${TESLA_REPO_NAME}-${TESLA_REPO_FILE_PREFIX}${TESLA_REPO_FILENAME} ${TESLA_REPO_NAME}
59+
60+
# after 1.11.5 use this command
61+
# ./configure --with-compat --add-dynamic-module=../ngx-http-auth-jwt-module --with-cc-opt='-std=gnu99'
4662
# build nginx module against nginx sources
47-
RUN wget http://nginx.org/download/nginx-1.12.0.tar.gz && \
48-
tar -xzf nginx-1.12.0.tar.gz && \
49-
rm nginx-1.12.0.tar.gz && \
50-
ln -sf nginx-1.12.0 nginx && \
63+
#
64+
# 1.10.2 from nginx by default use config flags... I had to add the -std=c99 and could not achieve "binary compatibility"
65+
# ./configure --add-dynamic-module=../ngx-http-auth-jwt-module --prefix=/etc/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 --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-file-aio --with-threads --with-ipv6 --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -std=c99'
66+
#
67+
# rh-nginx110 uses these config flags
68+
# ./configure --add-dynamic-module=../ngx-http-auth-jwt-module --prefix=/opt/rh/rh-nginx110/root/usr/share/nginx --sbin-path=/opt/rh/rh-nginx110/root/usr/sbin/nginx --modules-path=/opt/rh/rh-nginx110/root/usr/lib64/nginx/modules --conf-path=/etc/opt/rh/rh-nginx110/nginx/nginx.conf --error-log-path=/var/opt/rh/rh-nginx110/log/nginx/error.log --http-log-path=/var/opt/rh/rh-nginx110/log/nginx/access.log --http-client-body-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/opt/rh/rh-nginx110/lib/nginx/tmp/scgi --pid-path=/var/opt/rh/rh-nginx110/run/nginx/nginx.pid --lock-path=/var/opt/rh/rh-nginx110/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_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-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=c99' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
69+
70+
RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && \
71+
tar -xzf nginx-$NGINX_VERSION.tar.gz && \
72+
rm nginx-$NGINX_VERSION.tar.gz && \
73+
ln -sf nginx-$NGINX_VERSION nginx && \
5174
cd /root/dl/nginx && \
5275
./configure --with-compat --add-dynamic-module=../ngx-http-auth-jwt-module --with-cc-opt='-std=gnu99' && \
5376
make modules && \
@@ -58,7 +81,6 @@ COPY resources/nginx.conf /etc/nginx/nginx.conf
5881
COPY resources/test-jwt-nginx.conf /etc/nginx/conf.d/test-jwt-nginx.conf
5982
RUN cp -r /usr/share/nginx/html /usr/share/nginx/secure
6083

61-
WORKDIR /etc/nginx
62-
CMD ["nginx"]
84+
ENTRYPOINT ["/usr/sbin/nginx"]
6385

6486
EXPOSE 8000

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# build
44
DOCKER_IMAGE_NAME=jwt-nginx
55
docker build -t ${DOCKER_IMAGE_NAME} .
6-
CONTAINER_ID=$(docker run -d -p 8000:8000 ${DOCKER_IMAGE_NAME})
6+
CONTAINER_ID=$(docker run --name "${DOCKER_IMAGE_NAME}-cont" -d -p 8000:8000 ${DOCKER_IMAGE_NAME})
77

88
MACHINE_IP=`docker-machine ip`
99

src/ngx_http_auth_jwt_module.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static void * ngx_http_auth_jwt_create_loc_conf(ngx_conf_t *cf);
2222
static char * ngx_http_auth_jwt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
2323
static int hex_char_to_binary( char ch, char* ret );
2424
static int hex_to_binary( const char* str, u_char* buf, int len );
25+
static char * ngx_str_t_to_char_ptr(ngx_pool_t *pool, ngx_str_t str);
2526

2627
static ngx_command_t ngx_http_auth_jwt_commands[] = {
2728

@@ -123,14 +124,12 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
123124
}
124125

125126
// the cookie data is not necessarily null terminated... we need a null terminated character pointer
126-
jwtCookieValChrPtr = ngx_alloc(jwtCookieVal.len + 1, r->connection->log);
127-
ngx_memcpy(jwtCookieValChrPtr, jwtCookieVal.data, jwtCookieVal.len);
128-
*(jwtCookieValChrPtr+jwtCookieVal.len) = '\0';
127+
jwtCookieValChrPtr = ngx_str_t_to_char_ptr(r->pool, jwtCookieVal);
129128

130129
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "rampartjwt: %s %d", jwtCookieValChrPtr, jwtCookieVal.len);
131130

132131
// convert key from hex to binary
133-
keyBinary = ngx_alloc(jwtcf->auth_jwt_key.len / 2, r->connection->log);
132+
keyBinary = ngx_palloc(r->pool, jwtcf->auth_jwt_key.len / 2);
134133
if (0 != hex_to_binary((char *)jwtcf->auth_jwt_key.data, keyBinary, jwtcf->auth_jwt_key.len))
135134
{
136135
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to turn hex key into binary");
@@ -207,29 +206,24 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
207206
uri.len = request_uri_var->len;
208207
ngx_memcpy(uri.data, request_uri_var->data, request_uri_var->len);
209208

210-
211-
char * tmp = ngx_alloc(uri.len + 1, r->connection->log);
212-
ngx_memcpy(tmp, uri.data, uri.len);
213-
*(tmp+uri.len) = '\0';
214-
215-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "found uri with querystring %s", tmp);
209+
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "found uri with querystring %s", ngx_str_t_to_char_ptr(r->pool, uri));
216210
}
217211
else
218212
{
219213
// fallback to the querystring without params
220214
uri = r->uri;
221215

222-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "fallback to querystring without params");
216+
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "fallback to querystring without params");
223217
}
224218

225219
// escape the URI
226-
escaped_len = 2 * ngx_escape_uri(NULL, uri.data, uri.len, NGX_ESCAPE_URI) + uri.len;
220+
escaped_len = 2 * ngx_escape_uri(NULL, uri.data, uri.len, NGX_ESCAPE_ARGS) + uri.len;
227221
uri_escaped.data = ngx_palloc(r->pool, escaped_len);
228222
uri_escaped.len = escaped_len;
229-
ngx_escape_uri(uri_escaped.data, uri.data, uri.len, NGX_ESCAPE_URI);
223+
ngx_escape_uri(uri_escaped.data, uri.data, uri.len, NGX_ESCAPE_ARGS);
230224

231225
r->headers_out.___location->value.len = loginlen + sizeof("?return_url=") - 1 + strlen(scheme) + sizeof("://") - 1 + server.len + uri_escaped.len;
232-
return_url = ngx_alloc(r->headers_out.___location->value.len, r->connection->log);
226+
return_url = ngx_palloc(r->pool, r->headers_out.___location->value.len);
233227
ngx_memcpy(return_url, jwtcf->auth_jwt_loginurl.data, jwtcf->auth_jwt_loginurl.len);
234228
int return_url_idx = jwtcf->auth_jwt_loginurl.len;
235229
ngx_memcpy(return_url+return_url_idx, "?return_url=", sizeof("?return_url=") - 1);
@@ -244,7 +238,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
244238
return_url_idx += uri_escaped.len;
245239
r->headers_out.___location->value.data = (u_char *)return_url;
246240

247-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "return_url: %s", return_url);
241+
// ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "return_url: %s", ngx_str_t_to_char_ptr(r->pool, r->headers_out.___location->value));
248242
}
249243
else
250244
{
@@ -335,7 +329,8 @@ hex_char_to_binary( char ch, char* ret )
335329
}
336330

337331
static int
338-
hex_to_binary( const char* str, u_char* buf, int len ) {
332+
hex_to_binary( const char* str, u_char* buf, int len )
333+
{
339334
u_char
340335
*cpy = buf;
341336
char
@@ -357,3 +352,13 @@ hex_to_binary( const char* str, u_char* buf, int len ) {
357352
return 0;
358353
}
359354

355+
/** copies an nginx string structure to a newly allocated character pointer */
356+
static char* ngx_str_t_to_char_ptr(ngx_pool_t *pool, ngx_str_t str)
357+
{
358+
char* char_ptr = ngx_palloc(pool, str.len + 1);
359+
ngx_memcpy(char_ptr, str.data, str.len);
360+
*(char_ptr + str.len) = '\0';
361+
return char_ptr;
362+
}
363+
364+

0 commit comments

Comments
 (0)