# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true cs__scoped_require 'monitor' module Contrast module Components module ContrastService # 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 connection to # the Service, as well as sending a message to the Service. class Interface include Contrast::Components::ComponentBase include Contrast::Components::Interface DEFAULT_SERVICE_LOG = 'contrast_service.log' # The Rails ActionDispatch regexp for localhost IP + literal localhost # https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/request.rb#L32 LOCALHOST = Regexp.union [/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/, /^::1$/, /^0:0:0:0:0:0:0:1(%.*)?$/, /^localhost$/] access_component :agent, :config def use_bundled_service? # Validates the config to decide if it's suitable for starting # the bundled service @_use_bundled_service ||= begin # Requirement says "must be true" but that # should be "must not be false" -- oops. !false?(CONFIG.root.agent.start_bundled_service) && # Either a valid host or a valid socket # Path validity is the service's problem (LOCALHOST.match?(host) || !!socket_path) end end def host @_host ||= (CONFIG.root.agent.service.host || Contrast::Configuration::DEFAULT_HOST).to_s end def port @_port ||= (CONFIG.root.agent.service.port || DEFAULT_PORT).to_i end def socket_path @_socket_path ||= CONFIG.root.agent.service.socket end def use_tcp? socket_path.nil? end def logger_path @_logger_path ||= CONFIG.root.agent.service.logger.path || DEFAULT_SERVICE_LOG end private def disabled? @_disabled = false?(CONFIG.root.agent.start_bundled_service) if @_disabled.nil? @_disabled end end COMPONENT_INTERFACE = Interface.new end end end