Class: Claws::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/claws/application.rb

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



3
4
5
# File 'lib/claws/application.rb', line 3

def initialize
  @detections = []
end

Instance Method Details

#analyze(filename, raw_contents) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/claws/application.rb', line 11

def analyze(filename, raw_contents)
  workflow = Workflow.load(raw_contents)

  file_ignores = workflow.ignores

  # enrich violations with snippets
  # skip violations if specifically ignored
  get_violations(filename, workflow).reject do |v|
    v.snippet = workflow.get_snippet(v.line)

    line_above = [1, v.line - 1].max
    ignores_for_line = file_ignores.fetch(line_above, [])
    ignores_for_line.include? v.name
  end
end

#get_job_violations(filename, workflow, job) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/claws/application.rb', line 70

def get_job_violations(filename, workflow, job)
  violations = []
  @detections.each do |detection|
    detection.on_job.each do |rule|
      violation = run_detection(
        filename:,
        detection:,
        rule:,
        workflow:,
        job:
      )

      violations << violation if violation

      next if rule.is_a? Symbol or !rule[:debug]

      enter_debug(
        result: !violation.nil?,
        expression: rule[:expression],
        values: {
          data: detection.data,
          workflow:,
          job:
        }
      )
    end
  end

  violations
end

#get_step_violations(filename, workflow, job, step) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/claws/application.rb', line 101

def get_step_violations(filename, workflow, job, step)
  violations = []

  @detections.each do |detection|
    detection.on_step.each do |rule|
      violation = run_detection(
        filename:,
        detection:,
        rule:,
        workflow:,
        job:,
        step:
      )

      violations << violation if violation

      next if rule.is_a? Symbol or !rule[:debug]

      enter_debug(
        result: !violation.nil?,
        expression: rule[:expression],
        values: {
          data: detection.data,
          workflow:,
          job:,
          step:
        }
      )
    end
  end

  violations
end

#get_violations(filename, workflow) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/claws/application.rb', line 27

def get_violations(filename, workflow)
  violations = get_workflow_violations(filename, workflow)

  workflow.jobs.each do |_job_name, job|
    violations += get_job_violations(filename, workflow, job)

    job.fetch("steps", []).each do |step|
      violations += get_step_violations(filename, workflow, job, step)
    end
  end

  violations
end

#get_workflow_violations(filename, workflow) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/claws/application.rb', line 41

def get_workflow_violations(filename, workflow)
  violations = []
  @detections.each do |detection|
    detection.on_workflow.each do |rule|
      violation = run_detection(
        filename:,
        detection:,
        rule:,
        workflow:
      )

      violations << violation if violation

      next if rule.is_a? Symbol or !rule[:debug]

      enter_debug(
        result: !violation.nil?,
        expression: rule[:expression],
        values: {
          data: detection.data,
          workflow:
        }
      )
    end
  end

  violations
end

#load_detection(detection) ⇒ Object



7
8
9
# File 'lib/claws/application.rb', line 7

def load_detection(detection)
  @detections << detection
end