Ejemplo n.º 1
0
        /// <summary>
        /// Save the Threat Model passed as argument as Template.
        /// </summary>
        /// <param name="model">Threat Model to be saved as Template.</param>
        /// <param name="definition">Definition of the information to export.</param>
        /// <param name="name">Name of the Template.</param>
        /// <param name="description">Description of the Template.</param>
        /// <param name="path">Path to the Template to be created.</param>
        public static void SaveTemplate(this IThreatModel model, [NotNull] DuplicationDefinition definition,
                                        [Required] string name, string description, [Required] string path)
        {
            var newModel = model.Duplicate(name, definition);

            newModel.Description = description;
            var serialization = ThreatModelManager.Serialize(newModel);

            var extension = Path.GetExtension(path)?.ToLower();

            switch (extension)
            {
            case ".tmt":
                var package = Package.Create(path);
                package.Add(Resources.ThreatModelTemplateFile, serialization);
                package.Save();
                break;

            case ".tmk":
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                using (var file = File.OpenWrite(path))
                {
                    using (var writer = new BinaryWriter(file))
                    {
                        writer.Write(serialization);
                    }
                }
                break;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Open an existing Template.
        /// </summary>
        /// <param name="path">Location of the Template, in the file system.</param>
        /// <returns>The Threat Model representing the Template.</returns>
        public static IThreatModel OpenTemplate([Required] string path)
        {
            var package = new Package(path);
            var bytes   = package.Read(Resources.ThreatModelTemplateFile);

            return(ThreatModelManager.Deserialize(bytes, true));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Save the Threat Model passed as argument as Template.
        /// </summary>
        /// <param name="model">Threat Model to be saved as Template.</param>
        /// <param name="definition">Definition of the information to export.</param>
        /// <param name="name">Name of the Template.</param>
        /// <param name="description">Description of the Template.</param>
        /// <param name="path">Path to the Template to be created.</param>
        public static void SaveTemplate(this IThreatModel model, [NotNull] DuplicationDefinition definition,
                                        [Required] string name, string description, [Required] string path)
        {
            var newModel = model.Duplicate(name, definition);

            newModel.Description = description;
            var serialization = ThreatModelManager.Serialize(newModel);

            var package = Package.Create(path);

            package.Add(Resources.ThreatModelTemplateFile, serialization);
            package.Save();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Open an existing Template.
        /// </summary>
        /// <param name="path">Location of the Template, in the file system.</param>
        /// <returns>The Threat Model representing the Template.</returns>
        public static IThreatModel OpenTemplate([Required] string path)
        {
            IThreatModel result = null;

            byte[] bytes;
            var    extension = Path.GetExtension(path)?.ToLower();

            switch (extension)
            {
            case ".tmt":
                var package = new Package(path);
                bytes = package.Read(Resources.ThreatModelTemplateFile);
                break;

            case ".tmk":
                using (var file = File.OpenRead(path))
                {
                    using (var ms = new MemoryStream())
                    {
                        file.CopyTo(ms);
                        bytes = ms.ToArray();
                    }
                }
                break;

            default:
                bytes = null;
                break;
            }

            if (bytes != null)
            {
                result = ThreatModelManager.Deserialize(bytes, true);
            }

            return(result);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Close a Template that has been opened.
 /// </summary>
 /// <param name="id">Identifier of the Template to be closed.</param>
 public static void CloseTemplate(Guid id)
 {
     ThreatModelManager.Remove(id);
 }