Skip to content

Commit

Permalink
Fixes #37440 - Enable multiple repositories for host registration
Browse files Browse the repository at this point in the history
  • Loading branch information
nadjaheitmann authored and MariaAga committed May 21, 2024
1 parent 3b135d0 commit f04d47c
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 144 deletions.
6 changes: 4 additions & 2 deletions app/controllers/api/v2/registration_commands_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class RegistrationCommandsController < V2::BaseController
param :insecure, :bool, desc: N_("Enable insecure argument for the initial curl")
param :packages, String, desc: N_("Packages to install on the host when registered. Can be set by `host_packages` parameter, example: `pkg1 pkg2`")
param :update_packages, :bool, desc: N_("Update all packages on the host")
param :repo, String, desc: N_("Repository URL / details, for example for Debian OS family: 'deb http://deb.example.com/ buster 1.0', for Red Hat and SUSE OS family: 'http://yum.theforeman.org/client/latest/el8/x86_64/'")

This comment has been minimized.

Copy link
@stejskalleos

stejskalleos May 28, 2024

Contributor

We need to maintain backward compatibility in API. We can't just remove API params without proper deprecation

This comment has been minimized.

Copy link
@nadjaheitmann

nadjaheitmann May 28, 2024

Author Contributor

Fixup here: #10187

param :repo_gpg_key_url, String, desc: N_("URL of the GPG key for the repository")
param :repo_data, Array, desc: N_("Array with repository URL and corresponding GPG key URL") do
param :repo, String, desc: N_("Repository URL / details, for example for Debian OS family: 'deb http://deb.example.com/ buster 1.0', for Red Hat and SUSE OS family: 'http://yum.theforeman.org/client/latest/el8/x86_64/'")
param :repo_gpg_key_url, String, desc: N_("URL of the GPG key for the repository")
end
end
def create
unless os_with_template?
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/api/v2/registration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class RegistrationController < V2::BaseController
param :setup_remote_execution, :bool, desc: N_("Set 'host_registration_remote_execution' parameter for the host. If it is set to true, SSH keys will be installed on the host")
param :packages, String, desc: N_("Packages to install on the host when registered. Can be set by `host_packages` parameter, example: `pkg1 pkg2`")
param :update_packages, :bool, desc: N_("Update all packages on the host")
param :repo, String, desc: N_("Repository URL / details, for example for Debian OS family: 'deb http://deb.example.com/ buster 1.0', for Red Hat OS family: 'http://yum.theforeman.org/client/latest/el8/x86_64/'")
param :repo_gpg_key_url, String, desc: N_("URL of the GPG key for the repository")
param :repo_data, Array, desc: N_("Array with repository URL and corresponding GPG key URL") do
param :repo, String, desc: N_("Repository URL / details, for example for Debian OS family: 'deb http://deb.example.com/ buster 1.0', for Red Hat OS family: 'http://yum.theforeman.org/client/latest/el8/x86_64/'")
param :repo_gpg_key_url, String, desc: N_("URL of the GPG key for the repository")
end
def global
find_global_registration

Expand Down
7 changes: 5 additions & 2 deletions app/controllers/concerns/foreman/controller/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def global_registration_vars
location = Location.authorized(:view_locations).find(params['location_id']) if params['location_id'].present?
host_group = Hostgroup.authorized(:view_hostgroups).find(params['hostgroup_id']) if params["hostgroup_id"].present?
operatingsystem = Operatingsystem.authorized(:view_operatingsystems).find(params['operatingsystem_id']) if params["operatingsystem_id"].present?
if params["repo_data"].present?
repo_data = {}
params['repo_data'].each { |repo| repo_data[repo['repository']] = repo['repo_gpg_key_url'] }
end

context = {
user: User.current,
Expand All @@ -30,8 +34,7 @@ def global_registration_vars
setup_remote_execution: ActiveRecord::Type::Boolean.new.deserialize(params['setup_remote_execution']),
packages: params['packages'],
update_packages: params['update_packages'],
repo: params['repo'],
repo_gpg_key_url: params['repo_gpg_key_url'],
repo_data: repo_data,
}

params.permit(permitted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export LC_ALL=C LANG=C
<%= "\n# Remote execution interface: [#{@remote_execution_interface}]" if @remote_execution_interface.present? -%>
<%= "\n# Packages: [#{@packages}]" if @packages.present? -%>
<%= "\n# Update packages: [#{@update_packages}]" unless @update_packages.nil? -%>
<%= "\n# Repository: [#{@repo}]" if @repo.present? -%>
<%= "\n# Repository GPG key URL: [#{@repo_gpg_key_url}]" if @repo_gpg_key_url.present? -%>
<%= "\n# Repository data: [#{@repo_data}]" if @repo_data.present? -%>
<%= "\n# Force: [#{@force}]" unless @force.nil? -%>
<%= "\n# Ignore subman errors: [#{@ignore_subman_errors}]" unless @ignore_subman_errors.nil? -%>
<%= "\n# Lifecycle environment id: [#{@lifecycle_environment_id}]" if @lifecycle_environment_id.present? -%>
Expand All @@ -58,43 +57,45 @@ cleanup_and_exit() {

<%= snippet_if_exists('before_registration') -%>
<% unless @repo.blank? -%>
<% unless @repo_data.blank? -%>
<% index = 0 %>
<% @repo_data.each do |repo, repo_gpg_key_url| -%>
echo '#'
echo '# Adding repository'
echo '#'


<% index += 1 %>
if [ x$PKG_MANAGER = xdnf -o x$PKG_MANAGER = xyum -o x$PKG_MANAGER = xzypper ]; then
cat << EOF > /tmp/foreman_registration.repo
[foreman_register]
name=foreman_register
baseurl=<%= shell_escape @repo %>
[foreman_register<%= index %>]
name=foreman_register<%= index %>
baseurl=<%= shell_escape repo %>
enabled=1
type=rpm-md
EOF
<% if @repo_gpg_key_url.present? -%>
<% if repo_gpg_key_url.present? -%>
echo gpgcheck=1 >> /tmp/foreman_registration.repo
echo gpgkey=<%= shell_escape @repo_gpg_key_url %> >> /tmp/foreman_registration.repo
echo gpgkey=<%= shell_escape repo_gpg_key_url %> >> /tmp/foreman_registration.repo
<% else -%>
echo gpgcheck=0 >> /tmp/foreman_registration.repo
<% end -%>
if [ x$PKG_MANAGER = xdnf -o x$PKG_MANAGER = xyum ]; then
mv -f /tmp/foreman_registration.repo /etc/yum.repos.d/foreman_registration.repo
mv -f /tmp/foreman_registration.repo /etc/yum.repos.d/foreman_registration<%= index %>.repo
echo "Building yum metadata cache, this may take a few minutes"
$PKG_MANAGER makecache
else
zypper --quiet --non-interactive addrepo /tmp/foreman_registration.repo
fi
elif [ -f /etc/debian_version ]; then
<%= save_to_file('/etc/apt/sources.list.d/foreman_registration.list', @repo) %>
<% if @repo_gpg_key_url.present? -%>
<%= save_to_file("/etc/apt/sources.list.d/foreman_registration#{index}.list", repo) %>
<% if repo_gpg_key_url.present? -%>
<%# "apt 1.2.35" on Ubuntu 16.04 does not support storing GPG public keys in "/etc/apt/trusted.gpg.d/" in ASCII format -%>
if [ "$(. /etc/os-release ; echo "$VERSION_ID")" = "16.04" ]; then
$PKG_MANAGER_INSTALL ca-certificates curl gnupg
curl --silent --show-error <%= shell_escape @repo_gpg_key_url %> | gpg --dearmor > /etc/apt/trusted.gpg.d/client.gpg
curl --silent --show-error <%= shell_escape repo_gpg_key_url %> | gpg --dearmor > /etc/apt/trusted.gpg.d/client<%= index %>.gpg
else
$PKG_MANAGER_INSTALL ca-certificates curl
curl --silent --show-error --output /etc/apt/trusted.gpg.d/client.asc <%= shell_escape @repo_gpg_key_url %>
curl --silent --show-error --output /etc/apt/trusted.gpg.d/client<%= index %>.asc <%= shell_escape repo_gpg_key_url %>
fi
<% end -%>
apt-get update
Expand All @@ -104,6 +105,7 @@ else
cleanup_and_exit 1
fi
<% end -%>
<% end -%>

register_host() {
curl --silent --show-error --cacert $SSL_CA_CERT --request POST <%= @registration_url %> \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ Object {
"operatingsystemId": undefined,
"organizationId": undefined,
"packages": "",
"repo": "",
"repoGpgKeyUrl": "",
"repoData": Array [],
"setupInsights": "",
"setupRemoteExecution": "",
"smartProxyId": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ exports[`RegistrationCommandsPage - Advanced renders 1`] = `
updatePackages={false}
/>
<Repository
handleRepo={[Function]}
handleRepoGpgKeyUrl={[Function]}
handleRepoData={[Function]}
isLoading={false}
repo=""
repoGpgKeyUrl=""
repoData={Array []}
/>
<TokenLifeTime
handleInvalidField={[Function]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,38 @@

exports[`RegistrationCommandsPage fields - Repository renders 1`] = `
<Fragment>
<RepositoryModal
handleModalToggle={[Function]}
handleRepoData={[Function]}
id="register_host_repo_modal"
isLoading={false}
isModalOpen={false}
ouiaId="register_host_repo_modal"
repoData={Array []}
/>
<FormGroup
fieldId="reg_repo"
label="Repository"
label="Repositories"
labelIcon={
<LabelIcon
text="A repository to be added before the registration is performed. It can be useful to e.g. make the subscription-manager packages available for the purpose of the registration. For Red Hat and SUSE family distributions, this should be the URL of the repository, e.g. 'http://rpm.example.com/'. For Debian OS families, it's the whole list file content, e.g. 'deb http://deb.example.com/ buster 1.0'."
text="Repositories to be added before the registration is performed. It can be useful to e.g. make the subscription-manager packages available for the purpose of the registration. GPG keys can also be provided here if necessary."
/>
}
>
<TextInput
id="reg_repo"
isDisabled={false}
onChange={[Function]}
ouiaId="red_repo"
type="text"
value=""
/>
</FormGroup>
<FormGroup
fieldId="reg_gpg_key_url"
label="Repository GPG key URL"
labelIcon={
<LabelIcon
text="If packages are GPG signed, the public key can be specified here to verify the packages signatures. It needs to be specified in the ascii form with the GPG public key header."
/>
}
>
<TextInput
id="reg_gpg_key_url"
isDisabled={false}
onChange={[Function]}
ouiaId="reg_gpg_key_url"
type="text"
value=""
/>
<Button
icon={
<PlusCircleIcon
color="currentColor"
noVerticalAlign={false}
size="sm"
/>
}
onClick={[Function]}
ouiaId="host_reg_add_more_repositories"
variant="link"
>
Add repositories for registration (0 set)
</Button>
</FormGroup>
</Fragment>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ export const advancedComponentProps = {
handleInvalidField: () => {},
packages: '',
handlePackages: () => {},
repo: '',
repoGpgKeyUrl: '',
handleRepo: () => {},
handleRepoGpgKeyUrl: () => {},
repoData: [],
handleRepoData: () => {},
updatePackages: false,
handleUpdatePackages: () => {},
isLoading: false,
Expand Down Expand Up @@ -101,10 +99,8 @@ export const updatePackagesProps = {
};

export const repositoryProps = {
repo: '',
handleRepo: () => {},
repoGpgKeyUrl: '',
handleRepoGpgKeyUrl: () => {},
repoData: [],
handleRepoData: () => {},
isLoading: false,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ const Advanced = ({
handleInvalidField,
packages,
handlePackages,
repo,
handleRepo,
repoGpgKeyUrl,
handleRepoGpgKeyUrl,
repoData,
handleRepoData,
updatePackages,
handleUpdatePackages,
isLoading,
Expand All @@ -47,10 +45,8 @@ const Advanced = ({
isLoading={isLoading}
/>
<Repository
repo={repo}
handleRepo={handleRepo}
repoGpgKeyUrl={repoGpgKeyUrl}
handleRepoGpgKeyUrl={handleRepoGpgKeyUrl}
repoData={repoData}
handleRepoData={handleRepoData}
isLoading={isLoading}
/>
<TokenLifeTime
Expand All @@ -72,11 +68,9 @@ Advanced.propTypes = {
handleJwtExpiration: PropTypes.func.isRequired,
handleInvalidField: PropTypes.func.isRequired,
packages: PropTypes.string,
repo: PropTypes.string,
repoGpgKeyUrl: PropTypes.string,
repoData: PropTypes.array.isRequired,
handlePackages: PropTypes.func.isRequired,
handleRepo: PropTypes.func.isRequired,
handleRepoGpgKeyUrl: PropTypes.func.isRequired,
handleRepoData: PropTypes.func.isRequired,
updatePackages: PropTypes.bool,
handleUpdatePackages: PropTypes.func.isRequired,
isLoading: PropTypes.bool.isRequired,
Expand All @@ -88,8 +82,6 @@ Advanced.defaultProps = {
setupInsights: '',
jwtExpiration: 4,
packages: '',
repo: '',
repoGpgKeyUrl: '',
updatePackages: false,
};

Expand Down
Loading

0 comments on commit f04d47c

Please sign in to comment.