Skip to content

Commit

Permalink
Maintain db functions for remediation collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Frankccv committed Sep 11, 2023
1 parent e9f76ac commit e416d8d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
17 changes: 10 additions & 7 deletions www/reportReader/busReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ async function queryIpMongo( attackId ) {
} finally {
// Close the MongoDB connection
client.close();
console.log("Query ip mongo" + ipAttacker)
return ipAttacker;
}
}
async function extractDescriptions(json1, json2) {
// Initialize an empty array to store the output JSON objects
const outputJson = {};
var ipAttacker = "10.2.2.3" ;

try{
if( typeof json1[0] === 'string'){
ipAttacker = await queryIpMongo( json1[0] );
const currentTimestampInSeconds = Math.floor(new Date().getTime() / 1000);
Expand All @@ -84,11 +83,17 @@ async function queryIpMongo( attackId ) {
outputJson.timestamp = currentTimestampInSeconds ;

}
} catch( err ){
console.error('Error:', err);

}
finally{
return outputJson;
}
}

function receiveMessage (channel, message) {
console.log( "[" + channel + "] " + message );
//console.log( "[" + channel + "] " + message );
try{
processMessage.process( message );
}catch( err ){
Expand Down Expand Up @@ -122,17 +127,15 @@ report_client_miugio.on('message', async function ( channel,message) {
//json.description = 'ciao';
// Print the JSON objects

console.log(jsonAttacks)
console.log(jsonRemediation)

//process message: insert into "sancus_report" collection
if( JSON.stringify(jsonAttacks) != '{}')

sancus_db.add("remediationAttack",[jsonAttacks]);

if( JSON.stringify(jsonRemediation) != '{}')
if( JSON.stringify(jsonRemediation) != '{}')

sancus_db.add("remediationVuln", [ jsonRemediation]);


}
catch (error) {
Expand Down
42 changes: 40 additions & 2 deletions www/reportReader/maintainDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ var additionalTime = 0;
* @param collectionPrefix
* @param timestamp: the timestamp of the last report of a probe having the given probeID
* @param probeID
* @returnsctionPrefix
* @param timestamp: the timestamp of the last report of a probe having the given probeID
* @param probeID
* @returns
*/
function _maintainCollection( db, collectionPrefix, timestamp, probeID ){
Expand Down Expand Up @@ -148,7 +151,23 @@ function _maintainDatabaseSize( database ){
setTimeout( _maintainDatabase, 1000, database );
});
}
function _maintainRemediationCollection ( database, collectionName, cb ) {
if( additionalTime === 0 )
return cb();
try{
console.log("_maintainRemediationCollection")
database.collection(collectionName).deleteMany({
timestamp: {
$lt: Math.floor(Date.now() / 1000) - (5 * 60) // 5 minutes in seconds
}
});

cb();
} catch( e ){
console.error( e );

}
}
function _maintainSecurityCollection( database, cb ){
//collection security is cutoff only if Database size >= the limit
if( additionalTime === 0 )
Expand Down Expand Up @@ -231,11 +250,31 @@ function _maintainDatabase( database ){
_maintainCollection( database, "data_sctp_" , timestamp );
_maintainCollection( database, "data_ndn_" , timestamp );
_maintainCollection( database, "availability_" , timestamp );


//collection security is cutoff only if Database size >= the limit
_maintainSecurityCollection( database, function(){

//this avoids delete DB so frequently
//it helps when trying delete documents from DB but the storage size does not reduce to DB_LIMIT_SIZE
// (as DB_LIMIT_SIZE is too small)
//maintain by db size
setTimeout( _maintainDatabaseSize, 10000, database );
});
//collection security is cutoff only if Database size >= the limit
console.log("Execute Db function for Remediation" );
_maintainRemediationCollection(database, "remediationAttack", function(){
console.log("DB remove remediationAttack");
//this avoids delete DB so frequently
//it helps when trying delete documents from DB but the storage size does not reduce to DB_LIMIT_SIZE
// (as DB_LIMIT_SIZE is too small)
//maintain by db size
setTimeout( _maintainDatabaseSize, 10000, database );
});
console.log("Execute Db function for Remediation");

_maintainRemediationCollection(database, "remediationVuln", function(){
console.log("DB remove remediationVuln");

//this avoids delete DB so frequently
//it helps when trying delete documents from DB but the storage size does not reduce to DB_LIMIT_SIZE
// (as DB_LIMIT_SIZE is too small)
Expand Down Expand Up @@ -273,4 +312,3 @@ process.on('SIGINT',function(){
console.log("Exit maintainer " + process.pid);
process.exit();
});

4 changes: 3 additions & 1 deletion www/routes/sancus/remediation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pub_sub = require("../../libs/kafka");
//Pass the message through the route
const { Kafka } = require('kafkajs');
const scriptCode = `
print("Remediation")// Your script code here
kubectl exec -it amf-45-ipds-0 -n ath-cmm-45 nft insert rule ip filter INPUT ip daddr 3.201.40.3 drop
// Your script code here
`;
async function produceMessage(msg) {
// Create a new Kafka instance
Expand Down
15 changes: 15 additions & 0 deletions www/test/test_remediation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const{ myRemediation, mySize} = require("../reportReader/maintainDB");
const { MongoClient } = require('mongodb');



const client = new MongoClient("mongodb://localhost:27017" ,{ useNewUrlParser: true, useUnifiedTopology: true });


// Connect to MongoDB
await client.connect();
const db = client.db("mmt-data");




0 comments on commit e416d8d

Please sign in to comment.