public bool Execute(IIdentity identity) { bool result = false; if (identity is IEntity entity && entity.Model is IThreatModel model) { var schemaManager = new AssociatedDiagramPropertySchemaManager(model); var propertyType = schemaManager.GetAssociatedDiagramIdPropertyType(); if (propertyType != null) { var property = entity.GetProperty(propertyType); if (property is IPropertyIdentityReference identityReference && identityReference.Value is IDiagram diagram) { result = true; var factory = ExtensionUtils.GetExtensionByLabel <IPanelFactory>("Diagram"); if (factory != null) { OpenPanel?.Invoke(factory, diagram); } } } } if (!result) { ShowWarning?.Invoke("The Entity is not associated to any Diagram."); } return(result); }
private void OnOpenDiagram(IDiagram diagram) { var factory = ExtensionUtils.GetExtensionByLabel <IPanelFactory>("Diagram"); if (factory != null && diagram != null) { OpenPanel?.Invoke(factory, diagram); } }
private void OpenDiagram(Guid diagramId) { var diagram = _model.GetDiagram(diagramId); var factory = ExtensionUtils.GetExtensionByLabel <IPanelFactory>("Diagram"); if (factory != null && diagram != null) { OpenPanel?.Invoke(factory, diagram); } }
private void _superTooltip_MarkupLinkClick(object sender, MarkupLinkClickEventArgs e) { if (Guid.TryParse(e.HRef, out var id)) { var diagram = _model.GetDiagram(id); var factory = ExtensionUtils.GetExtensionByLabel <IPanelFactory>("Diagram"); if (factory != null && diagram != null) { OpenPanel?.Invoke(factory, diagram); } } }
public void ExecuteRibbonAction(IThreatModel threatModel, IActionDefinition action) { try { switch (action.Name) { case "Merge": var dialog = new OpenFileDialog() { AddExtension = true, AutoUpgradeEnabled = true, CheckFileExists = true, CheckPathExists = true, DefaultExt = "tm", DereferenceLinks = true, Filter = "Threat Model (*.tm)|*.tm|Threat Model Template (*.tmt)|*.tmt|Threat Model Json Template (*.tmk)|*.tmk", FilterIndex = 0, Title = "Select file to be merged", RestoreDirectory = true }; if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK) { using (var merge = new MergeDialog()) { merge.SetExecutionMode(_executionMode); if (merge.Initialize(threatModel, dialog.FileName)) { if (merge.ShowDialog(Form.ActiveForm) == DialogResult.OK) { RefreshPanels?.Invoke(this); var factory = ExtensionUtils.GetExtensionByLabel <IPanelFactory>("Diagram"); if (factory != null) { ClosePanels?.Invoke(factory); UpdateStatusInfoProviders?.Invoke(); } ShowMessage?.Invoke("Merge succeeded."); } } } } break; } } catch { ShowWarning?.Invoke("Merge failed.\nPlease close the document without saving it."); throw; } }
public bool Execute(IIdentity identity) { bool result = false; if (identity is IEntity entity && entity.Model is IThreatModel model) { using (var dialog = new DiagramSelectionDialog(entity)) { if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK) { var diagram = dialog.Diagram; if (diagram == null) { diagram = model.AddDiagram(dialog.DiagramName); } if (diagram != null) { var schemaManager = new AssociatedDiagramPropertySchemaManager(model); var propertyType = schemaManager.GetAssociatedDiagramIdPropertyType(); if (propertyType != null) { var property = entity.GetProperty(propertyType); if (property == null) { property = entity.AddProperty(propertyType, diagram.Id.ToString("N")); } else { property.StringValue = diagram.Id.ToString("N"); } result = true; DiagramAssociationHelper.NotifyDiagramAssociation(entity, diagram); var factory = ExtensionUtils.GetExtensionByLabel <IPanelFactory>("Diagram"); if (factory != null) { OpenPanel?.Invoke(factory, diagram); ShowMessage?.Invoke("Diagram has been associated successfully."); } } } } } } return(result); }
/// <summary> /// Creates a new ItemRow for a given Property. /// </summary> /// <param name="container">Container of the Property.</param> /// <param name="property">Property to be analyzed.</param> /// <returns>The created ItemRow.</returns> public static Cell Create([NotNull] IPropertiesContainer container, [NotNull] IProperty property) { Cell result = null; var propertyType = property.PropertyType; if (property is IPropertyIdentityReference propertyIdentityReference) { if (propertyIdentityReference.Value is IIdentity identity && identity is IThreatModelChild child) { result = new Cell($"{identity.Name}", $"[{child.Model.GetIdentityTypeInitial(identity)}] ", null, new[] { identity.Id }); } } else if (property is IPropertyJsonSerializableObject propertyJsonSerializableObject) { var propertyViewerId = propertyJsonSerializableObject.PropertyType.CustomPropertyViewer; if (!string.IsNullOrWhiteSpace(propertyViewerId)) { var factory = ExtensionUtils.GetExtensionByLabel <IPropertyViewerFactory>(propertyViewerId); if (factory != null) { var propertyViewer = factory.CreatePropertyViewer(container, property); if (propertyViewer != null) { var blocks = propertyViewer.Blocks?.Where(x => x.Printable).ToArray(); if (blocks?.Any() ?? false) { result = new Cell(blocks.First().Text); } } } } } else if (property is IPropertyList propertyList) { result = new Cell(propertyList.Value?.ToString()?.TrimEnd(' ', '\r', '\n')); } return(result); }
public void Execute([NotNull] IThreatModel model) { var schemaManager = new DevOpsConfigPropertySchemaManager(model); var iteration = schemaManager.CurrentIteration ?? schemaManager.PreviousIteration; if (iteration != null) { var extensionId = ExtensionUtils.GetExtensionByLabel <IConfigurationPanelFactory <Form> >( "Extensions Configuration Panel")?.GetExtensionId(); if (extensionId != null) { var normalizationReference = model.GetExtensionConfiguration(extensionId)? .GlobalGet <int>("normalization") ?? 0; var risk = model.EvaluateRisk(normalizationReference); if (risk > 0f) { schemaManager.SetIterationRisk(iteration, risk); } } } }
public void ExecuteCustomAction([NotNull] IActionDefinition action) { string text = null; bool warning = false; try { switch (action.Name) { case "AddDataFlow": //text = "Add Data Flow"; //_model.AddEntity<IDataStore>(); break; case "RemoveDataFlow": var selected = _grid.GetSelectedCells()?.OfType <GridCell>() .Select(x => x.GridRow) .Distinct() .ToArray(); if (_currentRow != null) { if ((selected?.Length ?? 0) > 1) { var outcome = MessageBox.Show(Form.ActiveForm, $"You have selected {selected.Length} Flows. Do you want to remove them all?\nPlease click 'Yes' to remove all selected Flows,\nNo to remove only the last one you selected, '{_currentRow?.Tag?.ToString()}'.\nPress Cancel to abort.", "Remove Flows", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button3); switch (outcome) { case DialogResult.Yes: bool removed = true; foreach (var row in selected) { bool r = false; if (row.Tag is IDataFlow flow) { r = _model.RemoveDataFlow(flow.Id); } removed &= r; if (r && row == _currentRow) { _properties.Item = null; _currentRow = null; } } if (removed) { text = "Remove Flows"; } else { warning = true; text = "One or more Flows cannot be removed."; } break; case DialogResult.No: if (_currentRow != null && _currentRow.Tag is IDataFlow flow2) { if (_model.RemoveDataFlow(flow2.Id)) { _properties.Item = null; _currentRow = null; text = "Remove Flow"; } else { warning = true; text = "The Flow cannot be removed."; } } break; } } else if (_currentRow != null && _currentRow.Tag is IDataFlow dataFlow && MessageBox.Show(Form.ActiveForm, $"You are about to remove Flow '{dataFlow.Name}'. Are you sure?", "Remove Flow", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { if (_model.RemoveDataFlow(dataFlow.Id)) { _properties.Item = null; text = "Remove Flow"; } else { warning = true; text = "The Flow cannot be removed."; } } } break; case "FindDataFlow": bool found = false; if (_currentRow != null && _currentRow.Tag is IDataFlow dataFlow2) { var diagrams = _model.Diagrams?.ToArray(); if (diagrams?.Any() ?? false) { foreach (var diagram in diagrams) { var flow = diagram.GetLink(dataFlow2.Id); if (flow != null) { found = true; var factory = ExtensionUtils.GetExtensionByLabel <IPanelFactory>("Diagram"); if (factory != null) { OpenPanel?.Invoke(factory, diagram); } break; } } } } if (!found) { warning = true; text = "The Flow has not been found in any Diagram."; } break; case "Refresh": LoadModel(); break; } if (warning) { ShowWarning?.Invoke(text); } else if (text != null) { ShowMessage?.Invoke($"{text} has been executed successfully."); } } catch { ShowWarning?.Invoke($"An error occurred during the execution of the action."); throw; } }
public void SetThreatModel(IThreatModel threatModel) { var schemaManager = new DevOpsConfigPropertySchemaManager(threatModel); var iterations = schemaManager.GetIterations()? .OrderBy(x => x.Start) .ToArray(); if (iterations?.Any() ?? false) { var current = schemaManager.CurrentIteration ?? schemaManager.PreviousIteration; float maxRisk = 0f; if (Chart?.ChartSeries.FirstOrDefault() is ChartSeries series) { SeriesPoint point; float risk = 0f; foreach (var iteration in iterations) { point = new SeriesPoint(iteration.Name); if (iteration == current) { var extensionId = ExtensionUtils.GetExtensionByLabel <IConfigurationPanelFactory <Form> >( "Extensions Configuration Panel")?.GetExtensionId(); if (extensionId != null) { var normalizationReference = threatModel.GetExtensionConfiguration(extensionId)? .GlobalGet <int>("normalization") ?? 0; if (normalizationReference > 0) { risk = threatModel.EvaluateRisk(normalizationReference); if (risk > 0f) { schemaManager.SetIterationRisk(iteration, risk); } } } } else { risk = schemaManager.GetIterationRisk(iteration); } point.ValueY = new object[] { ((object)risk) }; series.SeriesPoints.Add(point); if (maxRisk < risk) { maxRisk = risk; } } } var residualRiskSchemaManager = new ResidualRiskEstimatorPropertySchemaManager(threatModel); var estimator = residualRiskSchemaManager.SelectedEstimator; if (estimator != null) { var parameters = residualRiskSchemaManager.Parameters?.ToArray(); if (parameters?.Any() ?? false) { var infinite = residualRiskSchemaManager.Infinite; if (infinite < 0) { infinite = estimator.DefaultInfinite; } var p = parameters.ToDictionary(x => x.Name, x => x.Value); AcceptableRisk = estimator.GetAcceptableRisk(threatModel, p, infinite, 0); } else { AcceptableRisk = 0f; } if (AcceptableRisk > maxRisk) { Chart.AxisY.MaxValue = AcceptableRisk * 1.2f; } } } }