Skip to content

Commit

Permalink
feat: add disable rule management in slack interactive endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuGilet committed Jan 6, 2025
1 parent 88479e4 commit ab3d331
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ const configuration = (function () {
config.baleen.protectedFrontApps = ['Pix_Test'];

config.slack.blockedAccessesChannelId = 'C08700JG7QU';
config.slack.botToken = 'fakeToken';

config.datadog.token = 'token';

Expand Down
8 changes: 7 additions & 1 deletion run/controllers/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getAppStatusFromScalingo } from '../services/slack/app-status-from-scal
import * as commands from '../services/slack/commands.js';
import shortcuts from '../services/slack/shortcuts.js';
import viewSubmissions from '../services/slack/view-submissions.js';
import blockActions from '../services/slack/block-actions.js';
import { AutomaticRule } from '../models/AutomaticRule.js';

function _getDeployStartedMessage(release, appName) {
return `Commande de déploiement de la release "${release}" pour ${appName} en production bien reçue.`;
Expand Down Expand Up @@ -154,8 +156,12 @@ const slack = {
return viewSubmissions.submitCreateAppOnScalingoConfirmation(payload);
}
return null;
case 'view_closed':
case 'block_actions':
if (payload?.actions[0]?.action_id === AutomaticRule.DISABLE) {
return blockActions.disableAutomaticRule(payload);
}
return null;
case 'view_closed':
default:
logger.info({ event: 'slack', message: 'This kind of interaction is not yet supported by Pix Bot.' });
return null;
Expand Down
123 changes: 123 additions & 0 deletions test/acceptance/run/slack_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import {
nock,
nockGithubWithConfigChanges,
nockGithubWithNoConfigChanges,
sinon,
StatusCodes,
} from '../../test-helper.js';
import dayjs from 'dayjs';
import { config } from '../../../config.js';
import { AutomaticRule } from '../../../run/models/AutomaticRule.js';

describe('Acceptance | Run | Slack', function () {
describe('POST /run/slack/interactive-endpoint', function () {
Expand Down Expand Up @@ -453,5 +457,124 @@ describe('Acceptance | Run | Slack', function () {
});
});
});

describe('when using the block action disable-automatic-rule', function () {
let clock;
let now;

beforeEach(function () {
now = new Date('2024-01-01');
clock = sinon.useFakeTimers({ now, toFake: ['Date'] });
});

afterEach(function () {
clock.restore();
});

it('should disable rule in CDN and update slack message', async function () {
// given
const ip = '127.0.0.1';
const ja3 = '9709730930';
const date = dayjs(now);
const rules = [
{ namespaceKey: 'namespaceKey1', ruleId: 'ruleId1' },
{ namespaceKey: 'namespaceKey2', ruleId: 'ruleId2' },
];
const messageTimestamp = '1735836582.877169';

const body = {
type: 'block_actions',
message: {
ts: messageTimestamp,
attachments: [
{
blocks: [
{ fields: [{ text: 'IP' }, { text: ip }] },
{ fields: [{ text: 'JA3' }, { text: ja3 }] },
{ elements: [{ text: `At ${date.format('DD/MM/YYYY HH:mm:ss')}` }] },
],
},
],
},
actions: [
{
action_id: AutomaticRule.DISABLE,
value: JSON.stringify(rules),
},
],
};

for (const rule of rules) {
nock('https://console.baleen.cloud/api', {
reqheaders: {
'X-Api-Key': config.baleen.pat,
'Content-type': 'application/json',
Cookie: `baleen-namespace=${rule.namespaceKey}`,
},
})
.patch(`/configs/custom-static-rules/${rule.ruleId}`, {
enabled: false,
})
.reply(200);
}

nock('https://slack.com', {
reqheaders: {
'Content-type': 'application/json',
Authorization: 'Bearer fakeToken',
},
})
.post(`/api/chat.update`, {
channel: 'C08700JG7QU',
ts: '1735836582.877169',
as_user: true,
text: 'Règle de blocage mise en place sur Baleen.',
attachments: [
{
color: '#106c1f',
blocks: [
{
fields: [
{ type: 'mrkdwn', text: 'IP' },
{ type: 'mrkdwn', text: '127.0.0.1' },
],
type: 'section',
},
{
fields: [
{ type: 'mrkdwn', text: 'JA3' },
{ type: 'mrkdwn', text: '9709730930' },
],
type: 'section',
},
{ elements: [{ type: 'mrkdwn', text: 'At 01/01/2024 01:00:00' }], type: 'context' },
{ type: 'divider' },
{
fields: [
{ type: 'mrkdwn', text: 'Règle désactivée le' },
{ type: 'mrkdwn', text: '01/01/2024 01:00:00' },
],
type: 'section',
},
],
fallback: 'Règle de blocage mise en place sur Baleen.',
},
],
})
.reply(200);

// when
const res = await server.inject({
method: 'POST',
url: '/run/slack/interactive-endpoint',
headers: createSlackWebhookSignatureHeaders(JSON.stringify(body)),
payload: body,
});

// then
expect(res.statusCode).to.equal(200);
expect(nock.isDone()).to.be.true;
});
});
});
});

0 comments on commit ab3d331

Please sign in to comment.