Skip to content

Commit

Permalink
Merge pull request #1137 from iuime/master
Browse files Browse the repository at this point in the history
Add Lark to notifications.
  • Loading branch information
yogeshojha committed May 12, 2024
2 parents 0365285 + e93b51b commit d7641f5
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 1 deletion.
17 changes: 17 additions & 0 deletions web/reNgine/common_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,23 @@ def send_slack_message(message):
hook_url = notif.slack_hook_url
requests.post(url=hook_url, data=json.dumps(message), headers=headers)

def send_lark_message(message):
"""Send lark message.
Args:
message (str): Message.
"""
headers = {'content-type': 'application/json'}
message = {"msg_type":"interactive","card":{"elements":[{"tag":"div","text":{"content":message,"tag":"lark_md"}}]}}
notif = Notification.objects.first()
do_send = (
notif and
notif.send_to_lark and
notif.lark_hook_url)
if not do_send:
return
hook_url = notif.lark_hook_url
requests.post(url=hook_url, data=json.dumps(message), headers=headers)

def send_discord_message(
message,
Expand Down
1 change: 1 addition & 0 deletions web/reNgine/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2937,6 +2937,7 @@ def send_notif(
message = enrich_notification(message, scan_history_id, subscan_id)
send_discord_message(message, **options)
send_slack_message(message)
send_lark_message(message)
send_telegram_message(message)


Expand Down
23 changes: 23 additions & 0 deletions web/scanEngine/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ class Meta:
"placeholder": "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
}))

send_to_lark = forms.BooleanField(
required=False,
widget=forms.CheckboxInput(
attrs={
"class": "form-check-input",
"id": "lark_checkbox",
}))

lark_hook_url = forms.CharField(
required=False,
widget=forms.TextInput(
attrs={
"class": "form-control",
"id": "lark_hook_url",
"placeholder": "https://open.larksuite.com/open-apis/bot/v2/hook/XXXXXXXXXXXXXXXXXXXXXXXX",
}))

send_to_discord = forms.BooleanField(
required=False,
widget=forms.CheckboxInput(
Expand Down Expand Up @@ -280,10 +297,12 @@ class Meta:

def set_value(self, key):
self.initial['send_to_slack'] = key.send_to_slack
self.initial['send_to_lark'] = key.send_to_lark
self.initial['send_to_discord'] = key.send_to_discord
self.initial['send_to_telegram'] = key.send_to_telegram

self.initial['slack_hook_url'] = key.slack_hook_url
self.initial['lark_hook_url'] = key.lark_hook_url
self.initial['discord_hook_url'] = key.discord_hook_url
self.initial['telegram_bot_token'] = key.telegram_bot_token
self.initial['telegram_bot_chat_id'] = key.telegram_bot_chat_id
Expand All @@ -298,6 +317,8 @@ def set_value(self, key):

if not key.send_to_slack:
self.fields['slack_hook_url'].widget.attrs['readonly'] = True
if not key.send_to_lark:
self.fields['lark_hook_url'].widget.attrs['readonly'] = True
if not key.send_to_discord:
self.fields['discord_hook_url'].widget.attrs['readonly'] = True
if not key.send_to_telegram:
Expand All @@ -307,10 +328,12 @@ def set_value(self, key):

def set_initial(self):
self.initial['send_to_slack'] = False
self.initial['send_to_lark'] = False
self.initial['send_to_discord'] = False
self.initial['send_to_telegram'] = False

self.fields['slack_hook_url'].widget.attrs['readonly'] = True
self.fields['lark_hook_url'].widget.attrs['readonly'] = True
self.fields['discord_hook_url'].widget.attrs['readonly'] = True
self.fields['telegram_bot_token'].widget.attrs['readonly'] = True
self.fields['telegram_bot_chat_id'].widget.attrs['readonly'] = True
Expand Down
2 changes: 2 additions & 0 deletions web/scanEngine/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('send_to_slack', models.BooleanField(default=False)),
('send_to_lark', models.BooleanField(default=False)),
('send_to_discord', models.BooleanField(default=False)),
('send_to_telegram', models.BooleanField(default=False)),
('slack_hook_url', models.CharField(blank=True, max_length=200, null=True)),
('lark_hook_url', models.CharField(blank=True, max_length=200, null=True)),
('discord_hook_url', models.CharField(blank=True, max_length=200, null=True)),
('telegram_bot_token', models.CharField(blank=True, max_length=100, null=True)),
('telegram_bot_chat_id', models.CharField(blank=True, max_length=100, null=True)),
Expand Down
2 changes: 2 additions & 0 deletions web/scanEngine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ class InterestingLookupModel(models.Model):
class Notification(models.Model):
id = models.AutoField(primary_key=True)
send_to_slack = models.BooleanField(default=False)
send_to_lark = models.BooleanField(default=False)
send_to_discord = models.BooleanField(default=False)
send_to_telegram = models.BooleanField(default=False)

slack_hook_url = models.CharField(max_length=200, null=True, blank=True)
lark_hook_url = models.CharField(max_length=200, null=True, blank=True)
discord_hook_url = models.CharField(max_length=200, null=True, blank=True)
telegram_bot_token = models.CharField(max_length=100, null=True, blank=True)
telegram_bot_chat_id = models.CharField(max_length=100, null=True, blank=True)
Expand Down
33 changes: 33 additions & 0 deletions web/scanEngine/templates/scanEngine/settings/notification.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ <h4 class="header-title">Send Notifications to: </h4>
<a href="https://api.slack.com/messaging/webhooks" target="_blank" class="float-end text-primary">Slack Webhook API Documentation</a>
</div>
</div>
<div class="row mt-3">
<label for="slack_url" class="col-xl-2 col-sm-3 col-sm-2 col-form-label">Lark</label>
<div class="col-xl-10 col-lg-9 col-sm-10">
<div class="input-group mb-4">
<div class="input-group-prepend">
<span class="input-group-text" style="height: 100%">
<div class="form-check form-switch">
{{form.send_to_lark}}
</div>
</span>
</div>
{{form.lark_hook_url}}
<div class="input-group-append">
<span class="input-group-text" id="basic-addon2">
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" width="24px" height="24px" fill="none"><path fill="#646A73" d="M7.844 4.564c.865.697 1.905 1.704 2.75 2.912.143.191.314.48.381.592l.028.047a7.34 7.34 0 0 0-.768 1.083l-.226-.412C8.545 6.276 5.75 4.564 5.614 4.481a.708.708 0 0 1-.005-.003l-.555-.329-.089-.07a.416.416 0 0 1 .255-.738l.35-.008h6.664c.132.005.265.03.392.076.145.054.264.145.381.257.112.12.227.247.336.374.519.562 1.104 1.407 1.104 1.407-.478.16-1.124.612-1.124.612a4.222 4.222 0 0 0-.29-.45 17.215 17.215 0 0 0-.865-1.045H7.844z"/><path fill="#646A73" d="M15.902 6.255a4.517 4.517 0 0 1 3.367.598c.206.14.656.529.201 1.027-1.416 1.391-1.829 2.688-2.192 3.833-.232.727-.45 1.414-.888 2.02-1.51 2.09-3.429 3.334-5.697 3.695-.468.074-.944.11-1.417.11-3.299 0-6.249-1.697-7.292-2.37h.003l-.135-.09c-.445-.315-.509-.603-.519-.824V5.88a.405.405 0 0 1 .69-.265l.401.491c4.235 5.168 7.605 6.72 9.688 7.109 1.872.353 3.027-.138 3.352-.308.278-.426.445-.953.636-1.555l.006-.018c.338-1.062.748-2.35 1.935-3.73a3.276 3.276 0 0 0-1.9-.14c-1.468.328-2.836 1.633-4.075 3.869-.112.2-.206.381-.285.56-.602-.09-1.111-.542-1.111-.542a7.52 7.52 0 0 1 .287-.56c1.432-2.604 3.096-4.135 4.945-4.535zM2.564 14.073c.954.621 4.451 2.694 7.943 2.137 1.3-.206 2.47-.763 3.497-1.658-2.31.21-6.297-.628-11.44-6.39v5.911z"/></svg>
</span>
</div>
</div>
</div>
<div class="col-12">
<a href="https://open.larksuite.com/document/client-docs/bot-v3/add-custom-bot" target="_blank" class="float-end text-primary">Lark Webhook API Documentation</a>
</div>
</div>
<div class="row mt-3">
<label for="slack_url" class="col-xl-2 col-sm-3 col-sm-2 col-form-label">Discord<br>(Recommended)</label>
<div class="col-xl-10 col-lg-9 col-sm-10">
Expand Down Expand Up @@ -183,6 +206,16 @@ <h4 class="col-form-label col-xl-3 col-sm-3 col-sm-2 mt-3 header-title">Upload S
}
});

var lark_checkbox = document.getElementById("lark_checkbox");
lark_checkbox.addEventListener('change', function() {
if (this.checked) {
document.getElementById("lark_hook_url").readOnly = false;
}
else{
document.getElementById("lark_hook_url").readOnly = true;
}
});

var discord_checkbox = document.getElementById("discord_checkbox");
discord_checkbox.addEventListener('change', function() {
if (this.checked) {
Expand Down
3 changes: 2 additions & 1 deletion web/scanEngine/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rolepermissions.decorators import has_permission_decorator

from reNgine.common_func import *
from reNgine.tasks import (run_command, send_discord_message, send_slack_message, send_telegram_message)
from reNgine.tasks import (run_command, send_discord_message, send_slack_message,send_lark_message, send_telegram_message)
from scanEngine.forms import *
from scanEngine.forms import ConfigurationForm
from scanEngine.models import *
Expand Down Expand Up @@ -307,6 +307,7 @@ def notification_settings(request, slug):
if form.is_valid():
form.save()
send_slack_message('*reNgine*\nCongratulations! your notification services are working.')
send_lark_message('*reNgine*\nCongratulations! your notification services are working.')
send_telegram_message('*reNgine*\nCongratulations! your notification services are working.')
send_discord_message('**reNgine**\nCongratulations! your notification services are working.')
messages.add_message(
Expand Down

0 comments on commit d7641f5

Please sign in to comment.