Skip to content

Commit

Permalink
Adding support for setting host headers
Browse files Browse the repository at this point in the history
Signed-off-by: Will Fisher <[email protected]>
  • Loading branch information
teknofire committed Jul 19, 2024
1 parent 2ff0b78 commit df7a871
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Policyfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# Specify a custom source for a single cookbook:
cookbook 'acep-caddy', path: '.'

default['golang']['version'] = '1.22.1'
default['golang']['version'] = '1.22.1'
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# a Supermarket.
#
# source_url 'https://github.com/<insert_org_here>/acep-caddy'
depends 'golang'
depends 'golang'
46 changes: 23 additions & 23 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,44 @@
# Copyright:: 2024, The Authors, All Rights Reserved.

if ubuntu_platform?
include_recipe 'acep-caddy::ubuntu'
include_recipe 'acep-caddy::ubuntu'
end

service 'caddy' do
action :enable
service 'caddy' do
action :enable
end

caddy_config = data_bag_item('caddy', node['caddy']['sites_data_bag'])
gcp_json = chef_vault_item('credentials', node['gcp']['service_account_vault'])

file node['gcp']['service_account_json'] do
content gcp_json["file-content"]
owner node['caddy']['user']
group node['caddy']['group']
mode '0700'
action :create
notifies :restart, 'service[caddy]', :delayed
content gcp_json['file-content']
owner node['caddy']['user']
group node['caddy']['group']
mode '0700'
action :create
notifies :restart, 'service[caddy]', :delayed
end

template '/etc/caddy/Caddyfile' do
source 'Caddyfile.erb'
owner node['caddy']['user']
group node['caddy']['group']
mode '0700'
variables acme_email: node['caddy']['acme_email'],
domains: caddy_config[:domains],
gcp_project: node['gcp'][:project],
gcp_service_account_file: node['gcp']['service_account_json']

action :create
# notifies :run, 'execute[caddy_fmt]', :immediately
notifies :restart, 'service[caddy]', :delayed
source 'Caddyfile.erb'
owner node['caddy']['user']
group node['caddy']['group']
mode '0700'
variables acme_email: node['caddy']['acme_email'],
domains: caddy_config[:domains],
gcp_project: node['gcp']['project'],
gcp_service_account_file: node['gcp']['service_account_json']

action :create
# notifies :run, 'execute[caddy_fmt]', :immediately
notifies :restart, 'service[caddy]', :delayed
end

# Take out fmt cause it will change the template config and cause chef
# To always update the config and restart caddy service

# execute 'caddy_fmt' do
# execute 'caddy_fmt' do
# command 'caddy fmt /etc/caddy/Caddyfile --overwrite'
# action :nothing
# end
# end
104 changes: 52 additions & 52 deletions recipes/ubuntu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,83 +12,83 @@
# action :add
# end

apt_repository 'xcaddy' do
uri 'https://dl.cloudsmith.io/public/caddy/xcaddy/deb/ubuntu'
components ['main']
key 'https://dl.cloudsmith.io/public/caddy/xcaddy/gpg.key'
action :add
apt_repository 'xcaddy' do
uri 'https://dl.cloudsmith.io/public/caddy/xcaddy/deb/ubuntu'
components ['main']
key 'https://dl.cloudsmith.io/public/caddy/xcaddy/gpg.key'
action :add
end

# Need to install golang in order to build the custom caddy binary
include_recipe 'golang::default'
package %w{xcaddy} do
action :install
notifies :run, 'execute[xcaddy_build]', :immediately
package %w(xcaddy) do
action :install
notifies :run, 'execute[xcaddy_build]', :immediately
end

# We're building a custom binary for caddy that includes the googleclouddns
# TODO: Create a resource for this
execute 'xcaddy_build' do
command '/usr/bin/bash -l -c "xcaddy build --with github.com/caddy-dns/googleclouddns --output /usr/bin/caddy"'
action :nothing
command '/usr/bin/bash -l -c "xcaddy build --with github.com/caddy-dns/googleclouddns --output /usr/bin/caddy"'
action :nothing
end

######################
# These items are not needed if we are using the stock caddy
######################

group node['caddy']['group'] do
action :create
system true
action :create
system true
end

user node['caddy']['user'] do
group node['caddy']['group']
manage_home true
home '/var/lib/caddy'
system true
shell '/bin/false'
action [:create, :manage]
group node['caddy']['group']
manage_home true
home '/var/lib/caddy'
system true
shell '/bin/false'
action [:create, :manage]
end

directory '/etc/caddy' do
owner node['caddy']['user']
group node['caddy']['group']
mode '0755'
action :create
owner node['caddy']['user']
group node['caddy']['group']
mode '0755'
action :create
end

directory '/var/lib/caddy' do
owner 'caddy'
group 'caddy'
mode '0750'
action :create
directory '/var/lib/caddy' do
owner 'caddy'
group 'caddy'
mode '0750'
action :create
end

systemd_unit 'caddy.service' do
content({
Unit: {
Description: 'Caddy HTTP/2 web server',
Documentation: 'https://caddyserver.com/docs/',
After: 'network.target network-online.target',
Wants: 'network-online.target',
},
Service: {
ExecStart: '/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile',
ExecReload: '/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force',
Restart: 'on-abnormal',
LimitNOFILE: 1048576,
Type: 'notify',
User: 'caddy',
Group: 'caddy',
TimeoutStopSec: '5s',
PrivateTmp: true,
ProtectSystem: 'full',
AmbientCapabilities: 'CAP_NET_ADMIN CAP_NET_BIND_SERVICE',
},
Install: {
WantedBy: 'multi-user.target',
}
})
action [:create, :enable]
content({
Unit: {
Description: 'Caddy HTTP/2 web server',
Documentation: 'https://caddyserver.com/docs/',
After: 'network.target network-online.target',
Wants: 'network-online.target',
},
Service: {
ExecStart: '/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile',
ExecReload: '/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force',
Restart: 'on-abnormal',
LimitNOFILE: 1048576,
Type: 'notify',
User: 'caddy',
Group: 'caddy',
TimeoutStopSec: '5s',
PrivateTmp: true,
ProtectSystem: 'full',
AmbientCapabilities: 'CAP_NET_ADMIN CAP_NET_BIND_SERVICE',
},
Install: {
WantedBy: 'multi-user.target',
},
})
action [:create, :enable]
end
3 changes: 3 additions & 0 deletions templates/site.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
handle @<%= @name %> {
encode gzip
reverse_proxy <%= @upstream %> {
<% if @host_header %>
header_up Host <%= @host_header %>
<% end %>
<% if @self_signed %>
transport http {
tls
Expand Down
3 changes: 2 additions & 1 deletion test/integration/data_bags/caddy/test_sites.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"name": "test_site",
"upstream": "http://localhost:8080",
"fqdn": "test.camio.lab.alaska.edu",
"self_signed": false
"self_signed": false,
"host_header": "localhost:8080"
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/default/deafult_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

describe port(443) do
it { should be_listening }
its('processes') {should include 'caddy'}
its('processes') { should include 'caddy' }
end

describe file('/etc/caddy/Caddyfile') do
it { should exist }
its('content') { should match /test-gcp-project/ }
end
end

0 comments on commit df7a871

Please sign in to comment.