Class: ForemanOpenscap::Policy

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

Constant Summary collapse

STEPS_LIST =
[N_('Deployment Options'), N_('Policy Attributes'), N_('SCAP Content'), N_('Schedule'), N_('Locations'), N_('Organizations'), N_('Hostgroups')]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PolicyCommon

#cron_line_split, #update_period_attrs, #valid_cron_line, #valid_day_of_month, #valid_weekday

Instance Attribute Details

#current_stepObject



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

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.



9
10
11
# File 'app/models/foreman_openscap/policy.rb', line 9

def wizard_initiated=(value)
  @wizard_initiated = value
end

Class Method Details

.deploy_by_variantsObject



28
29
30
# File 'app/models/foreman_openscap/policy.rb', line 28

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

Instance Method Details

#change_deploy_type(params) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'app/models/foreman_openscap/policy.rb', line 67

def change_deploy_type(params)
  self.class.transaction do
    if params[:deploy_by] && deploy_by != params[:deploy_by]
      assign_attributes params
      ForemanOpenscap::LookupKeyOverrider.new(self).override
    end

    errors.none? && update(params)
  end
end

#current_step?(step_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def current_step?(step_name)
  current_step == step_name
end

#first_step?Boolean

Returns:

  • (Boolean)


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

def first_step?
  current_step == steps.first
end

#host_idsObject



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

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

#host_ids=(ids) ⇒ Object



98
99
100
# File 'app/models/foreman_openscap/policy.rb', line 98

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

#hostgroup_idsObject



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

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

#hostgroup_ids=(ids) ⇒ Object



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

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

#hostgroupsObject



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

def hostgroups
  ::Hostgroup.find(hostgroup_ids)
end

#hostgroups=(hostgroups) ⇒ Object



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

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

#hostsObject



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

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

#hosts=(hosts) ⇒ Object



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

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

#last_step?Boolean

Returns:

  • (Boolean)


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

def last_step?
  current_step == steps.last
end

#next_stepObject



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

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

#previous_stepObject



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

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

#rewind_stepObject



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

def rewind_step
  @current_step = previous_step
end

#scan_nameObject



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

def scan_name
  name
end

#should_validate?(step_name) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
206
207
208
209
210
211
# File 'app/models/foreman_openscap/policy.rb', line 203

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



151
152
153
# File 'app/models/foreman_openscap/policy.rb', line 151

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

#step_to_i(step_name) ⇒ Object



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

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

#stepsObject



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

def steps
  STEPS_LIST
end

#to_encObject



188
189
190
191
192
193
194
195
196
197
# File 'app/models/foreman_openscap/policy.rb', line 188

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



199
200
201
# File 'app/models/foreman_openscap/policy.rb', line 199

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

#to_htmlObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/foreman_openscap/policy.rb', line 53

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



177
178
179
180
181
182
183
184
185
186
# File 'app/models/foreman_openscap/policy.rb', line 177

def unassign_hosts(hosts)
  policy_host_assets = ForemanOpenscap::Asset.joins(:asset_policies).where(
    :assetable_type => 'Host::Base',
    :assetable_id => hosts.map(&:id),
    :foreman_openscap_asset_policies => { :policy_id => id }
  ).pluck(:id)

  self.asset_ids = self.asset_ids - policy_host_assets
  ForemanOpenscap::Asset.where(:id => policy_host_assets).destroy_all
end

#used_hostgroup_idsObject



173
174
175
# File 'app/models/foreman_openscap/policy.rb', line 173

def used_hostgroup_ids
  []
end

#used_location_idsObject



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

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



166
167
168
169
170
171
# File 'app/models/foreman_openscap/policy.rb', line 166

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)


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

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

#wizard_initiated?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'app/models/foreman_openscap/policy.rb', line 213

def wizard_initiated?
  @wizard_initiated
end