示例#1
0
        private static Run CreateRun(
            IEnumerable <string> analysisTargets,
            bool computeTargetsHash,
            IEnumerable <string> invocationTokensToRedact)
        {
            var run = new Run();

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

                foreach (string target in analysisTargets)
                {
                    var fileReference = new FileData();

                    if (computeTargetsHash)
                    {
                        string md5, sha1, sha256;

                        HashUtilities.ComputeHashes(target, out md5, out sha1, out sha256);
                        fileReference.Hashes = new List <Hash>(new Hash[]
                        {
                            new Hash()
                            {
                                Value     = md5,
                                Algorithm = AlgorithmKind.MD5,
                            },
                            new Hash()
                            {
                                Value     = sha1,
                                Algorithm = AlgorithmKind.Sha1,
                            },
                            new Hash()
                            {
                                Value     = sha256,
                                Algorithm = AlgorithmKind.Sha256,
                            },
                        });
                    }
                    run.Files.Add(new Uri(target).ToString(), new List <FileData> {
                        fileReference
                    });
                }
            }

            string invocation = Environment.CommandLine;

            if (invocationTokensToRedact != null)
            {
                foreach (string tokenToRedact in invocationTokensToRedact)
                {
                    invocation = invocation.Replace(tokenToRedact, SarifConstants.RemovedMarker);
                }
            }
            run.Invocation = invocation;
            run.StartTime  = DateTime.UtcNow;
            return(run);
        }
        public ResultLogger(
            Assembly assembly,
            string outputFilePath,
            bool verbose,
            IEnumerable <string> analysisTargets,
            bool computeTargetsHash,
            string prereleaseInfo)
        {
            Verbose = verbose;

            _fileStream     = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
            _textWriter     = new StreamWriter(_fileStream);
            _jsonTextWriter = new JsonTextWriter(_textWriter);

            // for debugging it is nice to have the following line added.
            _jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented;

            _issueLogJsonWriter = new ResultLogJsonWriter(_jsonTextWriter);

            var toolInfo = new ToolInfo();

            toolInfo.InitializeFromAssembly(assembly, prereleaseInfo);

            RunInfo runInfo = new RunInfo();

            runInfo.AnalysisTargets = new List <FileReference>();

            foreach (string target in analysisTargets)
            {
                var fileReference = new FileReference()
                {
                    Uri = target.CreateUriForJsonSerialization(),
                };

                if (computeTargetsHash)
                {
                    string sha256Hash = HashUtilities.ComputeSha256Hash(target) ?? "[could not compute file hash]";
                    fileReference.Hashes = new List <Hash>(new Hash[]
                    {
                        new Hash()
                        {
                            Value     = sha256Hash,
                            Algorithm = AlgorithmKind.Sha256,
                        }
                    });
                }
                runInfo.AnalysisTargets.Add(fileReference);
            }
            runInfo.InvocationInfo = Environment.CommandLine;

            _issueLogJsonWriter.WriteToolAndRunInfo(toolInfo, runInfo);
        }
示例#3
0
        private static Run CreateRun(
            IEnumerable <string> analysisTargets,
            bool computeTargetsHash,
            bool logEnvironment,
            IEnumerable <string> invocationTokensToRedact)
        {
            var run = new Run();

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

                foreach (string target in analysisTargets)
                {
                    var fileReference = new FileData();

                    if (computeTargetsHash)
                    {
                        string md5, sha1, sha256;

                        HashUtilities.ComputeHashes(target, out md5, out sha1, out sha256);
                        fileReference.Hashes = new HashSet <Hash>
                        {
                            new Hash()
                            {
                                Value     = md5,
                                Algorithm = AlgorithmKind.MD5,
                            },
                            new Hash()
                            {
                                Value     = sha1,
                                Algorithm = AlgorithmKind.Sha1,
                            },
                            new Hash()
                            {
                                Value     = sha256,
                                Algorithm = AlgorithmKind.Sha256,
                            },
                        };
                    }
                    run.Files.Add(new Uri(target).ToString(), new List <FileData> {
                        fileReference
                    });
                }
            }


            run.Invocation = Invocation.Create(logEnvironment);

            // 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.Parameters       = Redact(run.Invocation.Parameters, invocationTokensToRedact);
                run.Invocation.Machine          = Redact(run.Invocation.Machine, invocationTokensToRedact);
                run.Invocation.Account          = Redact(run.Invocation.Account, invocationTokensToRedact);
                run.Invocation.Parameters       = Redact(run.Invocation.Parameters, 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);
        }