예제 #1
0
        public bool Save([NotNull] IThreatModel model, LocationType locationType, [Required] string location,
                         bool autoAddDateTime, IEnumerable <IExtensionMetadata> extensions, out string newLocation)
        {
            bool result = false;

            newLocation = null;

            if (model is IThreatModel tm)
            {
                var tmSerialized = ThreatModelManager.Serialize(tm);

                newLocation = autoAddDateTime
                    ? Path.Combine(Path.GetDirectoryName(location),
                                   $"{StripDateTimeSuffix(Path.GetFileNameWithoutExtension(location))}_{DateTime.Now.ToString("yyyyMMddHHmmss")}{Path.GetExtension(location)}")
                    : location;

                File.WriteAllBytes(newLocation, tmSerialized);

                if (autoAddDateTime)
                {
                    File.Copy(newLocation, Path.Combine(Path.GetDirectoryName(location), $"{StripDateTimeSuffix(Path.GetFileNameWithoutExtension(location))}{Path.GetExtension(location)}"), true);
                }

                result = true;
            }

            return(result);
        }
        public bool Save([NotNull] IThreatModel model, LocationType locationType, [Required] string location,
                         bool autoAddDateTime, IEnumerable <IExtensionMetadata> extensions, out string newLocation)
        {
            bool result = false;

            newLocation = null;

            if (model is IThreatModel tm)
            {
                var tmSerialized = ThreatModelManager.Serialize(tm);
                var extList      = new ExtensionsList
                {
                    Extensions =
                        new List <ExtensionInfo>(extensions.Where(x => x != null).Select(x => new ExtensionInfo(x.Id, x.Label)))
                };
                var extSerialized = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(extList, new JsonSerializerSettings()
                {
                    TypeNameHandling = TypeNameHandling.None,
                }));

                newLocation = autoAddDateTime
                    ? Path.Combine(Path.GetDirectoryName(location),
                                   $"{StripDateTimeSuffix(Path.GetFileNameWithoutExtension(location))}_{DateTime.Now.ToString("yyyyMMddHHmmss")}{Path.GetExtension(location)}")
                    : location;

                var package = Package.Create(newLocation);
                package.Add(ThreatModelFile, tmSerialized);
                package.Add(ExtensionsFile, extSerialized);
                package.Save();

                result = true;
            }

            return(result);
        }
예제 #3
0
        public bool Save(IThreatModel model, LocationType locationType, string location, SecureString password)
        {
            bool result = false;

            if (model is IThreatModel tm)
            {
                var tmSerialized = ThreatModelManager.Serialize(tm);

                var package = Package.Create(location);
                package.Add(ThreatModelFile, tmSerialized, password);
                package.Save();

                result = true;
            }

            return(result);
        }
예제 #4
0
        static IThreatModel ConvertModel(ModelLoader loader, string fileName)
        {
            Console.Write($"--- Converting file {Path.GetFileName(fileName)}");

            var model = loader.ConvertModel(fileName);

            if (model != null)
            {
                var tmSerialized = ThreatModelManager.Serialize(model);
                var dest         = Path.Combine(Path.GetDirectoryName(fileName), $"{Path.GetFileNameWithoutExtension(fileName)}.tm");
                var package      = Package.Create(dest);
                package.Add("threatmodel.json", tmSerialized);
                package.Save();
            }

            Console.WriteLine(" - done.");

            return(model);
        }