Ejemplo n.º 1
0
        public override IEnumerable <OSSIndexQueryObject> GetPackages(params string[] o)
        {
            List <OSSIndexQueryObject> packages = new List <OSSIndexQueryObject> ();

            if (this.UseDockerContainer)
            {
                Docker.ProcessStatus process_status;
                string process_output, process_error;
                if (Docker.ExecuteInContainer(this.DockerContainerId, @"yum list installed", out process_status, out process_output, out process_error))
                {
                    string[] p = process_output.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    for (int i = 0; i < p.Count(); i++)
                    {
                        string[] k = p [i].Split("|".ToCharArray());
                        packages.Add(new OSSIndexQueryObject("dpkg", k [0], k [1]));
                    }
                }
                else
                {
                    throw new Exception(string.Format("Error running {0} command on docker container {1}: {2}", @"dpkg-query -W -f ${Package}'${Version}\n",
                                                      this.DockerContainerId, process_error));
                }
                return(packages);
            }
            else
            {
                string command   = @"yum";
                string arguments = @"list installed";
                Regex  process_output_pattern = new Regex(@"^(\S+)\s(\S+)$", RegexOptions.Compiled);
                HostEnvironment.ProcessStatus process_status;
                string process_output, process_error;
                if (HostEnvironment.Execute(command, arguments, out process_status, out process_output, out process_error))
                {
                    string[] p = process_output.Split("\n".ToCharArray());
                    for (int i = 0; i < p.Count(); i++)
                    {
                        Match m = process_output_pattern.Match(p [i].TrimStart());
                        if (!m.Success)
                        {
                            throw new Exception("Could not parse dpkg command output row: " + i.ToString()
                                                + "\n" + p [i]);
                        }
                        else
                        {
                            packages.Add(new OSSIndexQueryObject("dpkg", m.Groups [1].Value, m.Groups [2].Value, ""));
                        }
                    }
                }
                else
                {
                    throw new Exception(string.Format("Error running {0} {1} command in host environment: {2}.", command,
                                                      arguments, process_error));
                }
                return(packages);
            }
        }
Ejemplo n.º 2
0
        public override IEnumerable <OSSIndexQueryObject> GetPackages(params string[] o)
        {
            List <OSSIndexQueryObject> packages = new List <OSSIndexQueryObject> ();

            if (this.UseDockerContainer)
            {
                Docker.ProcessStatus process_status;
                string process_output, process_error;
                if (Docker.ExecuteInContainer(this.DockerContainerId, @"rpm -qa --qf ""%{NAME} %{VERSION}\n""", out process_status, out process_output, out process_error))
                {
                    string[] p = process_output.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    Regex    process_output_pattern = new Regex(@"^(\S+)\s(\S+)", RegexOptions.Compiled);
                    Match    m;
                    for (int i = 0; i < p.Count(); i++)
                    {
                        m = process_output_pattern.Match(p[i]);
                        if (!m.Success)
                        {
                            throw new Exception("Could not parse rpm command output row: " + i.ToString() + "\n" + p [i]);
                        }
                        else
                        {
                            packages.Add(new OSSIndexQueryObject("rpm", m.Groups [1].Value, m.Groups [2].Value));
                        }
                    }
                }
                else
                {
                    throw new Exception(string.Format("Error running {0} command on docker container {1}: {2}", @"rpm -qa --qf ""%{NAME} %{VERSION}\n""",
                                                      this.DockerContainerId, process_error));
                }
                return(packages);
            }
            else
            {
                string command   = @"rpm";
                string arguments = @"-qa --qf ""%{NAME} %{VERSION}\n""";
                Regex  process_output_pattern = new Regex(@"^(\S+)\s(\S+)", RegexOptions.Compiled);
                HostEnvironment.ProcessStatus process_status;
                string process_output, process_error;
                if (HostEnvironment.Execute(command, arguments, out process_status, out process_output, out process_error))
                {
                    string[] p = process_output.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    Match    m;
                    for (int i = 0; i < p.Count(); i++)
                    {
                        m = process_output_pattern.Match(p[i]);
                        if (!m.Success)
                        {
                            throw new Exception("Could not parse rpm command output row: " + i.ToString() + "\n" + p [i]);
                        }
                        else
                        {
                            packages.Add(new OSSIndexQueryObject("rpm", m.Groups [1].Value, m.Groups [2].Value));
                        }
                    }
                }
                else
                {
                    throw new Exception(string.Format("Error running {0} {1} command in host environment: {2}.", command,
                                                      arguments, process_error));
                }
                return(packages);
            }
        }
Ejemplo n.º 3
0
        public PackageSource(Dictionary <string, object> package_source_options)
        {
            this.PackageSourceOptions = package_source_options;

            if (this.PackageSourceOptions.ContainsKey("File"))
            {
                this.PackageManagerConfigurationFile = (string)this.PackageSourceOptions["File"];
                if (!File.Exists(this.PackageManagerConfigurationFile))
                {
                    throw new ArgumentException("Could not find the file " + this.PackageManagerConfigurationFile + ".");
                }
            }
            else
            {
                this.PackageManagerConfigurationFile = "";
            }

            #region Cache option
            if (this.PackageSourceOptions.ContainsKey("Cache") && (bool)this.PackageSourceOptions["Cache"] == true)
            {
                this.ProjectVulnerabilitiesCacheEnabled = true;
                if (this.PackageSourceOptions.ContainsKey("CacheFile") && !string.IsNullOrEmpty((string)this.PackageSourceOptions["CacheFile"]))
                {
                    this.ProjectVulnerabilitiesCacheFile = (string)this.PackageSourceOptions["CacheFile"];
                }
                else
                {
                    this.ProjectVulnerabilitiesCacheFile = AppDomain.CurrentDomain.BaseDirectory + "DevAudit-net.cache";
                }
                if (this.PackageSourceOptions.ContainsKey("CacheTTL") && !string.IsNullOrEmpty((string)this.PackageSourceOptions["CacheTTL"]))

                {
                    int cache_ttl;
                    if (Int32.TryParse((string)this.PackageSourceOptions["CacheTTL"], out cache_ttl))
                    {
                        if (cache_ttl > 60 * 24 * 30)
                        {
                            throw new ArgumentOutOfRangeException("The value for the cache ttl is too large: " + this.PackageSourceOptions["CacheTTL"] + ".");
                        }
                        this.ProjectVulnerabilitiesCacheTTL = TimeSpan.FromMinutes(cache_ttl);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException("The value for the cache ttl is not an integer: " + (string)this.PackageSourceOptions["CacheTTL"] + ".");
                    }
                }
                else
                {
                    this.ProjectVulnerabilitiesCacheTTL = TimeSpan.FromMinutes(180);
                }
                if (this.PackageSourceOptions.ContainsKey("CacheDump"))
                {
                    this.ProjectVulnerabilitiesCacheDump = true;
                }
                else
                {
                    this.ProjectVulnerabilitiesCacheDump = false;
                }
                this.ProjectVulnerabilitiesCacheInitialiseTask =
                    Task <BPlusTree <string, Tuple <OSSIndexProject, IEnumerable <OSSIndexProjectVulnerability> > > > .Run(() =>
                {
                    return(this.InitialiseProjectVulnerabilitiesCache(this.ProjectVulnerabilitiesCacheFile));    //Assembly.GetExecutingAssembly().Location + "win-audit.cache");
                });
            }
            else
            {
                this.ProjectVulnerabilitiesCacheEnabled = false;
            }
            #endregion

            #region User Docker container option
            if (this.PackageSourceOptions.ContainsKey("DockerContainerId"))
            {
                Docker.ProcessStatus process_status;
                string process_output, process_error;
                if (Docker.GetContainer((string)this.PackageSourceOptions["DockerContainerId"], out process_status, out process_output, out process_error))
                {
                    this.UseDockerContainer = true;
                    this.DockerContainerId  = (string)this.PackageSourceOptions["DockerContainerId"];
                }
                else
                {
                    if (process_status == Docker.ProcessStatus.DockerNotInstalled)
                    {
                        throw new ArgumentException(string.Format("Failed to find docker container {0}. Docker does not appear to be installed or the command-line tools are not on the current PATH. Error is:  {1}",
                                                                  (string)this.PackageSourceOptions["DockerContainerId"], process_error));
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("Failed to find docker container {0}. Error is:  {1}",
                                                                  (string)this.PackageSourceOptions["DockerContainerId"], process_error));
                    }
                }
            }
            else
            {
                this.UseDockerContainer = false;
            }
            #endregion
        }