Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple findings in subscriptions #313

Open
wants to merge 4 commits into
base: templates-providers
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export class EventSubscriptionDto {
@IsOptional()
public projectId?: string; // if projectId is not set, the subscription is for all projects

@IsString()
@IsArray()
@IsString({ each: true })
@IsNotEmpty()
public finding!: string;
public findings!: string[];

@IsString()
@IsNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Event Subscriptions Controller (e2e)', () => {

const subscription: EventSubscriptionDto = {
name: 'My test subscription',
finding: 'HostnameIpFinding',
findings: ['HostnameIpFinding'],
jobName: 'TcpPortScanningJob',
jobParameters: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class EventSubscription {
public projectId?: Types.ObjectId;

@Prop()
public finding!: string;
public findings!: string[];

@Prop()
public jobName!: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class EventSubscriptionsService {
projectId: dto.projectId ? new Types.ObjectId(dto.projectId) : null,
isEnabled: dto.isEnabled != null ? dto.isEnabled : false,
name: dto.name,
finding: dto.finding,
findings: dto.findings,
jobName: dto.jobName,
jobParameters: dto.jobParameters,
conditions: dto.conditions,
Expand Down Expand Up @@ -56,7 +56,7 @@ export class EventSubscriptionsService {
const sub: Partial<EventSubscription> = {
projectId: dto.projectId ? new Types.ObjectId(dto.projectId) : null,
name: dto.name,
finding: dto.finding,
findings: dto.findings,
jobName: dto.jobName,
jobParameters: dto.jobParameters,
conditions: dto.conditions,
Expand Down Expand Up @@ -84,7 +84,7 @@ export class EventSubscriptionsService {
{ projectId: { $eq: new Types.ObjectId(projectId) } },
{ projectId: null },
],
finding: { $eq: finding },
findings: { $in: finding },
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ describe('Event Subscriptions Service', () => {
// Arrange
const c1 = await project('sub-c1');
const c2 = await project('sub-c2');
const finding = 'HostnameFinding';
const finding = ['HostnameFinding'];
const name = 'my special name';

const subData: EventSubscriptionDto = {
projectId: '',
name: 'my sub',
jobName: 'DomainNameResolvingJob',
finding: finding,
findings: finding,
cooldown: 3600,
};

Expand All @@ -61,7 +61,7 @@ describe('Event Subscriptions Service', () => {
// Act
const subs = await subscriptionsService.getAllForFinding(
c2._id.toString(),
finding,
finding[0],
);

// Assert
Expand All @@ -74,14 +74,14 @@ describe('Event Subscriptions Service', () => {
// Arrange
const c1 = await project('sub-c12');
const c2 = await project('sub-c22');
const finding = 'HostnameFinding';
const finding2 = 'HostnameIpFinding';
const finding = ['HostnameFinding'];
const finding2 = ['HostnameIpFinding'];

const subData: EventSubscriptionDto = {
projectId: '',
name: 'my sub',
jobName: 'DomainNameResolvingJob',
finding: finding,
findings: finding,
cooldown: 3600,
};

Expand All @@ -92,22 +92,92 @@ describe('Event Subscriptions Service', () => {
const s2 = await subscription({
...subData,
projectId: c2._id.toString(),
finding: finding2,
findings: finding2,
});

// Act
const subs = await subscriptionsService.getAllForFinding(
c2._id.toString(),
finding2,
finding2[0],
);

// Assert
expect(subs.length).toStrictEqual(1);
expect(subs[0].projectId).toStrictEqual(c2._id);
expect(subs[0].finding).toStrictEqual(finding2);
expect(subs[0].findings[0]).toStrictEqual(finding2[0]);
});
});

it('Should get the event subscriptions with multiple findings', async () => {
// Arrange
const c1 = await project('sub-c12');
const finding = ['IpFinding'];
const findings2 = ['HostnameIpFinding', 'IpFinding'];

const subData: EventSubscriptionDto = {
projectId: '',
name: 'my sub',
jobName: 'DomainNameResolvingJob',
findings: finding,
cooldown: 3600,
};

const s1 = await subscription({
...subData,
projectId: c1._id.toString(),
});
const s2 = await subscription({
...subData,
projectId: c1._id.toString(),
findings: findings2,
});

// Act
const subs = await subscriptionsService.getAllForFinding(
c1._id.toString(),
findings2[1],
);

// Assert
expect(subs.length).toStrictEqual(2);
});

it('Should get only the event subscriptions with multiple findings', async () => {
// Arrange
const c1 = await project('sub-c12');
const finding = ['IpFinding'];
const findings2 = ['HostnameIpFinding', 'IpFinding'];

const subData: EventSubscriptionDto = {
projectId: '',
name: 'my sub',
jobName: 'DomainNameResolvingJob',
findings: finding,
cooldown: 3600,
};

const s1 = await subscription({
...subData,
projectId: c1._id.toString(),
});
const s2 = await subscription({
...subData,
projectId: c1._id.toString(),
findings: findings2,
});

// Act
const subs = await subscriptionsService.getAllForFinding(
c1._id.toString(),
findings2[0],
);

// Assert
expect(subs.length).toStrictEqual(1);
expect(subs[0].projectId).toStrictEqual(c1._id);
expect(subs[0].findings[0]).toStrictEqual(findings2[0]);
});

async function project(name: string) {
return await projectService.addProject({
name: name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ describe('Findings Handler Base', () => {
const sub = new EventSubscription();
sub.projectId = new Types.ObjectId('507f1f77bcf86cd799439011');
sub.conditions = [];
sub.finding = 'HostnameFinding';
sub.findings = ['HostnameFinding'];
sub.jobName = cjName;
const customParam = { name: 'custom-job-param', value: 'ASDF' };
sub.jobParameters = [customParam];
Expand Down Expand Up @@ -843,7 +843,7 @@ describe('Findings Handler Base', () => {
cooldown: 82800,
builtIn: true,
isEnabled: true,
finding: 'PortFinding',
findings: ['PortFinding'],
jobName: 'HttpServerCheckJob',
jobParameters: [
{ name: 'targetIp', value: '${ip}' },
Expand All @@ -854,7 +854,7 @@ describe('Findings Handler Base', () => {
es.conditions[0] = es.conditions[0] as JobCondition;
let yaml = [
`name: ${es.name}`,
`finding: ${es.finding}`,
`findings: ${JSON.stringify(es.findings)}`,
`triggerInterval: ${es.cooldown}`,
`job:`,
` name: ${es.jobName}`,
Expand All @@ -874,7 +874,7 @@ describe('Findings Handler Base', () => {
const sub = SubscriptionsUtils.parseEventSubscriptionYaml(yaml);

// Assert
expect(sub.finding).toStrictEqual(es.finding);
expect(sub.findings).toStrictEqual(es.findings);
expect(sub.name).toStrictEqual(es.name);
expect(sub.jobName).toStrictEqual(es.jobName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export class SubscriptionsUtils {
const sub: EventSubscription = {
name: subYamlJson.name,
isEnabled: true,
finding: subYamlJson.finding,
findings: subYamlJson.findings,
jobName: subYamlJson.job.name,
cooldown: subYamlJson.cooldown,
projectId: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class EventSubscriptionsService implements GenericSubscriptionService<Eve
discriminator: newSub.discriminator ? newSub.discriminator : undefined,
isEnabled: subscription.isEnabled,
name: newSub.name,
finding: newSub.finding,
findings: newSub.findings,
cooldown: newSub.cooldown,
projectId: newSub.projectId ? newSub.projectId : allProjectsSubscriptions,
job: {
Expand All @@ -68,7 +68,7 @@ export class EventSubscriptionsService implements GenericSubscriptionService<Eve
name: subscription.name,
isEnabled: subscription.isEnabled,
discriminator: subscription.discriminator ? subscription.discriminator : undefined,
finding: subscription.finding,
findings: subscription.findings,
cooldown: subscription.cooldown,
jobName: subscription.job.name,
projectId: subscription.projectId === allProjectsSubscriptions ? undefined : subscription.projectId,
Expand All @@ -94,7 +94,7 @@ export class EventSubscriptionsService implements GenericSubscriptionService<Eve
_id: data._id,
isEnabled: data.isEnabled,
name: data.name,
finding: data.finding,
findings: data.findings,
projectId: data.projectId ? data.projectId : allProjectsSubscriptions,
cooldown: data.cooldown,
builtIn: data.builtIn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@
<!-- Subscription trigger (Cron or Finding) -->
<ng-container matColumnDef="trigger">
<th mat-header-cell *matHeaderCellDef i18n="Subscription trigger|Subscription trigger">Trigger</th>
<td mat-cell *matCellDef="let element">{{ element.finding || element.cronExpression }}</td>
<td mat-cell *matCellDef="let element">
@if (element.cronExpression) {
{{ element.cronExpression }}
}
@if (element.findings) {
@for (finding of element.findings; track finding; let i = $index; let last = $last) {
{{ finding }}{{ !last ? ', ' : '' }}
}
}
</td>
</ng-container>

<!-- Job Column -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class ListSubscriptionsComponent {
subscription.job?.name,
subscription.name,
cron.cronExpression,
event.finding,
event.findings,
cron.cronExpression ? 'cron' : 'event',
subscription.isEnabled === false ? 'disabled' : 'enabled',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface EventSubscriptionData extends SubscriptionData {
cooldown: number;
discriminator?: string;
builtIn: boolean;
finding: string;
findings: string[];
}

export interface CronSubscriptionData extends SubscriptionData {
Expand Down
Loading