public override Run VisitRun(Run node)
        {
            if (node != null)
            {
                bool scrapeFileReferences = _loggingOptions.Includes(LoggingOptions.ComputeFileHashes) ||
                                            _loggingOptions.Includes(LoggingOptions.PersistFileContents);

                if (scrapeFileReferences)
                {
                    var visitor = new AddFileReferencesVisitor();
                    visitor.VisitRun(node);
                }

                if (node.Files != null)
                {
                    var keys = node.Files.Keys.ToArray();
                    foreach (var key in keys)
                    {
                        var value = node.Files[key];
                        if (value != null)
                        {
                            node.Files[key] = VisitDictionaryValueNullChecked(key, value);
                        }
                    }
                }
            }

            return(node);
        }
示例#2
0
        /// <summary>Converts a tool log file into the SARIF format.</summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <exception cref="ArgumentException">Thrown when one or more arguments have unsupported or
        /// illegal values.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception>
        /// <param name="toolFormat">The tool format of the input file.</param>
        /// <param name="inputFileName">The input log file name.</param>
        /// <param name="outputFileName">The name of the file to which the resulting SARIF log shall be
        /// written. This cannot be a directory.</param>
        /// <param name="conversionOptions">Options for controlling the conversion.</param>
        /// <param name="pluginAssemblyPath">Path to plugin assembly containing converter types.</param>
        public void ConvertToStandardFormat(
            string toolFormat,
            string inputFileName,
            string outputFileName,
            LoggingOptions loggingOptions = LoggingOptions.None,
            string pluginAssemblyPath     = null)
        {
            if (inputFileName == null)
            {
                throw new ArgumentNullException(nameof(inputFileName));
            }
            if (outputFileName == null)
            {
                throw new ArgumentNullException(nameof(outputFileName));
            }

            if (Directory.Exists(outputFileName))
            {
                throw new ArgumentException("Specified file output path exists but is a directory.", nameof(outputFileName));
            }

            if (!loggingOptions.Includes(LoggingOptions.OverwriteExistingOutputFile) && File.Exists(outputFileName))
            {
                throw new InvalidOperationException("Output file already exists and option to overwrite was not specified.");
            }

            if (toolFormat.MatchesToolFormat(ToolFormat.PREfast))
            {
                string sarif = ConvertPREfastToStandardFormat(inputFileName);
                File.WriteAllText(outputFileName, sarif);
            }
            else
            {
                // FileMode settings here will results in an exception being raised if the input
                // file does not exist, and that an existing output file will be overwritten
                using (var input = File.OpenRead(inputFileName))
                    using (var outputTextStream = File.Create(outputFileName))
                        using (var outputTextWriter = new StreamWriter(outputTextStream))
                            using (var outputJson = new JsonTextWriter(outputTextWriter))
                            {
                                if (loggingOptions.Includes(LoggingOptions.PrettyPrint))
                                {
                                    outputJson.Formatting = Formatting.Indented;
                                }

                                using (var output = new ResultLogJsonWriter(outputJson))
                                {
                                    ConvertToStandardFormat(toolFormat, input, output, loggingOptions, pluginAssemblyPath);
                                }
                            }
            }
        }
示例#3
0
 private static void Validate(FileData fileData, LoggingOptions loggingOptions)
 {
     if (loggingOptions.Includes(LoggingOptions.PersistFileContents))
     {
         fileData.Contents.Should().NotBeNull();
     }
     else
     {
         fileData.Contents.Should().BeNull();
     }
 }
示例#4
0
        private static Run CreateRun(
            IEnumerable <string> analysisTargets,
            LoggingOptions loggingOptions,
            IEnumerable <string> invocationTokensToRedact,
            IEnumerable <string> invocationPropertiesToLog)
        {
            var run = new Run();

            if (analysisTargets != null)
            {
                run.Files = new Dictionary <string, FileData>();

                foreach (string target in analysisTargets)
                {
                    string fileDataKey = UriHelper.MakeValidUri(target);

                    var fileData = FileData.Create(
                        new Uri(target, UriKind.RelativeOrAbsolute),
                        loggingOptions);

                    run.Files.Add(fileDataKey, fileData);
                }
            }

            run.Invocation = Invocation.Create(loggingOptions.Includes(LoggingOptions.PersistEnvironment), invocationPropertiesToLog);

            // TODO we should actually redact across the complete log file context
            // by a dedicated rewriting visitor or some other approach.
            if (invocationTokensToRedact != null)
            {
                run.Invocation.CommandLine      = Redact(run.Invocation.CommandLine, invocationTokensToRedact);
                run.Invocation.Machine          = Redact(run.Invocation.Machine, invocationTokensToRedact);
                run.Invocation.Account          = Redact(run.Invocation.Account, invocationTokensToRedact);
                run.Invocation.WorkingDirectory = Redact(run.Invocation.WorkingDirectory, invocationTokensToRedact);

                if (run.Invocation.EnvironmentVariables != null)
                {
                    string[] keys = run.Invocation.EnvironmentVariables.Keys.ToArray();

                    foreach (string key in keys)
                    {
                        string value = run.Invocation.EnvironmentVariables[key];
                        run.Invocation.EnvironmentVariables[key] = Redact(value, invocationTokensToRedact);
                    }
                }
            }
            return(run);
        }