Skip to content

Commit

Permalink
Fix backend/src/api files
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed May 2, 2024
1 parent 4d89031 commit e3f1f1c
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 113 deletions.
16 changes: 14 additions & 2 deletions backend/env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ dev:

staging:
REGION: us-east-1
ENDPOINT_TYPE: REGIONAL
RESOURCE_POLICY:
- Effect: Allow
Principal: '*'
Action: execute-api:Invoke
Resource: execute-api:/${self:provider.stage}/*/*
ENDPOINT_TYPE: REGIONAL
COGNITO_URL: https://cognito-idp.us-east-1.amazonaws.com
BACKEND_DOMAIN: https://api.staging-cd.crossfeed.cyber.dhs.gov
EMAIL_REGION: us-east-1
DB_DIALECT: postgres
DB_PORT: 5432
DB_HOST: ${ssm:/crossfeed/staging/DATABASE_HOST}
Expand Down Expand Up @@ -72,6 +75,9 @@ prod:
Principal: '*'
Action: execute-api:Invoke
Resource: execute-api:/${self:provider.stage}/*/*
COGNITO_URL: https://cognito-idp.us-east-1.amazonaws.com
BACKEND_DOMAIN: https://api.crossfeed.cyber.dhs.gov
EMAIL_REGION: us-east-1
DB_DIALECT: postgres
DB_PORT: 5432
DB_HOST: ${ssm:/crossfeed/prod/DATABASE_HOST}
Expand Down Expand Up @@ -120,6 +126,7 @@ prod:

staging-lz:
REGION: us-gov-east-1
ENDPOINT_TYPE: PRIVATE
RESOURCE_POLICY:
- Effect: Deny
Principal: '*'
Expand All @@ -132,7 +139,9 @@ staging-lz:
Principal: '*'
Action: execute-api:Invoke
Resource: execute-api:/${self:provider.stage}/*/*
ENDPOINT_TYPE: PRIVATE
COGNITO_URL: https://cognito-idp.us-gov-west-1.amazonaws.com
BACKEND_DOMAIN: https://api.staging.crossfeed.cyber.dhs.gov
EMAIL_REGION: us-gov-west-1
DB_DIALECT: postgres
DB_PORT: 5432
DB_HOST: ${ssm:/crossfeed/staging/DATABASE_HOST}
Expand Down Expand Up @@ -201,6 +210,9 @@ prod-lz:
Principal: '*'
Action: execute-api:Invoke
Resource: execute-api:/${self:provider.stage}/*/*
COGNITO_URL: https://cognito-idp.us-gov-west-1.amazonaws.com
BACKEND_DOMAIN: https://api.crossfeed.cyber.dhs.gov
EMAIL_REGION: us-gov-west-1
DB_DIALECT: postgres
DB_PORT: 5432
DB_HOST: ${ssm:/crossfeed/prod/DATABASE_HOST}
Expand Down
6 changes: 3 additions & 3 deletions backend/src/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ app.use(
directives: {
defaultSrc: [
"'self'",
'https://cognito-idp.us-gov-west-1.amazonaws.com',
'https://api.crossfeed.cyber.dhs.gov'
`'${process.env.COGNITO_URL}'`,
`'${process.env.BACKEND_DOMAIN}'`
],
objectSrc: ["'none'"],
scriptSrc: [
"'self'",
'https://api.crossfeed.cyber.dhs.gov'
`'${process.env.BACKEND_DOMAIN}'`
// Add any other allowed script sources here
],
frameAncestors: ["'none'"]
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface UserInfo {
}

const client = jwksClient({
jwksUri: `https://cognito-idp.us-gov-west-1.amazonaws.com/${process.env.REACT_APP_USER_POOL_ID}/.well-known/jwks.json`,
jwksUri: `${process.env.COGNITO_URL}/${process.env.REACT_APP_USER_POOL_ID}/.well-known/jwks.json`,
getKeysInterceptor: () => {
const jwksJson = JSON.parse(process.env.REACT_APP_USER_POOL_KEY!);
return jwksJson.keys;
Expand Down
68 changes: 6 additions & 62 deletions backend/src/api/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import * as nodemailer from 'nodemailer';
import logger from '../tools/lambda-logger';
import * as handlebars from 'handlebars';

const AWS = require('aws-sdk');
const httpProxy = require('https-proxy-agent');
export const REGION_STATE_MAP = {
Alabama: '4',
Alaska: '10',
Expand Down Expand Up @@ -151,18 +149,9 @@ export const sendEmail = async (
body: string
) => {
try {
process.env.HTTPS_PROXY = 'http://proxy.lz.us-cert.gov:8080';
process.env.HTTP_PROXY = 'http://proxy.lz.us-cert.gov:8080';
const proxyAgent = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
AWS.config.update({
httpOptions: {
agent: proxyAgent ? httpProxy(proxyAgent) : undefined
}
});
const transporter = nodemailer.createTransport({
SES: new SES({
region: 'us-gov-west-1',
endpoint: 'https://email.us-gov-west-1.amazonaws.com'
region: process.env.EMAIL_REGION!
})
});

Expand All @@ -185,18 +174,9 @@ export const sendEmail = async (
};

export const sendRegistrationTextEmail = async (recipient: string) => {
process.env.HTTPS_PROXY = 'http://proxy.lz.us-cert.gov:8080';
process.env.HTTP_PROXY = 'http://proxy.lz.us-cert.gov:8080';
const proxyAgent = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
AWS.config.update({
httpOptions: {
agent: proxyAgent ? httpProxy(proxyAgent) : undefined
}
});
const transporter = nodemailer.createTransport({
SES: new SES({
region: 'us-gov-west-1',
endpoint: 'https://email.us-gov-west-1.amazonaws.com'
region: process.env.EMAIL_REGION!
})
});

Expand All @@ -217,18 +197,9 @@ export const sendRegistrationTextEmail = async (recipient: string) => {
};

export const sendRegistrationHtmlEmail = async (recipient: string) => {
process.env.HTTPS_PROXY = 'http://proxy.lz.us-cert.gov:8080';
process.env.HTTP_PROXY = 'http://proxy.lz.us-cert.gov:8080';
const proxyAgent = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
AWS.config.update({
httpOptions: {
agent: proxyAgent ? httpProxy(proxyAgent) : undefined
}
});
const transporter = nodemailer.createTransport({
SES: new SES({
region: 'us-gov-west-1',
endpoint: 'https://email.us-gov-west-1.amazonaws.com'
region: process.env.EMAIL_REGION!
})
});

Expand Down Expand Up @@ -273,18 +244,9 @@ export const sendUserRegistrationEmail = async (
replyTo: process.env.CROSSFEED_SUPPORT_EMAIL_REPLYTO!
};

process.env.HTTPS_PROXY = 'http://proxy.lz.us-cert.gov:8080';
process.env.HTTP_PROXY = 'http://proxy.lz.us-cert.gov:8080';
const proxyAgent = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
AWS.config.update({
httpOptions: {
agent: proxyAgent ? httpProxy(proxyAgent) : undefined
}
});
const transporter = nodemailer.createTransport({
SES: new SES({
region: 'us-gov-west-1',
endpoint: 'https://email.us-gov-west-1.amazonaws.com'
region: process.env.EMAIL_REGION!
})
});
await transporter.sendMail(mailOptions);
Expand Down Expand Up @@ -318,18 +280,9 @@ export const sendRegistrationDeniedEmail = async (
replyTo: process.env.CROSSFEED_SUPPORT_EMAIL_REPLYTO!
};

process.env.HTTPS_PROXY = 'http://proxy.lz.us-cert.gov:8080';
process.env.HTTP_PROXY = 'http://proxy.lz.us-cert.gov:8080';
const proxyAgent = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
AWS.config.update({
httpOptions: {
agent: proxyAgent ? httpProxy(proxyAgent) : undefined
}
});
const transporter = nodemailer.createTransport({
SES: new SES({
region: 'us-gov-west-1',
endpoint: 'https://email.us-gov-west-1.amazonaws.com'
region: process.env.EMAIL_REGION!
})
});
await transporter.sendMail(mailOptions);
Expand Down Expand Up @@ -363,18 +316,9 @@ export const sendRegistrationApprovedEmail = async (
replyTo: process.env.CROSSFEED_SUPPORT_EMAIL_REPLYTO!
};

process.env.HTTPS_PROXY = 'http://proxy.lz.us-cert.gov:8080';
process.env.HTTP_PROXY = 'http://proxy.lz.us-cert.gov:8080';
const proxyAgent = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
AWS.config.update({
httpOptions: {
agent: proxyAgent ? httpProxy(proxyAgent) : undefined
}
});
const transporter = nodemailer.createTransport({
SES: new SES({
region: 'us-gov-west-1',
endpoint: 'https://email.us-gov-west-1.amazonaws.com'
region: process.env.EMAIL_REGION!
})
});
await transporter.sendMail(mailOptions);
Expand Down
2 changes: 0 additions & 2 deletions backend/src/api/saved-searches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import { connectToDatabase, SavedSearch, Vulnerability } from '../models';
import { validateBody, wrapHandler, NotFound, Unauthorized } from './helpers';
import { FindManyOptions } from 'typeorm';
import logger from '../tools/lambda-logger';

export const del = wrapHandler(async (event) => {
const id = event.pathParameters?.searchId;
Expand Down Expand Up @@ -125,7 +124,6 @@ export const list = wrapHandler(async (event) => {
: PAGE_SIZE;
const page = event.query?.page ? parseInt(event.query?.page) : 1;

logger.info(event.query);
const result = await SavedSearch.findAndCount({
where,
take: pageSize,
Expand Down
29 changes: 26 additions & 3 deletions backend/src/api/scan-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import {
IsIn,
ValidateNested,
isUUID,
IsUUID,
IsOptional,
IsObject
} from 'class-validator';
import { Type } from 'class-transformer';
import { ScanTask, connectToDatabase } from '../models';
import { validateBody, wrapHandler, NotFound, Unauthorized } from './helpers';
import { SelectQueryBuilder } from 'typeorm';
import { isGlobalViewAdmin, isGlobalWriteAdmin } from './auth';
import {
getTagOrganizations,
isGlobalViewAdmin,
isGlobalWriteAdmin
} from './auth';
import ECSClient from '../tasks/ecs-client';

const PAGE_SIZE = parseInt(process.env.PAGE_SIZE ?? '') || 25;
Expand All @@ -25,6 +30,14 @@ class ScanTaskFilters {
@IsString()
@IsOptional()
status?: string;

@IsUUID()
@IsOptional()
organization?: string;

@IsUUID()
@IsOptional()
tag?: string;
}

class ScanTaskSearch {
Expand All @@ -46,7 +59,7 @@ class ScanTaskSearch {
@IsOptional()
filters?: ScanTaskFilters;

filterResultQueryset(qs: SelectQueryBuilder<ScanTask>) {
async filterResultQueryset(qs: SelectQueryBuilder<ScanTask>, event) {
if (this.filters?.name) {
qs.andWhere('scan.name ILIKE :name', {
name: `${this.filters?.name}`
Expand All @@ -57,6 +70,16 @@ class ScanTaskSearch {
status: `${this.filters?.status}`
});
}
if (this.filters?.organization) {
qs.andWhere('organization.id = :org', {
org: this.filters.organization
});
}
if (this.filters?.tag) {
qs.andWhere('organization.id IN (:...orgs)', {
orgs: await getTagOrganizations(event, this.filters.tag)
});
}
return qs;
}

Expand All @@ -67,7 +90,7 @@ class ScanTaskSearch {
.skip(PAGE_SIZE * (this.page - 1))
.take(PAGE_SIZE);

this.filterResultQueryset(qs);
await this.filterResultQueryset(qs, event);
return qs.getManyAndCount();
}
}
Expand Down
30 changes: 0 additions & 30 deletions backend/src/api/scans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,6 @@ interface ScanSchema {
}

export const SCAN_SCHEMA: ScanSchema = {
testProxy: {
type: 'fargate',
isPassive: false,
global: true,
description: 'Not a real scan, used to test proxy'
},
test: {
type: 'fargate',
isPassive: false,
global: true,
description: 'Not a real scan, used to test'
},
censys: {
amass: {
type: 'fargate',
isPassive: false,
Expand Down Expand Up @@ -234,23 +221,6 @@ export const SCAN_SCHEMA: ScanSchema = {
cpu: '1024',
memory: '8192'
},
dotgov: {
type: 'fargate',
isPassive: true,
global: true,
description:
'Create organizations based on root domains from the dotgov registrar dataset. All organizations are created with the "dotgov" tag and have a " (dotgov)" suffix added to their name.'
},
searchSync: {
type: 'fargate',
isPassive: true,
global: true,
cpu: '2048',
memory: '16384',
description:
'Syncs records with Elasticsearch so that they appear in search results.'
},
intrigueIdent: {
wappalyzer: {
type: 'fargate',
isPassive: true,
Expand Down
5 changes: 0 additions & 5 deletions backend/src/api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ import {
isOrgAdmin,
isGlobalWriteAdmin
} from './auth';
import { Type, plainToClass } from 'class-transformer';
import { IsNull } from 'typeorm';
import { create } from './organizations';
import logger from '../tools/lambda-logger';
import { fetchAssessmentsByUser } from '../tasks/rscSync';

class UserSearch {
Expand Down Expand Up @@ -327,7 +323,6 @@ export const invite = wrapHandler(async (event) => {
let user = await User.findOne({
email: body.email
});
logger.info(user);
let organization: Organization | undefined;

if (body.organization) {
Expand Down
9 changes: 4 additions & 5 deletions backend/src/api/vulnerabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ class VulnerabilitySearch {
: `vulnerability.${this.sort}`;
let qs = Vulnerability.createQueryBuilder('vulnerability')
.leftJoinAndSelect('vulnerability.domain', 'domain')
.leftJoinAndSelect('domain.organization', 'organization');
.leftJoinAndSelect('domain.organization', 'organization')
.leftJoinAndSelect('vulnerability.service', 'service');

if (groupBy) {
qs = qs
Expand All @@ -185,9 +186,7 @@ class VulnerabilitySearch {
])
.orderBy('cnt', 'DESC');
} else {
qs = qs
.leftJoinAndSelect('vulnerability.service', 'service')
.orderBy(sort, this.order);
qs = qs.orderBy(sort, this.order);
}

if (pageSize !== -1) {
Expand Down Expand Up @@ -283,7 +282,7 @@ export const update = wrapHandler(async (event) => {
* - Vulnerabilities
*/
export const list = wrapHandler(async (event) => {
await connectToDatabase();
await connectToDatabase(true);
const search = await validateBody(VulnerabilitySearch, event.body);
const [result, count] = await search.getResults(event);
return {
Expand Down
Loading

0 comments on commit e3f1f1c

Please sign in to comment.