Revision 145c81d6
Added by Dominic Cleal over 7 years ago
app/controllers/unattended_controller.rb | ||
---|---|---|
alias_method_chain f, :unattended
|
||
end
|
||
|
||
before_action :set_admin_user, :unless => Proc.new { preview? }
|
||
# We want to find out our requesting host
|
||
before_action :get_host_details, :allowed_to_install?, :except => :hostgroup_template
|
||
before_action :handle_ca, :if => Proc.new { params[:kind] == 'provision' }
|
||
... | ... | |
before_action :load_template_vars, :only => :host_template
|
||
# all of our requests should be returned in text/plain
|
||
after_action :set_content_type
|
||
before_action :set_admin_user, :only => :built
|
||
|
||
# this actions is called by each operatingsystem post/finish script - it notify us that the OS installation is done.
|
||
def built
|
||
... | ... | |
type = 'iPXE' if type == 'gPXE'
|
||
|
||
if (config = @host.provisioning_template({ :kind => type }))
|
||
if !preview?
|
||
User.as_anonymous_admin do
|
||
safe_render config
|
||
end
|
||
else
|
||
safe_render config
|
||
end
|
||
safe_render config
|
||
else
|
||
error_message = N_("unable to find %{type} template for %{host} running %{os}")
|
||
render_custom_error(:not_found, error_message, {:type => type, :host => @host.name, :os => @host.operatingsystem})
|
test/functional/unattended_controller_test.rb | ||
---|---|---|
assert_response :success
|
||
end
|
||
|
||
test "template with host parameters should return parameters values" do
|
||
host_param = FactoryGirl.create(:host_parameter, :host => @ub_host, :name => 'my_param')
|
||
@request.env["HTTP_X_RHN_PROVISIONING_MAC_0"] = "eth0 #{@ub_host.mac}"
|
||
ProvisioningTemplate.any_instance.stubs(:template).returns("param: <%= @host.params['my_param'] %>")
|
||
get :host_template, {:kind => 'provision'}
|
||
assert_match "param: #{host_param.value}", @response.body
|
||
context "template with host parameters" do
|
||
setup do
|
||
@host_param = FactoryGirl.create(:host_parameter, :host => @rh_host, :name => 'my_param')
|
||
@secret_param = FactoryGirl.create(:host_parameter, :host => @rh_host, :name => 'secret_param')
|
||
setup_user 'view', 'hosts'
|
||
setup_user 'view', 'params', 'name = my_param'
|
||
@rh_host.provisioning_template(:kind => :provision).update_attribute(:template, "params: <%= @host.params['my_param'] %>, <%= @host.params['secret_param'] %>")
|
||
end
|
||
|
||
test "in preview should only show permitted parameters" do
|
||
get :host_template, {:kind => 'provision', :hostname => @rh_host.name}, set_session_user(:one)
|
||
assert_equal "params: #{@host_param.value}, ", @response.body
|
||
end
|
||
|
||
test "in unattended mode should show all parameters" do
|
||
@request.env["HTTP_X_RHN_PROVISIONING_MAC_0"] = "eth0 #{@rh_host.mac}"
|
||
get :host_template, {:kind => 'provision'}
|
||
assert_equal "params: #{@host_param.value}, #{@secret_param.value}", @response.body
|
||
end
|
||
|
||
context "and ptable with host parameters" do
|
||
setup do
|
||
@rh_host.ptable.update_attribute(:template, "params: <%= @host.params['my_param'] %>, <%= @host.params['secret_param'] %>")
|
||
@rh_host.provisioning_template(:kind => :provision).update_attribute(:template, "ptable: <%= @host.diskLayout %>\nparams: <%= @host.params['my_param'] %>, <%= @host.params['secret_param'] %>")
|
||
end
|
||
|
||
test "in preview should only show permitted parameters" do
|
||
get :host_template, {:kind => 'provision', :hostname => @rh_host.name}, set_session_user(:one)
|
||
assert_equal "ptable: params: #{@host_param.value}, \nparams: #{@host_param.value}, ", @response.body
|
||
end
|
||
|
||
test "in unattended mode should show all parameters" do
|
||
@request.env["HTTP_X_RHN_PROVISIONING_MAC_0"] = "eth0 #{@rh_host.mac}"
|
||
get :host_template, {:kind => 'provision'}
|
||
assert_equal "ptable: params: #{@host_param.value}, #{@secret_param.value}\nparams: #{@host_param.value}, #{@secret_param.value}", @response.body
|
||
end
|
||
end
|
||
end
|
||
|
||
context "location or organizations are not enabled" do
|
Also available in: Unified diff
fixes #17395 - use admin user for load_template_vars
When `load_template_vars` is called from before_action, the current user
must already be set to ensure caching of parameters inside Host#params
is done with consistent permissions. Otherwise when Foreman::Renderer's
kickstart_attributes (or similar) renders a ptable that uses params,
the method will cache an empty parameters hash.
(cherry picked from commit f8a2b268619722b7b696ab5ab000d2b0e5ae42df)