Skip to content

Commit 7b36a35

Browse files
committed
Fixes #32678 - katello_ca_consumer in registration template
Move `rhsm_reconfigure` script from `katello_consumer.rpm` to `global_registration` template so the `rpm` is not needed anymore Migrated script is without support of RHEL5 and older `subscription-manager` versions (0.96 and bellow)
1 parent 7d12d02 commit 7b36a35

File tree

4 files changed

+101
-26
lines changed

4 files changed

+101
-26
lines changed

app/controllers/concerns/foreman/controller/registration.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ def global_registration_vars
2626
location: (location || User.current.default_location || User.current.my_locations.first),
2727
hostgroup: host_group,
2828
operatingsystem: operatingsystem,
29-
url_host: registration_url.host,
30-
registration_url: registration_url,
3129
setup_insights: ActiveRecord::Type::Boolean.new.deserialize(params['setup_insights']),
3230
setup_remote_execution: ActiveRecord::Type::Boolean.new.deserialize(params['setup_remote_execution']),
3331
packages: params['packages'],
@@ -40,6 +38,7 @@ def global_registration_vars
4038
.to_h
4139
.symbolize_keys
4240
.merge(context)
41+
.merge(context_urls)
4342
end
4443

4544
def safe_render(template)
@@ -96,6 +95,10 @@ def registration_url
9695
fail Foreman::Exception.new(msg)
9796
end
9897

98+
def context_urls
99+
{ registration_url: registration_url }
100+
end
101+
99102
def setup_host_params
100103
clean_host_params
101104

app/views/unattended/provisioning_templates/registration/global_registration.erb

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,36 @@ if [ -f /etc/os-release ] ; then
3737
. /etc/os-release
3838
fi
3939

40+
# Choose package manager
41+
# apt-get for Debian & Ubuntu
42+
# dnf for Fedora (version >= 22) & RHEL family (version > 7)
43+
# yum for Fedora (version < 22) & RHEL family (version < 8)
44+
if [ x$ID = xfedora ]; then
45+
if [ "${VERSION_ID%.*}" -gt 21 ]; then
46+
PKG_MANAGER='dnf'
47+
else
48+
PKG_MANAGER='yum'
49+
fi
50+
elif [ -f /etc/redhat-release ] ; then
51+
if [ "${VERSION_ID%.*}" -gt 7 ]; then
52+
PKG_MANAGER='dnf'
53+
else
54+
PKG_MANAGER='yum'
55+
fi
56+
elif [ -f /etc/debian_version ]; then
57+
PKG_MANAGER='apt-get'
58+
fi
59+
4060
SSL_CA_CERT=$(mktemp)
4161
cat << EOF > $SSL_CA_CERT
4262
<%= foreman_server_ca_cert %>
4363
EOF
4464

65+
cleanup_and_exit() {
66+
rm -f $SSL_CA_CERT
67+
exit $1
68+
}
69+
4570
<% unless @repo.blank? -%>
4671
echo '#'
4772
echo '# Adding repository'
@@ -58,8 +83,8 @@ gpgkey=<%= shell_escape @repo_gpg_key_url %>
5883
EOF
5984

6085
echo "Building yum metadata cache, this may take a few minutes"
61-
yum makecache
62-
elif [ x$ID = xdebian ] || [ x$ID = xubuntu ]; then
86+
$PKG_MANAGER makecache
87+
elif [ -f /etc/debian_version ]; then
6388
cat << EOF > /etc/apt/sources.list.d/foreman_registration.list
6489
<%= shell_escape @repo %>
6590
EOF
@@ -71,7 +96,7 @@ EOF
7196

7297
else
7398
echo "Unsupported operating system, can't add repository."
74-
exit 1
99+
cleanup_and_exit 1
75100
fi
76101
<% end -%>
77102

@@ -102,7 +127,7 @@ echo "#"
102127
if [ -f /etc/redhat-release ]; then
103128
register_katello_host(){
104129
UUID=$(subscription-manager identity | head -1 | awk '{print $3}')
105-
curl --silent --show-error --cacert $SSL_CA_CERT --request POST "<%= @registration_url %>" \
130+
curl --silent --show-error --cacert $KATELLO_SERVER_CA_CERT --request POST "<%= @registration_url %>" \
106131
--data "uuid=$UUID" \
107132
<%= headers.join(' ') %> \
108133
<%= " --data 'host[organization_id]=#{@organization.id}' \\\n" if @organization -%>
@@ -115,42 +140,77 @@ if [ -f /etc/redhat-release ]; then
115140
<%= " --data packages=#{shell_escape(@packages)} \\\n" if @packages.present? -%>
116141
<%= " --data 'update_packages=#{@update_packages}' \\\n" unless @update_packages.nil? -%>
117142

118-
}
143+
}
144+
145+
KATELLO_SERVER_CA_CERT=/etc/rhsm/ca/katello-server-ca.pem
146+
RHSM_CFG=/etc/rhsm/rhsm.conf
119147

148+
# Backup rhsm.conf
149+
if [ -f $RHSM_CFG ] ; then
150+
test -f $RHSM_CFG.bak || cp $RHSM_CFG $RHSM_CFG.bak
151+
fi
152+
153+
# rhn-client-tools conflicts with subscription-manager package
154+
# since rhn tools replaces subscription-manager, we need to explicitly
155+
# install subscription-manager after the rhn tools cleanup
156+
if [ x$ID = xol ]; then
157+
$PKG_MANAGER remove -y rhn-client-tools
158+
$PKG_MANAGER install -y --setopt=obsoletes=0 subscription-manager
159+
fi
160+
161+
# Prepare SSL certificate
162+
mkdir -p /etc/rhsm/ca
163+
cp -f $SSL_CA_CERT $KATELLO_SERVER_CA_CERT
164+
chmod 644 $KATELLO_SERVER_CA_CERT
165+
166+
# Prepare subscription-manager
120167
<% if @force -%>
121168
if [ -x "$(command -v subscription-manager)" ] ; then
122169
subscription-manager unregister || true
123170
subscription-manager clean
124171
fi
125172

126-
yum remove -y katello-ca-consumer\*
173+
$PKG_MANAGER remove -y katello-ca-consumer\*
127174
<% end -%>
128175

129-
# rhn-client-tools conflicts with subscription-manager package
130-
# since rhn tools replaces subscription-manager, we need to explicitly
131-
# install subscription-manager after the rhn tools cleanup
132-
if [ x$ID = xol ]; then
133-
yum remove -y rhn-client-tools
134-
yum install -y --setopt=obsoletes=0 subscription-manager
176+
if ! [ -x "$(command -v subscription-manager)" ] ; then
177+
$PKG_MANAGER install -y subscription-manager
178+
else
179+
$PKG_MANAGER upgrade -y subscription-manager
135180
fi
136181

137-
CONSUMER_RPM=$(mktemp --suffix .rpm)
138-
curl --silent --show-error --output $CONSUMER_RPM <%= subscription_manager_configuration_url(hostname: @url_host) %>
182+
if ! [ -f $RHSM_CFG ] ; then
183+
echo "'$RHSM_CFG' not found, cannot configure subscription-manager"
184+
cleanup_and_exit 1
185+
fi
139186

140-
# Workaround for systems with enabled FIPS,
141-
# where installation of RPM generated on RHEL7 cause 'no digest' error
142-
# See https://projects.theforeman.org/issues/32068
143-
if [ "$(cat /proc/sys/crypto/fips_enabled)" = "1" ]; then
144-
rpm -ivh --nodigest --nofiledigest $CONSUMER_RPM
187+
# Configure subscription-manager
188+
test -f $RHSM_CFG.bak || cp $RHSM_CFG $RHSM_CFG.bak
189+
subscription-manager config \
190+
--server.hostname="<%= @rhsm_url.host %>" \
191+
--server.port="<%= @rhsm_url.port %>" \
192+
--server.prefix="<%= @rhsm_url.path %>" \
193+
--rhsm.repo_ca_cert="$KATELLO_SERVER_CA_CERT" \
194+
--rhsm.baseurl="<%= @pulp_content_url %>"
195+
196+
# Older versions of subscription manager may not recognize
197+
# report_package_profile and package_profile_on_trans options.
198+
# So set them separately and redirect out & error to /dev/null
199+
# to fail silently.
200+
subscription-manager config --rhsm.package_profile_on_trans=1 > /dev/null 2>&1 || true
201+
subscription-manager config --rhsm.report_package_profile=1 > /dev/null 2>&1 || true
202+
203+
# Configuration for EL6
204+
if grep --quiet full_refresh_on_yum $RHSM_CFG; then
205+
sed -i "s/full_refresh_on_yum\s*=.*$/full_refresh_on_yum = 1/g" $RHSM_CFG
145206
else
146-
yum localinstall $CONSUMER_RPM -y
207+
full_refresh_config="#config for on-premise management\nfull_refresh_on_yum = 1"
208+
sed -i "/baseurl/a $full_refresh_config" $RHSM_CFG
147209
fi
148210

149-
rm -f $CONSUMER_RPM
150-
151211
subscription-manager register <%= '--force' if @force %> \
152-
--org='<%= @organization.label %>' \
153-
--activationkey=<%= shell_escape(activation_keys) %> || <%= @ignore_subman_errors ? 'true' : 'exit 1' %>
212+
--org='<%= @organization.label %>' \
213+
--activationkey='<%= activation_keys %>' || <%= @ignore_subman_errors ? 'true' : 'cleanup_and_exit 1' %>
154214

155215
register_katello_host | bash
156216
else
@@ -159,3 +219,5 @@ fi
159219
<% else -%>
160220
register_host | bash
161221
<% end -%>
222+
223+
cleanup_and_exit

config/initializers/uri_jail.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class URI::Generic::Jail < Safemode::Jail
2+
allow :host, :path, :port, :query, :scheme
3+
end

test/unit/foreman/renderer/scope/macros/base_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ class BaseMacrosTest < ActiveSupport::TestCase
139139
end
140140
end
141141

142+
test 'URI::Generic jail test' do
143+
allowed = [:host, :path, :port, :query, :scheme]
144+
allowed.each do |m|
145+
assert URI::HTTP::Jail.allowed?(m), "Method #{m} is not available in URI::HTTP::Jail while should be allowed."
146+
end
147+
end
148+
142149
context 'subnet helpers' do
143150
setup do
144151
host = FactoryBot.build(:host)

0 commit comments

Comments
 (0)