Skip to content

Commit fc0b15c

Browse files
committed
fix(config): modify template to parse multi-level blocks correctly
1 parent 2ac89f4 commit fc0b15c

3 files changed

Lines changed: 55 additions & 13 deletions

File tree

caddy/files/default/Caddyfile.tmpl.jinja

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
{%- for k, v in value.items() -%}
88
{%- if v is number or v is string -%}
99
{{ server_config([v], k, ind) }}
10-
{%- elif v|length() > 0 and (v[0] is number or v[0] is string) -%}
11-
{{ server_config(v, k, ind) }}
12-
{%- else -%}
10+
{%- elif v is iterable and v is not string -%}
1311
{{ '\t' * ind }}{{ k }}{{ operator }}{{ '{' }}
1412
{{ server_config(v, ind = ind + ind_increment) }}
1513
{{- '\t' * ind }}{{ '}' }}{{ lb }}
14+
{%- elif value is iterable and value is not string -%}
15+
{{ server_config(value, key, ind + ind_increment) }}
1616
{%- endif -%}
1717
{%- endfor -%}
18-
{%- elif value is iterable -%}
19-
{{ server_config(value, key, ind + ind_increment) }}
2018
{%- endif -%}
2119
{%- endfor -%}
2220
{%- endmacro -%}
@@ -32,4 +30,4 @@
3230
}
3331
# Vhosts
3432
# https://caddyserver.com/docs/caddyfile
35-
{{ server_config(caddy['servers']) }}
33+
{{ server_config(caddy['servers']) }}

test/integration/default/controls/config.rb

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
'File managed by Salt at <salt://caddy/files/default/Caddyfile.tmpl.jinja>'
1414
)
1515
end
16+
# rubocop:disable Lint/RedundantCopDisableDirective
17+
# rubocop:disable Layout/LineLength
1618
its('content') do
17-
# rubocop:disable Lint/RedundantCopDisableDirective
18-
# rubocop:disable Layout/LineLength
1919
should match(
2020
"{\n"\
2121
"\temail webmaster@example.net\n"\
@@ -48,6 +48,7 @@
4848
its('content') do
4949
should match(
5050
"example.net {\n"\
51+
"\tencode gzip\n"\
5152
"\thandle_path /\\* {\n"\
5253
"\t\troot \\* /var/www/example.net/\n"\
5354
"\t\tfile_server\n"\
@@ -58,8 +59,36 @@
5859
"\t}\n"\
5960
'}'
6061
)
61-
# rubocop:enable Layout/LineLength
62-
# rubocop:enable Lint/RedundantCopDisableDirective
6362
end
63+
its('content') do
64+
should match(
65+
"multi.level.example.net {\n"\
66+
"\tencode gzip\n"\
67+
"\troute /path/\\* {\n"\
68+
"\t\tbasicauth \\* {\n"\
69+
"\t\t\tuser JDJhJDE0JFJxLjBBTXhVTHF2VHBoVU54LjFXQnVEQkZYdFhlVmUzSmxRL3VrQVcwS05IQnVIS2FBREIy\n"\
70+
"\t\t}\n"\
71+
"\t\troot \\* /var/www/html/\n"\
72+
"\t\ttry_files {path} /path/to/index.cgi\n"\
73+
"\t\t@cgi path \\*.cgi\n"\
74+
"\t\thandle /path/to/index.cgi {\n"\
75+
"\t\t\treverse_proxy @cgi unix//run/fcgiwrap.socket {\n"\
76+
"\t\t\t\ttransport fastcgi {\n"\
77+
"\t\t\t\t\tresolve_root_symlink\n"\
78+
"\t\t\t\t\tsplit .cgi\n"\
79+
"\t\t\t\t}\n"\
80+
"\t\t\t}\n"\
81+
"\t\t}\n"\
82+
"\t\tfile_server\n"\
83+
"\t}\n"\
84+
"\tlog {\n"\
85+
"\t\tlevel WARN\n"\
86+
"\t\toutput file /var/log/caddy/multi.level.example.net.log\n"\
87+
"\t}\n"\
88+
'}'
89+
)
90+
end
91+
# rubocop:enable Layout/LineLength
92+
# rubocop:enable Lint/RedundantCopDisableDirective
6493
end
6594
end

test/salt/pillar/default.sls

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ caddy:
1313
config:
1414
dir: /etc/caddy
1515
file: Caddyfile
16-
validate: true
17-
format: true
1816
global_options:
1917
- email: webmaster@example.net
2018
- auto_https: 'off'
@@ -23,7 +21,6 @@ caddy:
2321
- output: file /var/log/caddy/caddy-general.log
2422
service:
2523
name: caddy
26-
xcaddy: {}
2724
servers:
2825
- localhost:
2926
- root: '* /usr/share/caddy'
@@ -33,9 +30,27 @@ caddy:
3330
- 'example.com:80':
3431
- redir: 'https://example.net'
3532
- example.net:
33+
- encode: gzip
3634
- 'handle_path /*':
3735
- root: '* /var/www/example.net/'
3836
- file_server
3937
- log:
4038
- level: WARN
4139
- output: file /var/log/caddy/example.net.log
40+
- multi.level.example.net:
41+
- encode: gzip
42+
- 'route /path/*':
43+
- 'basicauth *':
44+
- user: JDJhJDE0JFJxLjBBTXhVTHF2VHBoVU54LjFXQnVEQkZYdFhlVmUzSmxRL3VrQVcwS05IQnVIS2FBREIy
45+
- root: '* /var/www/html/'
46+
- try_files: '{path} /path/to/index.cgi'
47+
- '@cgi': 'path *.cgi'
48+
- handle /path/to/index.cgi:
49+
- reverse_proxy @cgi unix//run/fcgiwrap.socket:
50+
- transport fastcgi:
51+
- resolve_root_symlink
52+
- split: .cgi
53+
- file_server
54+
- log:
55+
- level: WARN
56+
- output: file /var/log/caddy/multi.level.example.net.log

0 commit comments

Comments
 (0)