Beispiel #1
0
        private static bool ApplyThreatType([NotNull] IIdentity identity,
                                            [NotNull] IThreatType threatType, [NotNull] SelectionRule rule)
        {
            bool result = false;

            if (rule.Evaluate(identity) && identity is IThreatEventsContainer container)
            {
                var threatEvent = container.AddThreatEvent(threatType);
                if (threatEvent == null)
                {
                    threatEvent = container.ThreatEvents.FirstOrDefault(x => x.ThreatTypeId == threatType.Id);
                }
                else
                {
                    result = true;
                }

                if (threatEvent != null)
                {
                    result |= threatEvent.ApplyMitigations();
                }
            }

            return(result);
        }
        private void _test_Click(object sender, EventArgs e)
        {
            _selectedInteractors.Items.Clear();
            _selectedProcesses.Items.Clear();
            _selectedDataStores.Items.Clear();
            _selectedDataFlows.Items.Clear();
            _modelSelected.Visible = false;

            var rule = Rule;

            if (_threatTypeRule != null && rule != null)
            {
                var entities = _model?.Entities?.OrderBy(x => x.Name).ToArray();
                if (entities?.Any() ?? false)
                {
                    foreach (var entity in entities)
                    {
                        if (_threatTypeRule.Evaluate(entity) && rule.Evaluate(entity))
                        {
                            var builder = new StringBuilder();
                            builder.AppendLine(entity.Name);
                            var diagrams = _model.Diagrams
                                .Where(x => x.Entities?.Any(y => y.AssociatedId == entity.Id) ?? false)
                                .ToArray();
                            if (diagrams?.Any() ?? false)
                            {
                                builder.AppendLine();
                                builder.AppendLine("Diagrams containing the Entity:");
                                foreach (var diagram in diagrams)
                                {
                                    builder.AppendLine($"- {diagram.Name}");
                                }
                            }

                            var item = new ListBoxItem()
                            {
                                Text = entity.ToString(),
                                Image = entity.GetImage(ImageSize.Small),
                                Tooltip = builder.ToString()
                            };

                            if (entity is IExternalInteractor)
                            {
                                _selectedInteractors.Items.Add(item);
                            }
                            else if (entity is IProcess)
                            {
                                _selectedProcesses.Items.Add(item);
                            }
                            else if (entity is IDataStore)
                            {
                                _selectedDataStores.Items.Add(item);
                            }
                        }
                    }
                }

                var dataFlows = _model?.DataFlows?.OrderBy(x => x.Name).ToArray();
                if (dataFlows?.Any() ?? false)
                {
                    foreach (var dataFlow in dataFlows)
                    {
                        if (_threatTypeRule.Evaluate(dataFlow) && rule.Evaluate(dataFlow))
                        {
                            var builder = new StringBuilder();
                            builder.AppendLine(dataFlow.Name);
                            var diagrams = _model.Diagrams
                                .Where(x => x.Links?.Any(y => y.AssociatedId == dataFlow.Id) ?? false)
                                .ToArray();
                            if (diagrams?.Any() ?? false)
                            {
                                builder.AppendLine();
                                builder.AppendLine("Diagrams containing the Flow:");
                                foreach (var diagram in diagrams)
                                {
                                    builder.AppendLine($"- {diagram.Name}");
                                }
                            }

                            _selectedDataFlows.Items.Add(new ListBoxItem()
                            {
                                Text = dataFlow.ToString(),
                                Image = Icons.Resources.flow_small,
                                Tooltip = builder.ToString()
                            });
                        }
                    }
                }

                _modelSelected.Visible = _threatTypeRule.Evaluate(_model) && rule.Evaluate(_model);
            }
        }