Skip to content

Commit d06af88

Browse files
committed
Implement port filters
1 parent 560e473 commit d06af88

File tree

2 files changed

+127
-29
lines changed

2 files changed

+127
-29
lines changed

packages/backend/jobs-manager/service/src/modules/database/reporting/port/port-search-query.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Tag } from '../../tags/tag.model';
1515
import { Host } from '../host/host.model';
1616
import { Project } from '../project.model';
1717
import { WebsiteService } from '../websites/website.service';
18-
import { Port } from './port.model';
18+
import { Port, PortDocument } from './port.model';
1919

2020
@Injectable()
2121
export class PortSearchQuery {
@@ -33,7 +33,7 @@ export class PortSearchQuery {
3333
query: string,
3434
firstSeenStartDate: number,
3535
firstSeenEndDate: number,
36-
): Promise<FilterQuery<Port>> {
36+
): Promise<FilterQuery<PortDocument>> {
3737
const terms = this.queryParser.parse(query || '', {
3838
completeTermsOnly: true,
3939
excludeEmptyValues: true,
@@ -43,7 +43,7 @@ export class PortSearchQuery {
4343

4444
this.ensureTerms(terms);
4545

46-
const finalFilter: FilterQuery<Port> = { $and: [] };
46+
const finalFilter: FilterQuery<PortDocument> = { $and: [] };
4747

4848
// "is" filters
4949
{
@@ -183,6 +183,28 @@ export class PortSearchQuery {
183183
}
184184
}
185185

186+
// "tag.id" filters
187+
{
188+
const t = this.consumeTerms(terms, '', 'tag.id');
189+
if (t.length) {
190+
const tagIds = t.map((x) => new Types.ObjectId(x.value));
191+
finalFilter.$and.push({
192+
tags: { $all: tagIds },
193+
});
194+
}
195+
}
196+
197+
// "-tag.id" filters
198+
{
199+
const t = this.consumeTerms(terms, '-', 'tag.id');
200+
if (t.length) {
201+
const tagIds = t.map((x) => new Types.ObjectId(x.value));
202+
finalFilter.$and.push({
203+
tags: { $nin: tagIds },
204+
});
205+
}
206+
}
207+
186208
// "port.number" filters
187209
{
188210
const t = this.consumeTerms(terms, '', 'port.number');
@@ -206,6 +228,28 @@ export class PortSearchQuery {
206228
}
207229
}
208230

231+
// "port.id" filters
232+
{
233+
const t = this.consumeTerms(terms, '', 'port.id');
234+
if (t.length) {
235+
const portIds = t.map((x) => new Types.ObjectId(x.value));
236+
finalFilter.$and.push({
237+
_id: { $in: portIds },
238+
});
239+
}
240+
}
241+
242+
// "-port.id" filters
243+
{
244+
const t = this.consumeTerms(terms, '-', 'port.id');
245+
if (t.length) {
246+
const portIds = t.map((x) => new Types.ObjectId(x.value));
247+
finalFilter.$and.push({
248+
_id: { $not: { $in: portIds } },
249+
});
250+
}
251+
}
252+
209253
// "port.protocol" filters
210254
{
211255
const t = this.consumeTerms(terms, '', 'port.protocol');

packages/backend/jobs-manager/service/src/modules/database/reporting/port/port.service.spec.ts

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2-
import { getName } from '../../../../test/test.utils';
32
import { AppModule } from '../../../app.module';
43
import { TagsDocument } from '../../tags/tag.model';
54
import { TagsService } from '../../tags/tag.service';
@@ -44,7 +43,7 @@ describe('Port Service', () => {
4443
await tagsService.delete(t._id);
4544
}
4645

47-
project1 = await project('project 1');
46+
project1 = await project('my first project');
4847
});
4948

5049
describe('Add ports', () => {
@@ -313,58 +312,113 @@ describe('Port Service', () => {
313312
});
314313

315314
describe('Get all', () => {
315+
let project1: ProjectDocument;
316+
let project2: ProjectDocument;
317+
318+
let foo: TagsDocument;
319+
let bar: TagsDocument;
320+
let baz: TagsDocument;
321+
let qux: TagsDocument;
322+
323+
let h1: HostDocument;
324+
let h2: HostDocument;
325+
let h3: HostDocument;
326+
327+
let p1: PortDocument;
328+
let p2: PortDocument;
329+
let p3: PortDocument;
330+
let p4: PortDocument;
331+
let p5: PortDocument;
332+
let p6: PortDocument;
333+
334+
beforeEach(async () => {
335+
// Arrange
336+
project1 = await project('project 1');
337+
project2 = await project('project 2');
338+
[foo, bar, baz, qux] = await tags('foo', 'bar', 'baz', 'qux');
339+
340+
h1 = await host('1.1.1.1', project1);
341+
[p1, p2] = await port([1, 2], h1, project1, [foo, qux]);
342+
343+
h2 = await host('1.2.2.2', project1);
344+
[p3, p4] = await port([3, 4], h2, project1, [bar, qux]);
345+
346+
h3 = await host('1.2.2.3', project2);
347+
[p5] = await port([5], h3, project2, [baz, qux]);
348+
[p6] = await port([6], h3, project2, [baz, qux], 'udp');
349+
await block(p6);
350+
});
351+
316352
it.each([
317353
['', [1, 2, 3, 4, 5, 6]],
354+
355+
// Projects
356+
['project: "project*"', [1, 2, 3, 4, 5, 6]],
318357
['project: "project 1"', [1, 2, 3, 4]],
319358
['project: "project 2"', [5, 6]],
320359
['-project: "project 2"', [1, 2, 3, 4]],
321-
['project: "project*"', [1, 2, 3, 4, 5, 6]],
360+
['project.name: "project*"', [1, 2, 3, 4, 5, 6]],
361+
['project.name: "project 1"', [1, 2, 3, 4]],
362+
['project.name: "project 2"', [5, 6]],
363+
['-project.name: "project 2"', [1, 2, 3, 4]],
364+
[() => `project.id: ${project1.id}`, [1, 2, 3, 4]],
365+
[() => `project.id: ${project2.id}`, [5, 6]],
366+
[() => `-project.id: ${project2.id}`, [1, 2, 3, 4]],
367+
368+
// Host
322369
['host: 1.1.1.1', [1, 2]],
323370
['host.ip: 1.1.1.1', [1, 2]],
371+
[() => `host.id: ${h1._id}`, [1, 2]],
324372
['host: 1.*', [1, 2, 3, 4, 5, 6]],
325373
['host: 1.2.2*', [3, 4, 5, 6]],
326374
['-host: 1.1.1.1', [3, 4, 5, 6]],
375+
['-host.ip: 1.1.1.1', [3, 4, 5, 6]],
376+
[() => `-host.id: ${h1.id}`, [3, 4, 5, 6]],
327377
['-host: 1.2.2*', [1, 2]],
378+
379+
// Port
328380
['port: 1', [1]],
329381
['port.number: 1', [1]],
382+
[() => `port.id: ${p1.id}`, [1]],
383+
[() => `-port.id: ${p1.id} -port.id: ${p3.id}`, [2, 4, 5, 6]],
330384
['-port: 1', [2, 3, 4, 5, 6]],
331385
['port.protocol: tcp', [1, 2, 3, 4, 5]],
332386
['-port.protocol: tcp', [6]],
333387
['port.protocol: udp', [6]],
334388
['-port.protocol: udp', [1, 2, 3, 4, 5]],
389+
390+
// Tag
335391
['tag: foo', [1, 2]],
392+
[() => `tag.id: ${foo._id}`, [1, 2]],
393+
[() => `-tag.id: ${foo._id}`, [3, 4, 5, 6]],
336394
['-tag: ba*', [1, 2]],
337395
['-tag: foo', [3, 4, 5, 6]],
338396
['tag: qux', [1, 2, 3, 4, 5, 6]],
339397
['tag: foo tag: qux', [1, 2]],
340398
['-tag: foo tag: qux', [3, 4, 5, 6]],
399+
400+
// Is
341401
['is: blocked', [6]],
342402
['-is: blocked', [1, 2, 3, 4, 5]],
343-
])('Filter by "%s"', async (query: string, expected: number[]) => {
344-
// Arrange
345-
const project2 = await project('project 2');
346-
const [foo, bar, baz, qux] = await tags('foo', 'bar', 'baz', 'qux');
347-
348-
const h1 = await host('1.1.1.1', project1);
349-
await port([1, 2], h1, project1, [foo, qux]);
350-
351-
const h2 = await host('1.2.2.2', project1);
352-
await port([3, 4], h2, project1, [bar, qux]);
353-
354-
const h3 = await host('1.2.2.3', project2);
355-
await port([5], h3, project2, [baz, qux]);
356-
await block(...(await port([6], h3, project2, [baz, qux], 'udp')));
357-
358-
// Act
359-
const allPorts = await portService.getAll(0, 10, { query });
360-
361-
// Assert
362-
expect(allPorts.map((x) => x.port).sort()).toStrictEqual(expected.sort());
363-
});
403+
])(
404+
'Filter by "%s"',
405+
async (query: string | (() => string), expected: number[]) => {
406+
// Arrange
407+
if (typeof query !== 'string') query = query();
408+
409+
// Act
410+
const allPorts = await portService.getAll(0, 10, { query });
411+
412+
// Assert
413+
expect(allPorts.map((x) => x.port).sort()).toStrictEqual(
414+
expected.sort(),
415+
);
416+
},
417+
);
364418
});
365419

366420
async function project(name: string = '') {
367-
const ccDto: CreateProjectDto = { name: `${getName(testPrefix)}` };
421+
const ccDto: CreateProjectDto = { name };
368422
return await projectService.addProject(ccDto);
369423
}
370424

@@ -383,7 +437,7 @@ describe('Port Service', () => {
383437
}
384438

385439
async function tags(...tags: string[]) {
386-
const createdTags = [];
440+
const createdTags: TagsDocument[] = [];
387441
for (const tag of tags) {
388442
createdTags.push(await tagsService.create(tag, '#ffffff'));
389443
}

0 commit comments

Comments
 (0)