Skip to content

Commit

Permalink
Support assigning multiple tags to a hostkey
Browse files Browse the repository at this point in the history
  • Loading branch information
deric committed Feb 16, 2023
1 parent 8c87149 commit c91979e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,22 @@ class YOURCUSTOMCLASS {
}
```

## Tag hostkey

Assign tags to exported `sshkey` resources (when `ssh::storeconfigs_enabled` is set to `true`).

```yaml
ssh::hostkeys::tags:
- hostkey_group1
- hostkey_group2
```

Host keys then can be imported using:

```puppet
Sshkey <<| tag == "hostkey_group1" |>>
```

## Excluding network interfaces or ipaddresses

Use hiera to exclude interfaces or ipaddresses from hostkey inclusion
Expand Down
27 changes: 19 additions & 8 deletions manifests/hostkeys.pp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
# @param use_trusted_facts
# Whether to use trusted or normal facts
#
# @param tags
# 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,
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,
) {
if $use_trusted_facts {
$fqdn_real = $trusted['certname']
Expand All @@ -44,8 +48,14 @@
$host_aliases = sort(unique(flatten([$fqdn_real, $hostname_real, $extra_aliases])))
}

if $storeconfigs_group {
tag 'hostkey_all', "hostkey_${storeconfigs_group}"
$storeconfigs_groups = $storeconfigs_group ? {
undef => [],
default => ['hostkey_all', "hostkey_${storeconfigs_group}"],
}

$_tags = $tags ? {
undef => $storeconfigs_groups,
default => $storeconfigs_groups + $tags,
}

['dsa', 'rsa', 'ecdsa', 'ed25519'].each |String $key_type| {
Expand All @@ -63,6 +73,7 @@
host_aliases => $host_aliases,
type => $key_type_real,
key => $facts['ssh'][$key_type]['key'],
tag => $_tags,
}
} else {
@@sshkey { "${fqdn_real}_${key_type}":
Expand Down
67 changes: 67 additions & 0 deletions spec/classes/hostkeys_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'ssh::hostkeys', type: 'class' do
_, os_facts = on_supported_os.first

let(:facts) { os_facts }

context 'with tags' do
let(:params) do
{
tags: ['group1', 'group2']
}
end

['dsa', 'rsa', 'ecdsa', 'ed25519'].each do |key_type|
it {
expect(exported_resources).to contain_sshkey("foo.example.com_#{key_type}")
.with(
ensure: 'present',
type: %r{^#{key_type}},
tag: ['group1', 'group2'],
)
}
end
end

context 'with storeconfigs_group' do
let(:params) do
{
storeconfigs_group: 'server_group',
}
end

['dsa', 'rsa', 'ecdsa', 'ed25519'].each do |key_type|
it {
expect(exported_resources).to contain_sshkey("foo.example.com_#{key_type}")
.with(
ensure: 'present',
type: %r{^#{key_type}},
tag: ['hostkey_all', 'hostkey_server_group'],
)
}
end
end

context 'with storeconfigs_group and tags' do
let(:params) do
{
storeconfigs_group: 'server_group',
tags: ['group1', 'group2'],
}
end

['dsa', 'rsa', 'ecdsa', 'ed25519'].each do |key_type|
it {
expect(exported_resources).to contain_sshkey("foo.example.com_#{key_type}")
.with(
ensure: 'present',
type: %r{^#{key_type}},
tag: ['hostkey_all', 'hostkey_server_group', 'group1', 'group2'],
)
}
end
end
end

0 comments on commit c91979e

Please sign in to comment.