Пример #1
0
        public IEnumerable <KeyValuePair <string, IPropertyType> > GetProperties([NotNull] IThreatModel model)
        {
            IEnumerable <KeyValuePair <string, IPropertyType> > result = null;

            var threatTypes = model.ThreatTypes?
                              .OrderByDescending(x => x.Severity, new SeverityComparer())
                              .ToArray();

            if (threatTypes?.Any() ?? false)
            {
                var dict = new Dictionary <string, IPropertyType>();

                foreach (var threatType in threatTypes)
                {
                    var properties = threatType.Properties?
                                     .Where(x => !(x.PropertyType?.DoNotPrint ?? true))
                                     .Select(x => x.PropertyType)
                                     .ToArray();

                    if (properties?.Any() ?? false)
                    {
                        foreach (var property in properties)
                        {
                            if (!dict.ContainsKey(property.Name))
                            {
                                dict.Add(property.Name, property);
                            }
                        }
                    }

                    var eventProperties = new ListThreatEventsPlaceholder().GetProperties(model)?.ToArray();
                    if (eventProperties?.Any() ?? false)
                    {
                        foreach (var ep in eventProperties)
                        {
                            var text = $"[From Events] {ep.Key}";
                            if (!dict.ContainsKey(text))
                            {
                                dict.Add(text, ep.Value);
                            }
                        }
                    }
                }

                result = dict.Where(x => !x.Key.StartsWith("[From Events] "))
                         .OrderBy(x => model.GetSchema(x.Value.SchemaId).Priority)
                         .ThenBy(x => model.GetSchema(x.Value.SchemaId).Namespace)
                         .ThenBy(x => model.GetSchema(x.Value.SchemaId).Name)
                         .ThenBy(x => x.Value.Priority)
                         .Union(dict.Where(x => x.Key.StartsWith("[From Events] "))
                                .OrderBy(x => model.GetSchema(x.Value.SchemaId).Priority)
                                .ThenBy(x => x.Value.Priority))
                         .ToArray();
            }

            return(result);
        }
Пример #2
0
        public IEnumerable <ListItem> GetList(IThreatModel model)
        {
            IEnumerable <ListItem> result = null;

            var threatTypes = model.ThreatTypes?
                              .OrderByDescending(x => x.Severity, new SeverityComparer())
                              .ThenBy(x => x.Name)
                              .ToArray();

            if (threatTypes?.Any() ?? false)
            {
                var list = new List <ListItem>();

                var eventProperties = new ListThreatEventsPlaceholder().GetProperties(model)?
                                      .OrderBy(x => model.GetSchema(x.Value.SchemaId).Priority)
                                      .ThenBy(x => model.GetSchema(x.Value.SchemaId).Namespace)
                                      .ThenBy(x => model.GetSchema(x.Value.SchemaId).Name)
                                      .ThenBy(x => x.Value.Priority)
                                      .ToArray();

                foreach (var threatType in threatTypes)
                {
                    var threatEvents = model.GetThreatEvents(threatType)?.ToArray();
                    if (threatEvents?.Any() ?? false)
                    {
                        var items = new List <ItemRow>();

                        items.Add(new TextRow("Severity", threatType.Severity.Name,
                                              threatType.Severity.TextColor, threatType.Severity.BackColor, true, true, 75));
                        items.Add(new TextRow("Description", threatType.Description));
                        items.Add(new ListRow("Affected Objects",
                                              threatEvents.Select(x =>
                                                                  new Line($"{x.Parent.Name}",
                                                                           $"[{model.GetIdentityTypeInitial(x.Parent)}] ",
                                                                           $" ({x.Severity.Name})",
                                                                           new [] { x.ParentId }))));
                        items.Add(new TableRow("Approved Mitigations", new[]
                        {
                            new TableColumn("Object", 150),
                            new TableColumn("Mitigation", 200),
                            new TableColumn("Severity", 75),
                            new TableColumn("Strength", 75)
                        }, GetCells(GetMitigations(threatEvents, MitigationStatus.Approved))));
                        items.Add(new TableRow("Existing Mitigations", new[]
                        {
                            new TableColumn("Object", 150),
                            new TableColumn("Mitigation", 200),
                            new TableColumn("Severity", 75),
                            new TableColumn("Strength", 75)
                        }, GetCells(GetMitigations(threatEvents, MitigationStatus.Existing))));
                        items.Add(new TableRow("Implemented Mitigations", new[]
                        {
                            new TableColumn("Object", 150),
                            new TableColumn("Mitigation", 200),
                            new TableColumn("Severity", 75),
                            new TableColumn("Strength", 75)
                        }, GetCells(GetMitigations(threatEvents, MitigationStatus.Implemented))));
                        items.Add(new TableRow("Planned Mitigations", new[]
                        {
                            new TableColumn("Object", 150),
                            new TableColumn("Mitigation", 200),
                            new TableColumn("Severity", 75),
                            new TableColumn("Strength", 75)
                        }, GetCells(GetMitigations(threatEvents, MitigationStatus.Planned))));
                        items.Add(new TableRow("Proposed Mitigations", new[]
                        {
                            new TableColumn("Object", 150),
                            new TableColumn("Mitigation", 200),
                            new TableColumn("Severity", 75),
                            new TableColumn("Strength", 75)
                        }, GetCells(GetMitigations(threatEvents, MitigationStatus.Proposed))));

                        var itemRows = threatType.GetItemRows()?.ToArray();
                        if (itemRows?.Any() ?? false)
                        {
                            items.AddRange(itemRows);
                        }

                        if (eventProperties?.Any() ?? false)
                        {
                            foreach (var ep in eventProperties)
                            {
                                if (threatEvents.Any(x => x.HasProperty(ep.Value)))
                                {
                                    items.Add(new TableRow($"[From Events] {ep.Key}", new []
                                    {
                                        new TableColumn("Object", 150),
                                        new TableColumn("Value", 350)
                                    }, GetCells(threatEvents.Where(x => x.HasProperty(ep.Value)), ep.Value)));
                                }
                            }
                        }

                        list.Add(new ListItem(threatType.Name, threatType.Id, items));
                    }
                }

                result = list;
            }

            return(result);
        }