Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [CAN BE SIMPLIFIED A LITTLE BIT] PVPN: Add boolean parameter 'routes_depend_on_peer' and apply relevant changes (#12173) #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 62 additions & 28 deletions manifests/peer.pp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#
define wireguard::peer (
Array[String] $allowedips,
String $iface = $title,
Array[String] $allowedips,
String $iface = $title,
Optional[
Pattern[/[A-Za-z0-9+\/=]{44}/]
] $publickey = $facts['wireguard'] ? {
] $publickey = $facts['wireguard'] ? {
undef => undef,
default => $facts['wireguard'][$iface],
},
Optional[String] $presharedkey = undef,
Optional[String] $endpoint = undef,
Integer[0,65535] $persistentkeepalive = 0, # 0 == off
Array[String] $export_tags = [],
String $peername = $::fqdn,
Optional[String] $presharedkey = undef,
Optional[String] $endpoint = undef,
Integer[0,65535] $persistentkeepalive = 0, # 0 == off
Array[String] $export_tags = [],
String $peername = $::fqdn,
Optional[Boolean] $routes_depend_on_peer = true,
) {
# the publickey is not optional despite the parameter specification
# saying otherwise but we don't want to fail a run if it's not there
Expand Down Expand Up @@ -54,28 +55,61 @@
tag => $export_tags,
}

# routes are only meaningful if there are multiple allowedips (Destination=)
if length($allowedips) > 1 {
# [Route]
$route = inline_epp(@(EOT), $template_params)
if $routes_depend_on_peer {
# routes are only meaningful if there are multiple allowedips (Destination=)
if length($allowedips) > 1 {
# [Route]
$route = inline_epp(@(EOT), $template_params)

[Route]
<% if length($peername) > 0 { -%>
# peer: <%= $peername %>
<% } -%>
Gateway=<%= $allowedips[0] %>
<% $allowedips.each |Integer $index, String $ip| {
# skip self (first allowedips)
if $index == 0 { next() } -%>
Destination=<%= $ip %>
<% } -%>
| EOT
<% if length($peername) > 0 { -%>
# peer: <%= $peername %>
<% } -%>
<% $allowedips.each |Integer $index, String $ip| {
# skip self (first allowedips)
if $index == 0 { next() } -%>
[Route]
Gateway=<%= $allowedips[0] %>
Destination=<%= $ip %>
<% unless $index == $allowedips.length - 1 { -%>

@@concat::fragment{ "[Route]-${::fqdn}-${iface}":
order => '10',
content => $route,
target => "${iface}.network",
tag => $export_tags,
<% } -%>
<% } -%>
| EOT

@@concat::fragment{ "[Route]-${::fqdn}-${iface}":
order => '10',
content => $route,
target => "${iface}.network",
tag => $export_tags,
}
}
} else {
# routes are only meaningful if there are multiple allowedips (Destination=)
if length($allowedips) > 1 {
# [Route]
$route = inline_epp(@(EOT), $template_params)

<% if length($peername) > 0 { -%>
# peer: <%= $peername %>
<% } -%>
<% $allowedips.each |Integer $index, String $ip| {
# skip self (first allowedips)
if $index == 0 { next() } -%>
[Route]
Destination=<%= $ip %>
Scope=link
<% unless $index == $allowedips.length - 1 { -%>

<% } -%>
<% } -%>
| EOT

@@concat::fragment{ "[Route]-${::fqdn}-${iface}":
order => '10',
content => $route,
target => "${iface}.network",
tag => $export_tags,
}
}
}
} else {
Expand Down