private void ApplySchema <T>([NotNull] IPropertySchema schema) where T : IEntity { var list = _entities?.Where(x => x is T).ToArray(); if (list?.Any() ?? false) { foreach (var current in list) { Apply(schema, current); } } IEnumerable <IEntityTemplate> templates = null; if (typeof(T) == typeof(IExternalInteractor)) { templates = _entityTemplates?.Where(x => x.EntityType == EntityType.ExternalInteractor).ToArray(); } else if (typeof(T) == typeof(IProcess)) { templates = _entityTemplates?.Where(x => x.EntityType == EntityType.Process).ToArray(); } else if (typeof(T) == typeof(IDataStore)) { templates = _entityTemplates?.Where(x => x.EntityType == EntityType.DataStore).ToArray(); } if (templates?.Any() ?? false) { foreach (var current in templates) { Apply(schema, current); } } }
private void RemoveRelatedForEntities([NotNull] IPropertySchema propertySchema) { var entities = _entities?.ToArray(); if (entities?.Any() ?? false) { foreach (var entity in entities) { RemoveRelated(propertySchema, entity); var events = entity.ThreatEvents?.ToArray(); if (events?.Any() ?? false) { foreach (var threatEvent in events) { RemoveRelated(propertySchema, threatEvent); var threatEventScenarios = threatEvent.Scenarios?.ToArray(); if (threatEventScenarios?.Any() ?? false) { foreach (var scenario in threatEventScenarios) { RemoveRelated(propertySchema, scenario); } } } } } } }
private static void InitializeBaseSchema([NotNull] IItemTemplate template, [NotNull] IPropertySchema baseSchema) { var propertyTypes = baseSchema.PropertyTypes?.ToArray(); if (propertyTypes?.Any() ?? false) { foreach (var propertyType in propertyTypes) { string value = null; if (propertyType is IListPropertyType listPropertyType) { value = listPropertyType.Values.FirstOrDefault()?.Id; } var containerProperty = template.GetProperty(propertyType); if (containerProperty == null) { template.AddProperty(propertyType, value); } else { containerProperty.StringValue = value; } } } }
public void Apply(IPropertySchema schema) { if (Instance is IPropertiesContainer container && schema.AppliesTo.HasFlag(container.PropertiesScope)) { 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); } } } }
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; } } } } }
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); } } } } } }
public StringPropertyType([Required] string name, [NotNull] IPropertySchema schema) : this() { _id = Guid.NewGuid(); _schemaId = schema.Id; Name = name; Visible = true; }
private void RemoveRelatedForDataFlows([NotNull] IPropertySchema propertySchema) { var dataFlows = _dataFlows?.ToArray(); if (dataFlows?.Any() ?? false) { foreach (var dataFlow in dataFlows) { RemoveRelated(propertySchema, dataFlow); var events = dataFlow.ThreatEvents?.ToArray(); if (events?.Any() ?? false) { foreach (var threatEvent in events) { RemoveRelated(propertySchema, threatEvent); var threatEventScenarios = threatEvent.Scenarios?.ToArray(); if (threatEventScenarios?.Any() ?? false) { foreach (var scenario in threatEventScenarios) { RemoveRelated(propertySchema, scenario); } } } } } } }
public void Add([NotNull] IPropertySchema propertySchema) { if (_schemas == null) { _schemas = new List <IPropertySchema>(); } _schemas.Add(propertySchema); }
public IdentityReferencePropertyType([Required] string name, [NotNull] IPropertySchema schema) : this() { _id = Guid.NewGuid(); _schemaId = schema.Id; _model = schema.Model; _modelId = schema.Model?.Id ?? Guid.Empty; Name = name; Visible = true; }
public JsonSerializableObjectPropertyType([Required] string name, [NotNull] IPropertySchema schema) : this() { _id = Guid.NewGuid(); _schemaId = schema.Id; _model = schema.Model; _modelId = schema.Model?.Id ?? Guid.Empty; Name = name; Visible = true; }
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; } } } } }
/// <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; } } } }
private void OnPropertyTypeAdded(IPropertySchema schema, IPropertyType propertyType) { var model = GetModel(); if (model != null && schema != null && propertyType != null && !HasProperty(propertyType)) { if (_properties?.Any(x => x.PropertyType != null && x.PropertyType.SchemaId == schema.Id) ?? false) { InternalAddProperty(model, propertyType, null); } } }
private void RemoveRelated([NotNull] IPropertySchema schema, IEnumerable <IPropertiesContainer> containers) { var array = containers?.ToArray(); if (array?.Any() ?? false) { foreach (var item in array) { RemoveRelated(schema, item); } } }
private void RemoveRelatedForMitigations([NotNull] IPropertySchema propertySchema) { var mitigations = _mitigations?.ToArray(); if (mitigations?.Any() ?? false) { foreach (var mitigation in mitigations) { RemoveRelated(propertySchema, mitigation); } } }
private void RemoveRelated([NotNull] IPropertySchema propertySchema) { RemoveRelatedForEntities(propertySchema); RemoveRelatedForDataFlows(propertySchema); RemoveRelated(propertySchema, _diagrams); RemoveRelated(propertySchema, _groups); RemoveRelated(propertySchema, this); RemoveRelated(propertySchema, _severities); RemoveRelatedForMitigations(propertySchema); RemoveRelated(propertySchema, _actors); RemoveRelated(propertySchema, _threatTypes); }
private void RemoveRelated([NotNull] IPropertySchema schema, IPropertiesContainer container) { if (container != null) { var properties = container.Properties?.Where(x => (x.PropertyType?.SchemaId ?? Guid.Empty) == schema.Id).ToArray(); if (properties?.Any() ?? false) { foreach (var property in properties) { container.RemoveProperty(property.PropertyType); } } } }
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); } } } }
public static void AddPropertyRule(this IRuleEditor ruleEditor, [NotNull] IPropertyType propertyType, [NotNull] IPropertySchema schema, Scope scope = Scope.Object) { ButtonItem item = new ButtonItem() { ButtonStyle = eButtonStyle.ImageAndText, Image = GetImage(scope), ImagePosition = eImagePosition.Left, Text = propertyType.Name, Tooltip = $"Name = {propertyType.Name}\nSchema = {schema.Name}\nNamespace = {schema.Namespace}", Tag = new PropertyTypeItemContext(propertyType, scope) }; ruleEditor.AddButton(item, scope); }
public IPropertySchema GetPropertySchema() { IPropertySchema result = _model.GetSchema(Properties.Resources.DevOpsPropertySchema, Properties.Resources.DefaultNamespace) ?? _model.AddSchema(Properties.Resources.DevOpsPropertySchema, Properties.Resources.DefaultNamespace); result.Description = Properties.Resources.DevOpsPropertySchemaDescription; result.Visible = false; result.AppliesTo = Scope.Mitigation; result.System = true; result.AutoApply = false; result.NotExportable = true; return(result); }
private bool RemoveSchema([NotNull] IPropertySchema schema, bool force) { bool result = false; if (force || !IsUsed(schema)) { RemoveRelated(schema); result = _schemas.Remove(schema); if (result) { UnregisterEvents(schema); SetDirty(); ChildRemoved?.Invoke(schema); } } return(result); }
public IPropertySchema AddSchema([Required] string name, [Required] string nspace) { IPropertySchema result = null; if (GetSchema(name, nspace) == null) { if (_schemas == null) { _schemas = new List <IPropertySchema>(); } result = new PropertySchema(this, name, nspace); _schemas.Add(result); SetDirty(); RegisterEvents(result); ChildCreated?.Invoke(result); } return(result); }
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); } } }
private bool IsUsed([NotNull] IPropertySchema propertySchema) { return((_entities?.Any(x => x.Properties?.Any(y => (y.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) || (_entities?.Any(x => x.ThreatEvents?.Any(y => y.Properties? .Any(z => (z.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) ?? false) || (_entities?.Any(x => x.ThreatEvents?.Any(y => y.Scenarios?.Any(z => z.Properties? .Any(t => (t.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) ?? false) ?? false) || (_dataFlows?.Any(x => x.Properties?.Any(y => (y.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) || (_dataFlows?.Any(x => x.ThreatEvents?.Any(y => y.Properties? .Any(z => (z.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) ?? false) || (_dataFlows?.Any(x => x.ThreatEvents?.Any(y => y.Scenarios?.Any(z => z.Properties? .Any(t => (t.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) ?? false) ?? false) || (_diagrams?.Any(x => x.Properties?.Any(y => (y.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) || (_groups?.Any(x => x.Properties?.Any(y => (y.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) || (Properties?.Any(y => (y.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) || (_severities?.Any(x => x.Properties?.Any(y => (y.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) || (_mitigations?.Any(x => x.Properties?.Any(y => (y.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) || (_actors?.Any(x => x.Properties?.Any(y => (y.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false) || (_threatTypes?.Any(x => x.Properties?.Any(y => (y.PropertyType?.SchemaId ?? Guid.Empty) == propertySchema.Id) ?? false) ?? false)); }
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); } } }
private void OnPropertyTypeRemoved(IPropertySchema schema, IPropertyType propertyType) { RemoveProperty(propertyType); }
public void Apply(IPropertySchema schema) { }
public ListMultiPropertyType([Required] string name, [NotNull] IPropertySchema schema) : base(name, schema) { }
public PropertyTypeCreationDialog([NotNull] IPropertySchema schema) : this() { _schema = schema; }