Class: Mihari::Emitters::Slack

Inherits:
Base show all
Defined in:
lib/mihari/emitters/slack.rb

Constant Summary collapse

DEFAULT_CHANNEL =
"#general"
DEFAULT_USERNAME =
"Mihari"

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, #parallel?, #result

Methods inherited from Actor

configuration_keys, 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) ⇒ Slack

Returns a new instance of Slack.

Parameters:

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


142
143
144
145
146
147
148
149
150
# File 'lib/mihari/emitters/slack.rb', line 142

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

  @webhook_url = params[:webhook_url] || Mihari.config.slack_webhook_url
  @channel = params[:channel] || Mihari.config.slack_channel || DEFAULT_CHANNEL
  @username = DEFAULT_USERNAME

  @artifacts = []
end

Instance Attribute Details

#artifactsArray<Mihari::Models::Artifact>

Returns:



135
136
137
# File 'lib/mihari/emitters/slack.rb', line 135

def artifacts
  @artifacts
end

#channelString (readonly)

Returns:

  • (String)


129
130
131
# File 'lib/mihari/emitters/slack.rb', line 129

def channel
  @channel
end

#usernameString (readonly)

Returns:

  • (String)


132
133
134
# File 'lib/mihari/emitters/slack.rb', line 132

def username
  @username
end

#webhook_urlString? (readonly)

Returns:

  • (String, nil)


126
127
128
# File 'lib/mihari/emitters/slack.rb', line 126

def webhook_url
  @webhook_url
end

Instance Method Details

#attachmentsArray<Mihari::Emitters::Attachment>

Build attachments

Returns:



191
192
193
# File 'lib/mihari/emitters/slack.rb', line 191

def attachments
  artifacts.map { |artifact| Attachment.new(data: artifact.data, data_type: artifact.data_type).to_a }.flatten
end

#call(artifacts) ⇒ Object

Parameters:



213
214
215
216
217
218
219
# File 'lib/mihari/emitters/slack.rb', line 213

def call(artifacts)
  return if artifacts.empty?

  @artifacts = artifacts

  notifier.post(text:, attachments:, mrkdwn: true)
end

#configured?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/mihari/emitters/slack.rb', line 164

def configured?
  webhook_url?
end

#notifier::Slack::Notifier

Returns:

  • (::Slack::Notifier)


178
179
180
181
182
183
184
# File 'lib/mihari/emitters/slack.rb', line 178

def notifier
  @notifier ||= lambda do
    return ::Slack::Notifier.new(webhook_url, channel:, username:) if timeout.nil?

    ::Slack::Notifier.new(webhook_url, channel:, username:, http_options: {timeout:})
  end.call
end

#targetString

Returns:

  • (String)


171
172
173
# File 'lib/mihari/emitters/slack.rb', line 171

def target
  channel
end

#textString

Build a text

Returns:

  • (String)


200
201
202
203
204
205
206
207
208
# File 'lib/mihari/emitters/slack.rb', line 200

def text
  tags = rule.tags.map(&:name)
  tags = ["N/A"] if tags.empty?
  [
    "*#{rule.title}*",
    "*Desc.*: #{rule.description}",
    "*Tags*: #{tags.join(", ")}"
  ].join("\n")
end

#webhook_url?Boolean

Check webhook URL is set

Returns:

  • (Boolean)


157
158
159
# File 'lib/mihari/emitters/slack.rb', line 157

def webhook_url?
  !webhook_url.nil?
end