Skip to content

Commit

Permalink
Unrevert Sprint 13 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Oct 17, 2024
1 parent f7dc1ec commit 4c96fa1
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 29 deletions.
175 changes: 175 additions & 0 deletions backend/src/api/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { SelectQueryBuilder } from 'typeorm';
import { Log } from '../models';
import { validateBody, wrapHandler } from './helpers';
import { IsDate, IsOptional, IsString } from 'class-validator';

type ParsedQuery = {
[key: string]: string | ParsedQuery;
};

const parseQueryString = (query: string): ParsedQuery => {
// Parses a query string that is used to search the JSON payload of a record
// Example => createdUserPayload.userId: 123124121424
const result: ParsedQuery = {};

const parts = query.match(/(\w+(\.\w+)*):\s*[^:]+/g);

if (!parts) {
return result;
}

parts.forEach((part) => {
const [key, value] = part.split(/:(.+)/);

if (!key || value === undefined) return;

const keyParts = key.trim().split('.');
let current = result;

keyParts.forEach((part, index) => {
if (index === keyParts.length - 1) {
current[part] = value.trim();
} else {
if (!current[part]) {
current[part] = {};
}
current = current[part] as ParsedQuery;
}
});
});

return result;
};

const generateSqlConditions = (
parsedQuery: ParsedQuery,
jsonPath: string[] = []
): string[] => {
const conditions: string[] = [];

for (const [key, value] of Object.entries(parsedQuery)) {
if (typeof value === 'object') {
const newPath = [...jsonPath, key];
conditions.push(...generateSqlConditions(value, newPath));
} else {
const jsonField =
jsonPath.length > 0
? `${jsonPath.map((path) => `'${path}'`).join('->')}->>'${key}'`
: `'${key}'`;
conditions.push(
`payload ${
jsonPath.length > 0 ? '->' : '->>'
} ${jsonField} = '${value}'`
);
}
}

return conditions;
};
class Filter {
@IsString()
value: string;

@IsString()
operator?: string;
}

class DateFilter {
@IsDate()
value: string;

@IsString()
operator:
| 'is'
| 'not'
| 'after'
| 'onOrAfter'
| 'before'
| 'onOrBefore'
| 'empty'
| 'notEmpty';
}
class LogSearch {
@IsOptional()
eventType?: Filter;
@IsOptional()
result?: Filter;
@IsOptional()
timestamp?: Filter;
@IsOptional()
payload?: Filter;
}

const generateDateCondition = (filter: DateFilter): string => {
const { operator } = filter;

switch (operator) {
case 'is':
return `log.createdAt = :timestamp`;
case 'not':
return `log.createdAt != :timestamp`;
case 'after':
return `log.createdAt > :timestamp`;
case 'onOrAfter':
return `log.createdAt >= :timestamp`;
case 'before':
return `log.createdAt < :timestamp`;
case 'onOrBefore':
return `log.createdAt <= :timestamp`;
case 'empty':
return `log.createdAt IS NULL`;
case 'notEmpty':
return `log.createdAt IS NOT NULL`;
default:
throw new Error('Invalid operator');
}
};

const filterResultQueryset = async (qs: SelectQueryBuilder<Log>, filters) => {
if (filters?.eventType) {
qs.andWhere('log.eventType ILIKE :eventType', {
eventType: `%${filters?.eventType?.value}%`
});
}
if (filters?.result) {
qs.andWhere('log.result ILIKE :result', {
result: `%${filters?.result?.value}%`
});
}
if (filters?.payload) {
try {
const parsedQuery = parseQueryString(filters?.payload?.value);
const conditions = generateSqlConditions(parsedQuery);
qs.andWhere(conditions[0]);
} catch (error) {}
}

if (filters?.timestamp) {
const timestampCondition = generateDateCondition(filters?.timestamp);
try {
} catch (error) {}
qs.andWhere(timestampCondition, {
timestamp: new Date(filters?.timestamp?.value)
});
}

return qs;
};

export const list = wrapHandler(async (event) => {
const search = await validateBody(LogSearch, event.body);

const qs = Log.createQueryBuilder('log');

const filterQs = await filterResultQueryset(qs, search);

const [results, resultsCount] = await filterQs.getManyAndCount();

return {
statusCode: 200,
body: JSON.stringify({
result: results,
count: resultsCount
})
};
});
3 changes: 2 additions & 1 deletion backend/src/models/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
VwSeverityStats,
VwDomainStats,
VwOrgStats,
Log,

// Models for the Mini Data Lake database
CertScan,
Expand Down Expand Up @@ -211,7 +212,7 @@ const connectDb = async (logging?: boolean) => {
VwSeverityStats,
VwDomainStats,
VwOrgStats,
Webpage
Log
],
synchronize: false,
name: 'default',
Expand Down
1 change: 1 addition & 0 deletions backend/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './service';
export * from './user';
export * from './vulnerability';
export * from './webpage';
export * from './log';
// Mini data lake models
export * from './mini_data_lake/cert_scans';
export * from './mini_data_lake/cidrs';
Expand Down
19 changes: 19 additions & 0 deletions backend/src/models/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Log extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column('json')
payload: Object;

@Column({ nullable: false })
createdAt: Date;

@Column({ nullable: true })
eventType: string;

@Column({ nullable: false })
result: string;
}
4 changes: 2 additions & 2 deletions frontend/src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ export const Layout: React.FC<PropsWithChildren<ContextType>> = ({
isFilterDrawerOpen={isFilterDrawerOpen}
setIsFilterDrawerOpen={setIsFilterDrawerOpen}
/>
<div className="main-content" id="main-content" tabIndex={-1} />

<Box
id="main-content"
tabIndex={-1}
display="block"
position="relative"
flex="1"
Expand Down
30 changes: 14 additions & 16 deletions frontend/src/components/SkipToMainContent/SkipToMainContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Box, Button, Tooltip } from '@mui/material';
import { Box, Button } from '@mui/material';

export const SkipToMainContent: React.FC = () => {
const handleClick = (event: any) => {
Expand All @@ -12,21 +12,19 @@ export const SkipToMainContent: React.FC = () => {

return (
<Box sx={{ paddingLeft: 1 }}>
<Tooltip title="Skip to main content" placement="right">
<Button
aria-label="Skip to main content"
variant="text"
tabIndex={0}
onClick={handleClick}
sx={{
outline: 'true',
fontSize: '0.6rem',
padding: 0
}}
>
Skip to main content
</Button>
</Tooltip>
<Button
aria-label="Skip to main content"
variant="text"
tabIndex={0}
onClick={handleClick}
sx={{
outline: 'true',
fontSize: '0.6rem',
padding: 0
}}
>
Skip to main content
</Button>
</Box>
);
};
5 changes: 5 additions & 0 deletions frontend/src/pages/AdminTools/AdminTools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ScansView from 'pages/Scans/ScansView';
import ScanTasksView from 'pages/Scans/ScanTasksView';
import { Box, Container, Tab } from '@mui/material';
import { TabContext, TabList, TabPanel } from '@mui/lab';
import { Logs } from 'components/Logs/Logs';

export const AdminTools: React.FC = () => {
const [value, setValue] = React.useState('1');
Expand All @@ -21,6 +22,7 @@ export const AdminTools: React.FC = () => {
<Tab label="Scans" value="1" />
<Tab label="Scan History" value="2" />
<Tab label="Notifications" value="3" />
<Tab label="User Logs" value="4" />
</TabList>
</Box>
<TabPanel value="1">
Expand All @@ -33,6 +35,9 @@ export const AdminTools: React.FC = () => {
<TabPanel value="3">
<Notifications />
</TabPanel>
<TabPanel value="4">
<Logs />
</TabPanel>
</TabContext>
</Box>
</Container>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/Risk/Risk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ const Risk: React.FC<ContextType & {}> = ({ filters, addFilter }) => {
.range(['#c7e8ff', '#135787']);
setStats(result);
},
[apiPost, riskFilters]
// eslint-disable-next-line react-hooks/exhaustive-deps
[riskFilters]
);

useEffect(() => {
Expand Down
7 changes: 1 addition & 6 deletions frontend/src/pages/TermsOfUse/TermsOfUse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ export const TermsOfUse: React.FC = () => {
issues.
</p>
<p>
CyHy Dashboard supports two types of users for your organization:
administrative users or view-only users. Administrative users can
add/delete domains for their organization, schedule and disable scans
for their organization, and invite others to create CyHy Dashboard
accounts to have access to the organization’s data. View-only users can
only view data provided to or collected by CyHy Dashboard.
Users can only view data provided to or collected by CyHy Dashboard.
</p>
{maximumRole === 'admin' && (
<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports[`matches admin snapshot 1`] = `
CyHy Dashboard is a free, self-service tool offered by the Department of Homeland Security’s Cybersecurity and Infrastructure Security Agency (CISA). Using both passive and active processes, CyHy Dashboard can continuously evaluate the cybersecurity posture of your public-facing, internet-accessible network assets for vulnerabilities or configuration issues.
</p>
<p>
CyHy Dashboard supports two types of users for your organization: administrative users or view-only users. Administrative users can add/delete domains for their organization, schedule and disable scans for their organization, and invite others to create CyHy Dashboard accounts to have access to the organization’s data. View-only users can only view data provided to or collected by CyHy Dashboard.
Users can only view data provided to or collected by CyHy Dashboard.
</p>
<p>
Once you create a CyHy Dashboard administrator account, input the Internet Protocol (IP) addresses or domains to be continuously evaluated, and select the scanning/evaluation protocols to be used, CyHy Dashboard will collect data about the sites you specified from multiple publicly-available resources, including through active interactions with your sites, if that option is selected by you. CyHy Dashboard will also examine any publicly-available, internet-accessible resources that appear to be related or otherwise associated with IPs or domains you have provided us to evaluate, presenting you with a list of those related sites for your awareness.
Expand Down Expand Up @@ -137,7 +137,7 @@ exports[`matches user snapshot 1`] = `
CyHy Dashboard is a free, self-service tool offered by the Department of Homeland Security’s Cybersecurity and Infrastructure Security Agency (CISA). Using both passive and active processes, CyHy Dashboard can continuously evaluate the cybersecurity posture of your public-facing, internet-accessible network assets for vulnerabilities or configuration issues.
</p>
<p>
CyHy Dashboard supports two types of users for your organization: administrative users or view-only users. Administrative users can add/delete domains for their organization, schedule and disable scans for their organization, and invite others to create CyHy Dashboard accounts to have access to the organization’s data. View-only users can only view data provided to or collected by CyHy Dashboard.
Users can only view data provided to or collected by CyHy Dashboard.
</p>
<p>
By creating a CyHy Dashboard view only account and using this service, you request CISA’s technical assistance to detect vulnerabilities and configuration issues through CyHy Dashboard and agree to the following:
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/Vulnerability/CveSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const CveSection: React.FC<HasCVEProps> = ({
component="img"
sx={{ height: 25, width: 30 }}
alt="NVD"
src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ5dvAqUgpcwQXcfmDxrMry2Z6mj8FAckqcjpbHupW7ReLf_DuyhR8_jIYcr8hs38yDorw&usqp=CAU"
src={nvd}
/>
<Typography
display="inline"
Expand Down

0 comments on commit 4c96fa1

Please sign in to comment.