Example #1
0
        private void ImportBaseElementTemplates([NotNull] ThreatModel source, [NotNull] IThreatModel target)
        {
            var elements = source.ElementTypes?
                           .Where(x => x.IsGeneric)
                           .ToArray();

            if (elements?.Any() ?? false)
            {
                IPropertySchema schema       = null;
                var             baseEISchema = new BaseExternalInteractorPropertySchemaManager(target).GetSchema();
                var             basePSchema  = new BaseProcessPropertySchemaManager(target).GetSchema();
                var             baseDSSchema = new BaseDataStorePropertySchemaManager(target).GetSchema();
                var             baseTBSchema = new BaseTrustBoundaryPropertySchemaManager(target).GetSchema();

                foreach (var element in elements)
                {
                    switch (element.ElementType)
                    {
                    case ElementType.StencilRectangle:
                        schema = baseEISchema;
                        break;

                    case ElementType.StencilEllipse:
                        schema = basePSchema;
                        break;

                    case ElementType.StencilParallelLines:
                        schema = baseDSSchema;
                        break;

                    case ElementType.BorderBoundary:
                    case ElementType.LineBoundary:
                        schema = baseTBSchema;
                        break;
                    }

                    if (schema != null)
                    {
                        var properties = element.Properties?.ToArray();

                        var outOfScope = schema.GetPropertyType("Out of Scope") ??
                                         schema.AddPropertyType("Out of Scope", PropertyValueType.Boolean);
                        var reason = schema.GetPropertyType("Reason For Out Of Scope") ??
                                     schema.AddPropertyType("Reason For Out Of Scope", PropertyValueType.String);

                        AddProperties(schema, null, properties);
                    }
                }
            }
        }
Example #2
0
 private void InitializeThreatsPropertySchema([NotNull] IPropertySchema schema,
                                              [NotNull] IEnumerable <PropertyDefinition> sourceProperties)
 {
     if (sourceProperties.Any())
     {
         foreach (var property in sourceProperties)
         {
             if (!property.HideFromUi && !IsSpecial(property.Name))
             {
                 var propertyType = schema.GetPropertyType(property.Label);
                 if (propertyType == null)
                 {
                     if (property.Values?.Any() ?? false)
                     {
                         propertyType = schema.AddPropertyType(property.Label, PropertyValueType.List);
                         if (propertyType is IListPropertyType listPropertyType)
                         {
                             listPropertyType.SetListProvider(new ListProviderExtension());
                             listPropertyType.Context = property.Values.TagConcat();
                         }
                     }
                     else
                     {
                         schema.AddPropertyType(property.Label, PropertyValueType.String);
                     }
                 }
             }
         }
     }
 }
Example #3
0
        private static void AddProperties([NotNull] IThreatModel model,
                                          [NotNull] IPropertySchema baseSchema, IPropertySchema secondarySchema,
                                          IPropertiesContainer container, [NotNull] IEnumerable <Property> properties)
        {
            foreach (var property in properties)
            {
                if (!string.IsNullOrWhiteSpace(property.Name))
                {
                    var propertyType = baseSchema.GetPropertyType(property.Name) ??
                                       secondarySchema?.GetPropertyType(property.Name);

                    if (propertyType != null && container != null)
                    {
                        var value = property.Value;
                        if (string.IsNullOrWhiteSpace(value) && propertyType is IListPropertyType listPropertyType)
                        {
                            value = listPropertyType.Values.FirstOrDefault()?.Id;
                        }

                        var containerProperty = container.GetProperty(propertyType);
                        if (containerProperty == null)
                        {
                            container.AddProperty(propertyType, value);
                        }
                        else
                        {
                            containerProperty.StringValue = value;
                        }
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Auxiliary function to add properties from the Threat to the Threat Event.
        /// </summary>
        /// <param name="threatEvent">Threat Event to be updated.</param>
        /// <param name="threat">Source Threat.</param>
        /// <param name="schema">Property Schema for the new Property Types.</param>
        public static void AddProperties([NotNull] IThreatEvent threatEvent,
                                         [NotNull] Threat threat, [NotNull] IPropertySchema schema)
        {
            var names  = threat.ParameterNames?.ToArray();
            var labels = threat.ParameterLabels?.ToArray();

            if ((names?.Any() ?? false) && (labels?.Any() ?? false) && names.Length == labels.Length)
            {
                for (int i = 0; i < names.Length; i++)
                {
                    var name = names[i];

                    switch (name)
                    {
                    case "Title":
                        threatEvent.Name = threat.GetValue(name);
                        break;

                    case "UserThreatDescription":
                        threatEvent.Description = threat.GetValue(name);
                        break;

                    case "Priority":
                        var severityValue = threat.GetValue(name);
                        if (!string.IsNullOrWhiteSpace(severityValue) &&
                            Enum.TryParse <DefaultSeverity>(severityValue, out var defaultSeverity))
                        {
                            var severity = threatEvent.Model?.GetMappedSeverity((int)defaultSeverity);
                            if (severity != null)
                            {
                                threatEvent.Severity = severity;
                            }
                        }
                        break;

                    default:
                        var label        = labels[i];
                        var propertyType = schema.GetPropertyType(label);
                        if (propertyType != null)
                        {
                            var property = threatEvent.GetProperty(propertyType);
                            if (property == null)
                            {
                                threatEvent.AddProperty(propertyType, threat.GetValueFromLabel(label));
                            }
                            else
                            {
                                property.StringValue = threat.GetValueFromLabel(label);
                            }
                        }
                        break;
                    }
                }
            }
        }
Example #5
0
        private static void AddProperties([NotNull] IPropertySchema schema,
                                          IPropertiesContainer container, [NotNull] IEnumerable <Property> properties)
        {
            foreach (var property in properties)
            {
                if (!string.IsNullOrWhiteSpace(property.Name))
                {
                    var propertyType = schema.GetPropertyType(property.Name);
                    if (propertyType == null)
                    {
                        switch (property.Type)
                        {
                        case PropertyType.String:
                            propertyType = schema.AddPropertyType(property.Name,
                                                                  PropertyValueType.String);
                            break;

                        case PropertyType.Boolean:
                            propertyType = schema.AddPropertyType(property.Name,
                                                                  PropertyValueType.Boolean);
                            break;

                        case PropertyType.List:
                            propertyType =
                                schema.AddPropertyType(property.Name, PropertyValueType.List);
                            if (propertyType is IListPropertyType listPropertyType)
                            {
                                listPropertyType.Context = property.Values.TagConcat();
                                listPropertyType.SetListProvider(new ListProviderExtension());
                            }
                            break;
                        }
                    }

                    if (propertyType != null && container != null)
                    {
                        var value = property.Value;
                        if (string.IsNullOrWhiteSpace(value) && propertyType is IListPropertyType listPropertyType)
                        {
                            value = listPropertyType.Values.FirstOrDefault()?.Id;
                        }

                        var containerProperty = container.GetProperty(propertyType);
                        if (containerProperty == null)
                        {
                            container.AddProperty(propertyType, value);
                        }
                        else
                        {
                            containerProperty.StringValue = value;
                        }
                    }
                }
            }
        }
Example #6
0
        private static void AddProperties([NotNull] IPropertySchema schema,
                                          [NotNull] IPropertiesContainer container, [NotNull] IEnumerable <Property> properties)
        {
            foreach (var property in properties)
            {
                if (!string.IsNullOrWhiteSpace(property.Name))
                {
                    var propertyType = schema.GetPropertyType(property.Name);
                    if (propertyType == null)
                    {
                        switch (property.Type)
                        {
                        case PropertyType.String:
                            propertyType = schema.AddPropertyType(property.Name,
                                                                  PropertyValueType.String);
                            break;

                        case PropertyType.Boolean:
                            propertyType = schema.AddPropertyType(property.Name,
                                                                  PropertyValueType.Boolean);
                            break;

                        case PropertyType.List:
                            propertyType =
                                schema.AddPropertyType(property.Name, PropertyValueType.List);
                            if (propertyType is IListPropertyType listPropertyType)
                            {
                                listPropertyType.Context = property.Values.TagConcat();
                                listPropertyType.SetListProvider(new ListProviderExtension());
                            }

                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }

                    var current = container.GetProperty(propertyType) ??
                                  container.AddProperty(propertyType, property.Value);
                }
            }
        }