This repository has been archived by the owner on Nov 23, 2017. It is now read-only.
/
default.rb
126 lines (111 loc) · 3.15 KB
/
default.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#
# Cookbook Name:: openvpn
# Recipe:: default
#
# Copyright 2009-2010, Opscode, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
routes = node['openvpn']['routes']
routes << node['openvpn'] if node['openvpn'].attribute?('push')
node.default['openvpn']['routes'] << routes.flatten!
key_dir = node["openvpn"]["key_dir"]
key_size = node["openvpn"]["key"]["size"]
package "openvpn" do
action :install
end
directory key_dir do
owner "root"
group "root"
mode 0700
end
directory "/etc/openvpn/easy-rsa" do
owner "root"
group "root"
mode 0755
end
%w{openssl.cnf pkitool vars Rakefile}.each do |f|
template "/etc/openvpn/easy-rsa/#{f}" do
source "#{f}.erb"
owner "root"
group "root"
mode 0755
end
end
template "/etc/openvpn/server.up.sh" do
source "server.up.sh.erb"
owner "root"
group "root"
mode 0755
notifies :restart, "service[openvpn]"
end
template "#{key_dir}/openssl.cnf" do
source "openssl.cnf.erb"
owner "root"
group "root"
mode 0644
end
file "#{key_dir}/index.txt" do
owner "root"
group "root"
mode 0600
action :create
end
file "#{key_dir}/serial" do
content "01"
not_if { ::File.exists?("#{key_dir}/serial") }
end
# Use unless instead of not_if otherwise OpenSSL::PKey::DH runs every time.
unless ::File.exists?("#{key_dir}/dh#{key_size}.pem")
require 'openssl'
file "#{key_dir}/dh#{key_size}.pem" do
content OpenSSL::PKey::DH.new(key_size).to_s
owner "root"
group "root"
mode 0600
end
end
bash "openvpn-initca" do
environment("KEY_CN" => "#{node['openvpn']['key']['org']} CA")
code <<-EOF
openssl req -batch -days #{node["openvpn"]["key"]["ca_expire"]} \
-nodes -new -newkey rsa:#{key_size} -sha1 -x509 \
-keyout #{node["openvpn"]["signing_ca_key"]} \
-out #{node["openvpn"]["signing_ca_cert"]} \
-config #{key_dir}/openssl.cnf
EOF
not_if { ::File.exists?(node["openvpn"]["signing_ca_cert"]) }
end
bash "openvpn-server-key" do
environment("KEY_CN" => "server")
code <<-EOF
openssl req -batch -days #{node["openvpn"]["key"]["expire"]} \
-nodes -new -newkey rsa:#{key_size} -keyout #{key_dir}/server.key \
-out #{key_dir}/server.csr -extensions server \
-config #{key_dir}/openssl.cnf && \
openssl ca -batch -days #{node["openvpn"]["key"]["ca_expire"]} \
-out #{key_dir}/server.crt -in #{key_dir}/server.csr \
-extensions server -md sha1 -config #{key_dir}/openssl.cnf
EOF
not_if { ::File.exists?("#{key_dir}/server.crt") }
end
template "/etc/openvpn/server.conf" do
source "server.conf.erb"
owner "root"
group "root"
mode 0644
notifies :restart, "service[openvpn]"
end
service "openvpn" do
action [:enable, :start]
end