Skip to content

Commit 88cf4a5

Browse files
committed
Match client and server conf; improve docs (#189)
As described in issue #198, make settings in the client configs (E.G. `auth`, `cipher`, `compress`, etc) match their server counterparts. This eliminates warnings in the OpenVPN log about inconsistent option usage. In addition, document how the `openvpn_user` resource works, including showing how additional configuration options can be added to client conf files (using `additional_vars` and `cookbook_user_conf`) Squashed commit of the following: commit 91542b9 Author: Josh Gitlin <[email protected]> Date: Fri May 14 15:04:28 2021 -0400 Add changelog entry commit 858f41c Author: Josh Gitlin <[email protected]> Date: Fri May 14 15:00:21 2021 -0400 Bugfix node name commit dab2949 Author: Josh Gitlin <[email protected]> Date: Fri May 14 14:33:29 2021 -0400 Cookstyle fixes commit d587ae1 Author: Josh Gitlin <[email protected]> Date: Fri May 14 14:31:33 2021 -0400 MDL fixes commit eda83e5 Author: Josh Gitlin <[email protected]> Date: Fri May 14 12:05:37 2021 -0400 Added documentation for vpn_user resource commit 61172d8 Author: Josh Gitlin <[email protected]> Date: Thu May 13 21:48:09 2021 -0400 Make client settings match server settings commit c14d15a Author: Josh Gitlin <[email protected]> Date: Tue May 11 21:36:18 2021 -0400 Add compression to client configs Signed-off-by: Josh Gitlin <[email protected]>
1 parent 433d725 commit 88cf4a5

File tree

5 files changed

+84
-8
lines changed

5 files changed

+84
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ This file is used to list changes made in each version of the openvpn cookbook.
44

55
## Unreleased
66

7+
- Make client config match server config (fixes [#189](https://github.com/sous-chefs/openvpn/issues/189))
8+
- Document usage of `openvpn_user` with examples for `additional_vars`
9+
710
## 5.3.0 - *2021-03-16*
811

912
- Fix openvpn_conf template handling

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ These attributes are set by the cookbook by default.
5858
- `node['openvpn']['configure_default_server']` - Boolean. Set this to false if you want to create all of your "conf" files with the LWRP.
5959
- `node['openvpn']['git_package']` - Boolean. Whether to use the `openvpn-git` package (Arch Linux only, default false).
6060
- `node['openvpn']['client_prefix']` - String. Name of the config that is created for clients. When imported into most vpn clients, this is the name that will be displayed for the connection. Default is 'vpn-prod'.
61-
- `node['openvpn']['cookbook_user_conf']` - String. The cookbook used by the `openvpn::users` recipe for the `client.conf.erb` template. You can override this to your own, such as your wrapper cookbook. Default is `'openvpn'`.
61+
- `node['openvpn']['cookbook_user_conf']` - String. The cookbook used by the `openvpn::users` recipe for the `client.conf.erb` template. You can override this to your own, such as your wrapper cookbook. Default is `'openvpn'`. See [Customizing user configuration](#customizing-user-configuration) under the [openvpn_user resource](#openvpn_user) section
6262
- `node['openvpn']['key_dir']` - Location to store keys, certificates and related files. Default `/etc/openvpn/keys`.
6363
- `node['openvpn']['signing_ca_cert']` - CA certificate for signing, default `/etc/openvpn/keys/ca.crt`
6464
- `node['openvpn']['signing_ca_key']` - CA key for signing, default `/etc/openvpn/keys/ca.key`
@@ -198,7 +198,54 @@ This cookbook also provides an 'up' script that runs when OpenVPN is started. Th
198198

199199
### openvpn_user
200200

201-
Implements a resource for creation of users and bundles.
201+
Implements a resource for creation of users and bundles. User configuration will attempt to match the server configuration as best as possible,
202+
by matching node attributes like `node['openvpn']['config']['compress']` and `node['openvpn']['config']['cipher']`. Reasonable default configuration
203+
for the user bundle is specified otherwise.
204+
205+
By default, an OpenVPN user _bundle_ is created, which is a gzipped TAR file (`.tgz` archive) containing the user configuration and the public/private
206+
keys. This is controlled by the `create_bundle` attribute of the `openvpn_user` resource; pass `create_bundle false` if you prefer to have inline `.ovpn`
207+
files created, containing the public and private keys all inside one OpenVPN config file.
208+
209+
#### Customizing user configuration
210+
211+
If the provided OpenVPN configuration does not meet your needs, either because you need different configuration directives, or you want to add directives which
212+
are not present, you can use the node attribute `node['openvpn']['cookbook_user_conf']` to look for the template files in a different cookbook, E.G. in your
213+
wrapper cookbook.
214+
215+
If you only need _additional_ directives, you can use the `additional_vars` attribute of the `openvpn_user` resource to pass additional template variables to your
216+
custom template. This way, you can render the user configuration from this cookbook using a partial, and append (or prepend) your own config inside your template.
217+
218+
#### Example
219+
220+
Adding a 2FA via a hardware token
221+
222+
`cookbooks/vpn_wrapper/recipes/user.rb`:
223+
224+
```ruby
225+
override["openvpn"]["cookbook_user_conf"] => "vpn_wrapper"
226+
openvpn_user "VPN User Bundle" do
227+
client_name "my_user"
228+
additional_vars(
229+
static_challenge: %{"Touch your hardware token now:" 0}
230+
)
231+
end
232+
```
233+
234+
`cookbooks/vpn_wrapper/templates/client.conf.erb`:
235+
236+
```ruby
237+
<%= render "client.conf.erb", cookbook: "openvpn" %>
238+
auth-user-pass
239+
static-challenge <%= @static_challenge %>
240+
```
241+
242+
`cookbooks/vpn_wrapper/templates/client-inline.conf.erb`:
243+
244+
```ruby
245+
<%= render "client-inline.conf.erb", cookbook: "openvpn" %>
246+
auth-user-pass
247+
static-challenge <%= @static_challenge %>
248+
```
202249

203250
### openvpn_config
204251

resources/user.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
destination_path = ::File.expand_path(new_resource.destination || key_dir)
2323
bundle_filename = "#{new_resource.client_name}.tar.gz"
2424
bundle_full_path = ::File.expand_path(::File.join(destination_path, bundle_filename))
25+
compression = if node['openvpn']['config']['compress']
26+
node['openvpn']['config']['compress']
27+
elsif node['openvpn']['config']['comp-lzo']
28+
'lzo'
29+
end
2530

2631
execute "generate-openvpn-#{new_resource.client_name}" do
2732
command "./pkitool #{new_resource.client_name}"
@@ -46,15 +51,15 @@
4651

4752
template "#{destination_path}/#{client_file_basename}.conf" do
4853
source 'client.conf.erb'
49-
cookbook node['openvpn']['cookbook_user_conf']
54+
cookbook lazy { node['openvpn']['cookbook_user_conf'] }
5055
variables(client_cn: new_resource.client_name)
5156
notifies :delete, "file[#{cleanup_name}]", :immediately
5257
only_if { new_resource.create_bundle }
5358
end
5459

5560
template "#{destination_path}/#{client_file_basename}.ovpn" do
5661
source new_resource.create_bundle ? 'client.conf.erb' : 'client-inline.conf.erb'
57-
cookbook node['openvpn']['cookbook_user_conf']
62+
cookbook lazy { node['openvpn']['cookbook_user_conf'] }
5863
if new_resource.create_bundle
5964
variables(client_cn: new_resource.client_name)
6065
else
@@ -66,6 +71,7 @@
6671
ca: IO.read(ca_cert_path),
6772
cert: IO.read(cert_path),
6873
key: IO.read(key_path),
74+
compression: compression,
6975
}.merge(new_resource.additional_vars) { |key, oldval, newval| oldval } # rubocop:disable Lint/UnusedBlockArgument
7076
end
7177
)

templates/client-inline.conf.erb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ resolv-retry infinite
1212
nobind
1313
persist-key
1414
persist-tun
15-
comp-lzo
15+
<% if @compression -%>
16+
compress <%= @compression %>
17+
<% end %>
18+
<%
19+
%w(cipher tls-cipher auth keysize link-mtu).each do |conf|
20+
if node['openvpn']['config'][conf]
21+
%><%= "#{conf} #{node['openvpn']['config'][conf]}" %>
22+
<%
23+
end
24+
end
25+
-%>
1626
verb 3
1727
<ca>
1828
<%= @ca -%>

templates/client.conf.erb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ persist-tun
1515
ca ca.crt
1616
cert <%= @client_cn %>.crt
1717
key <%= @client_cn %>.key
18-
comp-lzo
18+
<% if @compression -%>
19+
<%= @compression %>
20+
<% end %>
21+
<%
22+
%w(cipher tls-cipher auth keysize link-mtu).each do |conf|
23+
if node['openvpn']['config'][conf]
24+
%><%= "#{conf} #{node['openvpn']['config'][conf]}" %>
25+
<%
26+
end
27+
end
28+
-%>
1929
verb 3
20-
<% if node['openvpn']['server_verification'] %>
30+
<% if node['openvpn']['server_verification'] -%>
2131
<%= node['openvpn']['server_verification'] %>
22-
<% end %>
32+
<% end -%>

0 commit comments

Comments
 (0)