Skip to content

Commit a28ff60

Browse files
authored
Transform objects to strings before conducting Jinja2 operations (#191)
1 parent 9801797 commit a28ff60

File tree

9 files changed

+63
-54
lines changed

9 files changed

+63
-54
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ BUG FIXES:
66

77
* Dictionaries are a sequence per Jinja2 contrary to Python's defaults (dictionaries are not a sequence in Python). The template conditionals assumed the latter.
88
* NAP DoS monitor directive would fail if some variables were commented out.
9+
* NGINX listen `so_keepalive` parameter was not working as intended when setting specific values.
10+
* Make sure all template objects are properly transformed into strings before doing Jinja2 operations.
11+
* Remove unnecessary parentheses.
912

1013
## 0.4.1 (October 25, 2021)
1114

molecule/default/converge.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,15 @@
513513
port: 443
514514
default_server: true
515515
ssl: false
516+
so_keepalive:
517+
keepidle: 30m
518+
keepintvl: 5
519+
keepcnt: 10
516520
- address: "[::]"
517521
port: 80
518522
default_server: true
519523
ssl: false
524+
open_file_cache: false
520525
server_name: localhost
521526
try_files:
522527
files: $uri

templates/http/auth.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ auth_request_set {{ auth_request['set']['variable'] }} {{ auth_request['set']['v
4545
{# NGINX HTTP Auth JWT -- ngx_http_auth_jwt_module #}
4646
{% macro auth_jwt(auth_jwt) %}
4747
{% if auth_jwt['enable'] is defined %}
48-
auth_jwt {{ 'off' if not auth_jwt['enable'] }}{{ auth_jwt['enable']['realm'] if auth_jwt['enable']['realm'] is defined }}{{ (' token=' + auth_jwt['enable']['token'] if auth_jwt['enable']['token'] is defined) }};
48+
auth_jwt {{ 'off' if not auth_jwt['enable'] }}{{ auth_jwt['enable']['realm'] if auth_jwt['enable']['realm'] is defined }}{{ (' token=' + auth_jwt['enable']['token'] | string) if auth_jwt['enable']['token'] is defined }};
4949
{% endif %}
5050
{% if auth_jwt['claim_set'] is defined %}{# 'claim_set' is only available in the 'http' context #}
5151
{% for claim in auth_jwt['claim_set'] if (auth_jwt['claim_set'] is not mapping and auth_jwt['claim_set'] is not string) %}

templates/http/core.j2

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ absolute_redirect {{ core['absolute_redirect'] | ternary('on', 'off') }};
77
{% endif %}
88
{% if core['aio'] is defined %}
99
aio {{ (core['aio'] | ternary('on', 'off')) if core['aio'] is boolean -}}
10-
{{- ('threads' if core['aio']['threads'] is defined and not core['aio']['threads'] is sameas false) -}}
10+
{{- 'threads' if core['aio']['threads'] is defined and not core['aio']['threads'] is sameas false -}}
1111
{{- ('=' + core['aio']['threads'] | string) if core['aio']['threads'] is not boolean }};
1212
{% endif %}
1313
{% if core['aio_write'] is defined and core['aio_write'] is boolean %}
@@ -62,8 +62,8 @@ directio {{ 'off' if not core['directio'] else core['directio'] }};
6262
directio_alignment {{ core['directio_alignment'] }};
6363
{% endif %}
6464
{% if core['disable_symlinks'] is defined %}
65-
disable_symlinks {{ (core['disable_symlinks'] | ternary('on', 'off')) if core['disable_symlinks'] is boolean else 'if_not_owner' if core['disable_symlinks'] == 'if_not_owner' -}}
66-
{{- core['disable_symlinks']['check'] if core['disable_symlinks']['check'] is defined and core['disable_symlinks']['check'] in ['on', 'if_not_owner'] }}{{ (' from=' + core['disable_symlinks']['from']) if core['disable_symlinks']['from'] is defined }};
65+
disable_symlinks {{ core['disable_symlinks'] | ternary('on', 'off') if core['disable_symlinks'] is boolean else 'if_not_owner' if core['disable_symlinks'] == 'if_not_owner' -}}
66+
{{- core['disable_symlinks']['check'] if core['disable_symlinks']['check'] is defined and core['disable_symlinks']['check'] in ['on', 'if_not_owner'] }}{{ (' from=' + core['disable_symlinks']['from'] | string) if core['disable_symlinks']['from'] is defined }};
6767
{% endif %}
6868
{% if core['error_page'] is defined and core['error_page'] is not string %}
6969
{% for page in core['error_page'] if core['error_page'] is not mapping %}
@@ -138,22 +138,23 @@ lingering_timeout {{ core['lingering_timeout'] }};
138138
{% endif %}
139139
{% if core['listen'] is defined %}{# 'listen' directive is only available in the 'server' context #}
140140
{% for listen in core['listen'] %}
141-
listen {{ listen['address'] if listen['address'] is defined }}{{ ':' if (listen['address'] is defined and listen['port'] is defined) }}{{ listen['port'] if listen['port'] is defined -}}
141+
listen {{ listen['address'] if listen['address'] is defined }}{{ ':' if listen['address'] is defined and listen['port'] is defined }}{{ listen['port'] if listen['port'] is defined -}}
142142
{{- ' default_server' if listen['default_server'] is defined and listen['default_server'] is boolean and listen['default_server'] | bool -}}
143143
{{- ' ssl' if listen['ssl'] is defined and listen['ssl'] is boolean and listen['ssl'] | bool -}}
144-
{{- ' http2' if (listen['http2'] is defined and listen['http2'] is boolean and listen['http2'] | bool) else ' spdy' if (listen['spdy'] is defined and listen['spdy'] is boolean and listen['spdy'] | bool) -}}
145-
{{- ' proxy_protocol' if (listen['proxy_protocol'] is defined and listen['proxy_protocol'] is boolean and listen['proxy_protocol'] | bool) -}}
144+
{{- ' http2' if listen['http2'] is defined and listen['http2'] is boolean and listen['http2'] | bool else ' spdy' if listen['spdy'] is defined and listen['spdy'] is boolean and listen['spdy'] | bool -}}
145+
{{- ' proxy_protocol' if listen['proxy_protocol'] is defined and listen['proxy_protocol'] is boolean and listen['proxy_protocol'] | bool -}}
146146
{{- (' setfib=' + listen['setfib'] | string) if listen['setfib'] is defined -}}
147147
{{- (' fastopen=' + listen['fastopen'] | string) if listen['fastopen'] is defined and listen['fastopen'] is number -}}
148148
{{- (' backlog=' + listen['backlog'] | string) if listen['backlog'] is defined and listen['backlog'] is number -}}
149149
{{- (' rcvbuf=' + listen['rcvbuf'] | string) if listen['rcvbuf'] is defined -}}
150150
{{- (' sndbuf=' + listen['sndbuf'] | string) if listen['sndbuf'] is defined -}}
151151
{{- (' accept_filter=' + listen['accept_filter'] | string) if listen['accept_filter'] is defined -}}
152-
{{- ' deferred' if (listen['deferred'] is defined and listen['deferred'] is boolean and listen['deferred'] | bool) -}}
153-
{{- ' bind' if (listen['bind'] is defined and listen['bind'] is boolean and listen['bind'] | bool) -}}
154-
{{- (' ipv6only=' + listen['ipv6only'] | ternary('on', 'off')) if listen['ipv6only'] is defined -}}
152+
{{- ' deferred' if listen['deferred'] is defined and listen['deferred'] is boolean and listen['deferred'] | bool -}}
153+
{{- ' bind' if listen['bind'] is defined and listen['bind'] is boolean and listen['bind'] | bool -}}
154+
{{- (' ipv6only=' + listen['ipv6only'] | ternary('on', 'off')) if listen['ipv6only'] is defined and listen['ipv6only'] is boolean -}}
155155
{{- ' reuseport' if (listen['reuseport'] is defined and listen['reuseport'] is boolean and listen['reuseport'] | bool) -}}
156-
{{- (' so_keepalive=' + (listen['so_keepalive'] | ternary('on', 'off')) if listen['so_keepalive'] is defined and listen['so_keepalive'] is boolean else ((listen['so_keepalive']['keepidle'] if listen['so_keepalive']['keepidle'] is defined else '') + ':' + (listen['so_keepalive']['keepintvl'] if listen['so_keepalive']['keepintvl'] is defined else '') + ':' + listen['so_keepalive']['keepcnt'] if listen['so_keepalive']['keepcnt'] is defined else '')) }};
156+
{{- (' so_keepalive=' + listen['so_keepalive'] | ternary('on', 'off')) if listen['so_keepalive'] is defined and listen['so_keepalive'] is boolean -}}
157+
{{- (' so_keepalive=' + (listen['so_keepalive']['keepidle'] | string if listen['so_keepalive']['keepidle'] is defined) + ':' + (listen['so_keepalive']['keepintvl'] | string if listen['so_keepalive']['keepintvl'] is defined) + ':' + (listen['so_keepalive']['keepcnt'] | string if listen['so_keepalive']['keepcnt'] is defined)) if listen['so_keepalive'] is defined and listen['so_keepalive'] is mapping }};
157158
{% endfor %}
158159
{% endif %}
159160
{% if core['log_not_found'] is defined and core['log_not_found'] is boolean %}
@@ -210,7 +211,7 @@ reset_timedout_connection {{ core['reset_timedout_connection'] | ternary('on', '
210211
{% if core['resolver']['address'] is defined %}
211212
resolver {{ core['resolver']['address'] if core['resolver']['address'] is string else core['resolver']['address'] | join(' ') -}}
212213
{{- (' valid=' + core['resolver']['valid'] | string) if core['resolver']['valid'] is defined -}}
213-
{{- (' ipv6=' + (core['resolver']['ipv6'] | ternary('on', 'off'))) if core['resolver']['ipv6'] is defined and core['resolver']['ipv6'] is boolean -}}
214+
{{- (' ipv6=' + core['resolver']['ipv6'] | ternary('on', 'off')) if core['resolver']['ipv6'] is defined and core['resolver']['ipv6'] is boolean -}}
214215
{{- (' status_zone=' + core['resolver']['status_zone'] | string) if core['resolver']['status_zone'] is defined }};
215216
{% endif %}
216217
{% if core['resolver_timeout'] is defined %}

templates/http/grpc.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{# NGINX HTTP GRPC template -- ngx_http_grpc_module #}
44
{% macro grpc(grpc) %}
55
{% if grpc['bind'] is defined %}
6-
grpc_bind {{ 'off' if not grpc['bind'] }}{{ (grpc['bind']['address']) if grpc['bind']['address'] is defined }}{{' transparent' if (grpc['bind']['transparent'] is defined and grpc['bind']['transparent'] is boolean and grpc['bind']['transparent'] | bool) }};
6+
grpc_bind {{ 'off' if not grpc['bind'] }}{{ grpc['bind']['address'] if grpc['bind']['address'] is defined }}{{' transparent' if grpc['bind']['transparent'] is defined and grpc['bind']['transparent'] is boolean and grpc['bind']['transparent'] | bool }};
77
{% endif %}
88
{% if grpc['buffer_size'] is defined %}
99
grpc_buffer_size {{ grpc['buffer_size'] }};

templates/http/modules.j2

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ expires {{ 'off' if not headers['expires'] else (headers['expires'] if headers['
9898
{% for health_check in health_check['health_checks'] %}
9999
health_check{{ (' interval=' + health_check['interval'] | string) if health_check['interval'] is defined -}}
100100
{{- (' jitter=' + health_check['jitter'] | string) if health_check['jitter'] is defined -}}
101-
{{- (' fails=' + health_check['fails'] | string) if (health_check['fails'] is defined and health_check['fails'] is number) -}}
102-
{{- (' passes=' + health_check['passes'] | string) if (health_check['passes'] is defined and health_check['passes'] is number) -}}
101+
{{- (' fails=' + health_check['fails'] | string) if health_check['fails'] is defined and health_check['fails'] is number -}}
102+
{{- (' passes=' + health_check['passes'] | string) if health_check['passes'] is defined and health_check['passes'] is number -}}
103103
{{- (' uri=' + health_check['uri'] | string) if health_check['uri'] is defined -}}
104-
{{- ' mandatory' if (health_check['mandatory'] is defined and health_check['mandatory'] is boolean and health_check['mandatory'] | bool) -}}
105-
{{- ' persistent' if (health_check['persistent'] is defined and health_check['persistent'] is boolean and health_check['persistent'] | bool) -}}
104+
{{- ' mandatory' if health_check['mandatory'] is defined and health_check['mandatory'] is boolean and health_check['mandatory'] | bool -}}
105+
{{- ' persistent' if health_check['persistent'] is defined and health_check['persistent'] is boolean and health_check['persistent'] | bool -}}
106106
{{- (' match=' + health_check['match'] | string) if health_check['match'] is defined -}}
107107
{{- (' port=' + health_check['port'] | string) if health_check['port'] is defined -}}
108-
{{- ' type=grpc' if (health_check['grpc_service'] is defined or health_check['grpc_status'] is defined) -}}
108+
{{- ' type=grpc' if health_check['grpc_service'] is defined or health_check['grpc_status'] is defined -}}
109109
{{- (' grpc_service=' + health_check['grpc_service'] | string) if health_check['grpc_service'] is defined -}}
110110
{{- (' grpc_status=' + health_check['grpc_status'] | string) if health_check['grpc_status'] is defined }};
111111
{% endfor %}
@@ -127,12 +127,12 @@ match {{ match['name'] }} {
127127
{% macro keyval(keyval) %}
128128
{% if keyval['keyvals'] is defined %}{# 'keyval' directive is only available in the 'http' context #}
129129
{% for keyval in keyval['keyvals'] %}
130-
keyval {{ keyval['key'] }} {{ keyval['variable'] }} {{ 'zone=' + keyval['zone']}};
130+
keyval {{ keyval['key'] }} {{ keyval['variable'] }} {{ 'zone=' + keyval['zone'] | string }};
131131
{% endfor %}
132132
{% endif %}
133133
{% if keyval['zones'] is defined %}{# 'keyval_zone' directive is only available in the 'http' context #}
134134
{% for zone in keyval['zones'] %}
135-
keyval_zone {{ 'zone=' + zone['name'] + ':' + zone['size'] }}{{ (' state=' + zone['state'] | string) if zone['state'] is defined }}{{ (' timeout=' + zone['timeout'] | string) if zone['timeout'] is defined }}{{ (' type=' + zone['type'] | string) if (zone['type'] is defined and zone['type'] in ['string', 'ip', 'prefix']) }}{{ ' sync' if (zone['sync'] is defined and zone['sync'] is boolean and zone['sync'] | bool) }};
135+
keyval_zone {{ 'zone=' + zone['name'] | string + ':' + zone['size'] | string }}{{ (' state=' + zone['state'] | string) if zone['state'] is defined }}{{ (' timeout=' + zone['timeout'] | string) if zone['timeout'] is defined }}{{ (' type=' + zone['type'] | string) if zone['type'] is defined and zone['type'] in ['string', 'ip', 'prefix'] }}{{ ' sync' if zone['sync'] is defined and zone['sync'] is boolean and zone['sync'] | bool }};
136136
{% endfor %}
137137
{% endif %}
138138

@@ -142,7 +142,7 @@ keyval_zone {{ 'zone=' + zone['name'] + ':' + zone['size'] }}{{ (' state=' + zon
142142
{% macro limit_req(limit_req) %}
143143
{% if limit_req['limit_reqs'] is defined %}
144144
{% for limit in limit_req['limit_reqs'] %}
145-
limit_req {{ 'zone=' + limit['zone'] }}{{ ' burst=' + limit['burst'] | string }}{{ (' nodelay' if not limit['delay'] else (' delay=' + limit['delay'] | string)) if limit['delay'] is defined }};
145+
limit_req {{ 'zone=' + limit['zone'] | string }}{{ ' burst=' + limit['burst'] | string }}{{ (' nodelay' if not limit['delay'] else (' delay=' + limit['delay'] | string)) if limit['delay'] is defined }};
146146
{% endfor %}
147147
{% endif %}
148148
{% if limit_req['dry_run'] is defined and limit_req['dry_run'] is boolean %}
@@ -156,7 +156,7 @@ limit_req_status {{ limit_req['status'] }};
156156
{% endif %}
157157
{% if limit_req['zones'] is defined %}{# 'limit_req_zone' directive is only available in the 'http' context #}
158158
{% for zone in limit_req['zones'] %}
159-
limit_req_zone {{ zone['key'] }} {{ 'zone=' + zone['name'] + ':' + zone['size'] }} {{ 'rate=' + zone['rate'] }}{{ ' sync' if zone['sync'] is defined and zone['sync'] is boolean and zone['sync'] | bool }};
159+
limit_req_zone {{ zone['key'] }} {{ 'zone=' + zone['name'] | string + ':' + zone['size'] | string }} {{ 'rate=' + zone['rate'] | string }}{{ ' sync' if zone['sync'] is defined and zone['sync'] is boolean and zone['sync'] | bool }};
160160
{% endfor %}
161161
{% endif %}
162162

@@ -166,14 +166,14 @@ limit_req_zone {{ zone['key'] }} {{ 'zone=' + zone['name'] + ':' + zone['size']
166166
{% macro log(log) %}
167167
{% if log['format'] is defined %}{# 'log_format' directive is only available in the 'http' context #}
168168
{% for format in log['format'] %}
169-
log_format {{ format['name'] }}{{ (' escape=' + format['escape']) if format['escape'] is defined and format['escape'] in ['default', 'json', 'none'] }} {{ format['format'] }};
169+
log_format {{ format['name'] }}{{ (' escape=' + format['escape'] | string) if format['escape'] is defined and format['escape'] in ['default', 'json', 'none'] }} {{ format['format'] }};
170170
{% endfor %}
171171
{% endif %}
172172
{% if log['access'] is defined and log['access'] is boolean and not log['access'] | bool %}
173173
access_log {{ 'off' }};
174174
{% elif log['access'] is defined %}
175175
{% for log in log['access'] %}
176-
access_log {{ 'off' if not log else log['path'] if log['path'] is defined }}{{ (' ' + log['format']) if log['format'] is defined -}}
176+
access_log {{ 'off' if not log else log['path'] if log['path'] is defined }}{{ (' ' + log['format'] | string) if log['format'] is defined -}}
177177
{{- (' buffer=' + log['buffer'] | string) if log['buffer'] is defined -}}
178178
{{- ' gzip' if log['gzip'] is defined and log['access']['gzip'] is boolean and log['gzip'] | bool else (' gzip=' + log['gzip'] | string) if log['gzip'] is defined and log['gzip'] is string -}}
179179
{{- (' flush=' + log['flush'] | string) if log['flush'] is defined -}}
@@ -196,11 +196,11 @@ open_log_file_cache {{ 'off' if not log['open_log_file_cache'] else ('max=' + lo
196196
{# NGINX HTTP Rewrite -- ngx_http_rewrite_module #}
197197
{% macro rewrite(rewrite) %}
198198
{% if rewrite['return'] is defined %}{# 'return' directive is not available in the 'http' context #}
199-
return {{ rewrite['return'] if (rewrite['return'] is string or rewrite['return'] is number) }}{{ rewrite['return']['code'] if rewrite['return']['code'] is defined }}{{ (' ' + rewrite['return']['text'] | string) if rewrite['return']['text'] is defined }}{{ (' ' + rewrite['return']['url']) if rewrite['return']['url'] is defined }};
199+
return {{ rewrite['return'] if (rewrite['return'] is string or rewrite['return'] is number) }}{{ rewrite['return']['code'] if rewrite['return']['code'] is defined }}{{ (' ' + rewrite['return']['text'] | string) if rewrite['return']['text'] is defined }}{{ (' ' + rewrite['return']['url'] | string) if rewrite['return']['url'] is defined }};
200200
{% endif %}
201201
{% if rewrite['rewrites'] is defined %}{# 'rewrite' directive is not available in the 'http' context #}
202202
{% for rewrite in rewrite['rewrites'] if (rewrite['rewrites'] is sequence and rewrite['rewrites'] is not string) %}
203-
rewrite {{ rewrite['regex'] }} {{ rewrite['replacement'] }}{{ (' ' + rewrite['flag']) if (rewrite['flag'] is defined and rewrite['flag'] in ['last', 'break', 'redirect', 'permanent']) }};
203+
rewrite {{ rewrite['regex'] }} {{ rewrite['replacement'] }}{{ (' ' + rewrite['flag'] | string) if rewrite['flag'] is defined and rewrite['flag'] in ['last', 'break', 'redirect', 'permanent'] }};
204204
{% endfor %}
205205
{% endif %}
206206
{% if rewrite['log'] is defined and rewrite['log'] is boolean %}

0 commit comments

Comments
 (0)