Class: Inspec::Runner
- Inherits:
-
Object
- Object
- Inspec::Runner
- Extended by:
- Forwardable
- Defined in:
- lib/inspec/runner.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #add_content(tests, libs, options = {}) ⇒ Object
- #add_profile(profile, options = {}) ⇒ Object
- #add_target(target, options = {}) ⇒ Object
- #configure_transport ⇒ Object
- #create_context(options = {}) ⇒ Object
-
#initialize(conf = {}) ⇒ Runner
constructor
A new instance of Runner.
-
#load_attributes(options) ⇒ Object
determine all attributes before the execution, fetch data from secrets backend.
- #normalize_map(hm) ⇒ Object
- #supports_profile?(profile) ⇒ Boolean
- #tests ⇒ Object
Constructor Details
#initialize(conf = {}) ⇒ Runner
Returns a new instance of Runner.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/inspec/runner.rb', line 20 def initialize(conf = {}) @rules = {} @conf = conf.dup @conf[:logger] ||= Logger.new(nil) @test_collector = @conf.delete(:test_collector) || begin require 'inspec/runner_rspec' RunnerRspec.new(@conf) end # list of profile attributes @attributes = [] load_attributes(@conf) configure_transport end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
19 20 21 |
# File 'lib/inspec/runner.rb', line 19 def attributes @attributes end |
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
19 20 21 |
# File 'lib/inspec/runner.rb', line 19 def backend @backend end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
19 20 21 |
# File 'lib/inspec/runner.rb', line 19 def rules @rules end |
Instance Method Details
#add_content(tests, libs, options = {}) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/inspec/runner.rb', line 118 def add_content(tests, libs, = {}) return if tests.nil? || tests.empty? # load all libraries ctx = create_context() ctx.load_libraries(libs.map { |x| [x[:content], x[:ref], x[:line]] }) # hand the context to the profile for further evaluation unless (profile = [:profile]).nil? profile.runner_context = ctx end # evaluate the test content tests = [tests] unless tests.is_a? Array tests.each { |t| add_test_to_context(t, ctx) } # merge all collect all attributes @attributes |= ctx.attributes # process the resulting rules filter_controls(ctx.rules, [:controls]).each do |rule_id, rule| register_rule(rule_id, rule) end end |
#add_profile(profile, options = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/inspec/runner.rb', line 92 def add_profile(profile, = {}) return if ![:ignore_supports] && !supports_profile?(profile) @test_collector.add_profile(profile) [:metadata] = profile. [:profile] = profile libs = profile.libraries.map do |k, v| { ref: k, content: v } end tests = profile.tests.map do |ref, content| r = profile.source_reader.target.abs_path(ref) { ref: r, content: content } end add_content(tests, libs, ) end |
#add_target(target, options = {}) ⇒ Object
69 70 71 72 73 |
# File 'lib/inspec/runner.rb', line 69 def add_target(target, = {}) profile = Inspec::Profile.for_target(target, ) fail "Could not resolve #{target} to valid input." if profile.nil? add_profile(profile, ) end |
#configure_transport ⇒ Object
49 50 51 52 |
# File 'lib/inspec/runner.rb', line 49 def configure_transport @backend = Inspec::Backend.create(@conf) @test_collector.backend = @backend end |
#create_context(options = {}) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/inspec/runner.rb', line 111 def create_context( = {}) = [:metadata] profile_id = nil profile_id = .params[:name] unless .nil? Inspec::ProfileContext.new(profile_id, @backend, @conf.merge()) end |
#load_attributes(options) ⇒ Object
determine all attributes before the execution, fetch data from secrets backend
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/inspec/runner.rb', line 55 def load_attributes() attributes = {} # read endpoints for secrets eg. yml file secrets_targets = ['attrs'] unless secrets_targets.nil? secrets_targets.each do |target| secrets = Inspec::SecretsBackend.resolve(target) # merge hash values attributes = attributes.merge(secrets.attributes) unless secrets.nil? || secrets.attributes.nil? end end ['attributes'] = attributes end |
#normalize_map(hm) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/inspec/runner.rb', line 41 def normalize_map(hm) res = {} hm.each {|k, v| res[k.to_s] = v } res end |
#supports_profile?(profile) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/inspec/runner.rb', line 75 def supports_profile?(profile) return true if profile..nil? if !profile..supports_runtime? fail 'This profile requires InSpec version '\ "#{profile..inspec_requirement}. You are running "\ "InSpec v#{Inspec::VERSION}.\n" end if !profile..supports_transport?(@backend) os_info = @backend.os[:name].to_s fail "This OS/platform (#{os_info}) is not supported by this profile." end true end |
#tests ⇒ Object
37 38 39 |
# File 'lib/inspec/runner.rb', line 37 def tests @test_collector.tests end |