Skip to content

Commit 767db05

Browse files
authored
Replace Jinja2 sequence checks with mapping (dictionary) checks (#189)
1 parent ad568a6 commit 767db05

File tree

15 files changed

+176
-151
lines changed

15 files changed

+176
-151
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.4.2 (Unreleased)
4+
5+
BUG FIXES:
6+
7+
Dictionaries are a sequence per Jinja2 contrary to Python's defaults (dictionaries are not a sequence in Python). The template conditionals assumed the latter.
8+
39
## 0.4.1 (October 25, 2021)
410

511
BUG FIXES:

defaults/main/template.yml

Lines changed: 64 additions & 63 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
roles:
33
- name: nginxinc.nginx
4-
version: 0.21.2
4+
version: 0.21.3
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
roles:
33
- name: nginxinc.nginx
4-
version: 0.21.2
4+
version: 0.21.3
55
- name: nginxinc.nginx_app_protect
6-
version: 0.6.1
6+
version: 0.6.2

molecule/default/converge.yml

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@
4646
daemon: true
4747
debug_points: abort
4848
env:
49-
- variable: MALLOC_OPTIONS
50-
- variable: PERL5LIB
51-
value: /data/site/modules
49+
variable: PERL5LIB
50+
value: /data/site/modules
5251
lock_file: logs/nginx.lock
5352
master_process: true
5453
pcre_jit: false
@@ -306,8 +305,10 @@
306305
cookie_domain:
307306
- domain: localhost
308307
replacement: example.com
309-
cookie_flags: false # -- set to false
310-
cookie_path: false
308+
cookie_flags: false
309+
cookie_path:
310+
path: $uri
311+
replacement: $uri
311312
force_ranges: false
312313
headers_hash_bucket_size: 64
313314
headers_hash_max_size: 512
@@ -346,6 +347,17 @@
346347
- field: Connection
347348
value: close
348349
socket_keepalive: false
350+
ssl_certificate: /etc/ssl/certs/molecule.crt
351+
ssl_certificate_key: /etc/ssl/private/molecule.key
352+
ssl_ciphers: HIGH
353+
ssl_conf_command:
354+
- Protocol TLSv1.2
355+
ssl_name: $proxy_host
356+
ssl_protocols: TLSv1.2
357+
ssl_server_name: false
358+
ssl_session_reuse: true
359+
ssl_verify: false
360+
ssl_verify_depth: 1
349361
store: false
350362
store_access:
351363
user: rw
@@ -361,23 +373,29 @@
361373
transparent: false
362374
buffer_size: 4k
363375
connect_timeout: 60s
364-
hide_header:
365-
- X-Accel-Redirect
366-
ignore_headers:
367-
- X-Accel-Redirect
376+
hide_header: X-Accel-Redirect
377+
ignore_headers: X-Accel-Redirect
368378
intercept_errors: false
369-
next_upstream:
370-
- timeout
379+
next_upstream: timeout
371380
next_upstream_timeout: 0
372381
next_upstream_tries: 0
373-
pass_header:
374-
- X-Accel-Charset
382+
pass_header: X-Accel-Charset
375383
read_timeout: 60s
376384
send_timeout: 60s
377385
set_header:
378-
- field: Accept-Encoding
379-
value: '""'
386+
field: Accept-Encoding
387+
value: '""'
380388
socket_keepalive: false
389+
ssl_certificate: /etc/ssl/certs/molecule.crt
390+
ssl_certificate_key: /etc/ssl/private/molecule.key
391+
ssl_ciphers: HIGH
392+
ssl_conf_command: Protocol TLSv1.2
393+
ssl_name: $proxy_host
394+
ssl_protocols: TLSv1.2
395+
ssl_server_name: false
396+
ssl_session_reuse: true
397+
ssl_verify: false
398+
ssl_verify_depth: 1
381399
access:
382400
allow:
383401
- all
@@ -416,9 +434,9 @@
416434
value: '"max-age=15768000; includeSubDomains"'
417435
always: true
418436
add_trailers:
419-
- name: Strict-Transport-Security
420-
value: '"max-age=15768000; includeSubDomains"'
421-
always: false
437+
name: Strict-Transport-Security
438+
value: '"max-age=15768000; includeSubDomains"'
439+
always: false
422440
expires:
423441
modified: true
424442
time: "12h"

molecule/plus/converge.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@
203203
name:
204204
- info
205205
header_set:
206-
- variable: $job
207-
name: info
206+
variable: $job
207+
name: info
208208
leeway: 0s
209209
type: nested
210210
require: jwt

templates/core.j2

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{# NGINX Core template -- ngx_core_module #}
44
{% macro main(main) %}
5-
{% if main['load_module'] is defined and main['load_module'] is sequence %}
5+
{% if main['load_module'] is defined and main['load_module'] is not mapping %}
66
{% for module in main['load_module'] if main['load_module'] is not string %}
77
load_module {{ module }};
88
{% else %}
@@ -34,7 +34,7 @@ worker_shutdown_timeout {{ main['worker_shutdown_timeout'] }};
3434
{% endif %}
3535

3636
{% if main['error_log'] is defined %}
37-
{% for log in main['error_log'] if (main['error_log'] is sequence and main['error_log'] is not string) %}
37+
{% for log in main['error_log'] if (main['error_log'] is not mapping and main['error_log'] is not string) %}
3838
error_log {{ log if log is string else log['file'] }}{{ (' ' + log['level'] | string) if log['level'] is defined }};
3939
{% else %}
4040
error_log {{ main['error_log'] if main['error_log'] is string else main['error_log']['file'] }}{{ (' ' + main['error_log']['level'] | string) if main['error_log']['level'] is defined }};
@@ -52,7 +52,7 @@ debug_points {{ main['debug_points'] }};
5252
{% endif %}
5353

5454
{% if main['env'] is defined %}
55-
{% for env in main['env'] if (main['env'] is sequence and main['env'] is not string) %}
55+
{% for env in main['env'] if (main['env'] is not mapping and main['env'] is not string) %}
5656
env {{ env if env is string else env['variable'] }}{{ ('=' + env['value'] | string) if env['value'] is defined }};
5757
{% else %}
5858
env {{ main['env'] if main['env'] is string else main['env']['variable'] }}{{ ('=' + main['env']['value'] | string) if main['env']['value'] is defined }};
@@ -70,9 +70,11 @@ pcre_jit {{ main['pcre_jit'] | ternary('on', 'off') }};
7070
{% if main['ssl_engine'] is defined %}
7171
ssl_engine {{ main['ssl_engine'] }};
7272
{% endif %}
73-
{% if main['thread_pool'] is defined %}
74-
{% for thread in main['thread_pool'] %}
73+
{% if main['thread_pool'] is defined and main['thread_pool'] is not string %}
74+
{% for thread in main['thread_pool'] if main['thread_pool'] is not mapping %}
7575
thread_pool {{ thread['name'] }} {{ ('threads=' + thread['threads'] | string) if thread['threads'] is number }}{{ (' max_queue=' + thread['max_queue'] | string) if thread['max_queue'] is defined and thread['max_queue'] is number }};
76+
{% else %}
77+
thread_pool {{ main['thread_pool']['name'] }} {{ ('threads=' + main['thread_pool']['threads'] | string) if main['thread_pool']['threads'] is number }}{{ (' max_queue=' + main['thread_pool']['max_queue'] | string) if main['thread_pool']['max_queue'] is defined and main['thread_pool']['max_queue'] is number }};
7678
{% endfor %}
7779
{% endif %}
7880
{% if main['timer_resolution'] is defined %}
@@ -90,7 +92,7 @@ accept_mutex {{ events['accept_mutex'] | ternary('on', 'off') }};
9092
{% if events['accept_mutex_delay'] is defined %}
9193
accept_mutex_delay {{ events['accept_mutex_delay'] }};
9294
{% endif %}
93-
{% if events['debug_connection'] is defined and events['debug_connection'] is sequence %}
95+
{% if events['debug_connection'] is defined and events['debug_connection'] is not mapping %}
9496
{% for connection in events['debug_connection'] if events['debug_connection'] is not string %}
9597
debug_connection {{ connection }};
9698
{% else %}

templates/http/app_protect.j2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ app_protect_policy_file {{ app_protect_waf['policy_file'] }};
1111
{% if app_protect_waf['security_log_enable'] is defined and app_protect_waf['security_log_enable'] is boolean %}
1212
app_protect_security_log_enable {{ app_protect_waf['security_log_enable'] | ternary('on', 'off') }};
1313
{% endif %}
14-
{% if app_protect_waf['security_log'] is defined %}
15-
{% for security_log in app_protect_waf['security_log'] if (app_protect_waf['security_log'] is sequence and app_protect_waf['security_log'] is not string) %}
14+
{% if app_protect_waf['security_log'] is defined and app_protect_waf['security_log'] is not string %}
15+
{% for security_log in app_protect_waf['security_log'] if app_protect_waf['security_log'] is not mapping %}
1616
app_protect_security_log {{ security_log['path'] }} {{ security_log['dest'] }};
1717
{% else %}
1818
app_protect_security_log {{ app_protect_waf['security_log']['path'] }} {{ app_protect_waf['security_log']['dest'] }};
@@ -39,7 +39,7 @@ app_protect_reconnect_period_seconds {{ app_protect_waf['reconnect_period_second
3939
{% if app_protect_waf['request_buffer_overflow_action'] is defined and app_protect_waf['request_buffer_overflow_action'] in ['pass', 'drop'] %}{# Available only in 'http' context #}
4040
app_protect_request_buffer_overflow_action {{ app_protect_waf['request_buffer_overflow_action'] }};
4141
{% endif %}
42-
{% if app_protect_waf['user_defined_signatures'] is defined and app_protect_waf['user_defined_signatures'] is sequence %}{# Available only in 'http' context #}
42+
{% if app_protect_waf['user_defined_signatures'] is defined and app_protect_waf['user_defined_signatures'] is not mapping %}{# Available only in 'http' context #}
4343
{% for signature in app_protect_waf['user_defined_signatures'] if app_protect_waf['user_defined_signatures'] is not string %}
4444
app_protect_user_defined_signatures {{ signature }};
4545
{% else %}
@@ -62,7 +62,7 @@ app_protect_dos_name {{ app_protect_dos['name'] }};
6262
{% endif %}
6363
{% if app_protect_dos['monitor'] is defined and app_protect_dos['monitor'] is mapping %}
6464
app_protect_dos_monitor uri={{ app_protect_dos['monitor']['uri'] | ternary(app_protect_dos['monitor']['uri'], app_protect_dos['monitor']) }}{{ app_protect_dos['monitor']['protocol'] | ternary((' protocol=' + app_protect_dos['monitor']['protocol'] | string), '') }}{{ app_protect_dos['monitor']['timeout'] | ternary((' timeout=' + app_protect_dos['monitor']['timeout'] | string), '') }};
65-
{% elif app_protect_dos['monitor'] is defined %}
65+
{% elif app_protect_dos['monitor'] is defined and app_protect_dos['monitor'] is string %}
6666
app_protect_dos_monitor {{ app_protect_dos['monitor'] }};
6767
{% endif %}
6868
{% if app_protect_dos['security_log_enable'] is defined and app_protect_dos['security_log_enable'] is boolean %}

templates/http/auth.j2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
{# NGINX HTTP Access -- ngx_http_access_module #}
55
{% macro access(access) %}
6-
{% if access['allow'] is defined and access['allow'] is sequence %}
6+
{% if access['allow'] is defined and access['allow'] is not mapping %}
77
{% for allow in access['allow'] if access['allow'] is not string %}
88
allow {{ allow }};
99
{% else %}
1010
allow {{ access['allow'] }};
1111
{% endfor %}
1212
{% endif %}
13-
{% if access['deny'] is defined and access['deny'] is sequence %}
13+
{% if access['deny'] is defined and access['deny'] is not mapping %}
1414
{% for deny in access['deny'] if access['deny'] is not string %}
1515
deny {{ deny }};
1616
{% else %}
@@ -48,14 +48,14 @@ auth_request_set {{ auth_request['set']['variable'] }} {{ auth_request['set']['v
4848
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) }};
4949
{% endif %}
5050
{% if auth_jwt['claim_set'] is defined %}{# 'claim_set' is only available in the 'http' context #}
51-
{% for claim in auth_jwt['claim_set'] if (auth_jwt['claim_set'] is sequence and auth_jwt['claim_set'] is not string) %}
51+
{% for claim in auth_jwt['claim_set'] if (auth_jwt['claim_set'] is not mapping and auth_jwt['claim_set'] is not string) %}
5252
auth_jwt_claim_set {{ claim['variable'] }} {{ (claim['name'] if claim['name'] is string else claim['name'] | join(' ')) }};
5353
{% else %}
5454
auth_jwt_claim_set {{ auth_jwt['claim_set']['variable'] }} {{ (auth_jwt['claim_set']['name'] if auth_jwt['claim_set']['name'] is string else auth_jwt['claim_set']['name'] | join(' ')) }};
5555
{% endfor %}
5656
{% endif %}
5757
{% if auth_jwt['header_set'] is defined %}{# 'header_set' is only available in the 'http' context #}
58-
{% for claim in auth_jwt['header_set'] if (auth_jwt['header_set'] is sequence and auth_jwt['header_set'] is not string) %}
58+
{% for claim in auth_jwt['header_set'] if (auth_jwt['header_set'] is not mapping and auth_jwt['header_set'] is not string) %}
5959
auth_jwt_header_set {{ claim['variable'] }} {{ (claim['name'] if claim['name'] is string else claim['name'] | join(' ')) }};
6060
{% else %}
6161
auth_jwt_header_set {{ auth_jwt['header_set']['variable'] }} {{ (auth_jwt['header_set']['name'] if auth_jwt['header_set']['name'] is string else auth_jwt['header_set']['name'] | join(' ')) }};

templates/http/core.j2

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@ directio_alignment {{ core['directio_alignment'] }};
6565
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' -}}
6666
{{- 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 }};
6767
{% endif %}
68-
{% if core['error_page'] is defined %}
69-
{% for page in core['error_page'] %}
68+
{% if core['error_page'] is defined and core['error_page'] is not string %}
69+
{% for page in core['error_page'] if core['error_page'] is not mapping %}
7070
error_page {{ page['code'] if page['code'] is number else page['code'] | join(' ') -}}
7171
{{- (' ' + page['response'] | string) if page['response'] is defined -}}
7272
{{- (' ' + page['uri'] | string) if page['uri'] is defined }};
73+
{% else %}
74+
error_page {{ core['error_page']['code'] if core['error_page']['code'] is number else core['error_page']['code'] | join(' ') -}}
75+
{{- (' ' + core['error_page']['response'] | string) if core['error_page']['response'] is defined -}}
76+
{{- (' ' + core['error_page']['uri'] | string) if core['error_page']['uri'] is defined }};
7377
{% endfor %}
7478
{% endif %}
7579
{% if core['etag'] is defined and core['etag'] is boolean %}
@@ -108,7 +112,7 @@ large_client_header_buffers {{ core['large_client_header_buffers']['number'] }}
108112
{% endif %}
109113
{% if core['limit_except'] is defined %}{# 'limit_except' directive is only available in the location context #}
110114
limit_except {{ core['limit_except']['method'] if core['limit_except']['method'] is string else core['limit_except']['method'] | join(' ') }} {
111-
{% if core['limit_except']['directive'] is sequence %}
115+
{% if core['limit_except']['directive'] is not mapping %}
112116
{% for directive in core['limit_except']['directive'] if core['limit_except']['directive'] is not string %}
113117
{{ directive }};
114118
{% else %}
@@ -260,10 +264,12 @@ tcp_nopush {{ core['tcp_nopush'] | ternary('on', 'off') }};
260264
{% if core['try_files']['files'] is defined %}{# 'try_files' directive is not available in the 'http' context #}
261265
try_files {{ core['try_files']['files'] if core['try_files']['files'] is string else core['try_files']['files'] | join(' ') }} {{ core['try_files']['uri'] if core['try_files']['uri'] is defined else core['try_files']['code'] if core['try_files']['code'] is defined }};
262266
{% endif %}
263-
{% if core['types'] is defined %}
267+
{% if core['types'] is defined and core['types'] is not string %}
264268
types {
265-
{% for type in core['types'] %}
269+
{% for type in core['types'] if core['types'] is not mapping %}
266270
{{ type['mime'] }} {{type['extensions'] if type['extensions'] is string else type['extensions'] | join(' ') }};
271+
{% else %}
272+
{{ core['types']['mime'] }} {{core['types']['extensions'] if core['types']['extensions'] is string else core['types']['extensions'] | join(' ') }};
267273
{% endfor %}
268274
}
269275
{% endif %}

0 commit comments

Comments
 (0)