]> git.openstreetmap.org Git - chef.git/blob - cookbooks/ssl/resources/certificate.rb
Force unlinking of certificates
[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       force_unlink true
45     end
46
47     file "/etc/ssl/private/#{name}.key" do
48       owner "root"
49       group "ssl-cert"
50       mode 0o440
51       content key
52       backup false
53       manage_symlink_source false
54       force_unlink true
55     end
56   elsif fallback_certificate
57     link "/etc/ssl/certs/#{name}.pem" do
58       to "#{fallback_certificate}.pem"
59     end
60
61     link "/etc/ssl/private/#{name}.key" do
62       to "#{fallback_certificate}.key"
63     end
64   else
65     template "/tmp/#{name}.ssl.cnf" do
66       cookbook "ssl"
67       source "ssl.cnf.erb"
68       owner "root"
69       group "root"
70       mode 0o644
71       variables :domains => Array(new_resource.domains)
72       not_if do
73         ::File.exist?("/etc/ssl/certs/#{new_resource.name}.pem") && ::File.exist?("/etc/ssl/private/#{new_resource.name}.key")
74       end
75     end
76
77     execute "/etc/ssl/certs/#{name}.pem" do
78       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"
79       user "root"
80       group "ssl-cert"
81       not_if do
82         ::File.exist?("/etc/ssl/certs/#{new_resource.name}.pem") && ::File.exist?("/etc/ssl/private/#{new_resource.name}.key")
83       end
84     end
85   end
86 end
87
88 action :delete do
89   file "/etc/ssl/certs/#{name}.pem" do
90     action :delete
91   end
92
93   file "/etc/ssl/private/#{name}.key" do
94     action :delete
95   end
96 end
97
98 def letsencrypt
99   @letsencrypt ||= search(:letsencrypt, "id:#{name}").first
100 end