Sha256: 41ba254a1c74000380754c9f0b2a131b0375e190160e1b863f861722b4ee21d8

Contents?: true

Size: 1.98 KB

Versions: 11

Compression:

Stored size: 1.98 KB

Contents

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

require 'contrast/utils/string_utils'
require 'contrast/utils/sha256_builder'
require 'yaml'

module Contrast
  module Api
    module Decorators
      # Used to decorate the Library protobuf model to handle Gem::Specification translation
      module Library
        StringUtils = Contrast::Utils::StringUtils

        def self.included klass
          klass.extend(ClassMethods)
        end

        # Used to add class methods to the Library class on inclusion of the decorator
        module ClassMethods
          def build digest, gem_specification
            msg = new
            msg.file_path = StringUtils.force_utf8(gem_specification.name) # rubocop:disable Security/Module/Name
            msg.hash_code   = StringUtils.force_utf8(digest)
            msg.version     = StringUtils.force_utf8(gem_specification.version)
            msg.manifest    = StringUtils.force_utf8(build_manifest(gem_specification))
            msg.external_ms = date_to_ms(gem_specification.date)
            msg.internal_ms = msg.external_ms
            msg.url         = StringUtils.force_utf8(gem_specification.homepage)
            msg.class_count = file_count(gem_specification.full_gem_path.to_s)
            msg.used_class_count = 0
            msg
          end

          # These are all the code files that are located in the Gem directory loaded
          # by the current environment; this includes more than Ruby files
          def file_count path
            Contrast::Utils::Sha256Builder.instance.files(path).length
          end

          def build_manifest spec
            StringUtils.force_utf8(spec.to_yaml.to_s)
          rescue StandardError
            nil
          end

          def date_to_ms date
            (date.to_f * 1000.0).to_i
          end
        end
      end
    end
  end
end

Contrast::Api::Dtm::Library.include(Contrast::Api::Decorators::Library)

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
contrast-agent-4.14.1 lib/contrast/api/decorators/library.rb
contrast-agent-4.14.0 lib/contrast/api/decorators/library.rb
contrast-agent-4.13.1 lib/contrast/api/decorators/library.rb
contrast-agent-4.13.0 lib/contrast/api/decorators/library.rb
contrast-agent-4.12.0 lib/contrast/api/decorators/library.rb
contrast-agent-4.11.0 lib/contrast/api/decorators/library.rb
contrast-agent-4.10.0 lib/contrast/api/decorators/library.rb
contrast-agent-4.9.1 lib/contrast/api/decorators/library.rb
contrast-agent-4.9.0 lib/contrast/api/decorators/library.rb
contrast-agent-4.8.0 lib/contrast/api/decorators/library.rb
contrast-agent-4.7.0 lib/contrast/api/decorators/library.rb