@appsensorlike/appsensorlike
Version:
A port of OWASP AppSensor reference implementation
220 lines (219 loc) • 8.72 kB
JavaScript
import { Utils } from "../../core.js";
/**
* Represents the configuration for server-side components. Additionally,
* contains various helper methods for common configuration-related
* actions.
*/
class ServerConfiguration {
constructor() {
this.rules = [];
this.detectionPoints = { detectionPoints: [] };
this.correlationSets = [];
this.clientApplicationIdentificationHeaderName = ServerConfiguration.DEFAULT_HEADER_NAME;
this.clientApplications = [];
this.serverHostName = '';
this.serverPort = 0;
this.serverSocketTimeout = 0;
//Change for adding new custom client specific detection points
this.customDetectionPoints = new Map();
}
checkValidInitialize() {
//when loaded from a config file
if (!this.rules) {
this.rules = [];
}
if (!this.correlationSets) {
this.correlationSets = [];
}
if (!this.clientApplications) {
this.clientApplications = [];
}
this.customDetectionPoints = new Map();
}
getCustomDetectionPoints() {
return this.customDetectionPoints;
}
setCustomDetectionPoints(customPoints) {
this.customDetectionPoints = customPoints;
return this;
}
getConfigurationFile() {
return this.configurationFile;
}
setConfigurationFile(configurationFile) {
this.configurationFile = configurationFile;
return this;
}
getRules() {
return this.rules;
}
setRules(rules) {
this.rules = rules;
return this;
}
getDetectionPoints() {
return this.detectionPoints.detectionPoints;
}
setDetectionPoints(detectionPoints) {
this.detectionPoints.detectionPoints = detectionPoints;
return this;
}
getCorrelationSets() {
return this.correlationSets;
}
setCorrelationSets(correlationSets) {
this.correlationSets = correlationSets;
return this;
}
getClientApplicationIdentificationHeaderName() {
return this.clientApplicationIdentificationHeaderName;
}
getClientApplicationIdentificationHeaderNameOrDefault() {
return (this.clientApplicationIdentificationHeaderName != null) ?
this.clientApplicationIdentificationHeaderName : ServerConfiguration.DEFAULT_HEADER_NAME;
}
setClientApplicationIdentificationHeaderName(clientApplicationIdentificationHeaderName) {
this.clientApplicationIdentificationHeaderName = clientApplicationIdentificationHeaderName;
return this;
}
getClientApplications() {
return this.clientApplications;
}
setClientApplications(clientApplications) {
this.clientApplications = clientApplications;
return this;
}
getServerHostName() {
return this.serverHostName;
}
setServerHostName(serverHostName) {
this.serverHostName = serverHostName;
return this;
}
getServerPort() {
return this.serverPort;
}
setServerPort(serverPort) {
this.serverPort = serverPort;
return this;
}
getServerSocketTimeout() {
return this.serverSocketTimeout;
}
setServerSocketTimeout(serverSocketTimeout) {
this.serverSocketTimeout = serverSocketTimeout;
return this;
}
/**
* Find related detection systems based on a given detection system.
* This simply means those systems that have been configured along with the
* specified system id as part of a correlation set.
*
* @param detectionSystem system ID to evaluate and find correlated systems
* @return collection of strings representing correlation set, INCLUDING specified system ID
*/
getRelatedDetectionSystems(detectionSystem) {
let relatedDetectionSystems = [];
if (detectionSystem !== null) {
relatedDetectionSystems.push(detectionSystem.getDetectionSystemId());
if (this.correlationSets !== null) {
for (const correlationSet of this.correlationSets) {
if (correlationSet.getClientApplications() !== null) {
if (correlationSet.getClientApplications().indexOf(detectionSystem.getDetectionSystemId()) > -1) {
relatedDetectionSystems = relatedDetectionSystems.concat(correlationSet.getClientApplications());
}
}
}
}
}
return relatedDetectionSystems;
}
/**
* Locate matching detection points configuration from server-side config file.
*
* @param search detection point that has been added to the system
* @return DetectionPoint populated with configuration information from server-side config
*/
findDetectionPoints(search, clientApplicationName = null) {
const matches = [];
if (search !== null) {
const customDetPoints = this.getCustomDetectionPoints();
if (clientApplicationName !== null &&
customDetPoints && customDetPoints.size > 0) {
for (const customDetectionPoint of customDetPoints) {
if (clientApplicationName === customDetectionPoint[0]) {
for (const customPoint of customDetectionPoint[1]) {
if (customPoint.typeMatches(search)) {
matches.push(customPoint);
}
}
}
}
}
for (const configuredDetectionPoint of this.getDetectionPoints()) {
if (configuredDetectionPoint.typeMatches(search)) {
matches.push(configuredDetectionPoint);
}
}
}
return matches;
}
/**
* Finds all {@link Rule}s that could have been triggered by the {@link AppSensorEvent}.
* A trigger {@link AppSensorEvent} must be the final {@link AppSensorEvent} so
* if the corresponding {@link MonitorPoint} is in the {@link Rule}'s final {@link Expression}
* it should be evaluated.
*
* @param triggerEvent the {@link AppSensorEvent} that triggered the {@link Rule}
* @return a list of {@link Rule}s applicable to triggerEvent
*/
findRules(triggerEvent) {
const matches = [];
if (this.rules) {
for (const rule of this.rules) {
const detPoint = triggerEvent.getDetectionPoint();
if (detPoint !== null && rule.checkLastExpressionForDetectionPoint(detPoint)) {
matches.push(rule);
}
}
}
return matches;
}
findClientApplication(clientApplicationName) {
let clientApplication = undefined;
clientApplication = ServerConfiguration.clientApplicationCache.get(clientApplicationName);
if (!clientApplication && this.clientApplications) {
for (const configuredClientApplication of this.clientApplications) {
if (configuredClientApplication.getName() === clientApplicationName) {
clientApplication = configuredClientApplication;
//cache
ServerConfiguration.clientApplicationCache.set(clientApplicationName, clientApplication);
break;
}
}
}
return clientApplication;
}
equals(obj) {
if (this === obj)
return true;
if (obj === null)
return false;
if (this.constructor.name !== obj.constructor.name)
return false;
const other = obj;
return Utils.equalsArrayEntitys(this.detectionPoints.detectionPoints, other.getDetectionPoints()) &&
Utils.equalsArrayEntitys(this.correlationSets, other.getCorrelationSets()) &&
this.clientApplicationIdentificationHeaderName === other.getClientApplicationIdentificationHeaderName() &&
Utils.equalsArrayEntitys(this.clientApplications, other.getClientApplications()) &&
this.serverHostName === other.getServerHostName() &&
this.serverPort === other.getServerPort() &&
this.serverSocketTimeout === other.getServerSocketTimeout();
// &&
// this.geolocateIpAddresses === other.isGeolocateIpAddresses() &&
// this.geolocationDatabasePath === other.getGeolocationDatabasePath();
}
}
ServerConfiguration.DEFAULT_HEADER_NAME = "X-Appsensor-Client-Application-Name";
ServerConfiguration.clientApplicationCache = new Map();
export { ServerConfiguration };