Skip to content

Commit 895461a

Browse files
Bryce LynnSean OMeara
Bryce Lynn
authored and
Sean OMeara
committed
[COOK-3691] - add LWRP for openvpn_conf
Signed-off-by: Sean OMeara <[email protected]>
1 parent 2437c08 commit 895461a

File tree

7 files changed

+174
-27
lines changed

7 files changed

+174
-27
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ These attributes are set by the cookbook by default.
4747
* `node["openvpn"]["signing_ca_key"]` - CA key for signing, default `/etc/openvpn/keys/ca.key`
4848
* `node["openvpn"]["routes"]` - Array of routes to add as `push` statements in the server.conf. Default is empty.
4949
* `node["openvpn"]["script_security"]` - Script Security setting to use in server config. Default is 1. The "up" script will not be included in the configuration if this is 0 or 1. Set it to 2 to use the "up" script.
50+
* `node["openvpn"]["configure_default_server"]` - Boolean. Set this to false if you want to create all of your "conf" files with the LWRP.
5051
* `node["openvpn"]["push"]` - DEPRECATED: Use `routes` above. If you're still using this in your roles, the recipe will append to `routes` attribute.
5152

5253
The following attributes are used to populate the `easy-rsa` vars file. Defaults are the same as the vars file that ships with OpenVPN.
@@ -132,6 +133,11 @@ To further customize the server configuration, there are two templates that can
132133
The first is the OpenVPN server configuration file. Modify to suit your needs for more advanced features of [OpenVPN](http://openvpn.net). The second is an `up` script run when OpenVPN starts. This is where you can add firewall rules, enable IP forwarding and other OS network settings required for OpenVPN. Attributes in the cookbook are provided as defaults, you can add more via the openvpn role if you need them.
133134
134135
136+
Using the LWRP
137+
--------------
138+
To create (possibly multiple) "conf" files on a server, use openvpn_conf "name". See the conf.rb file in the resources directory to find the supported attributes, or add some of your own. If you don't want to use the default "server.conf" from the default recipe, set `node["openvpn"]["configure_default_server"]` to false, then use the LWRP to configure as many as you like.
139+
140+
135141
SSL Certificates
136142
----------------
137143
Some of the easy-rsa tools are copied to /etc/openvpn/easy-rsa to provide the minimum to generate the certificates using the default and users recipes. We provide a Rakefile to make it easier to generate client certificate sets if you're not using the data bags above. To generate new client certificates you will need `rake` installed (either as a gem or a package), then run:

attributes/default.rb

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
default['openvpn']['signing_ca_cert'] = "#{node["openvpn"]["key_dir"]}/ca.crt"
3131
default['openvpn']['routes'] = []
3232
default['openvpn']['script_security'] = 1
33+
# set this to false if you want to just use the lwrp
34+
default['openvpn']['configure_default_server'] = true
3335
default['openvpn']['user'] = 'nobody'
3436
default['openvpn']['group'] = case node['platform_family']
3537
when 'rhel'

metadata.rb

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@
9292
:default => '1',
9393
:recipes => ['openvpn::default']
9494

95+
attribute 'openvpn/configure_default_server',
96+
:display_name => 'Configure Default Server',
97+
:description => 'Boolean to determine whether the default recipe will create a "conf" file for the default server. Set to false if you want to use only the LWRP to create the conf files.',
98+
:default => 'true',
99+
:recipes => ['openvpn::default']
100+
95101
attribute 'openvpn/key/ca_expire',
96102
:display_name => 'OpenVPN Root CA Expiry',
97103
:description => 'In how many days should the root CA key expire',

providers/conf.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#
2+
# Cookbook Name:: openvpn
3+
# Provider:: conf
4+
#
5+
# Copyright 2013, Tacit Knowledge, Inc.
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+
use_inline_resources
20+
21+
action :create do
22+
vars = {
23+
:log => new_resource.log, :port => new_resource.port,
24+
:proto => new_resource.proto, :type => new_resource.type,
25+
:local => new_resource.local, :routes => new_resource.routes,
26+
:script_security => new_resource.script_security,
27+
:key_dir => new_resource.key_dir, :key_size => new_resource.key_size,
28+
:subnet => new_resource.subnet, :netmask => new_resource.netmask,
29+
:user => new_resource.user, :group => new_resource.group,
30+
:verb => new_resource.verb, :mute => new_resource.mute,
31+
:dhcp_dns => new_resource.dhcp_dns, :tls_key => new_resource.tls_key,
32+
:dhcp_domain => new_resource.dhcp_domain,
33+
:duplicate_cn => new_resource.duplicate_cn,
34+
:interface_num => new_resource.interface_num,
35+
:client_subnet_route => new_resource.client_subnet_route,
36+
:max_clients => new_resource.max_clients,
37+
:status_log => new_resource.status_log,
38+
:plugins => new_resource.plugins
39+
}
40+
41+
template "/etc/openvpn/#{new_resource.name}.conf" do
42+
source 'server.conf.erb'
43+
owner 'root'
44+
group 'root'
45+
mode 0644
46+
variables vars
47+
end
48+
end
49+
50+
action :delete do
51+
file "/etc/openvpn/#{new_resource.name}.conf" do
52+
action :delete
53+
end
54+
end

recipes/default.rb

+15-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,21 @@
119119
not_if { ::File.exists?("#{key_dir}/server.crt") }
120120
end
121121

122-
template '/etc/openvpn/server.conf' do
123-
source 'server.conf.erb'
124-
owner 'root'
125-
group 'root'
126-
mode '0644'
122+
openvpn_conf 'server' do
123+
port node['openvpn']['port']
124+
proto node['openvpn']['proto']
125+
type node['openvpn']['type']
126+
local node['openvpn']['local']
127+
routes node['openvpn']['routes']
128+
script_security node['openvpn']['script_security']
129+
key_dir node['openvpn']['key_dir']
130+
key_size node['openvpn']['key']['size']
131+
subnet node['openvpn']['subnet']
132+
netmask node['openvpn']['netmask']
133+
user node['openvpn']['user']
134+
group node['openvpn']['group']
135+
log node['openvpn']['log']
136+
only_if { node['openvpn']['configure_default_server'] }
127137
notifies :restart, 'service[openvpn]'
128138
end
129139

resources/conf.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#
2+
# Cookbook Name:: openvpn
3+
# Resource:: conf
4+
#
5+
# Copyright 2013, Tacit Knowledge, Inc.
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+
actions :create, :delete
20+
default_action :create
21+
22+
attribute :name, :kind_of => String, :name_attribute => true
23+
attribute :port, :kind_of => String
24+
attribute :proto, :kind_of => String
25+
attribute :type, :kind_of => String
26+
attribute :local, :kind_of => String
27+
attribute :routes, :kind_of => Array
28+
attribute :script_security, :kind_of => Integer
29+
attribute :key_dir, :kind_of => String
30+
attribute :key_size, :kind_of => Integer
31+
attribute :subnet, :kind_of => String
32+
attribute :netmask, :kind_of => String
33+
attribute :user, :kind_of => String
34+
attribute :group, :kind_of => String
35+
attribute :log, :kind_of => String
36+
attribute :verb, :kind_of => Integer, :default => 1
37+
attribute :mute, :kind_of => Integer, :default => 10
38+
attribute :dhcp_dns, :kind_of => String
39+
attribute :dhcp_domain, :kind_of => String
40+
attribute :tls_key, :kind_of => String
41+
attribute :duplicate_cn, :kind_of => [TrueClass, FalseClass], :default => false
42+
attribute :interface_num, :kind_of => Integer
43+
attribute :client_subnet_route, :kind_of => String
44+
attribute :max_clients, :kind_of => Integer
45+
attribute :status_log, :kind_of => String, :default => '/etc/openvpn/openvpn-status.log'
46+
attribute :plugins, :kind_of => Array, :default => []

templates/default/server.conf.erb

+45-22
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,73 @@
22
#
33
# Generated by Chef - local changes will be overwritten
44

5-
port <%= node['openvpn']['port'] %>
6-
proto <%= node['openvpn']['proto'] %>
7-
<% if node['openvpn']['type'] == 'server-bridge' -%>
8-
dev tap
5+
port <%= @port %>
6+
proto <%= @proto %>
7+
<% if @type == "server-bridge" -%>
8+
dev tap<%= @interface_num %>
99
<% else -%>
10-
dev tun
10+
dev tun<%= @interface_num %>
11+
<% end -%>
12+
<% @plugins.each do |p| -%>
13+
plugin <%= p %>
1114
<% end -%>
1215
keepalive 10 120
16+
<% if @max_clients -%>
17+
max-clients <%= @max_clients %>
18+
<% end -%>
1319
comp-lzo
14-
local <%= node['openvpn']['local'] %>
15-
<% if node['openvpn']['routes'] -%>
16-
<% node['openvpn']['routes'].each do |route| -%>
20+
local <%= @local %>
21+
<% if @routes -%>
22+
<% @routes.each do |route| -%>
1723
<%= route %>
1824
<% end -%>
1925
<% end -%>
20-
<% if node['openvpn']['script_security'] > 1 -%>
26+
<% if @script_security > 1 -%>
2127
up /etc/openvpn/server.up.sh
2228
<% end -%>
2329

30+
<% if @dhcp_dns -%>
31+
push "dhcp-option DNS <%= @dhcp_dns %>"
32+
<% end -%>
33+
<% if @dhcp_domain -%>
34+
push "dhcp-option DOMAIN <%= @dhcp_domain %>"
35+
<% end -%>
36+
<% if @duplicate_cn -%>
37+
duplicate-cn
38+
<% end -%>
39+
<% if @client_subnet_route -%>
40+
client-config-dir ccd
41+
route <%= @client_subnet_route %>
42+
<% end -%>
43+
2444
# Keys and certificates.
25-
ca <%= node['openvpn']['key_dir'] %>/ca.crt
26-
key <%= node['openvpn']['key_dir'] %>/server.key # This file should be kept secret.
27-
cert <%= node['openvpn']['key_dir'] %>/server.crt
28-
dh <%= node['openvpn']['key_dir'] %>/dh<%= node['openvpn']['key']['size'] %>.pem
45+
ca <%= @key_dir %>/ca.crt
46+
key <%= @key_dir %>/server.key # This file should be kept secret.
47+
cert <%= @key_dir %>/server.crt
48+
dh <%= @key_dir %>/dh<%= @key_size %>.pem
49+
<% if @tls_key -%>
50+
tls-auth <%= @tls_key %> 0
51+
<% end -%>
2952

3053
ifconfig-pool-persist /etc/openvpn/ipp.txt
3154

32-
<% if node['openvpn']['type'] == 'server' -%>
33-
<%= node['openvpn']['type'] %> <%= node['openvpn']['subnet'] %> <%= node['openvpn']['netmask'] %>
55+
<% if @type == "server" -%>
56+
<%= @type %> <%= @subnet %> <%= @netmask %>
3457
<% end -%>
3558

36-
user <%= node['openvpn']['user'] %>
37-
group <%= node['openvpn']['group'] %>
59+
user <%= @user %>
60+
group <%= @group %>
3861

3962
# avoid accessing certain resources on restart
4063
persist-key
4164
persist-tun
4265

4366
# current client connections
44-
status /etc/openvpn/openvpn-status.log
67+
status <%= @status_log %>
4568

4669
# logging settings.
47-
log-append <%= node['openvpn']['log'] %>
48-
verb 1 # don't spam the log with messages.
49-
mute 10 # suppress identical messages > 10 occurances.
70+
log-append <%= @log %>
71+
verb <%= @verb %> # don't spam the log with messages.
72+
mute <%= @mute %> # suppress identical messages > 10 occurances.
5073

51-
script-security <%= node['openvpn']['script_security'] %>
74+
script-security <%= @script_security %>

0 commit comments

Comments
 (0)