Skip to content

Commit

Permalink
add openvpn::deploy::(export/client)
Browse files Browse the repository at this point in the history
fix linting, add credit
restructure deploy manifests
fixes voxpupuli#231
  • Loading branch information
to-kn committed Jan 6, 2018
1 parent 52d29b5 commit 2ad1401
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 13 deletions.
24 changes: 24 additions & 0 deletions lib/facter/openvpn_deploy_cert_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Facter.add("openvpn::deploy_cert_data") do
setcode do
clients = {}
path = '/etc/openvpn'
if File.directory?(path)
Dir.entries(path).each do |server|
if File.directory?("#{path}/#{server}/download-configs")
Dir.entries("#{path}/#{server}/download-configs").each do |client|
if File.directory?("#{path}/#{server}/download-configs/#{client}") and client !~ /^\.\.?$/ and client !~ /\.tblk$/
clients["#{server}-#{client}-conf"] = File.open("#{path}/#{server}/download-configs/#{client}/#{client}.conf", "r").read
clients["#{server}-#{client}-ca"] = File.open("#{path}/#{server}/download-configs/#{client}/keys/#{client}/ca.crt", "r").read
clients["#{server}-#{client}-crt"] = File.open("#{path}/#{server}/download-configs/#{client}/keys/#{client}/#{client}.crt", "r").read
clients["#{server}-#{client}-key"] = File.open("#{path}/#{server}/download-configs/#{client}/keys/#{client}/#{client}.key", "r").read
if File.exists?("#{path}/#{server}/download-configs/#{client}/keys/#{client}/ta.key")
clients["#{server}-#{client}-ta"] = File.open("#{path}/#{server}/download-configs/#{client}/keys/#{client}/ta.key", "r").read
end
end
end
end
end
end
clients
end
end
15 changes: 10 additions & 5 deletions manifests/client.pp
Original file line number Diff line number Diff line change
Expand Up @@ -258,27 +258,31 @@
ensure => directory,
}

file { "${etc_directory}/openvpn/${server}/download-configs/${name}/keys/${name}/${name}.crt":
file { "${server}-${name}-crt":
ensure => link,
path => "${etc_directory}/openvpn/${server}/download-configs/${name}/keys/${name}/${name}.crt",
target => "${etc_directory}/openvpn/${ca_name}/easy-rsa/keys/${name}.crt",
require => Exec["generate certificate for ${name} in context of ${ca_name}"],
}

file { "${etc_directory}/openvpn/${server}/download-configs/${name}/keys/${name}/${name}.key":
file { "${server}-${name}-key":
ensure => link,
path => "${etc_directory}/openvpn/${server}/download-configs/${name}/keys/${name}/${name}.key",
target => "${etc_directory}/openvpn/${ca_name}/easy-rsa/keys/${name}.key",
require => Exec["generate certificate for ${name} in context of ${ca_name}"],
}

file { "${etc_directory}/openvpn/${server}/download-configs/${name}/keys/${name}/ca.crt":
file { "${server}-${name}-ca":
ensure => link,
path => "${etc_directory}/openvpn/${server}/download-configs/${name}/keys/${name}/ca.crt",
target => "${etc_directory}/openvpn/${ca_name}/easy-rsa/keys/ca.crt",
require => Exec["generate certificate for ${name} in context of ${ca_name}"],
}

if $tls_auth {
file { "${etc_directory}/openvpn/${server}/download-configs/${name}/keys/${name}/ta.key":
file { "${server}-${name}-ta":
ensure => link,
path => "${etc_directory}/openvpn/${server}/download-configs/${name}/keys/${name}/ta.key",
target => "${etc_directory}/openvpn/${server}/easy-rsa/keys/ta.key",
require => Exec["generate certificate for ${name} in context of ${server}"],
before => [
Expand Down Expand Up @@ -314,7 +318,8 @@
before => Exec["tar the thing ${server} with ${name}"];
}

file { "${etc_directory}/openvpn/${server}/download-configs/${name}/${name}.conf":
file { "${server}-${name}-conf":
path => "${etc_directory}/openvpn/${server}/download-configs/${name}/${name}.conf",
owner => root,
group => $::openvpn::params::root_group,
mode => '0444',
Expand Down
59 changes: 59 additions & 0 deletions manifests/deploy/client.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# == Define: openvpn::deploy::client
#
# Collect the exported configs for an Host and ensure a running Openvpn Service
#
# === Parameters
#
# $server which Openvpn::Server[$server] does the config belong to?
# String
#
# $manage_etc should the /etc/openvpn directory be managed? (warning, all unmanaged files will be purged!)
#
# === Variables
#
# None
#
# === Examples
#
# openvpn::deploy::client { 'test-client':
# server => 'test_server',
# }
#
# === Authors
#
# Phil Bayfield https://bitbucket.org/Philio/
#

define openvpn::deploy::client (
String $server,
Boolean $manage_etc = true,
) {

include openvpn::deploy::prepare

Class['openvpn::deploy::install']
-> Openvpn::Deploy::Client[$name]
~> Class['openvpn::deploy::service']


if ($manage_etc == true) {
file { [
"${::openvpn::params::etc_directory}/openvpn",
"${::openvpn::params::etc_directory}/openvpn/keys",
"${::openvpn::params::etc_directory}/openvpn/keys/${name}",
]:
ensure => directory,
purge => true,
force => true,
require => Package['openvpn'];
}
} else {
file { "${::openvpn::params::etc_directory}/openvpn/keys/${name}":
ensure => directory,
require => Package['openvpn'];
}
}

File <<| tag == "${server}-${name}" |>>

}
93 changes: 93 additions & 0 deletions manifests/deploy/export.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# == Define: openvpn::deploy::export
#
# Prepare all Openvpn-Client-Configs to be exported
#
# === Parameters
#
# $server which Openvpn::Server[$server] does the config belong to?
# String
#
# $tls_auth should the ta* files be exported too?
#
# === Variables
#
# None
#
# === Examples
#
# openvpn::deploy::export { 'test-client':
# server => 'test_server',
# }
#
# === Authors
#
# Phil Bayfield https://bitbucket.org/Philio/
#

define openvpn::deploy::export (
String $server,
Boolean $tls_auth = false,
) {

Openvpn::Server[$server]
-> Openvpn::Client[$name]
-> Openvpn::Deploy::Export[$name]

if $::openvpn::deploy_cert_data {
$data = $::openvpn::deploy_cert_data
} else {
fail('openvpn::deploy_cert_data not defined, is pluginsync enabled?')
}

@@file { "exported-${server}-${name}-config":
ensure => file,
path => "${::openvpn::params::etc_directory}/openvpn/${name}.conf",
owner => 'root',
group => 'root',
mode => '0600',
content => $data["exported-${server}-${name}-conf"],
tag => "${server}-${name}",
}

@@file { "exported-${server}-${name}-ca":
ensure => file,
path => "${::openvpn::params::etc_directory}/openvpn/keys/${name}/ca.crt",
owner => 'root',
group => 'root',
mode => '0600',
content => $data["${server}-${name}-ca"],
tag => "${server}-${name}",
}

@@file { "exported-${server}-${name}-crt":
ensure => file,
path => "${::openvpn::params::etc_directory}/openvpn/keys/${name}/${name}.crt",
owner => 'root',
group => 'root',
mode => '0600',
content => $data["${server}-${name}-crt"],
tag => "${server}-${name}",
}

@@file { "exported-${server}-${name}-key":
ensure => file,
path => "${::openvpn::params::etc_directory}/openvpn/keys/${name}/${name}.key",
owner => 'root',
group => 'root',
mode => '0600',
content => $data["${server}-${name}-key"],
tag => "${server}-${name}",
}

if $tls_auth {
@@file { "exported-${server}-${name}-ta":
ensure => file,
path => "${::openvpn::params::etc_directory}/openvpn/keys/${name}/ta.key",
owner => 'root',
group => 'root',
mode => '0600',
content => $data["${server}-${name}-ta"],
tag => "${server}-${name}",
}
}
}
26 changes: 26 additions & 0 deletions manifests/deploy/install.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# == Class: openvpn::deploy::install
#
# Installs the Openvpn profile
#
# === Parameters
#
# None
#
# === Variables
#
# None
#
# === Examples
#
# include openvpn::deploy::install
#
# === Authors
#
# Phil Bayfield https://bitbucket.org/Philio/
#

class openvpn::deploy::install {

ensure_packages(['openvpn'])

}
29 changes: 29 additions & 0 deletions manifests/deploy/prepare.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# == Class: openvpn::deploy::prepare
#
# Base profile
#
# === Parameters
#
# None
#
# === Variables
#
# None
#
# === Examples
#
# include openvpn::deploy::prepare
#
# === Authors
#
# Phil Bayfield https://bitbucket.org/Philio/
#

class openvpn::deploy::prepare {

class { 'openvpn::params': }

class { 'openvpn::deploy::install': }
~> class { 'openvpn::deploy::service': }

}
31 changes: 31 additions & 0 deletions manifests/deploy/service.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# == Class: openvpn::deploy::service
#
# Base profile
#
# === Parameters
#
# None
#
# === Variables
#
# None
#
# === Examples
#
# include openvpn::deploy::service
#
# === Authors
#
# Phil Bayfield https://bitbucket.org/Philio/
#

class openvpn::deploy::service {

service { 'openvpn':
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true;
}

}
7 changes: 0 additions & 7 deletions spec/classes/openvpn_install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@
it { is_expected.to contain_package('openvpn-auth-ldap') }
it { is_expected.to contain_package('easy-rsa') }
end

context 'stretch' do
let(:operatingsystemrelease) { '9.0' }

it { is_expected.to contain_package('openvpn-auth-ldap') }
it { is_expected.to contain_package('easy-rsa') }
end
end

context 'redhat/centos' do
Expand Down
1 change: 0 additions & 1 deletion spec/defines/openvpn_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
it { is_expected.not_to contain_schedule('renew crl.pem schedule on test_server') }
it { is_expected.not_to contain_exec('renew crl.pem on test_server') }
it { is_expected.not_to contain_file('/etc/openvpn/test_server.conf').with_content(%r{^secret}) }

it { is_expected.not_to contain_file('/etc/openvpn/test_server.conf').with_content(%r{verb}) }
it { is_expected.to contain_file('/etc/openvpn/test_server.conf').with_content(%r{cipher AES-256-CBC}) }
it { is_expected.not_to contain_file('/etc/openvpn/test_server.conf').with_content(%r{persist-key}) }
Expand Down

0 comments on commit 2ad1401

Please sign in to comment.