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

LZ to DMZ Sync #709

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ frontend/.pnp.js
backend/coverage
docs/coverage
frontend/coverage
backend/dmzLzSyncTestData.ts

# production
frontend/build
Expand Down
2 changes: 1 addition & 1 deletion backend/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ env:
es6: true
node: true
parser: '@typescript-eslint/parser'
ignorePatterns: [dist/**]
ignorePatterns: [dist/**, dmzLzSyncTestDAta.ts]
extends:
- plugin:prettier/recommended
- plugin:@typescript-eslint/eslint-recommended
Expand Down
13 changes: 13 additions & 0 deletions backend/db-init/initmdl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Create the user (role)
CREATE USER mdl_local WITH PASSWORD 'mini_data_lake';

SELECT CURRENT_USER;

-- Optionally grant privileges on a specific database (if required)
CREATE DATABASE mini_data_lake_local;
GRANT ALL PRIVILEGES ON DATABASE mini_data_lake_local TO mdl_local;
ALTER SCHEMA public OWNER TO mdl_local;
ALTER DATABASE mini_data_lake_local OWNER TO mdl_local;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;

SET ROLE mdl_local;
24 changes: 12 additions & 12 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@
"deploy-worker-staging": "./tools/deploy-worker.sh",
"lint": "eslint '**/*.{ts,tsx,js,jsx}'",
"lint:fix": "eslint '**/*.{ts,tsx,js,jsx}' --fix",
"mdl-datagen": "ts-node src/tools/generators.ts",
"pesyncdb": "docker compose exec -T backend npx ts-node src/tools/run-pesyncdb.ts",
"scan-exec": "docker compose exec -T backend npx ts-node src/tools/run-scanExecution.ts",
"syncdb": "docker compose exec -T backend npx ts-node src/tools/run-syncdb.ts",
"syncmdl": "docker compose exec -T backend npx ts-node src/tools/run-syncmdl.ts",
"syncnewmdl": "docker compose exec -T backend npx ts-node src/tools/run-syncnewmdl.ts",
"test": "jest --detectOpenHandles",
"test-python": "pytest"
},
Expand Down
49 changes: 48 additions & 1 deletion backend/src/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as cves from './cves';
import * as domains from './domains';
import * as notifications from './notifications';
import * as search from './search';
import * as sync from './sync';
import * as vulnerabilities from './vulnerabilities';
import * as organizations from './organizations';
import * as scans from './scans';
Expand All @@ -30,6 +31,7 @@ import { Request, Response, NextFunction } from 'express';
import fetch from 'node-fetch';
import * as searchOrganizations from './organizationSearch';
import { Logger, RecordMessage } from '../tools/logger';
import { parse } from 'papaparse';

const sanitizer = require('sanitizer');

Expand All @@ -52,7 +54,10 @@ const handlerToExpress =
pathParameters: req.params,
query: req.query,
requestContext: req.requestContext,
body: JSON.stringify(req.body || '{}'),
body:
typeof req.body !== 'string'
? JSON.stringify(req.body || '{}')
: req.body,
headers: req.headers,
path: req.originalUrl
},
Expand Down Expand Up @@ -89,6 +94,39 @@ app.use(
})
); // limit 1000 requests per 15 minutes

app.use((req, res, next) => {
// Middleware to parse CSV data
if (req.headers['content-type'] === 'text/csv') {
let data = '';

req.on('data', (chunk) => {
data += chunk;
});
req.on('end', () => {
const parsedData = parse(data, {
header: true,
skipEmptyLines: true
});

if (parsedData.errors.length > 0) {
return res
.status(400)
.json({ message: 'CSV Parsing Error', errors: parsedData.errors });
}
// We don't need to parse the data, just validate it
// CSV Parsing will happen in the handler
req.body = data;
next();
});

req.on('error', () => {
res.status(500).json({ message: 'Error reading CSV data' });
});
} else {
next();
}
});

app.use(express.json({ strict: false }));

// These CORS origins work in all Crossfeed environments
Expand Down Expand Up @@ -762,6 +800,15 @@ authenticatedRoute.post(
handlerToExpress(reports.list_reports)
);

if (process.env.IS_LOCAL) {
console.log('Unauthenticated local route');
app.post('/sync', handlerToExpress(sync.ingest));
} else {
authenticatedRoute.post('/sync', handlerToExpress(sync.ingest));
}

// authenticatedRoute.post('/sync', handlerToExpress(sync.ingest));

//Authenticated Registration Routes
authenticatedRoute.put(
'/users/:userId/register/approve',
Expand Down
7 changes: 4 additions & 3 deletions backend/src/api/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export const export_ = wrapHandler(async (event) => {
res.products = Object.values(products).join(', ');
return res;
});
const url = await client.saveCSV(
const res = await client.saveCSV(
Papa.unparse({
fields: [
'name',
Expand All @@ -247,13 +247,14 @@ export const export_ = wrapHandler(async (event) => {
],
data: result
}),
'domains'
'domains',
process.env.EXPORT_BUCKET_NAME
);

return {
statusCode: 200,
body: JSON.stringify({
url
url: res.url
})
};
});
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const export_ = wrapHandler(async (event) => {
return res;
});
const client = new S3Client();
const url = await client.saveCSV(
const { url } = await client.saveCSV(
Papa.unparse({
fields: [
'name',
Expand Down
Loading
Loading