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

Save hosts and tickets to datalake #250

Merged
merged 6 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions backend/src/models/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ export const connectToDatalake = async (logging?: boolean) => {
if (!dl_connection?.isConnected) {
console.log('Connected to datalake');
dl_connection = await connectDl(logging);
} else {
console.log("didn't connect");
}
return dl_connection;
};
Expand Down
7 changes: 4 additions & 3 deletions backend/src/models/mini_data_lake/ticket_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ import {
Column,
PrimaryGeneratedColumn,
BaseEntity,
ManyToOne
ManyToOne,
Unique
} from 'typeorm';

import { Ticket } from './tickets';
import { VulnScan } from './vuln_scans';

@Entity()
@Unique(['eventTimestamp', 'ticket', 'action'])
export class TicketEvent extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({
nullable: true,
type: 'varchar',
unique: true
type: 'varchar'
})
reference: string | null;

Expand Down
30 changes: 30 additions & 0 deletions backend/src/tasks/helpers/saveHost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { plainToClass } from 'class-transformer';
import { Host, connectToDatalake } from '../../models';

export default async (host:Host): Promise<string> => {

Check failure on line 4 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
console.log(`Starting to save host ${host.ipString} to datalake`)

Check failure on line 5 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `····console.log(`Starting·to·save·host·${host.ipString}·to·datalake`)` with `··console.log(`Starting·to·save·host·${host.ipString}·to·datalake`);`
await connectToDatalake();
const hostUpdatedValues = Object.keys(host)

Check failure on line 7 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `····` with `··`
.map((key) => {

Check failure on line 8 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
if (['id'].indexOf(key) > -1)

Check failure on line 9 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `········if·(['id'].indexOf(key)·>·-1)⏎·········` with `······if·(['id'].indexOf(key)·>·-1)`
return '';
else if (key === 'organization') return 'organizationId';

Check failure on line 11 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
else if (key === 'ip') return 'ipId';

Check failure on line 12 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
return host[key] !== null ? key : '';

Check failure on line 13 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
})

Check failure on line 14 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
.filter((key) => key !== '');

Check failure on line 15 in backend/src/tasks/helpers/saveHost.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
const host_id: string = (
await Host.createQueryBuilder()
.insert()
.values(host)
.orUpdate({
conflict_target:["id"],
overwrite: hostUpdatedValues
}
)
.returning('id')
.execute()
).identifiers[0].id;

return host_id;
};
2 changes: 1 addition & 1 deletion backend/src/tasks/helpers/saveIpToMdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { plainToClass } from 'class-transformer';
import { Ip, connectToDatalake } from '../../models';

export default async (ipObj: Ip): Promise<string | null> => {
console.log('Starting to save IP to datalake');
console.log(`Starting to save IP to datalake: ${ipObj.ip}`);
await connectToDatalake();
const ipUpdatedValues = Object.keys(ipObj)
.map((key) => {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/tasks/helpers/saveOrganizationToMdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async (
cidrs: Cidr[],
location: Location | null
): Promise<string> => {
console.log('Starting to save Org to datalake');
console.log(`Saving org ${organization.acronym} to datalake`);
await connectToDatalake();

const cidr_entities: Cidr[] = [];
Expand Down
35 changes: 35 additions & 0 deletions backend/src/tasks/helpers/saveTicket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { plainToClass } from 'class-transformer';
import {
Ticket,
DL_Organization,
Cidr,
Location,
connectToDatalake
} from '../../models';

export default async (ticket: Ticket): Promise<string> => {
console.log(`Starting to save Ticket to datalake`);
await connectToDatalake();
const ticketUpdatedValues = Object.keys(ticket)
.map((key) => {
if (['id'].indexOf(key) > -1) return '';
else if (key === 'organization') return 'organizationId';
else if (key === 'ip') return 'ipId';
else if (key === 'cve') return 'cveId';
return ticket[key] !== null ? key : '';
})
.filter((key) => key !== '');
const ticket_id: string = (
await Ticket.createQueryBuilder()
.insert()
.values(ticket)
.orUpdate({
conflict_target: ['id'],
overwrite: ticketUpdatedValues
})
.returning('id')
.execute()
).identifiers[0].id;

return ticket_id;
};
32 changes: 32 additions & 0 deletions backend/src/tasks/helpers/saveTicketEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { plainToClass } from 'class-transformer';
import {
TicketEvent,
DL_Organization,
Cidr,
Location,
connectToDatalake
} from '../../models';

export default async (ticket_event: TicketEvent): Promise<string> => {
await connectToDatalake();
const ticketEventUpdatedValues = Object.keys(ticket_event)
.map((key) => {
if (['eventTimestamp', 'action', 'ticket'].indexOf(key) > -1) return '';
else if (key === 'vulnScan') return 'vulnScanId';
return ticket_event[key] !== null ? key : '';
})
.filter((key) => key !== '');
const ticket_event_id: string = (
await TicketEvent.createQueryBuilder()
.insert()
.values(ticket_event)
.orUpdate({
conflict_target: ['eventTimestamp', 'action', 'ticketId'],
overwrite: ticketEventUpdatedValues
})
.returning('id')
.execute()
).identifiers[0].id;

return ticket_event_id;
};
Loading
Loading