Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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: 1 addition & 1 deletion src/db/dbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export abstract class StigDB {
abstract traverseNodeIn(id: string): Promise<StixObject[]>;
abstract traverseNodeOut(id: string): Promise<StixObject[]>;
abstract getDiff(nodes: StixObject[], edges: StixRelationshipObject[]): Promise<[StixObject, Delta][]>;
abstract updateDB(stix_nodes: StixObject[], stix_edges: StixRelationshipObject[]): Promise<{ nodes: number; edges: number; errors: number; }> ;
abstract updateDB(stix_nodes: StixObject[], stix_edges: StixRelationshipObject[]): Promise<{ nodes: number; edges: number; errors: number; invalIds:string[]|undefined}> ;
abstract executeQuery(query: string): Promise<StixObject[]>;
abstract close(): void;
abstract is_closed(): boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/db/neo4j/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class Neo4jStigDB implements StigDB {
* @returns Promise<string>
* @memberof StigDB
*/
public updateDB(stix_nodes: StixObject[], stix_edges: StixRelationshipObject[]): Promise<{ nodes: number; edges: number; errors: number; }> {
public updateDB(stix_nodes: StixObject[], stix_edges: StixRelationshipObject[]): Promise<{ nodes: number; edges: number; errors: number; invalIds: string[]|undefined}> {
const time = moment().utc().format('YYYY-MM-DDTHH:mm:ss.SSS[Z]');
const node_params = stix_nodes.map(stix => {
const stixCoreType = stencilItems.find(stencilItem => stencilItem.id === stix.type)?.type;
Expand Down Expand Up @@ -239,7 +239,7 @@ export class Neo4jStigDB implements StigDB {
return this.wrapSession(async (s: Session) => {
const nodes = node_params.length === 0 ? 0 : await s.executeWrite(dbWrite(set_node_query, node_params));
const edges = edge_params.length === 0 ? 0 : await s.executeWrite(dbWrite(set_rel_query, edge_params));
return { nodes, edges, errors: stix_edges.length + stix_nodes.length - nodes - edges };
return { nodes, edges, errors: stix_edges.length + stix_nodes.length - nodes - edges, invalIds:undefined};
});
}

Expand Down
11 changes: 9 additions & 2 deletions src/layouts/DBOperationsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ const DBOperationsContainer: React.FC = () => {
const stix_nodes: StixObject[] = nodes.map(cycore2stix).filter(s => s !== undefined);
const stix_edges = edges.map(cycore2stix).filter(s => s !== undefined) as StixRelationshipObject[];
(async () => {
const { nodes, edges, errors } = await commit(stix_nodes, stix_edges);
const { nodes, edges, errors, invalIds} = await commit(stix_nodes, stix_edges);
const toastType: AlertType = errors === 0 ? "success" : "error";
const message = errors === 0 ? `Submitted ${nodes}/${stix_nodes.length} node(s) and ${edges}/${stix_edges.length} edge(s)` : `An error occurred while saving to the database`;
const message = errors === 0 ? `Submitted ${nodes}/${stix_nodes.length} node(s) and ${edges}/${stix_edges.length} edge(s)` : `${errors} error(s) occurred while saving to the database`;
addNotification(message, toastType);
if (invalIds !== undefined && invalIds.length>0){
let mesg = "The following STIX objects were invalid: "
invalIds.forEach(objID => {
mesg+="\n"+objID
});
addNotification(mesg, "error")
}
})();
}

Expand Down
14 changes: 10 additions & 4 deletions src/util/DbFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,25 @@ export async function use_db(config: DBProfile) {
}
}

export async function commit(nodes: StixObject[], edges: StixRelationshipObject[]): (Promise<{ nodes: number; edges: number; errors: number; }>) {
export async function commit(nodes: StixObject[], edges: StixRelationshipObject[]): (Promise<{ nodes: number; edges: number; errors: number; invalIds:string[]|undefined;}>) {
let filteredNodes = [];
let filteredEdges = [];
try {
if (!nodes.every(checkProps) || !edges.every(checkProps)) throw new Error('Invalid stix');
// if (!nodes.every(checkProps) || !edges.every(checkProps)) throw new Error('Invalid stix');
let invalIds:string[] = [];
nodes.forEach(n=>{if (!checkProps(n)){invalIds.push(n.id);}})
edges.forEach(e=>{if (!checkProps(e)){invalIds.push(e.id);}})
if (invalIds.length > 0){
return { nodes: 0, edges: 0, errors: invalIds.length, invalIds: invalIds };
}
filteredNodes = nodes;//.filter(checkProps);
filteredEdges = edges;//.filter(checkProps);
} catch (error) {
console.error(error);
return { nodes: 0, edges: 0, errors: 1 };
return { nodes: 0, edges: 0, errors: 1, invalIds: undefined };
}
const pair: [StixObject[], StixRelationshipObject[]] = [filteredNodes, filteredEdges];
return wrapReturn(pair, () => ({ nodes: 0, edges: 0, errors: 0 }), ([n, e]) => currentDB.updateDB(n, e));
return wrapReturn(pair, () => ({ nodes: 0, edges: 0, errors: 0, invalIds: undefined }), ([n, e]) => currentDB.updateDB(n, e));
}

export function db_delete(stix: StixObject[]) {
Expand Down