Class: Aikido::Zen::Scan

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/scan.rb

Overview

Scans track information about a single call made by one of our Sinks including whether it was detected as an attack or how long it took.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sink:, context:) ⇒ Scan

Returns a new instance of Scan.

Parameters:



26
27
28
29
30
31
# File 'lib/aikido/zen/scan.rb', line 26

def initialize(sink:, context:)
  @sink = sink
  @context = context
  @errors = []
  @performed = false
end

Instance Attribute Details

#attackAikido::Zen::Attack? (readonly)

Returns a detected Attack, or nil if the scan was considered safe.

Returns:



16
17
18
# File 'lib/aikido/zen/scan.rb', line 16

def attack
  @attack
end

#contextAikido::Zen::Context (readonly)

Returns the current Context, wrapping the HTTP request during which this scan was performed.

Returns:

  • (Aikido::Zen::Context)

    the current Context, wrapping the HTTP request during which this scan was performed.



12
13
14
# File 'lib/aikido/zen/scan.rb', line 12

def context
  @context
end

#durationFloat? (readonly)

Returns duration in (fractional) seconds of the scan.

Returns:

  • (Float, nil)

    duration in (fractional) seconds of the scan.



19
20
21
# File 'lib/aikido/zen/scan.rb', line 19

def duration
  @duration
end

#errorsArray<Hash> (readonly)

Returns list of captured exceptions while scanning.

Returns:

  • (Array<Hash>)

    list of captured exceptions while scanning.



22
23
24
# File 'lib/aikido/zen/scan.rb', line 22

def errors
  @errors
end

#sinkAikido::Zen::Sink (readonly)

Returns the originating Sink.

Returns:



8
9
10
# File 'lib/aikido/zen/scan.rb', line 8

def sink
  @sink
end

Instance Method Details

#attack?Boolean

Returns whether this scan detected an Attack.

Returns:

  • (Boolean)

    whether this scan detected an Attack.



38
39
40
# File 'lib/aikido/zen/scan.rb', line 38

def attack?
  @attack != nil
end

#errors?Boolean

Returns whether any errors were caught by this Scan.

Returns:

  • (Boolean)

    whether any errors were caught by this Scan.



43
44
45
# File 'lib/aikido/zen/scan.rb', line 43

def errors?
  @errors.any?
end

#performvoid

This method returns an undefined value.

Runs a block of code, capturing its return value as the potential Attack object (or nil, if safe), and how long it took to run.

Yield Returns:



52
53
54
55
56
57
58
# File 'lib/aikido/zen/scan.rb', line 52

def perform
  @performed = true
  started_at = monotonic_time
  @attack = yield
ensure
  @duration = monotonic_time - started_at
end

#performed?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/aikido/zen/scan.rb', line 33

def performed?
  @performed
end

#track_error(error, scanner) ⇒ nil

Keep track of exceptions encountered during scanning.

Parameters:

  • error (Exception)
  • scanner (#call)

Returns:

  • (nil)


66
67
68
69
# File 'lib/aikido/zen/scan.rb', line 66

def track_error(error, scanner)
  @errors << {error: error, scanner: scanner}
  nil
end