Skip to content

Commit

Permalink
Fixes #34710 - Use foreman request address
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernhard committed Apr 1, 2022
1 parent 7fa5c26 commit 343d6c3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
50 changes: 36 additions & 14 deletions app/services/foreman/foreman_url_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ def default_url_options

apipie :method, 'Returns Foreman unattended URL' do
desc 'Returns URL to Foreman or Smart Proxy depending on host.subnet.template proxy configuration.'
# optional object_of: String, desc: 'template kind (provision, script, ...)'
# optional object_of: Hash, desc: 'URL parameters, use key named :unencoded for parameters which must not be URL-encoded'
required :action, String, desc: 'template kind (provision, script, ...)'
optional :params, Hash, desc: 'URL parameters, which will be URL-escaped'
optional :unescaped_params, Hash, desc: 'URL parameters, which will be added raw - not URL-escaped'
returns String, desc: "Rendered URL"
end
def foreman_url(action, params = {}, unescaped_params = {})
# Get basic stuff
config = URI.parse(Setting[:unattended_url])
url_options = foreman_url_options_from_settings_or_request(config)

host = @host
host = self if @host.nil? && self.class < Host::Base
template_proxy = host.try(:provision_interface).try(:subnet).try(:template_proxy)
options = foreman_url_options
config = options[:config]
host = options[:host]
url_options = options[:url_options]

# Set token
params[:token] = host.token.value if host.try(:build) && host.try(:token)
Expand All @@ -39,20 +37,44 @@ def foreman_url(action, params = {}, unescaped_params = {})
raw_string += unescaped_params.map { |k, v| "#{k}=#{v}" }.join('&')
end

url_options[:action] = action
url_options[:path] = config.path
render_foreman_url(host, url_options, params) + raw_string
end

apipie :method, 'Returns Foreman request address' do
desc 'Returns Foreman or Smart Proxy FQDN with port depending on host.subnet.template proxy configuration.'
returns String, desc: "Rendered request address"
end
def foreman_request_addr
url_options = foreman_url_options[:url_options]

result = url_options[:host]
result += ":#{url_options[:port]}" unless url_options[:port].to_s.empty? || url_options[:port] == 80
result
end

private

def foreman_url_options
# Get basic stuff
config = URI.parse(Setting[:unattended_url])
url_options = foreman_url_options_from_settings_or_request(config)

host = @host
host = self if @host.nil? && self.class < Host::Base
template_proxy = host.try(:provision_interface).try(:subnet).try(:template_proxy)

# Use template_url from the request if set, but otherwise look for a Template
# feature proxy, as PXE templates are written without an incoming request.
url = @template_url
url ||= foreman_url_from_templates_proxy(template_proxy) if template_proxy.present?

url_options = foreman_url_options_from_url(url) if url.present?

url_options[:action] = action
url_options[:path] = config.path
render_foreman_url(host, url_options, params) + raw_string
{ :config => config, :host => host, :url_options => url_options }
end

private

def foreman_url_options_from_settings_or_request(config)
{
:protocol => config.scheme || 'http',
Expand Down
1 change: 1 addition & 0 deletions app/services/foreman/renderer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Configuration
:snippet_if_exists,
:indent,
:foreman_server_fqdn, :foreman_server_url,
:foreman_request_addr,
:log_debug, :log_info, :log_warn, :log_error, :log_fatal,
:template_name,
:dns_lookup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ping --count 1 ${netX/gateway} || echo Ping to Gateway failed or ping command no
echo Trying to ping DNS: ${netX/dns}
ping --count 1 ${netX/dns} || echo Ping to DNS failed or ping command not available.

set boot-url tftp://${next-server}/
set boot-url tftp://<%= foreman_request_addr %>/
kernel ${boot-url}<%= @host.operatingsystem.bootfile(medium_provider,:kernel) %>

initrd <%= foreman_url('script') %> peSetup.cmd
Expand Down
12 changes: 12 additions & 0 deletions test/unit/foreman_url_renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,17 @@ class Renderer
assert_equal "#{template_server_from_proxy}/unattended/#{action}?token=#{token}", renderer.foreman_url(action)
assert_requested(:get, "https://template.proxy:8443/unattended/templateServer", times: 1)
end

test "should render foreman request addr" do
Setting[:unattended_url] = 'http://www.example.com'
renderer.host = host
assert_equal "www.example.com", renderer.foreman_request_addr
end

test "should render template_url for foreman request addr" do
renderer.host = host
renderer.template_url = "http://www.example.com"
assert_equal "www.example.com", renderer.foreman_request_addr
end
end
end

0 comments on commit 343d6c3

Please sign in to comment.