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

Move REX ssh key management into separate class #727

Merged
merged 1 commit into from
Mar 18, 2022
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
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}"
wbclark marked this conversation as resolved.
Show resolved Hide resolved

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