コード例 #1
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;
                        }
                    }
                }
            }
        }
コード例 #2
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);
                            }
                        }
                    }
                }
            }
        }
コード例 #3
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);
                }
            }
        }
コード例 #4
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;
                }
            }
        }
コード例 #5
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);
        }
コード例 #6
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;
                        }
                    }
                }
            }
        }
コード例 #7
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;
                }
            }
        }
コード例 #8
0
        private void ApplyProperties([NotNull] IPropertiesContainer source, [NotNull] IPropertiesContainer target)
        {
            var properties = source.Properties?.ToArray();

            if (properties?.Any() ?? false)
            {
                foreach (var property in properties)
                {
                    if (target.Properties?.All(x => x.PropertyTypeId != property.PropertyTypeId) ?? true)
                    {
                        target.AddProperty(property.PropertyType, property.StringValue);
                    }
                }
            }
        }
コード例 #9
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;
            }
        }
コード例 #10
0
        public bool EnableAnnotations([NotNull] IPropertiesContainer container)
        {
            bool result = false;

            var propertyType = GetAnnotationsPropertyType();

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

            return(result);
        }
コード例 #11
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;
                }
            }
        }
コード例 #12
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);
                }
            }
        }
コード例 #13
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);
        }
コード例 #14
0
        private void SetInfo([NotNull] IPropertiesContainer container, DevOpsConnectionInfo connectionInfo)
        {
            var info = GetInfo(container);

            if (info == null)
            {
                var propertyType = GetDevOpsInfoPropertyType();
                if (propertyType != null)
                {
                    var property = (container.GetProperty(propertyType) as IPropertyJsonSerializableObject) ??
                                   container.AddProperty(propertyType, null) as IPropertyJsonSerializableObject;
                    if (property != null)
                    {
                        info       = new DevOpsInfo();
                        info.Infos = new List <DevOpsConnectionInfo>();
                        info.Infos.Add(connectionInfo);
                        property.Value = info;
                    }
                }
            }
            else
            {
                var existing = info.Infos?
                               .FirstOrDefault(x => string.CompareOrdinal(x.ConnectorId, connectionInfo.ConnectorId) == 0 &&
                                               string.CompareOrdinal(x.Url?.ToLower(), connectionInfo.Url?.ToLower()) == 0 &&
                                               string.CompareOrdinal(x.Project, connectionInfo.Project) == 0);
                if (existing != null)
                {
                    info.Infos.Remove(existing);
                }
                if (info.Infos == null)
                {
                    info.Infos = new List <DevOpsConnectionInfo>();
                }
                info.Infos.Add(connectionInfo);
            }
        }
コード例 #15
0
        public void SetFalsePositive([NotNull] IPropertiesContainer container,
                                     [Required] string analyzerId,
                                     [Required] string reason)
        {
            var propertyType = GetFalsePositivePropertyType();

            if (propertyType != null)
            {
                var property = container.GetProperty(propertyType) as IPropertyJsonSerializableObject;
                if (property == null)
                {
                    var list = new FalsePositiveList
                    {
                        FalsePositives = new List <FalsePositiveInfo> {
                            CreateInfo(analyzerId, reason)
                        }
                    };
                    property = container.AddProperty(propertyType, null) as IPropertyJsonSerializableObject;
                    if (property != null)
                    {
                        property.Value = list;
                    }
                }
                else
                {
                    if (property.Value is FalsePositiveList list &&
                        !(list.FalsePositives?.Any(x => string.CompareOrdinal(x.QualityInitializerId, analyzerId) == 0) ?? false))
                    {
                        if (list.FalsePositives == null)
                        {
                            list.FalsePositives = new List <FalsePositiveInfo>();
                        }
                        list.FalsePositives.Add(CreateInfo(analyzerId, reason));
                    }
                }
            }
        }
コード例 #16
0
        /// <summary>
        /// Clone Properties between Containers.
        /// </summary>
        /// <param name="source">Source Container.</param>
        /// <param name="target">Target Container.</param>
        public static void CloneProperties(this IPropertiesContainer source,
                                           [NotNull] IPropertiesContainer target)
        {
            var properties = source?.Properties?.ToArray();

            if ((properties?.Any() ?? false) &&
                target is IThreatModelChild child &&
                child.Model is IThreatModel model &&
                properties.FirstOrDefault()?.Model is IThreatModel sourceModel)
            {
                foreach (var property in properties)
                {
                    if (property.PropertyType is IPropertyType sourcePropertyType &&
                        sourceModel.GetSchema(sourcePropertyType.SchemaId) is IPropertySchema sourceSchema)
                    {
                        if (model.GetSchema(sourceSchema.Name, sourceSchema.Namespace) is IPropertySchema
                            targetSchema)
                        {
                            var propertyType = targetSchema.GetPropertyType(sourcePropertyType.Name);
                            if (propertyType != null)
                            {
                                var propertyTarget = target.GetProperty(propertyType);
                                if (propertyTarget == null)
                                {
                                    target.AddProperty(propertyType, property.StringValue);
                                }
                                else
                                {
                                    propertyTarget.StringValue = property.StringValue;
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #17
0
        private void Apply([NotNull] IPropertySchema schema, [NotNull] IPropertiesContainer container)
        {
            var existingProp = container.Properties?.ToArray();
            var schemaProp   = schema.PropertyTypes?.ToArray();
            var missing      = existingProp == null ? schemaProp : schemaProp?.Except(existingProp.Select(x => x.PropertyType)).ToArray();
            var inExcess     = existingProp?.Where(x => x.PropertyType != null &&
                                                   x.PropertyType.SchemaId == schema.Id && !(schemaProp?.Any(y => y.Id == x.PropertyTypeId) ?? false)).Select(x => x.PropertyType).ToArray();

            if (missing?.Any() ?? false)
            {
                foreach (var item in missing)
                {
                    container.AddProperty(item, null);
                }
            }

            if (inExcess?.Any() ?? false)
            {
                foreach (var item in inExcess)
                {
                    container.RemoveProperty(item);
                }
            }
        }