Class: Mihari::Emitters::Webhook

Inherits:
Base
  • Object
show all
Defined in:
lib/mihari/emitters/webhook.rb

Constant Summary collapse

DEFAULT_TEMPLATE =
%{
    json.rule do
    json.id rule.id
    json.title rule.title
    json.description rule.description
  end

  json.artifacts artifacts.map(&:data)

  json.tags rule.tags.map(&:name)
}

Constants included from Concerns::Retriable

Concerns::Retriable::DEFAULT_CONDITION, Concerns::Retriable::RETRIABLE_ERRORS

Instance Attribute Summary collapse

Attributes inherited from Base

#rule

Attributes inherited from Actor

#options

Instance Method Summary collapse

Methods inherited from Base

inherited, #result

Methods inherited from Actor

key, key_aliases, keys, #result, #retry_exponential_backoff, #retry_interval, #retry_times, #timeout, type, #validate_configuration!

Methods included from Concerns::Retriable

#retry_on_error

Methods included from Concerns::Configurable

#configuration_keys?

Constructor Details

#initialize(rule:, options: nil, **params) ⇒ Webhook

Returns a new instance of Webhook.

Parameters:

  • rule (Mihari::Rule)
  • options (Hash, nil) (defaults to: nil)
  • params (Hash, nil)


38
39
40
41
42
43
44
45
46
47
# File 'lib/mihari/emitters/webhook.rb', line 38

def initialize(rule:, options: nil, **params)
  super(rule: rule, options: options)

  @url = Addressable::URI.parse(params[:url])
  @headers = params[:headers] || {}
  @method = params[:method] || "POST"
  @template = params[:template] || DEFAULT_TEMPLATE

  @artifacts = []
end

Instance Attribute Details

#artifactsArray<Mihari::Models::Artifact>

Returns:



19
20
21
# File 'lib/mihari/emitters/webhook.rb', line 19

def artifacts
  @artifacts
end

#headersHash (readonly)

Returns:

  • (Hash)


10
11
12
# File 'lib/mihari/emitters/webhook.rb', line 10

def headers
  @headers
end

#methodString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/mihari/emitters/webhook.rb', line 13

def method
  @method
end

#templateString (readonly)

Returns:

  • (String)


16
17
18
# File 'lib/mihari/emitters/webhook.rb', line 16

def template
  @template
end

#urlAddressable::URI? (readonly)

Returns:

  • (Addressable::URI, nil)


7
8
9
# File 'lib/mihari/emitters/webhook.rb', line 7

def url
  @url
end

Instance Method Details

#call(artifacts) ⇒ Object

Parameters:



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mihari/emitters/webhook.rb', line 68

def call(artifacts)
  return if artifacts.empty?

  @artifacts = artifacts

  # returns body to prevent Parallel issue (Parallel fails to handle HTTP:Response object)
  case method
  when "GET"
    http.get(url).body.to_s
  when "POST"
    http.post(url, json: json).body.to_s
  end
end

#configured?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/mihari/emitters/webhook.rb', line 52

def configured?
  return false if url.nil?

  %w[http https].include? url.scheme.downcase
end

#targetString

Returns:

  • (String)


61
62
63
# File 'lib/mihari/emitters/webhook.rb', line 61

def target
  URI(url).host || "N/A"
end