2024-10-29 00:52:07 -07:00
|
|
|
import { SecurityConfig } from '@n8n/config';
|
2024-01-22 09:25:36 -08:00
|
|
|
import { Flags } from '@oclif/core';
|
|
|
|
import { ApplicationError } from 'n8n-workflow';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { Container } from 'typedi';
|
2024-01-22 09:25:36 -08:00
|
|
|
|
2024-09-12 09:07:18 -07:00
|
|
|
import { RISK_CATEGORIES } from '@/security-audit/constants';
|
|
|
|
import { SecurityAuditService } from '@/security-audit/security-audit.service';
|
2023-11-13 02:50:43 -08:00
|
|
|
import type { Risk } from '@/security-audit/types';
|
2024-09-12 09:07:18 -07:00
|
|
|
|
2024-08-22 02:10:37 -07:00
|
|
|
import { BaseCommand } from './base-command';
|
2023-01-05 04:28:40 -08:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
export class SecurityAudit extends BaseCommand {
|
2023-01-05 04:28:40 -08:00
|
|
|
static description = 'Generate a security audit report for this n8n instance';
|
|
|
|
|
|
|
|
static examples = [
|
|
|
|
'$ n8n audit',
|
|
|
|
'$ n8n audit --categories=database,credentials',
|
|
|
|
'$ n8n audit --days-abandoned-workflow=10',
|
|
|
|
];
|
|
|
|
|
|
|
|
static flags = {
|
2024-01-22 09:25:36 -08:00
|
|
|
help: Flags.help({ char: 'h' }),
|
|
|
|
categories: Flags.string({
|
2023-01-05 04:28:40 -08:00
|
|
|
default: RISK_CATEGORIES.join(','),
|
|
|
|
description: 'Comma-separated list of categories to include in the audit',
|
|
|
|
}),
|
2024-06-24 03:13:18 -07:00
|
|
|
|
2024-01-22 09:25:36 -08:00
|
|
|
'days-abandoned-workflow': Flags.integer({
|
2024-10-29 00:52:07 -07:00
|
|
|
default: Container.get(SecurityConfig).daysAbandonedWorkflow,
|
2023-01-05 04:28:40 -08:00
|
|
|
description: 'Days for a workflow to be considered abandoned if not executed',
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
async run() {
|
2024-01-22 09:25:36 -08:00
|
|
|
const { flags: auditFlags } = await this.parse(SecurityAudit);
|
2023-01-05 04:28:40 -08:00
|
|
|
|
|
|
|
const categories =
|
|
|
|
auditFlags.categories?.split(',').filter((c): c is Risk.Category => c !== '') ??
|
|
|
|
RISK_CATEGORIES;
|
|
|
|
|
|
|
|
const invalidCategories = categories.filter((c) => !RISK_CATEGORIES.includes(c));
|
|
|
|
|
|
|
|
if (invalidCategories.length > 0) {
|
|
|
|
const message =
|
|
|
|
invalidCategories.length > 1
|
|
|
|
? `Invalid categories received: ${invalidCategories.join(', ')}`
|
|
|
|
: `Invalid category received: ${invalidCategories[0]}`;
|
|
|
|
|
|
|
|
const hint = `Valid categories are: ${RISK_CATEGORIES.join(', ')}`;
|
|
|
|
|
2023-11-29 03:25:10 -08:00
|
|
|
throw new ApplicationError([message, hint].join('. '));
|
2023-01-05 04:28:40 -08:00
|
|
|
}
|
|
|
|
|
2023-11-13 02:50:43 -08:00
|
|
|
const result = await Container.get(SecurityAuditService).run(
|
|
|
|
categories,
|
|
|
|
auditFlags['days-abandoned-workflow'],
|
|
|
|
);
|
2023-01-05 04:28:40 -08:00
|
|
|
|
|
|
|
if (Array.isArray(result) && result.length === 0) {
|
|
|
|
this.logger.info('No security issues found');
|
|
|
|
} else {
|
|
|
|
process.stdout.write(JSON.stringify(result, null, 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async catch(error: Error) {
|
|
|
|
this.logger.error('Failed to generate security audit');
|
|
|
|
this.logger.error(error.message);
|
|
|
|
}
|
|
|
|
}
|