Sha256: f6a850a420f08fd05b4b8c6fc869c24a09d6a24657de81d3a4c2dfd7685e69e8

Contents?: true

Size: 1.96 KB

Versions: 8

Compression:

Stored size: 1.96 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

module Contrast
  module Agent
    module Inventory
      # this module is included in classes that need access to the applications dependencies
      module Dependencies
        CONTRAST_AGENT = 'contrast-agent'

        # the #clone is necessary here, as a require in another thread could
        # potentially result in adding a key to the loaded_specs hash during
        # iteration.  (as in RUBY-330)
        # this takes care of filtering out contrast-only dependencies
        def loaded_specs
          specs = Gem.loaded_specs.clone
          specs.delete_if { |name, _v| contrast?(name) }
        end

        private

        def contrast_gems
          @_contrast_gems ||= find_contrast_gems
        end

        def contrast? name
          contrast_gems.include?(name)
        end

        # Go through all dependents, given as a pair from the DependencyList: `dependency`
        # is the dependency itself, filled with all its specs. `dependents` is the array of reverse
        # dependencies for the aforementioned dependency. If the dependency is also in contrast_dep_set,
        # then contrast depends on it. If its array of dependents is 1, then contrast is the
        # only dependency in that list. Since only contrast depends on it, we should ignore it.
        def find_contrast_gems
          ignore = Set.new([CONTRAST_AGENT])
          contrast_specs = Gem::DependencyList.from_specs.specs.find do |dependency|
            dependency.name == CONTRAST_AGENT
          end
          contrast_dep_set = contrast_specs.dependencies.map(&:name).to_set

          Gem::DependencyList.from_specs.spec_predecessors.each_pair do |dependency, dependents|
            ignore.add(dependency.name) if contrast_dep_set.include?(dependency.name) && dependents.length == 1
          end
          ignore
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
contrast-agent-4.4.1 lib/contrast/agent/inventory/dependencies.rb
contrast-agent-4.4.0 lib/contrast/agent/inventory/dependencies.rb
contrast-agent-4.3.2 lib/contrast/agent/inventory/dependencies.rb
contrast-agent-4.3.1 lib/contrast/agent/inventory/dependencies.rb
contrast-agent-4.3.0 lib/contrast/agent/inventory/dependencies.rb
contrast-agent-4.2.0 lib/contrast/agent/inventory/dependencies.rb
contrast-agent-4.1.0 lib/contrast/agent/inventory/dependencies.rb
contrast-agent-4.0.0 lib/contrast/agent/inventory/dependencies.rb