Skip to content

Commit 06a77bc

Browse files
authored
feat/proxy-modular-attachments (#195)
1 parent b4108c4 commit 06a77bc

File tree

7 files changed

+70
-17
lines changed

7 files changed

+70
-17
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ local/*.hash
66

77
.DS_Store
88
.idea
9+
10+
# git add --force '**/*.local*'
11+
12+
*.local.caddy

config/recipes/repo/push.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
cwd path_destination
55
user node['app']['user']
66
command <<-EOH
7-
git add --all
7+
git add --all && git add --force '**/*.local*'
88
if ! git diff --quiet || ! git diff --cached --quiet; then
99
git commit --allow-empty -m "initial commit [skip ci]"
1010
git push -f origin HEAD:main

libs/broker/templates/mosquitto.conf.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
listener <%= @port %>
2+
listener 9001
3+
protocol websockets
24

35
persistence true
46
persistence_location <%= @data_dir %>

libs/proxy/attributes/default.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
default['app']['group'] = Default.group(node)
55

66
default['proxy']['dir']['app'] = '/app/proxy'
7+
default['proxy']['dir']['config'] = '/app/proxy/conf.d'
78
default['proxy']['dir']['logs'] = '/app/proxy/logs'
89

910
default['proxy']['config']['domain'] = 'lan'
11+
12+
default['proxy']['logs']['roll_size'] = '2MiB'
13+
default['proxy']['logs']['roll_keep'] = '3'
14+
default['proxy']['logs']['roll_for'] = '1d'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# http:
2+
# use_x_forwarded_for: true
3+
# trusted_proxies:
4+
# - <IP_REVERSE_PROXY> # e. g. 192.168.178.101

libs/proxy/recipes/default.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Env.dump(self, ['ip', cookbook_name], repo: cookbook_name)
22

3-
Common.directories(self, [node['proxy']['dir']['app'], node['proxy']['dir']['logs']])
3+
Common.directories(self, [node['proxy']['dir']['app'], node['proxy']['dir']['config'], node['proxy']['dir']['logs']])
44

55
package 'caddy'
66

@@ -9,22 +9,30 @@
99
node.run_state['proxy_hosts'] = Utils.proxmox(node, 'nodes/pve/lxc').map do |state|
1010
config = Utils.proxmox(node, "nodes/pve/lxc/#{state['vmid']}/config")
1111
ip = config['net0'] ? config['net0'].match(/ip=([\d\.]+)/)&.[](1) : "404"
12-
"#{state['name']}.#{node['proxy']['config']['domain']} #{ip}"
12+
"#{state['name']}.#{node['proxy']['config']['domain']} #{ip} #{state['name']}"
1313
end
1414
Logs.info(node.run_state['proxy_hosts'])
1515
end
1616
end
1717

1818
template "#{node['proxy']['dir']['app']}/Caddyfile" do
1919
source 'Caddyfile.erb'
20-
owner 'root'
21-
group 'root'
20+
owner node['app']['user']
21+
group node['app']['group']
2222
mode '0644'
23-
variables(
24-
log_dir: node['proxy']['dir']['logs'], hosts: lazy { node.run_state['proxy_hosts'] || [] } )
23+
variables( hosts: lazy { node.run_state['proxy_hosts'] || [] }, config_dir: node['proxy']['dir']['config'],
24+
log_dir: node['proxy']['dir']['logs'], logs_roll_size: node['proxy']['logs']['roll_size'],
25+
logs_roll_keep: node['proxy']['logs']['roll_keep'], logs_roll_for: node['proxy']['logs']['roll_for'] )
26+
end
27+
28+
remote_directory node['proxy']['dir']['config'] do
29+
source 'config'
30+
owner node['app']['user']
31+
group node['app']['group']
32+
mode '0664'
2533
end
2634

2735
Common.application(self, cookbook_name,
28-
exec: "/bin/caddy run --config #{node['proxy']['dir']['app']}/Caddyfile",
29-
subscribe: "template[#{node['proxy']['dir']['app']}/Caddyfile]",
36+
exec: "/bin/caddy run --config #{node['proxy']['dir']['app']}/Caddyfile --adapter caddyfile",
37+
subscribe: ["template[#{node['proxy']['dir']['app']}/Caddyfile]", "remote_directory[#{node['proxy']['dir']['config']}]"],
3038
unit: { 'Service' => { 'AmbientCapabilities' => 'CAP_NET_BIND_SERVICE' } } )

libs/proxy/templates/Caddyfile.erb

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,44 @@
55
respond @external 403
66
}
77

8-
<% @hosts.each do |entry| -%><% domain, ip = entry.split(' ') -%>
9-
<%= domain %> {
10-
reverse_proxy <%= ip %>
11-
tls internal
12-
log {
13-
output file <%= @log_dir %>/<%= domain %>.log
8+
(security_headers) {
9+
header {
10+
Strict-Transport-Security "max-age=31536000;"
11+
X-Frame-Options "DENY"
12+
X-Content-Type-Options "nosniff"
13+
Permissions-Policy "interest-cohort=()"
14+
-Server
1415
}
15-
import internal
1616
}
1717

18-
<% end -%>
18+
(proxy_headers) {
19+
header_up Host {host}
20+
header_up X-Real-IP {remote_ip}
21+
header_up X-Forwarded-For {remote_ip}
22+
header_up X-Forwarded-Proto {scheme}
23+
}
24+
25+
<% @hosts.each do |entry| -%>
26+
<% domain, upstream, hostname = entry.split(' ') -%>
27+
<%= domain %> {
28+
import security_headers
29+
import internal
30+
31+
reverse_proxy <%= upstream %> {
32+
import proxy_headers
33+
header_up X-Container-IP <%= upstream.split(':').first %>
34+
}
35+
36+
tls internal
37+
38+
log {
39+
output file <%= @log_dir %>/<%= domain %>.log {
40+
roll_size <%= @logs_roll_size %>
41+
roll_keep <%= @logs_roll_keep %>
42+
roll_keep_for <%= @logs_roll_for %>
43+
}
44+
}
45+
46+
import <%= @config_dir %>/<%= hostname %>*.caddy
47+
}
48+
<% end -%>

0 commit comments

Comments
 (0)