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

Fix: Capture STIG Name on Upload #603

Merged
merged 1 commit into from
Aug 24, 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
5 changes: 3 additions & 2 deletions app/models/security_requirements_guide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def self.from_mapping(benchmark_mapping)
"#{SecurityRequirementsGuide.revision(benchmark_mapping.plaintext.first)}" rescue nil
release_date = SecurityRequirementsGuide.release_date(benchmark_mapping.plaintext.first)
# rubocop:enable Style/RescueModifier

SecurityRequirementsGuide.new(srg_id: id, title: title, version: version, release_date: release_date)
name = id&.tr('_', ' ')&.gsub(/(?<=\d)-/, '.')
name = "#{name} - Ver #{version.to_s[1]}, Rel #{version.to_s.last}"
SecurityRequirementsGuide.new(srg_id: id, title: title, name: name, version: version, release_date: release_date)
end

# If the SRGs do not conform nicely and this function gets complex, remove the version parse logic
Expand Down
7 changes: 5 additions & 2 deletions app/models/stig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Stig < ApplicationRecord
has_many :stig_rules, dependent: :destroy

validates :stig_id, :title, :version, :xml, presence: true
validates :stig_id, :title, :name, :version, :xml, presence: true
validates :stig_id, uniqueness: {
scope: :version,
message: 'ID has already been taken'
Expand All @@ -19,8 +19,11 @@ def self.from_mapping(benchmark_mapping)
"#{SecurityRequirementsGuide.revision(benchmark_mapping.plaintext.first)}"
benchmark_date = SecurityRequirementsGuide.release_date(benchmark_mapping.plaintext.first)
description = benchmark_mapping&.description&.first
name = id&.tr('_', ' ')&.gsub(/(?<=\d)-/, '.')
name = "#{name} - Ver #{version.to_s[1]}, Rel #{version.to_s.last}"

Stig.new(stig_id: id, title: title, version: version, description: description, benchmark_date: benchmark_date)
Stig.new(stig_id: id, title: title, name: name, version: version, description: description,
benchmark_date: benchmark_date)
end

def parsed_benchmark
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230824150144_make_name_not_null_in_stigs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MakeNameNotNullInStigs < ActiveRecord::Migration[6.1]
def change
change_column_null :stigs, :name, false
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_08_14_135633) do
ActiveRecord::Schema.define(version: 2023_08_24_150144) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -276,7 +276,7 @@
t.date "benchmark_date"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "name"
t.string "name", null: false
t.index ["stig_id", "version"], name: "stigs_stig_id_version_index", unique: true
end

Expand Down
4 changes: 1 addition & 3 deletions lib/tasks/stig_and_srg_puller.rake
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ namespace :stig_and_srg_puller do
new_object = model.from_mapping(parsed_benchmark)
new_object.xml = Nokogiri::XML(xml)
id = model == Stig ? new_object.stig_id : new_object.srg_id
name = id.tr('_', ' ').gsub(/(?<=\d)-/, '.')
name = "#{name} - Ver #{new_object.version[1]}, Rel #{new_object.version.last}"
new_object.name = item[:file_present] ? item[:name] : name

if new_object.save
@new_items += 1
puts "Successfully pulled and saved #{new_object.name}"
Expand Down
2 changes: 2 additions & 0 deletions spec/controllers/stigs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
sign_in user
stig2 = Stig.from_mapping(Xccdf::Benchmark.parse(stig.xml))
stig2.xml = stig.xml
stig2.name = stig.name
stig2.save!

expect do
Expand All @@ -42,6 +43,7 @@
sign_in user2
stig2 = Stig.from_mapping(Xccdf::Benchmark.parse(stig.xml))
stig2.xml = stig.xml
stig2.name = stig.name
stig2.save!

expect do
Expand Down
5 changes: 3 additions & 2 deletions spec/factories/stigs.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

XML_FILE = File.read('./spec/fixtures/files/U_A10_Networks_ADC_ALG_STIG_V2R1_Manual-xccdf.xml')
XML_FILE_STIG = File.read('./spec/fixtures/files/U_A10_Networks_ADC_ALG_STIG_V2R1_Manual-xccdf.xml')

FactoryBot.define do
factory :stig do
stig_id { FFaker::Name.name.underscore }
name { FFaker::Name.name }
title { FFaker::Name.name }
description { 'MyText' }
version { "V#{rand(0..9)}R#{rand(0..9)}" }
xml { XML_FILE }
xml { XML_FILE_STIG }
benchmark_date { '2023-07-20' }
end
end
1 change: 1 addition & 0 deletions spec/models/stig_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
it 'validates presence of stig_id, title, version, and xml' do
expect(stig.stig_id).to be_present
expect(stig.title).to be_present
expect(stig.name).to be_present
expect(stig.version).to be_present
expect(stig.xml).to be_present
end
Expand Down
Loading