Class: Contrast::Agent::Reporting::LibraryDiscovery

Inherits:
ReportableHash show all
Defined in:
lib/contrast/agent/reporting/reporting_events/library_discovery.rb

Overview

This is the new Library Discovery class which will include all the needed information for the new reporting system to relay this information in the Application Update messages. These libraries are used by TeamServer to construct the dependency information for the SCA feature. They represent those gems that are included in the GemSpec, not necessarily those that have had files in them required.

Constant Summary collapse

StringUtils =
Contrast::Utils::StringUtils

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ReportableHash

#event_json, #valid?

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Constructor Details

#initialize(digest, spec) ⇒ LibraryDiscovery

Returns a new instance of LibraryDiscovery.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 34

def initialize digest, spec
  @file = StringUtils.force_utf8(spec.name) # rubocop:disable Security/Module/Name
  @hash = StringUtils.force_utf8(digest)
  @version     = StringUtils.force_utf8(spec.version)
  @manifest    = StringUtils.force_utf8(StringUtils.force_utf8(spec.to_yaml.to_s))
  @external_date = (spec.date.to_f * 1000.0).to_i
  @internal_date = @external_date
  @url         = StringUtils.force_utf8(spec.homepage)
  @class_count = Contrast::Utils::Sha256Builder.instance.files(spec.full_gem_path.to_s).length
  @tags = Contrast::INVENTORY.tags
  super()
end

Instance Attribute Details

#class_countObject (readonly)

optional attributes



25
26
27
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 25

def class_count
  @class_count
end

#external_dateObject (readonly)

required attributes



25
26
27
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 25

def external_date
  @external_date
end

#fileObject (readonly)

required attributes



25
26
27
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 25

def file
  @file
end

#hashObject (readonly)

required attributes



25
26
27
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 25

def hash
  @hash
end

#internal_dateObject (readonly)

required attributes



25
26
27
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 25

def internal_date
  @internal_date
end

#manifestObject

Returns the value of attribute manifest.



32
33
34
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 32

def manifest
  @manifest
end

#tagsObject (readonly)

optional attributes



25
26
27
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 25

def tags
  @tags
end

#urlObject (readonly)

optional attributes



25
26
27
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 25

def url
  @url
end

#versionObject (readonly)

optional attributes



25
26
27
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 25

def version
  @version
end

Instance Method Details

#to_controlled_hashHash

Convert the instance variables on the class, and other information, into the identifiers required for TeamServer to process the JSON form of this message.

Returns:

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 52

def to_controlled_hash
  validate
  {
      classCount: class_count,
      externalDate: external_date,
      file: file,
      hash: hash,
      internalDate: internal_date,
      manifest: manifest,
      url: url,
      version: version,
      tags: tags
  }.compact
end

#validateObject

Ensure the required fields are present.

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/contrast/agent/reporting/reporting_events/library_discovery.rb', line 70

def validate
  if Contrast::Utils::DuckUtils.empty_duck?(file)
    raise(ArgumentError, "#{ self } did not have a proper file. Unable to continue.")
  end
  if Contrast::Utils::DuckUtils.empty_duck?(hash)
    raise(ArgumentError, "#{ self } did not have a proper hash. Unable to continue.")
  end
  unless external_date
    raise(ArgumentError, "#{ self } did not have a proper external date. Unable to continue.")
  end
  unless internal_date
    raise(ArgumentError, "#{ self } did not have a proper internal date. Unable to continue.")
  end

  nil
end