-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into extend-rule-tree
- Loading branch information
Showing
6 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
# Stores information about rules. This comes from SCAP. | ||
module V2 | ||
# Model for Rules | ||
class Fix < ApplicationRecord | ||
# FIXME: clean up after the remodel | ||
self.table_name = :fixes | ||
|
||
belongs_to :rule | ||
has_one :security_guide, through: :rule | ||
|
||
def self.from_parser(obj, existing: nil, rule_id: nil, system: nil) | ||
record = existing || new(rule_id: rule_id, system: system) | ||
|
||
record.assign_attributes(strategy: obj.strategy, disruption: obj.disruption, | ||
complexity: obj.complexity, text: obj.text) | ||
|
||
record | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
# WARNING: this module is already onboarded to APIv2 !!! | ||
|
||
module Xccdf | ||
# Methods related to saving rule fixes | ||
module Fixes | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def fixes | ||
@fixes ||= rules.flat_map do |rule| | ||
rule.op_source.fixes.map do |op_fix| | ||
existing = old_fixes[rule.id + '__' + op_fix.system] | ||
::V2::Fix.from_parser(op_fix, existing: existing, rule_id: rule.id, system: op_fix.system) | ||
end | ||
end | ||
end | ||
|
||
def save_fixes | ||
# Import the new records first with validation | ||
::V2::Fix.import!(new_fixes, ignore: true) | ||
|
||
# Update the fields on existing fixes, validation is not necessary | ||
::V2::Fix.import(old_fixes.values, | ||
on_duplicate_key_update: { | ||
conflict_target: %i[rule_id system], | ||
columns: %i[strategy disruption complexity text] | ||
}, validate: false) | ||
end | ||
|
||
private | ||
|
||
def new_fixes | ||
@new_fixes ||= fixes.select(&:new_record?) | ||
end | ||
|
||
# :nocov: | ||
def old_fixes | ||
@old_fixes ||= ::V2::Fix.where( | ||
rule_id: ::V2::Rule.where(security_guide_id: @benchmark&.id) | ||
).index_by { |fix| fix.rule_id + '__' + fix.system } | ||
end | ||
# :nocov: | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class CreateFixes < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :fixes, id: :uuid do |t| | ||
t.string :strategy | ||
t.string :disruption | ||
t.string :complexity | ||
t.string :system | ||
t.text :text | ||
t.references :rule, type: :uuid, index: true, null: false | ||
|
||
t.timestamps null: true | ||
end | ||
|
||
add_index :fixes, :system | ||
add_index :fixes, %i[rule_id system], unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters