Skip to content

Commit

Permalink
Move REX ssh key management into separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
wbclark committed Mar 15, 2022
1 parent 98f2a3f commit 96ae112
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
33 changes: 6 additions & 27 deletions manifests/plugin/remote_execution/ssh.pp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
Boolean $ssh_kerberos_auth = false,
Enum['ssh', 'ssh-async'] $mode = 'ssh'
) {

$ssh_identity_path = "${ssh_identity_dir}/${ssh_identity_file}"

include foreman_proxy::params
Expand All @@ -62,32 +61,12 @@
}

if $generate_keys {
file { $ssh_identity_dir:
ensure => directory,
owner => $foreman_proxy::user,
group => $foreman_proxy::user,
mode => '0700',
}
-> exec { 'generate_ssh_key':
command => "${ssh_keygen} -f ${ssh_identity_path} -N '' -m pem",
user => $foreman_proxy::user,
cwd => $ssh_identity_dir,
creates => $ssh_identity_path,
}
if $install_key {
# Ensure the .ssh directory exists with the right permissions
file { '/root/.ssh':
ensure => directory,
owner => 'root',
group => 'root',
mode => '0700',
}
-> exec { 'install_ssh_key':
path => '/usr/bin:/usr/sbin:/bin',
command => "cat ${ssh_identity_path}.pub >> /root/.ssh/authorized_keys",
unless => "grep -f ${ssh_identity_path}.pub /root/.ssh/authorized_keys",
require => Exec['generate_ssh_key'],
}
class { 'foreman_proxy::plugin::remote_execution::ssh::keys':
install_key => $install_key,
ssh_identity_path => $ssh_identity_path,
ssh_keygen => $ssh_keygen,
user => $foreman_proxy::user,
group => $foreman_proxy::user,
}
}
}
54 changes: 54 additions & 0 deletions manifests/plugin/remote_execution/ssh/keys.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# = Foreman Proxy Remote Execution SSH plugin key management
#
# This class generates and installs Remote Execution SSH keys for Foreman proxy
#
# === Parameters:
#
# $install_key:: Automatically install generated SSH key to root authorized keys
# which allows managing this host through Remote Execution
#
# $ssh_identity_path:: Fully qualified path where SSH keys are stored
#
# $ssh_keygen:: Location of the ssh-keygen binary
#
# $user:: User owner of the directory and keys.
#
# $group:: Group owner of the directory and keys.
#
class foreman_proxy::plugin::remote_execution::ssh::keys (
Boolean $install_key = false,
Stdlib::Absolutepath $ssh_identity_path = '/var/lib/foreman-proxy/ssh/id_rsa_foreman_proxy',
String $ssh_keygen = '/usr/bin/ssh-keygen',
String $user = 'foreman-proxy',
String $group = 'foreman-proxy',
) {
$ssh_identity_dir = dirname($ssh_identity_path)

file { $ssh_identity_dir:
ensure => directory,
owner => $user,
group => $group,
mode => '0700',
}
-> exec { 'generate_ssh_key':
command => "${ssh_keygen} -f ${ssh_identity_path} -N '' -m pem",
user => $user,
cwd => $ssh_identity_dir,
creates => $ssh_identity_path,
}
if $install_key {
# Ensure the .ssh directory exists with the right permissions
file { '/root/.ssh':
ensure => directory,
owner => 'root',
group => 'root',
mode => '0700',
}
-> exec { 'install_ssh_key':
path => '/usr/bin:/usr/sbin:/bin',
command => "cat ${ssh_identity_path}.pub >> /root/.ssh/authorized_keys",
unless => "grep -f ${ssh_identity_path}.pub /root/.ssh/authorized_keys",
require => Exec['generate_ssh_key'],
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe 'foreman_proxy::plugin::remote_execution::ssh::keys' do
it { is_expected.to compile.with_all_deps }
end

0 comments on commit 96ae112

Please sign in to comment.