]> git.openstreetmap.org Git - chef.git/blob - cookbooks/ssl/resources/certificate.rb
Update some custom resources for Chef 14 compatibility
[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 :certificate, String, :name_property => true
23 property :domains, [String, Array], :required => true
24
25 action :create do
26   node.default[:letsencrypt][:certificates][new_resource.certificate] = {
27     :domains => Array(domains)
28   }
29
30   if letsencrypt
31     certificate = letsencrypt["certificate"]
32     key = letsencrypt["key"]
33   end
34
35   if certificate
36     file "/etc/ssl/certs/#{new_resource.certificate}.pem" do
37       owner "root"
38       group "root"
39       mode 0o444
40       content certificate
41       backup false
42       manage_symlink_source false
43       force_unlink true
44     end
45
46     file "/etc/ssl/private/#{new_resource.certificate}.key" do
47       owner "root"
48       group "ssl-cert"
49       mode 0o440
50       content key
51       backup false
52       manage_symlink_source false
53       force_unlink true
54     end
55   else
56     template "/tmp/#{new_resource.certificate}.ssl.cnf" do
57       cookbook "ssl"
58       source "ssl.cnf.erb"
59       owner "root"
60       group "root"
61       mode 0o644
62       variables :domains => Array(new_resource.domains)
63       not_if do
64         ::File.exist?("/etc/ssl/certs/#{new_resource.certificate}.pem") && ::File.exist?("/etc/ssl/private/#{new_resource.certificate}.key")
65       end
66     end
67
68     execute "/etc/ssl/certs/#{new_resource.certificate}.pem" do
69       command "openssl req -x509 -newkey rsa:2048 -keyout /etc/ssl/private/#{new_resource.certificate}.key -out /etc/ssl/certs/#{new_resource.certificate}.pem -days 365 -nodes -config /tmp/#{new_resource.certificate}.ssl.cnf"
70       user "root"
71       group "ssl-cert"
72       not_if do
73         ::File.exist?("/etc/ssl/certs/#{new_resource.certificate}.pem") && ::File.exist?("/etc/ssl/private/#{new_resource.certificate}.key")
74       end
75     end
76   end
77 end
78
79 action :delete do
80   file "/etc/ssl/certs/#{new_resource.certificate}.pem" do
81     action :delete
82   end
83
84   file "/etc/ssl/private/#{new_resource.certificate}.key" do
85     action :delete
86   end
87 end
88
89 action_class do
90   def letsencrypt
91     @letsencrypt ||= search(:letsencrypt, "id:#{new_resource.certificate}").first
92   end
93 end