create checks if CLI or SLack, so don't process/return both formats every time

This commit is contained in:
Charlotte Croce 2025-04-20 20:59:12 -04:00
parent 964eaa8ae9
commit b98502284a
4 changed files with 123 additions and 102 deletions

View file

@ -2,7 +2,6 @@
* sigma_create_handler.js * sigma_create_handler.js
* *
* Handles Sigma rule conversion requests from Slack commands * Handles Sigma rule conversion requests from Slack commands
* Action handlers moved to sigma_action_core.js
*/ */
const logger = require('../../utils/logger'); const logger = require('../../utils/logger');
const { handleError } = require('../../utils/error_handler'); const { handleError } = require('../../utils/error_handler');

View file

@ -22,6 +22,9 @@ const handleCommand = async (command, respond) => {
try { try {
logger.debug(`${FILE_NAME}: Processing sigma-details command: ${command.text}`); logger.debug(`${FILE_NAME}: Processing sigma-details command: ${command.text}`);
// Determine if request is from CLI
const isCliRequest = command.channel_id === 'cli' || command.channel_name === 'cli';
if (!command || !command.text) { if (!command || !command.text) {
logger.warn(`${FILE_NAME}: Empty command received for sigma-details`); logger.warn(`${FILE_NAME}: Empty command received for sigma-details`);
await respond({ await respond({
@ -61,25 +64,28 @@ const handleCommand = async (command, respond) => {
return; return;
} }
// For Slack responses, generate Block Kit blocks // Create response based on interface type
let blocks; if (isCliRequest) {
// For CLI, just return the raw data
await respond({
responseData: sigmaRuleDetailsResult.explanation,
response_type: 'cli'
});
} else {
// For Slack, generate and return Block Kit blocks
try { try {
// This is for Slack - get the Block Kit UI components const blocks = getSigmaRuleDetailsBlocks(sigmaRuleDetailsResult.explanation);
blocks = getSigmaRuleDetailsBlocks(sigmaRuleDetailsResult.explanation); await respond({
blocks: blocks,
response_type: 'in_channel'
});
} catch (blockError) { } catch (blockError) {
await handleError(blockError, `${FILE_NAME}: Block generation`, respond, { await handleError(blockError, `${FILE_NAME}: Block generation`, respond, {
responseType: 'ephemeral', responseType: 'ephemeral',
customMessage: 'Error generating rule details view' customMessage: 'Error generating rule details view'
}); });
return;
} }
}
// Return the response with both blocks for Slack and responseData for CLI
await respond({
blocks: blocks, // For Slack interface
responseData: sigmaRuleDetailsResult.explanation, // For CLI interface
response_type: 'in_channel'
});
} catch (error) { } catch (error) {
await handleError(error, `${FILE_NAME}: Details command handler`, respond, { await handleError(error, `${FILE_NAME}: Details command handler`, respond, {
responseType: 'ephemeral' responseType: 'ephemeral'

View file

@ -110,7 +110,16 @@ const handleCommand = async (command, respond) => {
return; return;
} }
// Generate blocks with pagination support const isCliRequest = command.channel_id === 'cli' || command.channel_name === 'cli';
if (isCliRequest) {
// For CLI, just return the raw data
await respond({
responseData: searchResult.results,
response_type: 'cli'
});
} else {
// For Slack, generate and return Block Kit blocks
let blocks; let blocks;
try { try {
logger.debug(`${FILE_NAME}: Calling getSearchResultBlocks with ${searchResult.results.length} results`); logger.debug(`${FILE_NAME}: Calling getSearchResultBlocks with ${searchResult.results.length} results`);
@ -129,27 +138,27 @@ const handleCommand = async (command, respond) => {
blocks = getSearchResultBlocks(keyword, searchResult.results, searchResult.pagination); blocks = getSearchResultBlocks(keyword, searchResult.results, searchResult.pagination);
} }
logger.debug(`${FILE_NAME}: Successfully generated ${blocks?.length || 0} blocks`); logger.debug(`${FILE_NAME}: Successfully generated ${blocks?.length || 0} blocks`);
// Determine if this should be visible to everyone or just the user
const isEphemeral = totalCount > 20;
// Add debug log before sending response
logger.debug(`${FILE_NAME}: About to send response with ${blocks?.length || 0} blocks`);
// Respond with the search results
// Respond with the search results
await respond({
blocks: blocks,
response_type: isEphemeral ? 'ephemeral' : 'in_channel'
});
} catch (blockError) { } catch (blockError) {
// Use error handler for block generation errors // Use error handler for block generation errors
await handleError(blockError, `${FILE_NAME}: Block generation`, respond, { await handleError(blockError, `${FILE_NAME}: Block generation`, respond, {
responseType: 'in_channel', responseType: 'in_channel',
customMessage: `Found ${searchResult.results.length} of ${totalCount} rules matching "${keyword}" (page ${page} of ${searchResult.pagination?.totalPages || 1}). Use /sigma-details [id] to view details.` customMessage: `Found ${searchResult.results.length} of ${totalCount} rules matching "${keyword}" (page ${page} of ${searchResult.pagination?.totalPages || 1}). Use /sigma-details [id] to view details.`
}); });
return;
} }
}
// Add debug log before sending response
logger.debug(`${FILE_NAME}: About to send response with ${blocks?.length || 0} blocks`);
// Determine if this should be visible to everyone or just the user
const isEphemeral = totalCount > 20;
// Respond with the search results
await respond({
blocks: blocks,
responseData: searchResult.results,
response_type: isEphemeral ? 'ephemeral' : 'in_channel'
});
// Add debug log after sending response // Add debug log after sending response
logger.debug(`${FILE_NAME}: Response sent successfully`); logger.debug(`${FILE_NAME}: Response sent successfully`);

View file

@ -22,6 +22,9 @@ const handleCommand = async (command, respond) => {
try { try {
logger.info(`${FILE_NAME}: Processing sigma-stats command`); logger.info(`${FILE_NAME}: Processing sigma-stats command`);
// Determine if request is from CLI by checking channel properties
const isCliRequest = command.channel_id === 'cli' || command.channel_name === 'cli';
await respond({ await respond({
text: 'Gathering Sigma rule statistics... This may take a moment.', text: 'Gathering Sigma rule statistics... This may take a moment.',
response_type: 'ephemeral' response_type: 'ephemeral'
@ -39,24 +42,28 @@ const handleCommand = async (command, respond) => {
return; return;
} }
// For Slack responses, generate Block Kit blocks // For CLI, only include responseData
let blocks; if (isCliRequest) {
await respond({
responseData: statsResult.stats,
response_type: 'cli'
});
}
// For Slack, only generate Block Kit blocks
else {
try { try {
blocks = getStatsBlocks(statsResult.stats); const blocks = getStatsBlocks(statsResult.stats);
await respond({
blocks: blocks,
response_type: 'in_channel'
});
} catch (blockError) { } catch (blockError) {
await handleError(blockError, `${FILE_NAME}: Block generation`, respond, { await handleError(blockError, `${FILE_NAME}: Block generation`, respond, {
responseType: 'ephemeral', responseType: 'ephemeral',
customMessage: 'Error generating statistics view' customMessage: 'Error generating statistics view'
}); });
return;
} }
}
// Return the response with both blocks for Slack and responseData for CLI
await respond({
blocks: blocks,
responseData: statsResult.stats, // Include raw data for CLI
response_type: 'in_channel'
});
} catch (error) { } catch (error) {
await handleError(error, `${FILE_NAME}: Stats command handler`, respond, { await handleError(error, `${FILE_NAME}: Stats command handler`, respond, {
responseType: 'ephemeral' responseType: 'ephemeral'