2 # Cookbook:: letsencrypt
5 # Copyright:: 2017, OpenStreetMap Foundation
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # https://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
20 include_recipe "accounts"
21 include_recipe "apache"
22 include_recipe "chef::knife"
24 keys = data_bag_item("chef", "keys")
31 directory "/etc/letsencrypt" do
37 directory "/var/lib/letsencrypt" do
43 directory "/var/log/letsencrypt" do
49 directory "/srv/acme.openstreetmap.org" do
55 directory "/srv/acme.openstreetmap.org/html" do
61 ssl_certificate "acme.openstreetmap.org" do
62 domains ["acme.openstreetmap.org", "acme.osm.org"]
63 notifies :reload, "service[apache2]"
66 apache_site "acme.openstreetmap.org" do
68 directory "/srv/acme.openstreetmap.org"
71 directory "/srv/acme.openstreetmap.org/config" do
77 directory "/srv/acme.openstreetmap.org/work" do
83 directory "/srv/acme.openstreetmap.org/logs" do
89 directory "/srv/acme.openstreetmap.org/.chef" do
95 file "/srv/acme.openstreetmap.org/.chef/client.pem" do
96 content keys["letsencrypt"].join("\n")
102 cookbook_file "/srv/acme.openstreetmap.org/.chef/knife.rb" do
109 remote_directory "/srv/acme.openstreetmap.org/bin" do
119 directory "/srv/acme.openstreetmap.org/requests" do
125 certificates = search(:node, "letsencrypt:certificates").each_with_object({}) do |n, c|
126 n[:letsencrypt][:certificates].each do |name, details|
127 c[name] ||= details.merge(:nodes => [])
131 :address => n.external_ipaddress || n.internal_ipaddress
136 certificates.each do |name, details|
137 template "/srv/acme.openstreetmap.org/requests/#{name}" do
145 execute "/srv/acme.openstreetmap.org/requests/#{name}" do
147 command "/srv/acme.openstreetmap.org/requests/#{name}"
148 cwd "/srv/acme.openstreetmap.org"
151 subscribes :run, "template[/srv/acme.openstreetmap.org/requests/#{name}]"
156 Dir.glob("*", :base => "/srv/acme.openstreetmap.org/requests") do |name|
157 next if certificates.include?(name)
159 file "/srv/acme.openstreetmap.org/requests/#{name}" do
163 execute "certbot-delete-#{name}" do
164 command "/usr/bin/certbot delete --config-dir /srv/acme.openstreetmap.org/config --work-dir /srv/acme.openstreetmap.org/work --logs-dir /srv/acme.openstreetmap.org/logs --cert-name #{name}"
165 cwd "/srv/acme.openstreetmap.org"
171 template "/srv/acme.openstreetmap.org/bin/check-certificates" do
172 source "check-certificates.erb"
176 variables :certificates => certificates
179 systemd_service "letsencrypt-renew" do
180 description "Renew letsencrypt certificates"
181 exec_start "/srv/acme.openstreetmap.org/bin/renew"
183 sandbox :enable_network => true
185 "/srv/acme.openstreetmap.org/config",
186 "/srv/acme.openstreetmap.org/html",
187 "/srv/acme.openstreetmap.org/logs",
188 "/srv/acme.openstreetmap.org/work"
192 systemd_timer "letsencrypt-renew" do
193 description "Renew letsencrypt certificates"
195 on_unit_inactive_sec "12h"
198 service "letsencrypt-renew.timer" do
199 action [:enable, :start]
202 systemd_service "letsencrypt-check" do
203 description "Check letsencrypt certificates"
204 exec_start "/srv/acme.openstreetmap.org/bin/check-certificates"
206 sandbox :enable_network => true
209 systemd_timer "letsencrypt-check" do
210 description "Check letsencrypt certificates"
212 on_unit_inactive_sec "12h"
215 service "letsencrypt-check.timer" do
216 action [:enable, :start]
219 template "/etc/logrotate.d/letsencrypt" do
220 source "logrotate.erb"