Skip to content

Commit fe302d4

Browse files
committed
Fixes #38499 - Introduce SSH cert support
1 parent f7a0588 commit fe302d4

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

app/services/foreman/renderer/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class Configuration
6464
:falsy?,
6565
:previous_revision,
6666
:foreman_short_version,
67-
:product_short_version
67+
:product_short_version,
68+
:service_restart
6869
]
6970

7071
DEFAULT_ALLOWED_HOST_HELPERS = [

app/services/foreman/renderer/scope/macros/helpers.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@ def generate_web_request(utility:, url:, ssl_ca_cert: nil, headers: nil, params:
181181
utility[:format_params].call(params).each { |param| command << param } if params
182182
command.join(" \\\n ")
183183
end
184+
185+
apipie :method, 'Generates shell command to restart the given service' do
186+
required :service, String, desc: 'Name of the service to restart'
187+
returns String, desc: 'Shell command to restart the given service'
188+
example 'service_restart("sshd") #=> "if command -v systemctl >/dev/null 2>&1; then\n systemctl restart sshd\nelse\n service sshd restart\nfi"'
189+
end
190+
def service_restart(service)
191+
<<-EOS.strip
192+
if command -v systemctl >/dev/null 2>&1; then
193+
systemctl restart #{service}
194+
else
195+
service #{service} restart
196+
fi
197+
EOS
198+
end
184199
end
185200
end
186201
end

app/views/unattended/provisioning_templates/snippet/remote_execution_ssh_keys.erb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ description: |
1010
1111
remote_execution_ssh_keys: public keys to be put in ~/.ssh/authorized_keys
1212
13+
remote_execution_ssh_ca_keys: public ssh CA keys to be put in
14+
/etc/ssh/sshd_config.d/60-user-ca.conf
15+
1316
remote_execution_ssh_user: user for which remote_execution_ssh_keys will be
1417
authorized
1518
@@ -33,7 +36,7 @@ if [ -z "$PKG_MANAGER" ]; then
3336
<%= indent(2) { snippet 'pkg_manager' } -%>
3437
fi
3538

36-
<% if !host_param('remote_execution_ssh_keys').blank? %>
39+
<% if !host_param('remote_execution_ssh_keys').blank? || !host_param('remote_execution_ssh_ca_keys').blank? %>
3740
<% ssh_user = host_param('remote_execution_ssh_user') || 'root' %>
3841

3942
user_exists=false
@@ -64,6 +67,23 @@ EOF
6467
# Restore SELinux context with restorecon, if it's available:
6568
command -v restorecon && restorecon -RvF <%= ssh_path %> || true
6669

70+
<% if host_param('remote_execution_ssh_ca_keys').present? -%>
71+
mkdir -p /etc/ssh/sshd_config.d
72+
73+
cat << EOF >> /etc/ssh/user-ca.pub
74+
<%= host_param('remote_execution_ssh_ca_keys').is_a?(String) ? host_param('remote_execution_ssh_ca_keys') : host_param('remote_execution_ssh_ca_keys').join("\n") %>
75+
EOF
76+
77+
chmod 0644 /etc/ssh/user-ca.pub
78+
79+
echo 'TrustedUserCAKeys /etc/ssh/user-ca.pub' >/etc/ssh/sshd_config.d/60-user-ca.conf
80+
command -v restorecon && restorecon -RvF /etc/ssh || true
81+
82+
# restart sshd
83+
$(command -v cloud-init && cloud-init status --wait) >/dev/null 2>&1 || true
84+
<%= service_restart(@host.operatingsystem.family == 'Debian' ? 'ssh' : 'sshd') %>
85+
<% end -%>
86+
6787
<% if ssh_user != 'root' && host_param('remote_execution_effective_user_method') == 'sudo' -%>
6888
if [ ! -x "$(command -v sudo)" ]; then
6989
$PKG_MANAGER_INSTALL sudo

0 commit comments

Comments
 (0)