コード例 #1
0
        public void SetProperty([NotNull] IPropertiesContainer container, [Required] string propertyName,
                                [NotNull] IEnumerable <string> values, [Required] string value)
        {
            var schema = GetSchema();

            if (schema != null)
            {
                var propertyType = schema.GetPropertyType(propertyName);

                if (propertyType == null)
                {
                    propertyType = schema.AddPropertyType(propertyName, PropertyValueType.List);
                    if (propertyType is IListPropertyType listPropertyType)
                    {
                        listPropertyType.SetListProvider(new ListProvider());
                        listPropertyType.Context = values?.TagConcat();
                    }
                }

                var property = container.GetProperty(propertyType);

                if (property == null)
                {
                    container.AddProperty(propertyType, value);
                }
                else
                {
                    property.StringValue = value;
                }
            }
        }
コード例 #2
0
        public void AddAnnotation([NotNull] IPropertiesContainer container, [NotNull] Annotation annotation)
        {
            var propertyType = GetAnnotationsPropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType);
                if (property == null)
                {
                    property = container.AddProperty(propertyType, null);
                    if (property is IPropertyJsonSerializableObject jsonSerializableObject)
                    {
                        var annotations = new Annotations.Annotations();
                        annotations.Add(annotation);
                        jsonSerializableObject.Value = annotations;
                    }
                }
                else if (property is IPropertyJsonSerializableObject jsonSerializableObject)
                {
                    if (!(jsonSerializableObject.Value is Annotations.Annotations annotations))
                    {
                        annotations = new Annotations.Annotations();
                        jsonSerializableObject.Value = annotations;
                    }
                    annotations.Add(annotation);
                }
            }
        }
コード例 #3
0
        private bool ApplyGenRule([NotNull] IPropertiesContainer container, string value)
        {
            var result = false;

            if (container is IThreatModelChild child)
            {
                var propertyType = new AutoGenRulesPropertySchemaManager(child.Model).GetPropertyType();
                if (propertyType != null)
                {
                    var property = container.GetProperty(propertyType);
                    if (property != null)
                    {
                        property.StringValue = value;
                    }
                    else
                    {
                        container.AddProperty(propertyType, value);
                    }

                    result = true;
                }
            }

            return(result);
        }
コード例 #4
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;
                        }
                    }
                }
            }
        }
コード例 #5
0
        private void AddItem([NotNull] IQualityAnalyzer analyzer,
                             [NotNull] IPropertiesContainer container,
                             [NotNull] IPropertyType propertyType)
        {
            var property = container.GetProperty(propertyType) as IPropertyJsonSerializableObject;

            if (property != null)
            {
                var list    = property.Value as FalsePositiveList;
                var finding = list?.FalsePositives.FirstOrDefault(x =>
                                                                  string.CompareOrdinal(x.QualityInitializerId, analyzer.GetExtensionId()) == 0);

                if (finding != null)
                {
                    _falsePositives.PrimaryGrid.Rows.Add(new GridRow(container.ToString(), finding.Reason, finding.Author, finding.Timestamp)
                    {
                        Tag = container
                    });
                }
            }

            if (container is IThreatEventsContainer threatEventsContainer)
            {
                var threatEvents = threatEventsContainer.ThreatEvents?.ToArray();
                if (threatEvents?.Any() ?? false)
                {
                    foreach (var te in threatEvents)
                    {
                        AddItem(analyzer, te, propertyType);
                    }
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Clone Properties between Containers.
        /// </summary>
        /// <param name="target">Target Container.</param>
        /// <param name="source">Source Container.</param>
        public static void MergeProperties(this IPropertiesContainer target,
                                           [NotNull] IPropertiesContainer source)
        {
            var properties = source?.Properties?.ToArray();

            if ((properties?.Any() ?? false) &&
                source is IThreatModelChild sourceChild &&
                sourceChild.Model is IThreatModel sourceModel &&
                target is IThreatModelChild targetChild &&
                targetChild.Model is IThreatModel targetModel)
            {
                foreach (var property in properties)
                {
                    var sourcePropertyType = property.PropertyType;
                    if (sourcePropertyType != null)
                    {
                        var sourceSchema = sourceModel.GetSchema(sourcePropertyType.SchemaId);
                        if (sourceSchema != null)
                        {
                            var targetSchema = targetModel.GetSchema(sourceSchema.Name, sourceSchema.Namespace) ??
                                               sourceSchema.Clone(targetModel);

                            var targetPropertyType = targetSchema.GetPropertyType(sourcePropertyType.Name) ??
                                                     sourcePropertyType.Clone(targetSchema);

                            var targetProperty = target.GetProperty(targetPropertyType);
                            if (targetProperty == null)
                            {
                                target.AddProperty(targetPropertyType, property.StringValue);
                            }
                        }
                    }
                }
            }
        }
コード例 #7
0
        public string GetInstanceId([NotNull] IPropertiesContainer container)
        {
            var schema       = GetSchema();
            var propertyType = schema.GetPropertyType(ThreatModelInstanceId);
            var property     = container.GetProperty(propertyType);

            return(property?.StringValue);
        }
コード例 #8
0
 public static void OvertakeAllProperties(
     this IPropertiesContainer targetContainer,
     IPropertiesContainer sourceContainer)
 {
     foreach (var actKey in sourceContainer.Properties.Keys)
     {
         targetContainer.SetProperty(actKey, sourceContainer.GetProperty(actKey));
     }
 }
コード例 #9
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;
                        }
                    }
                }
            }
        }
コード例 #10
0
        private DevOpsInfo GetInfo([NotNull] IPropertiesContainer container)
        {
            DevOpsInfo result = null;

            var propertyType = GetDevOpsInfoPropertyType();

            if (propertyType != null)
            {
                result = (container.GetProperty(propertyType) as IPropertyJsonSerializableObject)?.Value as DevOpsInfo;
            }

            return(result);
        }
コード例 #11
0
        public void SetTop([NotNull] IPropertiesContainer container, bool top)
        {
            var propertyType = GetTopPropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType) ?? container.AddProperty(propertyType, null);
                if (property is IPropertyBool boolProperty)
                {
                    boolProperty.Value = top;
                }
            }
        }
コード例 #12
0
        public static SelectionRule GetRule(this IPropertiesContainer container, IThreatModel model = null)
        {
            SelectionRule result = null;

            if (model == null && container is IThreatModelChild child)
            {
                model = child.Model;
            }

            if (model != null)
            {
                var schemaManager = new AutoGenRulesPropertySchemaManager(model);
                var propertyType  = schemaManager.GetPropertyType();
                if (container.HasProperty(propertyType))
                {
                    if (container.GetProperty(propertyType) is IPropertyJsonSerializableObject jsonSerializableObject)
                    {
                        result = jsonSerializableObject.Value as SelectionRule;
                    }
                }
                else
                {
                    var oldSchema       = model.GetSchema(OldSchemaName, Properties.Resources.DefaultNamespace);
                    var oldPropertyType = oldSchema?.GetPropertyType(OldPropertyName);
                    if (oldPropertyType != null)
                    {
                        if (container.GetProperty(oldPropertyType) is IPropertyJsonSerializableObject
                            jsonSerializableObject)
                        {
                            result = jsonSerializableObject.Value as SelectionRule;
                            container.AddProperty(propertyType, jsonSerializableObject.StringValue);
                            container.RemoveProperty(oldPropertyType);
                        }
                    }
                }
            }

            return(result);
        }
コード例 #13
0
        public bool AreAnnotationsEnabled([NotNull] IPropertiesContainer container)
        {
            bool result = false;

            var propertyType = GetAnnotationsPropertyType();

            if (propertyType != null)
            {
                result = container.GetProperty(propertyType) != null;
            }

            return(result);
        }
コード例 #14
0
        public string GetProperty([NotNull] IPropertiesContainer container, [Required] string propertyName)
        {
            string result = null;

            var schema       = _model.GetSchema(_schemaName, Resources.DefaultNamespace);
            var propertyType = schema?.GetPropertyType(propertyName);

            if (propertyType != null)
            {
                result = container.GetProperty(propertyType)?.StringValue;
            }

            return(result);
        }
コード例 #15
0
        public void RemoveAnnotations([NotNull] IPropertiesContainer container)
        {
            var propertyType = GetAnnotationsPropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType);
                if (property is IPropertyJsonSerializableObject jsonSerializableObject &&
                    jsonSerializableObject.Value is Annotations.Annotations annotations)
                {
                    annotations.Clear();
                }
            }
        }
コード例 #16
0
        public bool HasReviewNotes([NotNull] IPropertiesContainer container)
        {
            bool result = false;

            var propertyType = GetAnnotationsPropertyType();

            if (container.GetProperty(propertyType) is IPropertyJsonSerializableObject jsonSerializableObject &&
                jsonSerializableObject.Value is Annotations.Annotations annotations)
            {
                result = annotations.Items.OfType <ReviewNote>().Any();
            }

            return(result);
        }
コード例 #17
0
        public bool HasClosedTopics([NotNull] IPropertiesContainer container)
        {
            bool result = false;

            var propertyType = GetAnnotationsPropertyType();

            if (container.GetProperty(propertyType) is IPropertyJsonSerializableObject jsonSerializableObject &&
                jsonSerializableObject.Value is Annotations.Annotations annotations)
            {
                result = annotations.Items.OfType <TopicToBeClarified>().Any(x => x.Answered);
            }

            return(result);
        }
コード例 #18
0
        public bool HasNotes([NotNull] IPropertiesContainer container)
        {
            bool result = false;

            var propertyType = GetAnnotationsPropertyType();

            if (container.GetProperty(propertyType) is IPropertyJsonSerializableObject jsonSerializableObject &&
                jsonSerializableObject.Value is Annotations.Annotations annotations)
            {
                result = annotations.Items.Any(x => !(x is Highlight) && !(x is TopicToBeClarified) && !(x is ReviewNote) && !(x is AnnotationAnswer));
            }

            return(result);
        }
コード例 #19
0
        public void SetInstanceId([NotNull] IPropertiesContainer container, [Required] string value)
        {
            var schema       = GetSchema();
            var propertyType = schema.GetPropertyType(ThreatModelInstanceId);
            var property     = container.GetProperty(propertyType);

            if (property == null)
            {
                container.AddProperty(propertyType, value);
            }
            else
            {
                property.StringValue = value;
            }
        }
コード例 #20
0
        public bool IsTop([NotNull] IPropertiesContainer container)
        {
            bool result = false;

            var propertyType = GetTopPropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType);
                if (property is IPropertyBool boolProperty)
                {
                    result = boolProperty.Value;
                }
            }

            return(result);
        }
コード例 #21
0
        public bool DisableAnnotations([NotNull] IPropertiesContainer container)
        {
            bool result = false;

            var propertyType = GetAnnotationsPropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType);
                if (property != null)
                {
                    result = container.RemoveProperty(propertyType);
                }
            }

            return(result);
        }
コード例 #22
0
        private bool Execute([NotNull] IPropertiesContainer container)
        {
            var result = false;

            if (container is IThreatModelChild child)
            {
                var propertyType = new AutoGenRulesPropertySchemaManager(child.Model).GetPropertyType();
                if (propertyType != null)
                {
                    DataObject dataObject = new DataObject();
                    dataObject.SetData("AutoGenRule", container.GetProperty(propertyType)?.StringValue ?? string.Empty);
                    Clipboard.SetDataObject(dataObject);
                    result = true;
                }
            }

            return(result);
        }
コード例 #23
0
        public static void SetRule(this IPropertiesContainer container, SelectionRule rule, IThreatModel model = null)
        {
            if (model == null && container is IThreatModelChild child)
            {
                model = child.Model;
            }

            if (model != null)
            {
                var schemaManager = new AutoGenRulesPropertySchemaManager(model);
                var propertyType  = schemaManager.GetPropertyType();
                var property      = container.GetProperty(propertyType) ?? container.AddProperty(propertyType, null);
                if (property is IPropertyJsonSerializableObject jsonSerializableObject)
                {
                    jsonSerializableObject.Value = rule;
                }
            }
        }
コード例 #24
0
        public IEnumerable <Annotation> GetAnnotations([NotNull] IPropertiesContainer container)
        {
            IEnumerable <Annotation> result = null;

            var propertyType = GetAnnotationsPropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType);
                if (property is IPropertyJsonSerializableObject jsonSerializableObject &&
                    jsonSerializableObject.Value is Annotations.Annotations annotations)
                {
                    result = annotations.Items;
                }
            }

            return(result);
        }
コード例 #25
0
        public void ResetFalsePositive([NotNull] IPropertiesContainer container,
                                       [NotNull] IQualityAnalyzer analyzer)
        {
            var propertyType = GetFalsePositivePropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType) as IPropertyJsonSerializableObject;
                if (property?.Value is FalsePositiveList list)
                {
                    var item = list.FalsePositives?.FirstOrDefault(x =>
                                                                   string.CompareOrdinal(x.QualityInitializerId, analyzer.GetExtensionId()) == 0);
                    if (item != null)
                    {
                        list.FalsePositives.Remove(item);
                    }
                }
            }
        }
コード例 #26
0
        public string GetReason([NotNull] IPropertiesContainer container,
                                [NotNull] IQualityAnalyzer analyzer)
        {
            string result = null;

            var propertyType = GetFalsePositivePropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType) as IPropertyJsonSerializableObject;
                if (property?.Value is FalsePositiveList list)
                {
                    result = list.FalsePositives?.FirstOrDefault(x =>
                                                                 string.CompareOrdinal(x.QualityInitializerId, analyzer.GetExtensionId()) == 0)?.Reason;
                }
            }

            return(result);
        }
コード例 #27
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);
                }
            }
        }
コード例 #28
0
        public bool IsFalsePositive([NotNull] IPropertiesContainer container,
                                    [NotNull] IQualityAnalyzer qualityAnalyzer)
        {
            bool result = false;

            var propertyType = GetFalsePositivePropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType) as IPropertyJsonSerializableObject;
                if (property?.Value is FalsePositiveList list)
                {
                    result = list.FalsePositives?
                             .Any(x =>
                                  string.CompareOrdinal(x.QualityInitializerId, qualityAnalyzer.GetExtensionId()) == 0) ?? false;
                }
            }

            return(result);
        }
コード例 #29
0
        private bool HasFalsePositives([NotNull] IQualityAnalyzer analyzer,
                                       [NotNull] IPropertiesContainer container,
                                       [NotNull] IPropertyType propertyType)
        {
            bool result = false;

            var property = container.GetProperty(propertyType) as IPropertyJsonSerializableObject;

            if (property != null)
            {
                var list    = property.Value as FalsePositiveList;
                var finding = list?.FalsePositives.FirstOrDefault(x =>
                                                                  string.CompareOrdinal(x.QualityInitializerId, analyzer.GetExtensionId()) == 0);

                if (finding != null)
                {
                    result = true;
                }
            }

            if (!result && container is IThreatEventsContainer threatEventsContainer)
            {
                var threatEvents = threatEventsContainer.ThreatEvents?.ToArray();
                if (threatEvents?.Any() ?? false)
                {
                    foreach (var te in threatEvents)
                    {
                        if (HasFalsePositives(analyzer, te, propertyType))
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }

            return(result);
        }
コード例 #30
0
        public static SelectionRule GetRule(this IPropertiesContainer container)
        {
            SelectionRule result = null;

            if (container is IThreatModelChild child && child.Model is IThreatModel model)
            {
                var schemaManager = new AutoThreatGenPropertySchemaManager(model);
                var propertyType  = schemaManager?.GetPropertyType();
                if (propertyType != null)
                {
                    var p = container.GetProperty(propertyType);
                    if (p is IPropertyJsonSerializableObject jsonProperty)
                    {
                        if (jsonProperty.Value is SelectionRule rule)
                        {
                            result = rule;
                        }
                    }
                }
            }

            return(result);
        }