Class: Mihari::Emitters::Slack
- Inherits:
-
Base
- Object
- Base
- Mihari::Emitters::Slack
show all
- Defined in:
- lib/mihari/emitters/slack.rb
Constant Summary
collapse
- DEFAULT_CHANNEL =
"#general"
- DEFAULT_USERNAME =
"Mihari"
Mixins::Retriable::DEFAULT_ON
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Base
inherited, #run
#retry_on_error
#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
#channel ⇒ String
119
120
121
|
# File 'lib/mihari/emitters/slack.rb', line 119
def channel
@channel
end
|
#username ⇒ String
122
123
124
|
# File 'lib/mihari/emitters/slack.rb', line 122
def username
@username
end
|
#webhook_url ⇒ String?
116
117
118
|
# File 'lib/mihari/emitters/slack.rb', line 116
def webhook_url
@webhook_url
end
|
Instance Method Details
#configuration_keys ⇒ Object
194
195
196
|
# File 'lib/mihari/emitters/slack.rb', line 194
def configuration_keys
%w[slack_webhook_url slack_channel]
end
|
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
|
#notifier ⇒ Object
150
151
152
|
# File 'lib/mihari/emitters/slack.rb', line 150
def notifier
@notifier ||= ::Slack::Notifier.new(webhook_url, channel: channel, username: username)
end
|
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
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?
146
147
148
|
# File 'lib/mihari/emitters/slack.rb', line 146
def valid?
webhook_url?
end
|
#webhook_url? ⇒ Boolean
137
138
139
|
# File 'lib/mihari/emitters/slack.rb', line 137
def webhook_url?
!webhook_url.nil?
end
|