]> git.openstreetmap.org Git - chef.git/blob - cookbooks/ssl/resources/certificate.rb
Issue letsencrypt certificates for wordpress sites
[chef.git] / cookbooks / ssl / resources / certificate.rb
1 #
2 # Cookbook Name:: ssl
3 # Resource:: ssl_certificate
4 #
5 # Copyright 2017, OpenStreetMap Foundation
6 #
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
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 default_action :create
21
22 property :name, String
23 property :domains, [String, Array], :required => true
24 property :fallback_certificate, String
25
26 action :create do
27   node.default[:letsencrypt][:certificates][name] = {
28     :domains => Array(domains)
29   }
30
31   if letsencrypt
32     certificate = letsencrypt["certificate"]
33     key = letsencrypt["key"]
34   end
35
36   if certificate
37     file "/etc/ssl/certs/#{name}.pem" do
38       owner "root"
39       group "root"
40       mode 0o444
41       content certificate
42       backup false
43       manage_symlink_source false
44     end
45
46     file "/etc/ssl/private/#{name}.key" do
47       owner "root"
48       group "ssl-cert"
49       mode 0o440
50       content key
51       backup false
52       manage_symlink_source false
53     end
54   elsif fallback_certificate
55     link "/etc/ssl/certs/#{name}.pem" do
56       to "#{fallback_certificate}.pem"
57     end
58
59     link "/etc/ssl/private/#{name}.key" do
60       to "#{fallback_certificate}.key"
61     end
62   else
63     template "/tmp/#{name}.ssl.cnf" do
64       cookbook "ssl"
65       source "ssl.cnf.erb"
66       owner "root"
67       group "root"
68       mode 0o644
69       variables :domains => Array(new_resource.domains)
70       not_if do
71         ::File.exist?("/etc/ssl/certs/#{new_resource.name}.pem") && ::File.exist?("/etc/ssl/private/#{new_resource.name}.key")
72       end
73     end
74
75     execute "/etc/ssl/certs/#{name}.pem" do
76       command "openssl req -x509 -newkey rsa:2048 -keyout /etc/ssl/private/#{new_resource.name}.key -out /etc/ssl/certs/#{new_resource.name}.pem -days 365 -nodes -config /tmp/#{new_resource.name}.ssl.cnf"
77       user "root"
78       group "ssl-cert"
79       not_if do
80         ::File.exist?("/etc/ssl/certs/#{new_resource.name}.pem") && ::File.exist?("/etc/ssl/private/#{new_resource.name}.key")
81       end
82     end
83   end
84 end
85
86 action :delete do
87   file "/etc/ssl/certs/#{name}.pem" do
88     action :delete
89   end
90
91   file "/etc/ssl/private/#{name}.key" do
92     action :delete
93   end
94 end
95
96 def letsencrypt
97   @letsencrypt ||= search(:letsencrypt, "id:#{name}").first
98 end