Skip to content

Commit

Permalink
Fixes #36688 - Provide option to use wget for the new Register Host f…
Browse files Browse the repository at this point in the history
…eature
  • Loading branch information
goarsna committed Aug 21, 2023
1 parent 9bb4bc7 commit 272fa72
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 35 deletions.
1 change: 1 addition & 0 deletions app/controllers/api/v2/registration_commands_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class RegistrationCommandsController < V2::BaseController
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/'")
param :repo_gpg_key_url, String, desc: N_("URL of the GPG key for the repository")
param :use_wget, :bool, desc: N_("Use wget instead of curl")
end
def create
unless os_with_template?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def global_registration_vars
update_packages: params['update_packages'],
repo: params['repo'],
repo_gpg_key_url: params['repo_gpg_key_url'],
use_wget: params['use_wget'],
}

params.permit(permitted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Foreman::Controller::RegistrationCommands

def command
args_query = "?#{registration_args.to_query}"
"curl -sS #{insecure} '#{registration_url(@smart_proxy)}#{args_query if args_query != '?'}' #{command_headers} | bash"
"#{application}#{insecure} '#{registration_url(@smart_proxy)}#{args_query if args_query != '?'}' #{command_headers} | bash"
end

def registration_args
Expand All @@ -16,7 +16,19 @@ def registration_args
end

def insecure
registration_params['insecure'] ? '--insecure' : ''
if useWget
registration_params['insecure'] ? ' --no-check-certificate' : ''
else
registration_params['insecure'] ? ' --insecure' : ''
end
end

def application
useWget ? 'wget --no-verbose -O-' : 'curl -sS'
end

def useWget
return registration_params['use_wget']
end

def registration_url(proxy = nil)
Expand All @@ -38,7 +50,7 @@ def command_headers
jwt_args[:expiration] = 4.hours.to_i
end

"-H 'Authorization: Bearer #{User.current.jwt_token!(**jwt_args)}'"
"--header 'Authorization: Bearer #{User.current.jwt_token!(**jwt_args)}'"
end

def host_config_params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ description: |
# Make sure, all command output can be parsed (e.g. from subscription-manager)
export LC_ALL=C LANG=C
<%
headers = ["-H 'Authorization: Bearer #{@auth_token}'"]
headers = ["--header 'Authorization: Bearer #{@auth_token}'"]
activation_keys = [(@hostgroup.params['kt_activation_keys'] if @hostgroup), @activation_keys].compact.join(',')
-%>

Expand All @@ -36,6 +36,7 @@ export LC_ALL=C LANG=C
<%= "\n# Ignore subman errors: [#{@ignore_subman_errors}]" unless @ignore_subman_errors.nil? -%>
<%= "\n# Lifecycle environment id: [#{@lifecycle_environment_id}]" if @lifecycle_environment_id.present? -%>
<%= "\n# Activation keys: [#{activation_keys}]" if activation_keys.present? -%>
<%= "\n# Use wget: [#{@use_wget}]" unless @use_wget.nil? -%>


if ! [ $(id -u) = 0 ]; then
Expand Down Expand Up @@ -84,9 +85,18 @@ EOF
fi
elif [ -f /etc/debian_version ]; then
<%= save_to_file('/etc/apt/sources.list.d/foreman_registration.list', @repo) %>

<%
if @use_wget
gpg_key_download_command="wget --no-verbose -O-"
else
gpg_key_download_command="curl --silent --show-error"
end
%>

<% if @repo_gpg_key_url.present? -%>
apt-get -y install ca-certificates gpg
curl --silent --show-error <%= shell_escape @repo_gpg_key_url %> | apt-key add -
<%= gpg_key_download_command-%> <%= shell_escape @repo_gpg_key_url %> | apt-key add -
<% end -%>
apt-get update

Expand All @@ -96,22 +106,36 @@ else
fi
<% end -%>

<%
if @use_wget
data_keyword = "--post-data"
else
data_keyword = "--data"
end

if @use_wget
registration_command="wget --no-verbose -O- --ca-certificate="
else
registration_command="curl --silent --show-error --request POST --cacert "
end
%>

register_host() {
curl --silent --show-error --cacert $SSL_CA_CERT --request POST <%= @registration_url %> \
<%= headers.join(' ') %> \
--data "host[name]=$(hostname --fqdn)" \
--data "host[build]=false" \
--data "host[managed]=false" \
<%= " --data 'host[organization_id]=#{@organization.id}' \\\n" if @organization -%>
<%= " --data 'host[location_id]=#{@location.id}' \\\n" if @location -%>
<%= " --data 'host[hostgroup_id]=#{@hostgroup.id}' \\\n" if @hostgroup -%>
<%= " --data 'host[operatingsystem_id]=#{@operatingsystem.id}' \\\n" if @operatingsystem -%>
<%= " --data host[interfaces_attributes][0][identifier]=#{shell_escape(@remote_execution_interface)} \\\n" if @remote_execution_interface.present? -%>
<%= " --data 'setup_insights=#{@setup_insights}' \\\n" unless @setup_insights.nil? -%>
<%= " --data 'setup_remote_execution=#{@setup_remote_execution}' \\\n" unless @setup_remote_execution.nil? -%>
<%= " --data remote_execution_interface=#{shell_escape(@remote_execution_interface)} \\\n" if @remote_execution_interface.present? -%>
<%= " --data packages=#{shell_escape(@packages)} \\\n" if @packages.present? -%>
<%= " --data 'update_packages=#{@update_packages}' \\\n" unless @update_packages.nil? -%>
<%= registration_command-%>$SSL_CA_CERT <%= @registration_url %> \
<%= headers.join(' ') %> \
<%= " #{data_keyword} \"host[name]=$(hostname --fqdn)\" \\\n" -%>
<%= " #{data_keyword} \"host[build]=false\" \\\n" -%>
<%= " #{data_keyword} \"host[managed]=false\" \\\n" -%>
<%= " #{data_keyword} 'host[organization_id]=#{@organization.id}' \\\n" if @organization -%>
<%= " #{data_keyword} 'host[location_id]=#{@location.id}' \\\n" if @location -%>
<%= " #{data_keyword} 'host[hostgroup_id]=#{@hostgroup.id}' \\\n" if @hostgroup -%>
<%= " #{data_keyword} 'host[operatingsystem_id]=#{@operatingsystem.id}' \\\n" if @operatingsystem -%>
<%= " #{data_keyword} host[interfaces_attributes][0][identifier]=#{shell_escape(@remote_execution_interface)} \\\n" if @remote_execution_interface.present? -%>
<%= " #{data_keyword} 'setup_insights=#{@setup_insights}' \\\n" unless @setup_insights.nil? -%>
<%= " #{data_keyword} 'setup_remote_execution=#{@setup_remote_execution}' \\\n" unless @setup_remote_execution.nil? -%>
<%= " #{data_keyword} remote_execution_interface=#{shell_escape(@remote_execution_interface)} \\\n" if @remote_execution_interface.present? -%>
<%= " #{data_keyword} packages=#{shell_escape(@packages)} \\\n" if @packages.present? -%>
<%= " #{data_keyword} 'update_packages=#{@update_packages}' \\\n" unless @update_packages.nil? -%>

}

Expand All @@ -121,21 +145,21 @@ echo "#"

<% if plugin_present?('katello') && activation_keys.present? -%>
register_katello_host(){
UUID=$(subscription-manager identity | grep --max-count 1 --only-matching '\([[:xdigit:]]\{8\}-[[:xdigit:]]\{4\}-[[:xdigit:]]\{4\}-[[:xdigit:]]\{4\}-[[:xdigit:]]\{12\}\)')
curl --silent --show-error --cacert $KATELLO_SERVER_CA_CERT --request POST "<%= @registration_url %>" \
<%= headers.join(' ') %> \
--data "uuid=$UUID" \
--data "host[build]=false" \
<%= " --data 'host[organization_id]=#{@organization.id}' \\\n" if @organization -%>
<%= " --data 'host[location_id]=#{@location.id}' \\\n" if @location -%>
<%= " --data 'host[hostgroup_id]=#{@hostgroup.id}' \\\n" if @hostgroup -%>
<%= " --data 'host[lifecycle_environment_id]=#{@lifecycle_environment_id}' \\\n" if @lifecycle_environment_id.present? -%>
<%= " --data 'setup_insights=#{@setup_insights}' \\\n" unless @setup_insights.nil? -%>
<%= " --data 'setup_remote_execution=#{@setup_remote_execution}' \\\n" unless @setup_remote_execution.nil? -%>
<%= " --data remote_execution_interface=#{shell_escape(@remote_execution_interface)} \\\n" if @remote_execution_interface.present? -%>
<%= " --data 'setup_remote_execution_pull=#{@setup_remote_execution_pull}' \\\n" unless @setup_remote_execution_pull.nil? -%>
<%= " --data packages=#{shell_escape(@packages)} \\\n" if @packages.present? -%>
<%= " --data 'update_packages=#{@update_packages}' \\\n" unless @update_packages.nil? -%>
UUID=$(subscription-manager identity | grep --max-count 1 --only-matching '\([[:xdigit:]]\{8\}-[[:xdigit:]]\{4\}-[[:xdigit:]]\{4\}-[[:xdigit:]]\{4\}-[[:xdigit:]]\{12\}\)')
<%= registration_command-%>$KATELLO_SERVER_CA_CERT "<%= @registration_url %>"" \
<%= headers.join(' ') %> \
<%= " #{data_keyword} \"uuid=$UUID\" \\\n" -%>
<%= " #{data_keyword} \"host[build]=false\" \\\n" -%>
<%= " #{data_keyword} 'host[organization_id]=#{@organization.id}' \\\n" if @organization -%>
<%= " #{data_keyword} 'host[location_id]=#{@location.id}' \\\n" if @location -%>
<%= " #{data_keyword} 'host[hostgroup_id]=#{@hostgroup.id}' \\\n" if @hostgroup -%>
<%= " #{data_keyword} 'host[lifecycle_environment_id]=#{@lifecycle_environment_id}' \\\n" if @lifecycle_environment_id.present? -%>
<%= " #{data_keyword} 'setup_insights=#{@setup_insights}' \\\n" unless @setup_insights.nil? -%>
<%= " #{data_keyword} 'setup_remote_execution=#{@setup_remote_execution}' \\\n" unless @setup_remote_execution.nil? -%>
<%= " #{data_keyword} remote_execution_interface=#{shell_escape(@remote_execution_interface)} \\\n" if @remote_execution_interface.present? -%>
<%= " #{data_keyword} 'setup_remote_execution_pull=#{@setup_remote_execution_pull}' \\\n" unless @setup_remote_execution_pull.nil? -%>
<%= " #{data_keyword} packages=#{shell_escape(@packages)} \\\n" if @packages.present? -%>
<%= " #{data_keyword} 'update_packages=#{@update_packages}' \\\n" unless @update_packages.nil? -%>

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const generalComponentProps = {
handleInsecure: () => {},
handleInvalidField: () => {},
isLoading: false,
useWget: false,
handleUseWget: () => {},
};
export const advancedComponentProps = {
configParams: {},
Expand Down Expand Up @@ -100,6 +102,12 @@ export const updatePackagesProps = {
isLoading: false,
};

export const useWgetProps = {
useWget: false,
handleUseWget: () => {},
isLoading: false,
};

export const repositoryProps = {
repo: '',
handleRepo: () => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import HostGroup from './fields/HostGroup';
import OperatingSystem from './fields/OperatingSystem';
import SmartProxy from './fields/SmartProxy';
import Insecure from './fields/Insecure';
import UseWget from './fields/UseWget';

const General = ({
organizationId,
Expand All @@ -28,6 +29,8 @@ const General = ({
handleInsecure,
handleInvalidField,
isLoading,
useWget,
handleUseWget,
}) => (
<>
<Taxonomies
Expand Down Expand Up @@ -65,6 +68,12 @@ const General = ({
isLoading={isLoading}
/>

<UseWget
useWget={useWget}
handleUseWget={handleUseWget}
isLoading={isLoading}
/>

<Insecure
insecure={insecure}
handleInsecure={handleInsecure}
Expand Down Expand Up @@ -97,6 +106,8 @@ General.propTypes = {
handleInsecure: PropTypes.func.isRequired,
handleInvalidField: PropTypes.func.isRequired,
isLoading: PropTypes.bool.isRequired,
useWget: PropTypes.bool.isRequired,
handleUseWget: PropTypes.func.isRequired,
};

General.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import PropTypes from 'prop-types';

import { FormGroup, Checkbox } from '@patternfly/react-core';
import LabelIcon from '../../../../../components/common/LabelIcon';

import { translate as __ } from '../../../../../common/I18n';

const UseWget = ({ useWget, handleUseWget, isLoading }) => (
<FormGroup fieldId="reg_use_wget">
<Checkbox
label={
<span>
{__('Use wget')}{' '}
<LabelIcon
text={__(
'If the target machine does not have curl installed, you can alternativly use wget for the whole registration process.'
)}
/>
</span>
}
id="reg_use_wget"
onChange={() => handleUseWget(!useWget)}
isDisabled={isLoading}
isChecked={useWget}
/>
</FormGroup>
);

UseWget.propTypes = {
useWget: PropTypes.bool.isRequired,
handleUseWget: PropTypes.func.isRequired,
isLoading: PropTypes.bool.isRequired,
};

export default UseWget;
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const RegistrationCommandsPage = () => {
const [repo, setRepo] = useState('');
const [repoGpgKeyUrl, setRepoGpgKeyUrl] = useState('');
const [invalidFields, setInvalidFields] = useState([]);
const [useWget, setUseWget] = useState(false);

// Command
const command = useSelector(selectCommand);
Expand Down Expand Up @@ -132,6 +133,7 @@ const RegistrationCommandsPage = () => {
repo,
repoGpgKeyUrl,
updatePackages,
useWget,
...pluginValues,
};

Expand Down Expand Up @@ -265,6 +267,8 @@ const RegistrationCommandsPage = () => {
handleInvalidField={handleInvalidField}
invalidFields={invalidFields}
isLoading={isLoading}
useWget={useWget}
handleUseWget={setUseWget}
/>

<Slot
Expand Down

0 comments on commit 272fa72

Please sign in to comment.