Skip to content

Commit

Permalink
Merge branch 'develop' into feature/uk-language
Browse files Browse the repository at this point in the history
  • Loading branch information
randywoods1 committed Jan 30, 2024
2 parents 294823e + bc2c7f2 commit a2fdce3
Show file tree
Hide file tree
Showing 26 changed files with 475 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,8 @@ public void DeleteCsafProduct(string vendorName, string productName)

public XmlDocument xml = new XmlDocument();
public int incrementalId = 0;
// public int rootNodeX = 0;
public int rootNodeY = 0;
public List<Geometry> nodeLocations = new List<Geometry>();

public void CreateMalcolmDiagram(int assessmentId, List<MalcolmData> processedData)
Expand Down Expand Up @@ -1250,7 +1252,10 @@ public void CreateMalcolmDiagram(int assessmentId, List<MalcolmData> processedDa
int nodeCount = processedData[0].Graphs.Count;

// Generate the actual Diagram/XML objects
WalkDownTree(processedData[0].Trees[2], "");
for (int i = 0; i < processedData[0].Trees.Count; i++)
{
WalkDownTree(processedData[0].Trees[i], "");
}

// Save that XML to the Assessments table -- Diagram Markup.
SaveDiagram(assessmentId, xml, new DiagramRequest(), true);
Expand All @@ -1271,7 +1276,11 @@ public void WalkDownTree(TempNode node, string parentId)
symbol = _context.COMPONENT_SYMBOLS.Where(x => x.Abbreviation == node.Role).FirstOrDefault();
if (symbol == null && node.Role != null)
{
symbol = _context.COMPONENT_SYMBOLS.Where(x => x.Malcolm_Role == node.Role).FirstOrDefault();
int symbolId = _context.COMPONENT_SYMBOLS_MAPPINGS.Where(x => x.Application == "Malcolm" && x.Malcolm_Role == node.Role)
.Select(x => x.Component_Symbol_Id).FirstOrDefault();

symbol = _context.COMPONENT_SYMBOLS.Where(x => x.Component_Symbol_Id == symbolId).FirstOrDefault();

if (symbol == null)
{
symbol = _context.COMPONENT_SYMBOLS.Where(x => x.Symbol_Name == "Unknown").FirstOrDefault();
Expand Down Expand Up @@ -1361,24 +1370,32 @@ public XmlElement CreateEdge(string source, string target, string parentLayer)

edge.AppendChild(geometry);


return edge;
}

public Geometry AssignCoordinates(string parentId, int w, int h)
{
int x = 0;
int y = 0;
XmlElement parentNode = (XmlElement)xml.SelectSingleNode($"//UserObject[@id='{parentId}']");
Geometry geometry = new Geometry();

if (parentNode == null)
{
geometry.x = x;
geometry.y = y;

// geometry.x = rootNodeX;
geometry.x = 0;
geometry.y = rootNodeY; //gives a buffer if not the first tree
geometry.w = w;
geometry.h = h;

if (rootNodeY != 0)
{
do
{
geometry.y += 120;
}
while (AreCoordinatesOverlapping(geometry));
}

nodeLocations.Add(geometry);
return geometry;
}
Expand All @@ -1389,6 +1406,7 @@ public Geometry AssignCoordinates(string parentId, int w, int h)
Geometry newCoordinatesToTry = new Geometry();
newCoordinatesToTry.w = w;
newCoordinatesToTry.h = h;

do
{
newCoordinatesToTry = CircleAroundParent(parentCoordinates, i, revolution);
Expand All @@ -1408,6 +1426,11 @@ public Geometry AssignCoordinates(string parentId, int w, int h)
geometry.w = newCoordinatesToTry.w;
geometry.h = newCoordinatesToTry.h;
nodeLocations.Add(geometry);

// keeps track of where the next tree has to start
if (rootNodeY <= geometry.y)
rootNodeY = geometry.y + 120;

return geometry;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Web;
using System.Xml;
using CSETWeb_Api.BusinessLogic.BusinessManagers.Diagram.analysis.rules;
using CSETWeb_Api.BusinessLogic.BusinessManagers.Diagram.analysis.rules.MalcolmRules;
using CSETWebCore.Business.BusinessManagers.Diagram.analysis;
using CSETWebCore.Business.Diagram.analysis.rules;
using CSETWebCore.Business.Diagram.Analysis;
Expand Down Expand Up @@ -89,7 +90,8 @@ private List<IDiagramAnalysisNodeMessage> AnalyzeNetwork(SimplifiedNetwork netwo
{
Assessment_Id = assessment_id,
Id = m.Number,
WarningText = sb.ToString()
WarningText = sb.ToString(),
Rule_Violated = m.Rule_Violated
});
});

Expand All @@ -99,5 +101,65 @@ private List<IDiagramAnalysisNodeMessage> AnalyzeNetwork(SimplifiedNetwork netwo

return msgs;
}

// will return a list of the violated rule numbers
public List<int> PerformMalcolmAnalysis(XmlDocument xDoc)
{
String sal = _context.STANDARD_SELECTION.Where(x => x.Assessment_Id == assessment_id).First().Selected_Sal_Level;
SimplifiedNetwork network = new SimplifiedNetwork(this.imageToTypePath, sal);
network.ExtractNetworkFromXml(xDoc);

List<int> rulesViolated = MalcolmAnalyzeNetwork(network);
return rulesViolated;
}

private List<int> MalcolmAnalyzeNetwork(SimplifiedNetwork network)
{
List<IRuleEvaluate> rules = new List<IRuleEvaluate>();
rules.Add(new Rule8(network));

//NetworkWalk walk = new NetworkWalk();
//walk.printGraphSimple(network.Nodes.Values.ToList());
List<int> rulesViolated = new List<int>();
List<IDiagramAnalysisNodeMessage> msgs = new List<IDiagramAnalysisNodeMessage>();
foreach (IRuleEvaluate rule in rules)
{
msgs.AddRange(rule.Evaluate());
}
foreach (IDiagramAnalysisNodeMessage message in msgs)
{
rulesViolated.Add(message.Rule_Violated);
}
// number and persist warning messages

//var oldWarnings = _context.NETWORK_WARNINGS.Where(x => x.Assessment_Id == assessment_id).ToList();
//_context.NETWORK_WARNINGS.RemoveRange(oldWarnings);
//_context.SaveChanges();

//int n = 0;
//msgs.ForEach(m =>
//{
// StringBuilder sb = new StringBuilder();
// m.SetMessages.ToList().ForEach(m2 =>
// {
// sb.AppendLine(m2);
// });

// m.Number = ++n;
// _context.NETWORK_WARNINGS.Add(new NETWORK_WARNINGS
// {
// Assessment_Id = assessment_id,
// Id = m.Number,
// WarningText = sb.ToString(),
// Rule_Violated = m.Rule_Violated
// });
//});


//_context.SaveChanges();


return rulesViolated;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class DiagramAnalysisNodeMessage : IDiagramAnalysisNodeMessage

public HashSet<String> SetMessages { get; set; }
public int MessageIdentifier { get; set; }
public int Rule_Violated { get; set; }

public DiagramAnalysisNodeMessage()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface IDiagramAnalysisNodeMessage
string vertex { get; set; }
string Message { get; set; }
int MessageIdentifier { get; set; }
int Rule_Violated { get; set; }
string NodeId1 { get; set; }
string NodeId2 { get; set; }
HashSet<string> SetMessages { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public bool IsIDSOrIPS
get
{
return this.Component_Symbol_Id == Constants.Constants.IDS_TYPE || this.Component_Symbol_Id == Constants.Constants.IPS_TYPE;
}
}

public bool IsIPS
{
get
{
return this.Component_Symbol_Id == Constants.Constants.IPS_TYPE;

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public List<IDiagramAnalysisNodeMessage> Messages
/// <param name="component1"></param>
/// <param name="component2"></param>
/// <param name="text"></param>
public void SetLineMessage(NetworkNode component1, NetworkNode component2, string text)
public void SetLineMessage(NetworkNode component1, NetworkNode component2, string text, int ruleViolated)
{
DiagramAnalysisNodeMessage messageNode;
//flag node and put up the message
Expand All @@ -65,7 +65,8 @@ public void SetLineMessage(NetworkNode component1, NetworkNode component2, strin
NodeId1 = component1.ID,
NodeId2 = component2.ID,
edgeId = appropriateEdgeId,
SetMessages = new HashSet<string>()
SetMessages = new HashSet<string>(),
Rule_Violated = ruleViolated
};

dictionaryLineMessages.Add(pair, messageNode);
Expand All @@ -84,7 +85,7 @@ private string findEdgeId(NetworkNode component1, NetworkNode component2, Compon
return link.ID;
}

public void SetNodeMessage(NetworkNode component, string text)
public void SetNodeMessage(NetworkNode component, string text, int ruleViolated)
{
DiagramAnalysisNodeMessage messageNode;
//flag node and put up the message
Expand All @@ -99,7 +100,8 @@ public void SetNodeMessage(NetworkNode component, string text)
{
Component = (NetworkComponent)component,
SetMessages = new HashSet<string>(),
NodeId1 = component.ID
NodeId1 = component.ID,
Rule_Violated = ruleViolated
};
dictionaryNodeMessages.Add(component.ComponentGuid, messageNode);
IsMessageAdded = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
////////////////////////////////
//
// Copyright 2023 Battelle Energy Alliance, LLC
//
//
////////////////////////////////
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSETWebCore.Business;
using CSETWebCore.Business.BusinessManagers.Diagram.analysis;
using CSETWebCore.Business.Diagram.Analysis;
using CSETWebCore.Business.Diagram.analysis.rules;

namespace CSETWeb_Api.BusinessLogic.BusinessManagers.Diagram.analysis.rules.MalcolmRules
{
class Rule8 : AbstractRule, IRuleEvaluate
{

private String rule8 = "The subnet should have an IPS (Intrusion Prevention System) inline to " +
"provide a swift response if malicious traffic penetrates the firewall.";

private SimplifiedNetwork network;

public Rule8(SimplifiedNetwork simplifiedNetwork)
{
this.network = simplifiedNetwork;
}

public List<IDiagramAnalysisNodeMessage> Evaluate()
{
var firewalls = network.Nodes.Values.Where(x => x.IsFirewall).ToList();
foreach (var firewall in firewalls)
{
Visited.Clear();
CheckRule8(firewall);
}
return this.Messages;
}

private HashSet<String> Visited = new HashSet<string>();

/// <summary>
/// Check Firewall for IPS and IDS past the firewall
/// </summary>
/// <param name="multiServiceComponent"></param>
/// <param name="visitedNodes"></param>
private void CheckRule8(NetworkComponent firewall)
{
// This code is here because component can be a multiple service component that is IPS
if (firewall.IsIPS)
{
return;
}

//recurse through all the edges and see if you can find an ids or ips
//if it is in the same zone
foreach (NetworkComponent child in firewall.Connections)
{
if (child.IsInSameZone(firewall))
{
if (child.IsIPS)
{
return;
}
else if (RecurseDownConnections(child, firewall))
{
return;
}
}
}

String componentName = "unnamed";
if (!String.IsNullOrWhiteSpace(firewall.ComponentName))
{
componentName = firewall.ComponentName;
}

String text = String.Format(rule8, componentName).Replace("\n", " ");
SetNodeMessage(firewall, text, 8); // 8 because rule8 was violated
}

private bool RecurseDownConnections(NetworkComponent itemToCheck, NetworkComponent firewall)
{
foreach (NetworkComponent child in itemToCheck.Connections)
{
if (Visited.Add(child.ID))
{
//Trace.WriteLine("->" + child.ComponentName + ":" + firewall.ComponentName);
if (child.IsInSameZone(firewall))
{
if (child.IsIPS)
{
//Trace.WriteLine("Found it");
return true;
}
else if (RecurseDownConnections(child, firewall))
{
return true;
}
}
}
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void checkRule1()
if (!allowToConnect.Contains(child.Component_Symbol_Id))
{
String text = String.Format(rule1, node.ComponentName, child.ComponentName).Replace("\n", " ");
SetLineMessage(node, child, text);
SetLineMessage(node, child, text, 1); // 1 because Rule1 was violated
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void CheckRule2(NetworkComponent firewall)
}

String text = String.Format(rule2, componentName).Replace("\n", " ");
SetNodeMessage(firewall, text);
SetNodeMessage(firewall, text, 2); // 2 because rule2 was violated
}

private bool RecurseDownConnections(NetworkComponent itemToCheck, NetworkComponent firewall)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void CheckRule34(NetworkComponent component)
}

String text = String.Format(rule3, componentName).Replace("\n", " ");
SetNodeMessage(component, text);
SetNodeMessage(component, text, 3); // 3 because rule3 was violated
}
else
{
Expand All @@ -85,7 +85,7 @@ private void CheckRule34(NetworkComponent component)
}

String text = String.Format(rule4, componentName).Replace("\n", " ");
SetNodeMessage(component, text);
SetNodeMessage(component, text, 4); // 4 because rule4 was violated
}
}
}
Expand Down
Loading

0 comments on commit a2fdce3

Please sign in to comment.