Class: InspecDelta::Object::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec_delta/objects/profile.rb

Overview

This class will take care of operations related to profile manipulation

Instance Method Summary collapse

Constructor Details

#initialize(profile_path) ⇒ Profile

Internal: Initializes the Profile object upon instantiation

profile_path: String, path to the inspec profile’s root directory. _options: Unused, contains extraneous parameters that may be passed to the initializer

Returns: Nothing

Raises:

  • (StandardError)


15
16
17
18
# File 'lib/inspec_delta/objects/profile.rb', line 15

def initialize(profile_path)
  @profile_path = profile_path
  raise StandardError, "Profile directory at #{@profile_path} not found" unless Dir.exist?(@profile_path)
end

Instance Method Details

#create_new_control_file(profile_control_path, benchmark_control) ⇒ Object

Creates a control file with the string representation of the benchmark control



64
65
66
# File 'lib/inspec_delta/objects/profile.rb', line 64

def create_new_control_file(profile_control_path, benchmark_control)
  File.open(profile_control_path, 'w') { |f| f.puts benchmark_control.to_ruby }
end

#formatObject

Formats a the ruby controls using rubocop with the rubo config file in the profile

Raises:

  • (StandardError)


22
23
24
25
26
27
28
# File 'lib/inspec_delta/objects/profile.rb', line 22

def format
  control_dir = File.join(File.expand_path(@profile_path), 'controls')
  rubo_file = File.join(File.expand_path(@profile_path), '.rubocop.yml')
  raise StandardError, "Rubocop configuration file at #{rubo_file} not found" unless File.exist?(rubo_file)

  `rubocop -a #{control_dir} -c #{rubo_file}`
end

#update(stig_file_path) ⇒ Object

Updates a profile metadata with definitions from a STIG xml file

Raises:

  • (StandardError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/inspec_delta/objects/profile.rb', line 34

def update(stig_file_path)
  raise StandardError, "STIG file at #{stig_file_path} not found" unless File.exist?(stig_file_path)

  control_dir = "#{@profile_path}/controls"
  benchmark = InspecDelta::Parser::Benchmark.get_benchmark(stig_file_path)
  benchmark.each do |control_id, control|
    benchmark_control = InspecDelta::Object::Control.from_benchmark(control)
    profile_control_path = File.join(File.expand_path(control_dir), "#{control_id}.rb")
    if File.file?(profile_control_path)
      update_existing_control_file(profile_control_path, benchmark_control)
    else
      create_new_control_file(profile_control_path, benchmark_control)
    end
  end
end

#update_existing_control_file(profile_control_path, benchmark_control) ⇒ Object

Updates a control file with the updates from the stig



54
55
56
57
58
# File 'lib/inspec_delta/objects/profile.rb', line 54

def update_existing_control_file(profile_control_path, benchmark_control)
  profile_control = InspecDelta::Object::Control.from_ruby(profile_control_path)
  updated_control = profile_control.apply_updates(benchmark_control)
  File.open(profile_control_path, 'w') { |f| f.puts updated_control }
end