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

add parameter to exclude interfaces with a regex #378

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
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
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([], [%r{^docker}]).and_return(['10.13.42.61', '10.0.0.110', '10.0.0.104', '10.0.0.109'])
end
end

describe 'with excluded re interfaces' do
it 'doesn\'t return the IPs of those interfaces' do
is_expected.to run.with_params([], [%r{docker0}, %r{no1$}]).and_return([])
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