From 9832dee0a07d82e6f2f215fce10149fbb24aa573 Mon Sep 17 00:00:00 2001 From: Bart-Jan Vrielink Date: Wed, 24 Apr 2019 16:50:49 +0200 Subject: [PATCH] Add for the possibility of an IPv6 address Also cover the case that there is no IPv4 address on an IPv6 only host. Included are rspec tests for the sshkey resource Test for correct host_aliases for the host's own sshkey resource. 3 possible combinations of IPv4 and IPv6 address are tested. Not tested are: - Virtual sshkey resources from other hosts. - Other parameters for the sshkey resource (type, key) - Non-default values for ssh_key_ensure and ssh_key_import. --- manifests/init.pp | 10 +++++++++- spec/classes/init_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/manifests/init.pp b/manifests/init.pp index ed49124..e0732dd 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -974,10 +974,18 @@ class ssh ( } } + # If either IPv4 or IPv6 stack is not configured on the agent, the + # corresponding $::ipaddress(6)? fact is not present. So, we cannot assume + # these variables are defined. Getvar (Stdlib 4.13+, ruby 1.8.7+) handles + # this correctly. + if getvar('::ipaddress') and getvar('::ipaddress6') { $host_aliases = [$::hostname, $::ipaddress, $::ipaddress6] } + elsif getvar('::ipaddress6') { $host_aliases = [$::hostname, $::ipaddress6] } + else { $host_aliases = [$::hostname, $::ipaddress] } + # export each node's ssh key @@sshkey { $::fqdn : ensure => $ssh_key_ensure, - host_aliases => [$::hostname, $::ipaddress], + host_aliases => $host_aliases, type => $ssh_key_type, key => $key, } diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index 2763344..fd91a1c 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -311,6 +311,32 @@ describe 'ssh' do } it { should have_ssh__config_entry_resource_count(0) } + + context 'with exported sshkey resources' do + subject { exported_resources} + context 'With only IPv4 address' do + let(:facts) { default_facts.merge( facts )} + it { should contain_sshkey('monkey.example.com').with( + 'ensure' => 'present', + 'host_aliases' => ['monkey', '127.0.0.1'] + )} + end + context 'With dual stack IP' do + let(:facts) { default_facts.merge({ :ipaddress6 => 'dead:beef::1/64' }) } + it { should contain_sshkey('monkey.example.com').with( + 'ensure' => 'present', + 'host_aliases' => ['monkey', '127.0.0.1', 'dead:beef::1/64'] + )} + end + context 'With only IPv6 address' do + let(:facts) { default_facts.merge({ :ipaddress6 => 'dead:beef::1/64', :ipaddress => nil }) } + it { should contain_sshkey('monkey.example.com').with( + 'ensure' => 'present', + 'host_aliases' => ['monkey', 'dead:beef::1/64'] + )} + end + end + end end