diff --git a/app/services/foreman/template_snapshot_service.rb b/app/services/foreman/template_snapshot_service.rb index 01ac0e9b3a5..16669c9635c 100644 --- a/app/services/foreman/template_snapshot_service.rb +++ b/app/services/foreman/template_snapshot_service.rb @@ -54,6 +54,10 @@ def self.windows10_dhcp new.windows10_dhcp end + def self.registration_template_default + new.registration_template_default + end + def self.render_template(template, host_name = :host4dhcp) host_stub = send(host_name.to_sym) source = Foreman::Renderer::Source::Snapshot.new(template) @@ -217,6 +221,17 @@ def windows10_dhcp define_host_params(host) end + def registration_template_default + Setting[:server_ca_file] = Rails.root.join('test/static_fixtures/certificates/example.com.crt') + Setting[:ssl_ca_file] = Rails.root.join('test/static_fixtures/certificates/example2.com.crt') + + host = FactoryBot.build(:host_for_snapshots_ipv4_dhcp_rocky9, + name: 'snapshot-registration-template-default', + subnet: FactoryBot.build(:subnet_ipv4_dhcp_for_snapshots), + interfaces: [ipv4_interface]) + define_host_params(host) + end + private def files diff --git a/app/views/unattended/provisioning_templates/registration/global_registration.erb b/app/views/unattended/provisioning_templates/registration/global_registration.erb index 973f1156c88..46953593067 100644 --- a/app/views/unattended/provisioning_templates/registration/global_registration.erb +++ b/app/views/unattended/provisioning_templates/registration/global_registration.erb @@ -2,6 +2,8 @@ kind: registration name: Global Registration model: ProvisioningTemplate +test_on: +- registration_template_default description: | The registration template used to render OS agnostic script that any host can use to register to this Foreman instance. It is rendered as a response in the registration API endpoint. The resulting diff --git a/test/unit/foreman/renderer/snapshots.yaml b/test/unit/foreman/renderer/snapshots.yaml index 438efa7552d..a263dd2f8ea 100644 --- a/test/unit/foreman/renderer/snapshots.yaml +++ b/test/unit/foreman/renderer/snapshots.yaml @@ -83,3 +83,4 @@ files: - finish/windows_default_finish.erb - script/windows_default_script.erb - snippet/windows_network.erb + - registration/global_registration.erb diff --git a/test/unit/foreman/renderer/snapshots/ProvisioningTemplate/registration/Global_Registration.registration_template_default.snap.txt b/test/unit/foreman/renderer/snapshots/ProvisioningTemplate/registration/Global_Registration.registration_template_default.snap.txt new file mode 100644 index 00000000000..8942eb7ee86 --- /dev/null +++ b/test/unit/foreman/renderer/snapshots/ProvisioningTemplate/registration/Global_Registration.registration_template_default.snap.txt @@ -0,0 +1,147 @@ +#!/bin/sh + +# Make sure, all command output can be parsed (e.g. from subscription-manager) +export LC_ALL=C LANG=C + +# Rendered with following template parameters: + + +if ! [ $(id -u) = 0 ]; then + echo "Please run as root" + exit 1 +fi + +# Select package manager for the OS (sets the $PKG_MANAGER* variables) +if [ -f /etc/os-release ] ; then + . /etc/os-release +fi + +if [ "${NAME%.*}" = 'FreeBSD' ]; then + PKG_MANAGER='pkg' + PKG_MANAGER_INSTALL="${PKG_MANAGER} install -y" + PKG_MANAGER_REMOVE="${PKG_MANAGER} delete -y" + PKG_MANAGER_UPGRADE="${PKG_MANAGER} install -y" +elif [ -f /etc/fedora-release -o -f /etc/redhat-release -o -f /etc/amazon-linux-release -o -f /etc/system-release ]; then + PKG_MANAGER='dnf' + if [ -f /etc/redhat-release -a "${VERSION_ID%.*}" -le 7 ]; then + PKG_MANAGER='yum' + elif [ -f /etc/system-release ]; then + PKG_MANAGER='yum' + fi + PKG_MANAGER_INSTALL="${PKG_MANAGER} install -y" + PKG_MANAGER_REMOVE="${PKG_MANAGER} remove -y" + PKG_MANAGER_UPGRADE="${PKG_MANAGER} upgrade -y" +elif [ -f /etc/debian_version ]; then + PKG_MANAGER='apt-get' + PKG_MANAGER_INSTALL="${PKG_MANAGER} install -y" + PKG_MANAGER_REMOVE="${PKG_MANAGER} remove -y" + PKG_MANAGER_UPGRADE="${PKG_MANAGER} -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' -o APT::Get::Upgrade-Allow-New='true' upgrade -y" +elif [ -f /etc/arch-release ]; then + PKG_MANAGER='pacman' + PKG_MANAGER_INSTALL="${PKG_MANAGER} --noconfirm -S" + PKG_MANAGER_REMOVE="${PKG_MANAGER} --noconfirm -R" + PKG_MANAGER_UPGRADE="${PKG_MANAGER} --noconfirm -S" +elif [ x$ID = xopensuse-tumbleweed -o x$ID = xsles ]; then + PKG_MANAGER='zypper' + PKG_MANAGER_INSTALL="${PKG_MANAGER} --non-interactive install --auto-agree-with-licenses" + PKG_MANAGER_REMOVE="${PKG_MANAGER} --non-interactive remove" + PKG_MANAGER_UPGRADE="${PKG_MANAGER} --non-interactive update" +fi + + +SSL_CA_CERT=$(mktemp) +cat << EOF > $SSL_CA_CERT +-----BEGIN CERTIFICATE----- +MIIFrTCCA5WgAwIBAgIJAOzdk0zTsuBPMA0GCSqGSIb3DQEBCwUAMF0xCzAJBgNV +BAYTAlhYMRAwDgYDVQQIEwdFeGFtcGxlMRAwDgYDVQQHEwdFeGFtcGxlMRQwEgYD +VQQKEwtFeGFtcGUgSW5jLjEUMBIGA1UEAxMLZXhhbXBsZS5jb20wHhcNMTcxMDA2 +MTMxMzMyWhcNMTgxMDA2MTMxMzMyWjBdMQswCQYDVQQGEwJYWDEQMA4GA1UECBMH +RXhhbXBsZTEQMA4GA1UEBxMHRXhhbXBsZTEUMBIGA1UEChMLRXhhbXBlIEluYy4x +FDASBgNVBAMTC2V4YW1wbGUuY29tMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC +CgKCAgEAslMkcsSL4hhnTLBJ/ZD3qldyD+7qTE9cB5dk9MH0Vj2GEezPnnUYZ0Cu +v0029+ZbwzOb7If8XzGxGU5tnqItZLfihrM3SKqshgJFaCjWEuLXfwRzHF2+jSk8 +mkG69Z0JtewIeQ7SqUReeZb4cV47Hag6gO5LVTnFXlHjGPrz98SH+Ho80XtIdmk0 +6fCzVXUZY+0EtXIul00LwnqsNc8UupuRpNC3VIG6ZCq5snwQ9Va1u+RP77nJlGQz +bKh5/yYaMd9WU3LDREkY0kSzJzcqzzQzEJr1JAY1zmtm5+NLdU2LPZjMAU6UiuJ4 +ABNdHwlvUWQ0SyPDqH2pnaytFlHgAH3G6brOj51Jpp86hOa/3iJ+M6gGtbNcYshd +LYTpr+kl34qri0677VQtpM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HptdDg+JyI1RIg= +-----END CERTIFICATE----- + +EOF + +cleanup_and_exit() { + rm -f $SSL_CA_CERT + exit $1 +} + + + +register_host() { + curl --silent --show-error --cacert $SSL_CA_CERT --request POST \ + -H 'Authorization: Bearer ' \ + --data "host[name]=$(hostname --fqdn)" \ + --data "host[build]=false" \ + --data "host[managed]=false" \ + +} + +echo "#" +echo "# Running registration" +echo "#" + +register_host | bash + + +cleanup_and_exit