Skip to content

Commit

Permalink
add parameter to exclude interfaces with a regex
Browse files Browse the repository at this point in the history
  • Loading branch information
saz committed Mar 27, 2024
1 parent 7e9d28f commit 8c19e64
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
7 changes: 5 additions & 2 deletions lib/puppet/functions/ssh/ipaddresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
Puppet::Functions.create_function(:'ssh::ipaddresses') do
dispatch :ipaddresses do
# @param excluded_interfaces An array of interface names to be excluded.
param 'Array[String[1]]', :excluded_interfaces
# @param excluded_interfaces_re An array of regexp matching interface names to be excluded.
param 'Array[Regexp]', :excluded_interfaces_re
# @return The IP addresses found.
optional_param 'Array[String[1]]', :excluded_interfaces
return_type 'Array[Stdlib::IP::Address]'
end

def ipaddresses(excluded_interfaces = [])
def ipaddresses(excluded_interfaces, excluded_interfaces_re)
facts = closure_scope['facts']

# always exclude loopback interface
Expand All @@ -36,6 +38,7 @@ def ipaddresses(excluded_interfaces = [])
interfaces.each do |iface, data|
# skip excluded interfaces
next if excluded_interfaces.include?(iface)
next if excluded_interfaces_re.any? { |pattern| pattern.match?(iface) }

%w[bindings bindings6].each do |binding_type|
next unless data.key?(binding_type)
Expand Down
17 changes: 9 additions & 8 deletions manifests/hostkeys.pp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
# Array of custom tags
#
class ssh::hostkeys (
Boolean $export_ipaddresses = true,
Optional[String[1]] $storeconfigs_group = undef,
Array $extra_aliases = [],
Array $exclude_interfaces = [],
Array $exclude_ipaddresses = [],
Boolean $use_trusted_facts = false,
Optional[Array[String[1]]] $tags = undef,
Boolean $export_ipaddresses = true,
Optional[String[1]] $storeconfigs_group = undef,
Array $extra_aliases = [],
Array $exclude_interfaces = [],
Array[Regexp] $exclude_interfaces_re = [],
Array $exclude_ipaddresses = [],
Boolean $use_trusted_facts = false,
Optional[Array[String[1]]] $tags = undef,
) {
if $use_trusted_facts {
$fqdn_real = $trusted['certname']
Expand All @@ -41,7 +42,7 @@
}

if $export_ipaddresses == true {
$ipaddresses = ssh::ipaddresses($exclude_interfaces)
$ipaddresses = ssh::ipaddresses($exclude_interfaces, $exclude_interfaces_re)
$ipaddresses_real = $ipaddresses - $exclude_ipaddresses
$host_aliases = sort(unique(flatten([$fqdn_real, $hostname_real, $extra_aliases, $ipaddresses_real])))
} else {
Expand Down
24 changes: 18 additions & 6 deletions spec/functions/ssh/ipaddresses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,31 @@

describe 'without parameters' do
it 'returns all IPs other than localhost' do
is_expected.to run.and_return(['172.17.0.1', '10.13.42.61', '10.0.0.110', '10.0.0.104', '10.0.0.109'])
is_expected.to run.with_params([], []).and_return(['172.17.0.1', '10.13.42.61', '10.0.0.110', '10.0.0.104', '10.0.0.109'])
end
end

describe 'with excluded interface' do
it 'doesn\'t return the IPs of excluded interface' do
is_expected.to run.with_params(['docker0']).and_return(['10.13.42.61', '10.0.0.110', '10.0.0.104', '10.0.0.109'])
is_expected.to run.with_params(['docker0'], []).and_return(['10.13.42.61', '10.0.0.110', '10.0.0.104', '10.0.0.109'])
end
end

describe 'with excluded interfaces' do
it 'doesn\'t return the IPs of those interfaces' do
is_expected.to run.with_params(%w[docker0 eno1]).and_return([])
is_expected.to run.with_params(%w[docker0 eno1], []).and_return([])
end
end

describe 'with excluded re interface' do
it 'doesn\'t return the IPs of excluded interface' do
is_expected.to run.with_params([], [/^docker/]).and_return(['10.13.42.61', '10.0.0.110', '10.0.0.104', '10.0.0.109'])

Check failure on line 35 in spec/functions/ssh/ipaddresses_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/RegexpLiteral: Use `%r` around regular expression. (https://rubystyle.guide#percent-r)
end
end

describe 'with excluded re interfaces' do
it 'doesn\'t return the IPs of those interfaces' do
is_expected.to run.with_params([], [/docker0/, /no1$/]).and_return([])

Check failure on line 41 in spec/functions/ssh/ipaddresses_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/RegexpLiteral: Use `%r` around regular expression. (https://rubystyle.guide#percent-r)

Check failure on line 41 in spec/functions/ssh/ipaddresses_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/RegexpLiteral: Use `%r` around regular expression. (https://rubystyle.guide#percent-r)
end
end
end
Expand All @@ -44,19 +56,19 @@

describe 'without parameters' do
it 'returns all IPs other than localhost' do
is_expected.to run.and_return(['172.17.0.1', '10.13.42.61'])
is_expected.to run.with_params([], []).and_return(['172.17.0.1', '10.13.42.61'])
end
end

describe 'with excluded interface' do
it 'doesn\'t return the IPs of excluded interface' do
is_expected.to run.with_params(['docker0']).and_return(['10.13.42.61'])
is_expected.to run.with_params(['docker0'], []).and_return(['10.13.42.61'])
end
end

describe 'with excluded interfaces' do
it 'doesn\'t return the IPs of those interfaces' do
is_expected.to run.with_params(%w[docker0 eno1]).and_return([])
is_expected.to run.with_params(%w[docker0 eno1], []).and_return([])
end
end
end
Expand Down

0 comments on commit 8c19e64

Please sign in to comment.