Skip to content

Commit

Permalink
Enhance flagFloatingIps
Browse files Browse the repository at this point in the history
Enhance flagFloatingIps
  • Loading branch information
DJensen94 committed Oct 3, 2024
1 parent 6cf0624 commit 36a6cc5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 52 deletions.
4 changes: 3 additions & 1 deletion backend/src/api/scans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export const SCAN_SCHEMA: ScanSchema = {
flagFloatingIps: {
type: 'fargate',
isPassive: true,
global: true,
global: false,
cpu: '2048',
memory: '16384',
description:
'Loops through all domains and determines if their associated IP can be found in a report Cidr block.'
},
Expand Down
36 changes: 24 additions & 12 deletions backend/src/tasks/flagFloatingIps.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import { CommandOptions } from './ecs-client';
import checkIpInCidr from './helpers/checkIpInCidr';
import checkOrgIsFceb from './helpers/checkOrgIsFceb';
import { Organization, connectToDatabase } from '../models';

export const handler = async (commandOptions: CommandOptions) => {
const { organizationId, organizationName } = commandOptions;
const db_connection = await connectToDatabase();
const organization_repo = db_connection.getRepository(Organization);

const organizations = await organization_repo.find({
where: {id: organizationId},

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

View workflow job for this annotation

GitHub Actions / lint

Replace `id:·organizationId` with `·id:·organizationId·`
relations: ['domains']
});

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

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`


for (const organization of organizations) {
for (const domain of organization.domains) {
if (domain.ip) {
const cidrSectorDict = await checkIpInCidr(
domain.ip,
organization.acronym
);
if (cidrSectorDict['isInCidr']) {
domain.fromCidr = true;
}
if (cidrSectorDict['isExecutive']) {
domain.isFceb = true;
console.log('Running on ', organizationName)

Check failure on line 18 in backend/src/tasks/flagFloatingIps.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (90% of all statements in
the enclosing function
have an explicit semicolon).
const isExecutive = await checkOrgIsFceb(organization.acronym);

if (isExecutive) {
// If executive, mark all domains as isFceb = true
for (const domain of organization.domains) {
domain.isFceb = true;
await domain.save(); // Save each domain
}
}

Check failure on line 27 in backend/src/tasks/flagFloatingIps.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎····else` with `·else·`
else{
for (const domain of organization.domains) {
if (domain.ip) {
// Set fromCidr field based on the check
domain.fromCidr = await checkIpInCidr(domain.ip, organization.acronym);;

Check failure on line 32 in backend/src/tasks/flagFloatingIps.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `domain.ip,·organization.acronym);;` with `⏎············domain.ip,⏎············organization.acronym`

Check failure on line 33 in backend/src/tasks/flagFloatingIps.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `······);⏎`
// Optionally save domain if its fromCidr value has changed
await domain.save(); // Save the domain
}
domain.save();
}
}
}
Expand Down
46 changes: 9 additions & 37 deletions backend/src/tasks/helpers/checkIpInCidr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,22 @@ import { Cidr, DL_Organization, connectToDatalake2 } from '../../models';
export default async (

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

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎··ip:·string,⏎··acronym:·string⏎` with `ip:·string,·acronym:·string`
ip: string,
acronym: string
): Promise<{ isInCidr: boolean; isExecutive: boolean }> => {
// await connectToDatalake2()
// const cidrRepository = getRepository(Cidr);
// const organizationRepository = getRepository(DL_Organization);

// Find the organization by acronym
): Promise<boolean> => {
// Connect to the database
const mdl_connection = await connectToDatalake2();
const mdl_organization_repo = mdl_connection.getRepository(DL_Organization);

// Find the organization by acronym
const organization = await mdl_organization_repo.findOne({
where: { acronym },
relations: ['cidrs', 'sectors', 'parent']
relations: ['cidrs']
});

if (!organization) {
return { isInCidr: false, isExecutive: false };
}

const isOrganizationExecutive = async (
org: DL_Organization
): Promise<boolean> => {
if (org.sectors.some((sector) => sector.acronym === 'EXECUTIVE')) {
return true;
}
if (org.parent) {
const parentOrg = await mdl_organization_repo.findOne({
where: { id: org.parent.id },
relations: ['sectors']
});

return parentOrg ? await isOrganizationExecutive(parentOrg) : false;
}
return false;
};

const isExecutive = await isOrganizationExecutive(organization);

// Get CIDRs related to the organization
const cidrs = organization.cidrs.map((cidr) => cidr.network);

if (cidrs.length === 0) {
return { isInCidr: false, isExecutive }; // No CIDRs associated with the organization
if (!organization || organization.cidrs.length === 0) {
return false; // Return false if the organization is not found or has no CIDRs
}

// Check if the IP is in any of the CIDRs
// Check if the IP is in any of the organization's CIDRs
const mdl_cidr_repo = mdl_connection.getRepository(Cidr);
const result = await mdl_cidr_repo
.createQueryBuilder('cidr')
Expand All @@ -57,5 +29,5 @@ export default async (
})
.getCount();

return { isInCidr: result > 0, isExecutive };
return result > 0; // Return true if the IP is in any CIDR, otherwise false
};
37 changes: 37 additions & 0 deletions backend/src/tasks/helpers/checkOrgIsFceb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getRepository } from 'typeorm';
import { DL_Organization, connectToDatalake2 } from '../../models';

export default async (acronym: string): Promise<boolean> => {
// Connect to the database
const mdl_connection = await connectToDatalake2();
const mdl_organization_repo = mdl_connection.getRepository(DL_Organization);

// Find the organization by acronym
const organization = await mdl_organization_repo.findOne({
where: { acronym },
relations: ['sectors', 'parent']
});

if (!organization) {
return false; // Return false if the organization is not found
}

const isOrganizationExecutive = async (org: DL_Organization): Promise<boolean> => {

Check failure on line 19 in backend/src/tasks/helpers/checkOrgIsFceb.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `org:·DL_Organization` with `⏎····org:·DL_Organization⏎··`
// Check if the current organization has the EXECUTIVE sector
if (org.sectors.some((sector) => sector.acronym === 'EXECUTIVE')) {
return true;
}
// If there is a parent organization, check it recursively
if (org.parent) {
const parentOrg = await mdl_organization_repo.findOne({
where: { id: org.parent.id },
relations: ['sectors']
});
return parentOrg ? await isOrganizationExecutive(parentOrg) : false;
}
return false;
};

// Check if the organization or its parents are executive
return await isOrganizationExecutive(organization);
};
4 changes: 2 additions & 2 deletions frontend/src/pages/Vulnerabilities/Vulnerabilities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ export const Vulnerabilities: React.FC<{ groupBy?: string }> = ({
domainId: vuln?.domain?.id,
product: vuln.cpe
? vuln.cpe
: vuln?.service?.products
? vuln?.service.products[0].cpe || 'N/A'
: (vuln.service && vuln.service.products && vuln.service.products.length > 0)

Check failure on line 287 in frontend/src/pages/Vulnerabilities/Vulnerabilities.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `(vuln.service·&&·vuln.service.products·&&·vuln.service.products.length·>·0)` with `vuln.service·&&⏎········vuln.service.products·&&⏎········vuln.service.products.length·>·0`
? (vuln.service.products[0].cpe || 'N/A')

Check failure on line 288 in frontend/src/pages/Vulnerabilities/Vulnerabilities.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `(vuln.service.products[0].cpe·||·'N/A')` with `vuln.service.products[0].cpe·||·'N/A'`
: 'N/A',
createdAt: vuln?.createdAt
? `${differenceInCalendarDays(
Expand Down

0 comments on commit 36a6cc5

Please sign in to comment.