/
registration.rb
137 lines (111 loc) · 4.54 KB
/
registration.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module Foreman::Controller::Registration
extend ActiveSupport::Concern
def find_global_registration
template_name = Setting[:default_global_registration_item]
@provisioning_template = ProvisioningTemplate.unscoped.find_by(name: template_name)
@global_registration_vars = global_registration_vars if @provisioning_template
end
private
def global_registration_vars
permitted = Foreman::Plugin.all
.map(&:allowed_registration_vars)
.flatten.compact.uniq
organization = Organization.authorized(:view_organizations).find(params['organization_id']) if params['organization_id'].present?
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?
context = {
user: User.current,
auth_token: api_authorization_token,
organization: (organization || User.current.default_organization || User.current.my_organizations.first),
location: (location || User.current.default_location || User.current.my_locations.first),
hostgroup: host_group,
operatingsystem: operatingsystem,
setup_insights: ActiveRecord::Type::Boolean.new.deserialize(params['setup_insights']),
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'],
}
params.permit(permitted)
.to_h
.symbolize_keys
.merge(context)
.merge(context_urls)
end
def safe_render(template)
render plain: template.render(host: @host, params: params)
rescue StandardError => e
Foreman::Logging.exception("Error rendering the #{template.name} template", e)
message = N_("There was an error rendering the %{name} template: %{error}") % { name: template.name, error: e }
render_error(message, status: :internal_server_error)
end
def render_error(error, options = {})
locals_exception = options&.dig(:locals, :exception)
locals_message = options&.dig(:locals, :message)
output = <<~ERROR
echo "ERROR: #{error}";
#{"echo \"#{locals_exception}\";" if locals_exception}
#{"echo \"#{locals_message}\";" if locals_message}
exit 1
ERROR
render plain: output.squeeze("\n"), status: options[:status]
end
def not_found(options = nil)
nf_opts = { locals: {} }
nf_opts[:status] = :not_found
case options
when String
nf_opts[:locals][:message] = options
when Hash
nf_opts[:locals].merge! options
else
render_error 'not_found', nf_opts
return false
end
render_error 'not_found', nf_opts
false
end
def registration_url
uri = if params[:url].present?
URI.join(params[:url], '/register')
else
URI(register_url)
end
return uri if uri.scheme && uri.host
msg = N_('URL in :url parameter is missing a scheme, please set http:// or https://')
fail Foreman::Exception.new(msg)
end
def context_urls
{ registration_url: registration_url }
end
def setup_host_params
clean_host_params
setup_host_param('host_registration_insights', params['setup_insights'])
setup_host_param('host_registration_remote_execution', params['setup_remote_execution'])
setup_host_param('host_packages', params['packages'], 'string')
setup_host_param('host_update_packages', params['update_packages'])
end
def clean_host_params
names = ['host_registration_insights', 'host_registration_remote_execution',
'host_packages', 'host_update_packages']
HostParameter.where(host: @host, name: names).destroy_all
end
def setup_host_param(name, value, key_type = 'boolean')
return if value.to_s.blank?
hp_value = if key_type == 'boolean'
ActiveRecord::Type::Boolean.new.deserialize(value)
else
value
end
HostParameter.create(host: @host, name: name, value: hp_value, key_type: key_type)
end
def api_authorization_token
scope = [{
controller: :registration,
actions: [:global, :host],
}]
User.current.jwt_token!(expiration: 4.hours.to_i, scope: scope)
end
end