Skip to content

Commit

Permalink
feat: Functionality to report API endpoints of the application (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitsuthar committed Mar 1, 2024
1 parent 88ba456 commit ef4f414
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
33 changes: 28 additions & 5 deletions lib/instrumentation-security/core/route-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,44 @@ const routeMap = new Map();
* @param {*} key
* @param {*} value
*/
function setRoute(key, value){
routeMap.set(key,value);
function setRoute(key, value) {
routeMap.set(key, value);
}

/**
* Utility to get route corresponding to key
* @param {*} key
* @returns
*/
function getRoute(key){
function getRoute(key) {
return routeMap.get(key);
}

module.exports={
/**
* Utility to get all api end points
* @returns all keys from routeMap
*/
function getAllAPIEndPoints() {
let apiEndpoints = [];
try {
routeMap.forEach((value, key) => {
let obj = {};
let splitted = key.split('@');
obj.method = splitted[0];
obj.path = splitted[1];
obj.handler = routeMap.get(key);
apiEndpoints.push(obj);
})
} catch (error) {

}

return apiEndpoints;
}

module.exports = {
getRoute,
setRoute,
routeMap
routeMap,
getAllAPIEndPoints
}
6 changes: 2 additions & 4 deletions lib/instrumentation-security/hooks/express/nr-express.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ module.exports = function initialize(shim, express, moduleName) {

if (express.Router.use) {
wrapExpress4(shim, express)
} else {
// wrapExpress3(shim, express)
}
}

}

/**
Expand Down Expand Up @@ -57,7 +56,6 @@ function wrapExpress4(shim, express) {
}
})


//Wrapper to hook process_params method to extract out path param and query params.
shim.wrap(express.Router, 'process_params', function wrapParam(shim, fn) {
if (!shim.isFunction(fn)) {
Expand Down
29 changes: 29 additions & 0 deletions lib/nr-security-agent/lib/core/apiEndpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/*
* Copyright 2023 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: New Relic Software License v1.0
*/

const { BasicInfo } = require('./event');
const applicationInfo = require('./applicationinfo').getInstance();
/**
* Function Constructor to create object of FuzzFail event
* @param {*} fuzzHeader
*/
function APIEndPoint (apiEndpoints) {
BasicInfo.call(this);
this.jsonVersion = applicationInfo.jsonVersion;
this.jsonName = 'sec-application-url-mapping';
this.eventType = "sec-application-url-mapping"
this.applicationUUID = applicationInfo.applicationUUID;
this.groupName = applicationInfo.groupName;
this.mappings = apiEndpoints;

return this;
}

APIEndPoint.prototype.constructor = APIEndPoint;

module.exports = {
APIEndPoint
};
11 changes: 10 additions & 1 deletion lib/nr-security-agent/lib/core/websocket-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ const initialize = function () {
setTimeout(() => {
setInterval(sendHC, HC_INTERVAL_MS);
sendHC();
try {
let apiEndpoints = require('../../../instrumentation-security/core/route-manager').getAllAPIEndPoints();
let data = require('./apiEndpoints').APIEndPoint(apiEndpoints);
logger.debug("All API end points of the application is:",JSON.stringify(data));
getDispatcherAndSendEvent(data);
} catch (error) {
logger.debug("Error while processing API endpoints",error);
}

try {
const framework = NRAgent.environment.get('Framework')[0];
const dispatcher = NRAgent.environment.get('Dispatcher')[0];
Expand All @@ -78,8 +87,8 @@ const initialize = function () {
} catch (error) {
logger.debug("Error while extracting data from APM environment",error);
}
}, 20000);

}, 30000);

setInterval(commonUtils.logRollOver, 30000);
initLogger.info("[STEP-5] => Security Agent components started");
Expand Down

0 comments on commit ef4f414

Please sign in to comment.