-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11196d1
commit a5a1ebc
Showing
8 changed files
with
175 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { config } from '../../../../../config.js'; | ||
import { httpAgent } from '../../../../http-agent.js'; | ||
import { logger } from '../../../logger.js'; | ||
|
||
async function updateMessage({ message, ts, attachments, channel = '#tech-releases', injectedHttpAgent = httpAgent }) { | ||
const url = 'https://slack.com/api/chat.update'; | ||
|
||
const headers = { | ||
'content-type': 'application/json', | ||
authorization: `Bearer ${config.slack.botToken}`, | ||
}; | ||
const payload = { | ||
channel, | ||
ts, | ||
text: message, | ||
attachments: attachments, | ||
}; | ||
|
||
const slackResponse = await injectedHttpAgent.post({ url, payload, headers }); | ||
if (slackResponse.isSuccessful) { | ||
if (!slackResponse.data.ok) { | ||
logger.error({ | ||
event: 'slack-update-message', | ||
message: `Slack error occured while sending message : ${slackResponse.data.error}`, | ||
stack: `Payload for error was ${JSON.stringify(payload)}`, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default { updateMessage }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { config } from '../../config.js'; | ||
import { Actions, Attachment, Button, Context, Divider, Message, Section } from 'slack-block-builder'; | ||
import dayjs from 'dayjs'; | ||
|
||
export class AutomaticRule { | ||
static DISABLE = 'disable-automatic-rule'; | ||
|
||
constructor({ ip, ja3, date = dayjs() }) { | ||
this.ip = ip; | ||
this.ja3 = ja3; | ||
this.date = date; | ||
} | ||
|
||
static parseMessage(message) { | ||
const messageObject = JSON.parse(message); | ||
|
||
const ip = messageObject.attachments[0]?.blocks[0]?.fields[1]?.text; | ||
if (!ip) { | ||
throw new Error('IP field not found.'); | ||
} | ||
|
||
const ja3 = messageObject.attachments[0]?.blocks[1]?.fields[1]?.text; | ||
if (!ja3) { | ||
throw new Error('JA3 field not found.'); | ||
} | ||
|
||
const date = messageObject.attachments[0]?.blocks[2]?.elements[0]?.text?.slice(3); | ||
if (!date) { | ||
throw new Error('Date field not found.'); | ||
} | ||
|
||
return new AutomaticRule({ ip, ja3, date }); | ||
} | ||
|
||
getInitialMessage({ addedRules }) { | ||
return this.#buildMessage({ isActive: true, addedRules }); | ||
} | ||
|
||
getDeactivatedMessage() { | ||
return this.#buildMessage({ isActive: false }); | ||
} | ||
|
||
#buildMessage({ isActive, addedRules }) { | ||
return { | ||
channel: `#${config.slack.blockedAccessesChannel}`, | ||
message: 'Règle de blocage mise en place sur Baleen.', | ||
attachments: Message() | ||
.attachments( | ||
Attachment({ color: '#106c1f' }) | ||
.blocks( | ||
Section().fields(`IP`, `${this.ip}`), | ||
Section().fields(`JA3`, `${this.ja3}`), | ||
Context().elements(`At ${this.date.format('DD/MM/YYYY HH:mm:ss')}`), | ||
Divider(), | ||
this.#buildMessageFooter({ isActive, addedRules }), | ||
) | ||
.fallback('Règle de blocage mise en place sur Baleen.'), | ||
) | ||
.buildToObject().attachments, | ||
}; | ||
} | ||
|
||
#buildMessageFooter({ isActive, addedRules }) { | ||
if (isActive) { | ||
return Actions().elements( | ||
Button().text('Désactiver').actionId(AutomaticRule.DISABLE).value(JSON.stringify(addedRules)).danger(), | ||
); | ||
} else { | ||
return Section().fields(`Règle désactivée le`, `${dayjs().format('DD/MM/YYYY HH:mm:ss')}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as cdnServices from '../cdn.js'; | ||
import { AutomaticRule } from '../../models/AutomaticRule.js'; | ||
import slackService from '../../../common/services/slack/surfaces/messages/update-message.js'; | ||
|
||
const blockActions = { | ||
async removeAutomaticRule(payload) { | ||
const rules = JSON.parse(payload.actions[0].value); | ||
const messageTimestamp = payload.message.ts; | ||
|
||
for (const rule of rules) { | ||
await cdnServices.disableRule(rule); | ||
} | ||
|
||
console.log("Message: " + JSON.parse(payload.message)); | ||
|
||
const automaticRule = AutomaticRule.parseMessage(payload.message); | ||
await slackService.updateMessage({ ts: messageTimestamp, ...automaticRule.getDeactivatedMessage() }); | ||
}, | ||
}; | ||
|
||
export default blockActions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters