Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure a rule's inspec code is updated after establishing rule satisfaction or reverting change on a rule #609

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/rule_satisfactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class RuleSatisfactionsController < ApplicationController

def create
if @rule.satisfies.empty? && (@rule.satisfied_by << @satisfied_by_rule)
# Save the rule to trigger callbacks (update inspec)
@satisfied_by_rule.save
render json: { toast: "Successfully marked #{@rule.version} as satisfied by #{@satisfied_by_rule.version}." }
else
render json: {
Expand All @@ -23,6 +25,8 @@ def create

def destroy
if @rule.satisfied_by.delete(@satisfied_by_rule)
# Save the rule to trigger callbacks (update inspec)
@satisfied_by_rule.save
render json: { toast: "#{@rule.version} is no longer marked as satisfied by #{@satisfied_by_rule.version}." }
else
render json: {
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/rules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def destroy

def revert
Rule.revert(@rule, params[:audit_id], params[:fields], params[:audit_comment])
# Save the rule to trigger callbacks (update inspec)
@rule.save
render json: { toast: 'Successfully reverted history for control.' }
rescue RuleRevertError => e
render json: {
Expand Down
2 changes: 2 additions & 0 deletions app/models/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ def create_rule_satisfactions
next if sb_rule.nil?

rule.satisfied_by << sb_rule
# Save the rule to trigger callbacks (update inspec)
sb_rule.save
end
end
end
Expand Down
20 changes: 8 additions & 12 deletions app/models/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# Rules, also known as Controls, are the smallest unit of enforceable configuration found in a
# Benchmark XCCDF.
class Rule < BaseRule
attr_accessor :skip_update_inspec_code

amoeba do
# Using set review_requestor_id: nil does not work as expected, must use nullify
nullify :review_requestor_id
Expand Down Expand Up @@ -38,12 +40,10 @@ class Rule < BaseRule
association_foreign_key: :rule_id

before_validation :set_rule_id
before_save :apply_audit_comment
before_save :sort_ident, :update_inspec_code
before_save :apply_audit_comment, :sort_ident
before_destroy :prevent_destroy_if_under_review_or_locked
after_destroy :update_component_rules_count
after_save :update_component_rules_count
after_save :update_satisfied_by_inspec_code
after_save :update_component_rules_count, :update_inspec_code

validates_with RuleSatisfactionValidator
validate :cannot_be_locked_and_under_review
Expand Down Expand Up @@ -202,6 +202,9 @@ def displayed_name
end

def update_inspec_code
return if skip_update_inspec_code

self.skip_update_inspec_code = true
desc = disa_rule_descriptions.first
control = Inspec::Object::Control.new
control.add_header('# -*- encoding : utf-8 -*-')
Expand All @@ -228,14 +231,7 @@ def update_inspec_code
end
control.add_post_body(inspec_control_body) if inspec_control_body.present?
self.inspec_control_file = control.to_ruby
end

def update_satisfied_by_inspec_code
sb = satisfied_by.first
return if sb.nil?

# trigger update_inspec_code callback
sb.save
save
end

def basic_fields
Expand Down
16 changes: 7 additions & 9 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,14 @@
c.rules.order('RANDOM()').limit(c.rules.size * rand(25..35) / 100)
.update(status: 'Applicable - Configurable')

rule_satisfactions = []
rules_with_duplicates_ids = c.rules.order('RANDOM()').limit(c.rules.size * rand(5..10) / 100).pluck(:id)
c.rules.where.not(id: rules_with_duplicates_ids).order('RANDOM()')
.limit(c.rules.size * rand(10..15) / 100).pluck(:id).each do |rule_id|
rule_satisfactions << RuleSatisfaction.new(
rule_id: rule_id,
satisfied_by_rule_id: rules_with_duplicates_ids.sample
)
# Add Rule satisfaction:
# Only Applicable - Configurable rule can satisfy other rules
rule_selection = c.rules.where(status: 'Applicable - Configurable')
c.rules.where.not(status: 'Applicable - Configurable').limit(3).each do |rule|
rule.satisfied_by << rule_selection.sample
# Save the rule to trigger callbacks
rule.save
end
RuleSatisfaction.import! rule_satisfactions

# Call update last to trigger callbacks
c.rules.update(locked: true, rule_weight: '10.0', rule_severity: RuleConstants::SEVERITIES.sample)
Expand Down