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

Class Method Summary collapse

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) ⇒ 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: rule, options: 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

Class Method Details

.configuration_keysObject



232
233
234
# File 'lib/mihari/emitters/slack.rb', line 232

def configuration_keys
  %w[slack_webhook_url slack_channel]
end

Instance Method Details

#attachmentsArray<Mihari::Emitters::Attachment>

Build attachments

Returns:



201
202
203
# File 'lib/mihari/emitters/slack.rb', line 201

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

#call(artifacts) ⇒ Object

Parameters:



223
224
225
226
227
228
229
# File 'lib/mihari/emitters/slack.rb', line 223

def call(artifacts)
  return if artifacts.empty?

  @artifacts = artifacts

  notifier.post(text: text, attachments: 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
185
186
187
188
189
190
191
192
193
194
# File 'lib/mihari/emitters/slack.rb', line 178

def notifier
  @notifier ||= [].tap do |out|
    out << if timeout.nil?
      ::Slack::Notifier.new(
        webhook_url,
        channel: channel, username: username
      )
    else
      ::Slack::Notifier.new(
        webhook_url,
        channel: channel,
        username: username,
        http_options: { timeout: timeout }
      )
    end
  end.first
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)


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

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