Class: Mihari::Emitters::Slack

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

Constant Summary collapse

DEFAULT_CHANNEL =
"#general"
DEFAULT_USERNAME =
"Mihari"

Constants included from Mixins::Retriable

Mixins::Retriable::DEFAULT_ON

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, #run

Methods included from Mixins::Retriable

#retry_on_error

Methods included from Mixins::Configurable

#configuration_values

Constructor Details

#initialize(*args, **kwargs) ⇒ Slack

Returns a new instance of Slack.



124
125
126
127
128
129
130
# File 'lib/mihari/emitters/slack.rb', line 124

def initialize(*args, **kwargs)
  super(*args, **kwargs)

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

Instance Attribute Details

#channelString (readonly)

Returns:

  • (String)


119
120
121
# File 'lib/mihari/emitters/slack.rb', line 119

def channel
  @channel
end

#usernameString (readonly)

Returns:

  • (String)


122
123
124
# File 'lib/mihari/emitters/slack.rb', line 122

def username
  @username
end

#webhook_urlString? (readonly)

Returns:

  • (String, nil)


116
117
118
# File 'lib/mihari/emitters/slack.rb', line 116

def webhook_url
  @webhook_url
end

Instance Method Details

#configuration_keysObject



194
195
196
# File 'lib/mihari/emitters/slack.rb', line 194

def configuration_keys
  %w[slack_webhook_url slack_channel]
end

#configured?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/mihari/emitters/slack.rb', line 198

def configured?
  valid?
end

#emit(rule:, artifacts:, **_options) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/mihari/emitters/slack.rb', line 185

def emit(rule:, artifacts:, **_options)
  return if artifacts.empty?

  attachments = to_attachments(artifacts)
  text = to_text(rule)

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

#notifierObject



150
151
152
# File 'lib/mihari/emitters/slack.rb', line 150

def notifier
  @notifier ||= ::Slack::Notifier.new(webhook_url, channel: channel, username: username)
end

#to_attachments(artifacts) ⇒ Array<Mihari::Emitters::Attachment>

Build attachements

Parameters:

Returns:



161
162
163
164
165
# File 'lib/mihari/emitters/slack.rb', line 161

def to_attachments(artifacts)
  artifacts.map do |artifact|
    Attachment.new(data: artifact.data, data_type: artifact.data_type).to_a
  end.flatten
end

#to_text(rule) ⇒ String

Build a text

Parameters:

Returns:

  • (String)


174
175
176
177
178
179
180
181
182
183
# File 'lib/mihari/emitters/slack.rb', line 174

def to_text(rule)
  tags = rule.tags
  tags = ["N/A"] if tags.empty?

  [
    "*#{rule.title}*",
    "*Desc.*: #{rule.description}",
    "*Tags*: #{tags.join(", ")}"
  ].join("\n")
end

#valid?Boolean

Check webhook URL is set. Alias of #webhook_url?

Returns:

  • (Boolean)


146
147
148
# File 'lib/mihari/emitters/slack.rb', line 146

def valid?
  webhook_url?
end

#webhook_url?Boolean

Check webhook URL is set

Returns:

  • (Boolean)


137
138
139
# File 'lib/mihari/emitters/slack.rb', line 137

def webhook_url?
  !webhook_url.nil?
end