Class: ForemanOpenscap::Policy

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Authorizable, Taxonomix
Defined in:
app/models/foreman_openscap/policy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_stepObject



106
107
108
# File 'app/models/foreman_openscap/policy.rb', line 106

def current_step
  @current_step || steps.first
end

#wizard_initiated=(value) ⇒ Object (writeonly)

Sets the attribute wizard_initiated

Parameters:

  • value

    the value to set the attribute wizard_initiated to.



7
8
9
# File 'app/models/foreman_openscap/policy.rb', line 7

def wizard_initiated=(value)
  @wizard_initiated = value
end

Class Method Details

.deploy_by_variantsObject



25
26
27
# File 'app/models/foreman_openscap/policy.rb', line 25

def self.deploy_by_variants
  %w[puppet ansible manual]
end

Instance Method Details

#current_step?(step_name) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/models/foreman_openscap/policy.rb', line 127

def current_step?(step_name)
  current_step == step_name
end

#first_step?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/foreman_openscap/policy.rb', line 123

def first_step?
  current_step == steps.first
end

#host_idsObject



79
80
81
# File 'app/models/foreman_openscap/policy.rb', line 79

def host_ids
  assets.where(:assetable_type => 'Host::Base').pluck(:assetable_id)
end

#host_ids=(ids) ⇒ Object



83
84
85
# File 'app/models/foreman_openscap/policy.rb', line 83

def host_ids=(ids)
  assign_ids ids, 'Host::Base'
end

#hostgroup_idsObject



63
64
65
# File 'app/models/foreman_openscap/policy.rb', line 63

def hostgroup_ids
  assets.where(:assetable_type => 'Hostgroup').pluck(:assetable_id)
end

#hostgroup_ids=(ids) ⇒ Object



67
68
69
# File 'app/models/foreman_openscap/policy.rb', line 67

def hostgroup_ids=(ids)
  assign_ids ids, 'Hostgroup'
end

#hostgroupsObject



71
72
73
# File 'app/models/foreman_openscap/policy.rb', line 71

def hostgroups
  Hostgroup.find(hostgroup_ids)
end

#hostgroups=(hostgroups) ⇒ Object



75
76
77
# File 'app/models/foreman_openscap/policy.rb', line 75

def hostgroups=(hostgroups)
  hostgroup_ids = hostgroups.map(&:id).map(&:to_s)
end

#hostsObject



87
88
89
# File 'app/models/foreman_openscap/policy.rb', line 87

def hosts
  Host.where(:id => host_ids)
end

#hosts=(hosts) ⇒ Object



91
92
93
# File 'app/models/foreman_openscap/policy.rb', line 91

def hosts=(hosts)
  host_ids = hosts.map(&:id).map(&:to_s)
end

#last_step?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/foreman_openscap/policy.rb', line 131

def last_step?
  current_step == steps.last
end

#next_stepObject



115
116
117
# File 'app/models/foreman_openscap/policy.rb', line 115

def next_step
  steps[steps.index(current_step) + 1]
end

#previous_stepObject



110
111
112
113
# File 'app/models/foreman_openscap/policy.rb', line 110

def previous_step
  return steps.last if current_step.empty?
  steps[steps.index(current_step) - 1]
end

#rewind_stepObject



119
120
121
# File 'app/models/foreman_openscap/policy.rb', line 119

def rewind_step
  @current_step = previous_step
end

#scan_nameObject



143
144
145
# File 'app/models/foreman_openscap/policy.rb', line 143

def scan_name
  name
end

#should_validate?(step_name) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
193
# File 'app/models/foreman_openscap/policy.rb', line 185

def should_validate?(step_name)
  if new_record? && wizard_initiated?
    step_index > step_to_i(step_name)
  elsif new_record? && !wizard_initiated?
    true
  else
    persisted?
  end
end

#step_indexObject



139
140
141
# File 'app/models/foreman_openscap/policy.rb', line 139

def step_index
  wizard_completed? ? steps.index(steps.last) : steps.index(current_step) + 1
end

#step_to_i(step_name) ⇒ Object



95
96
97
# File 'app/models/foreman_openscap/policy.rb', line 95

def step_to_i(step_name)
  steps.index(step_name) + 1
end

#stepsObject



99
100
101
102
103
104
# File 'app/models/foreman_openscap/policy.rb', line 99

def steps
  base_steps = [N_('Deployment Options'), N_('Policy Attributes'), N_('SCAP Content'), N_('Schedule')]
  base_steps << N_('Locations') if SETTINGS[:locations_enabled]
  base_steps << N_('Organizations') if SETTINGS[:organizations_enabled]
  base_steps << N_('Hostgroups') # always be last.
end

#to_encObject



170
171
172
173
174
175
176
177
178
179
# File 'app/models/foreman_openscap/policy.rb', line 170

def to_enc
  {
    'id'            => self.id,
    'profile_id'    => profile_for_scan,
    'content_path'  => "/var/lib/openscap/content/#{self.scap_content.digest}.xml",
    'tailoring_path' => tailoring_file ? "/var/lib/openscap/tailoring/#{self.tailoring_file.digest}.xml" : '',
    'download_path' => "/compliance/policies/#{self.id}/content/#{scap_content.digest}",
    'tailoring_download_path' => tailoring_file ? "/compliance/policies/#{self.id}/tailoring/#{tailoring_file.digest}" : ''
  }.merge(period_enc)
end

#to_enc_legacyObject



181
182
183
# File 'app/models/foreman_openscap/policy.rb', line 181

def to_enc_legacy
  to_enc.tap { |hash| hash['download_path'] = "/compliance/policies/#{self.id}/content" }
end

#to_htmlObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/foreman_openscap/policy.rb', line 49

def to_html
  if scap_content.nil?
    return html_error_message(_('Cannot generate HTML guide, scap content is missing.'))
  end

  if (proxy = scap_content.proxy_url)
    api = ProxyAPI::Openscap.new(:url => proxy)
  else
    return html_error_message(_('Cannot generate HTML guide, no valid OpenSCAP proxy server found.'))
  end

  api.policy_html_guide(scap_content.scap_file, scap_content_profile.try(:profile_id))
end

#unassign_hosts(hosts) ⇒ Object



165
166
167
168
# File 'app/models/foreman_openscap/policy.rb', line 165

def unassign_hosts(hosts)
  host_asset_ids = ForemanOpenscap::Asset.where(:assetable_type => 'Host::Base', :assetable_id => hosts.map(&:id)).pluck(:id)
  self.asset_ids = self.asset_ids - host_asset_ids
end

#update_period_attrsObject



199
200
201
202
203
204
205
206
207
208
# File 'app/models/foreman_openscap/policy.rb', line 199

def update_period_attrs
  case period
  when 'monthly'
    erase_period_attrs(%w[cron_line weekday])
  when 'weekly'
    erase_period_attrs(%w[cron_line day_of_month])
  when 'custom'
    erase_period_attrs(%w[weekday day_of_month])
  end
end

#used_hostgroup_idsObject



161
162
163
# File 'app/models/foreman_openscap/policy.rb', line 161

def used_hostgroup_ids
  []
end

#used_location_idsObject



147
148
149
150
151
152
# File 'app/models/foreman_openscap/policy.rb', line 147

def used_location_ids
  Location.joins(:taxable_taxonomies).where(
    'taxable_taxonomies.taxable_type' => 'ForemanOpenscap::Policy',
    'taxable_taxonomies.taxable_id'   => id
  ).pluck("#{Location.arel_table.name}.id")
end

#used_organization_idsObject



154
155
156
157
158
159
# File 'app/models/foreman_openscap/policy.rb', line 154

def used_organization_ids
  Organization.joins(:taxable_taxonomies).where(
    'taxable_taxonomies.taxable_type' => 'ForemanOpenscap::Policy',
    'taxable_taxonomies.taxable_id'   => id
  ).pluck("#{Location.arel_table.name}.id")
end

#wizard_completed?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'app/models/foreman_openscap/policy.rb', line 135

def wizard_completed?
  new_record? && current_step.blank?
end

#wizard_initiated?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'app/models/foreman_openscap/policy.rb', line 195

def wizard_initiated?
  @wizard_initiated
end