# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true module Contrast module Components # A wrapper build around the Common Agent Configuration project to allow # for access of the values contained in its # parent_configuration_spec.yaml. # Specifically, this allows for querying the state of the Application, # including the Client, Process, and Server information. module AppContextExtend SUPPORTED_FRAMEWORKS = %w[rails sinatra grape rack].cs__freeze SUPPORTED_SERVERS = %w[passenger puma thin unicorn].cs__freeze def build_app_startup_message @_build_app_startup_message ||= Contrast::Api::Dtm::ApplicationCreate.build end def build_agent_startup_message msg = Contrast::Api::Dtm::AgentStartup.build(server_name, server_path, server_type) logger.info('Application context', server_name: msg.server_name, server_path: msg.server_path, server_type: msg.server_type, application_name: app_name, application_path: path, application_language: Contrast::Utils::ObjectShare::RUBY) msg end def pid Process.pid end def ppid Process.ppid end def pgid Process.getpgid(pid) end def client_id @_client_id ||= [app_name, pgid].join('-') end def app_and_server_information { application_info: find_gem_information(SUPPORTED_FRAMEWORKS), server_info: find_gem_information(SUPPORTED_SERVERS) } end def find_gem_information arr arr.each do |framework| next unless Gem.loaded_specs.key?(framework) loaded = Gem.loaded_specs[framework] next unless loaded name = loaded.instance_variable_get(:@name) version = loaded.instance_variable_get(:@version).to_s return [name, version].join(' ') end nil end def instrument_middleware_stack? !Contrast::Utils::JobServersRunning.job_servers_running? end def disabled_agent_rake_tasks ::Contrast::CONFIG.root.agent.ruby.disabled_agent_rake_tasks end end end end