# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true cs__scoped_require 'socket' cs__scoped_require 'contrast/extensions/ruby_core/module' cs__scoped_require 'contrast/components/interface' cs__scoped_require 'contrast/utils/object_share' cs__scoped_require 'contrast/utils/string_utils' module Contrast module Utils # Gather information about the application and server environment # that the agent is running in. class EnvironmentUtil CS_VERSION = 'VERSION' # Possible names of constants that would hold version info # TODO - RUBY-770 VERSION_CONSTANT_CANDIDATES = %w[ VERSION ::VERSION Version::VERSION ::Version::VERSION ::Application::VERSION ].cs__freeze class << self ######### These are helpers #################### def present? str !str.nil? && !str.to_s.empty? end ######### Actually env/framework dependent #################### # See CONTRAST-16380 for more details # TODO RUBY-770 def determine_application_version @_determine_application_version ||= begin candidates = VERSION_CONSTANT_CANDIDATES.map do |name| # If there's a constant named 'name' (VERSION, etc.), get its value. begin name.split('::').inject(Object) do |mod, class_name| mod.cs__const_get(class_name) end rescue LoadError, StandardError nil end end candidates.compact! candidate = candidates.first || ENV[CS_VERSION] case candidate when Hash candidate.values.join(Contrast::Utils::ObjectShare::PERIOD) when Array candidate.join(Contrast::Utils::ObjectShare::PERIOD) when String candidate when NilClass return nil else candidate.inspect end end end # Read libraries from the gemfile and append to the given ApplicationUpdate message # or other class that has a libraries map association # TODO - RUBY-770 def add_library_to_app_update app_update_pb, library_tags = '' libraries = Contrast::Utils::GemfileReader.instance.library_pb_list libraries.each do |n| n.tags = library_tags app_update_pb.libraries[n.hash_code] = n end end end end end end