Class: Necropsy::Guardrail::Baseline

Inherits:
Object
  • Object
show all
Defined in:
lib/necropsy/guardrail/baseline.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, findings:) ⇒ Baseline

Returns a new instance of Baseline.



37
38
39
40
41
# File 'lib/necropsy/guardrail/baseline.rb', line 37

def initialize(path:, findings:)
  @path = path
  @findings = findings
  @fingerprints = findings.filter_map { |finding| finding['fingerprint'] }.to_set
end

Instance Attribute Details

#fingerprintsObject (readonly)

Returns the value of attribute fingerprints.



9
10
11
# File 'lib/necropsy/guardrail/baseline.rb', line 9

def fingerprints
  @fingerprints
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/necropsy/guardrail/baseline.rb', line 9

def path
  @path
end

Class Method Details

.load(path) ⇒ Object



11
12
13
14
15
16
# File 'lib/necropsy/guardrail/baseline.rb', line 11

def self.load(path)
  return new(path: path, findings: []) unless File.exist?(path)

  payload = YAML.safe_load_file(path, aliases: false) || {}
  new(path: path, findings: Array(payload['findings']))
end

.write(report, path:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/necropsy/guardrail/baseline.rb', line 18

def self.write(report, path:)
  findings = report.findings.map do |finding|
    {
      'fingerprint' => finding.fingerprint,
      'classification' => finding.classification.to_s,
      'confidence' => finding.confidence.to_s,
      'node_id' => finding.node.id,
      'file' => finding.node.file,
      'line' => finding.node.line
    }
  end
  payload = {
    'version' => 1,
    'generated_at' => Time.now.utc.iso8601,
    'findings' => findings
  }
  File.write(path, payload.to_yaml)
end

Instance Method Details

#count_at_least(confidence) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/necropsy/guardrail/baseline.rb', line 47

def count_at_least(confidence)
  threshold = CONFIDENCE_LEVELS.fetch(confidence)
  @findings.count do |finding|
    level = finding['confidence']&.to_sym
    level && CONFIDENCE_LEVELS.fetch(level, -1) >= threshold
  end
end

#include?(finding) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/necropsy/guardrail/baseline.rb', line 43

def include?(finding)
  fingerprints.include?(finding.fingerprint)
end