# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/agent/reporting/client/interface_base' module Contrast module Agent module Reporting module Client # Interface to safely manage connections across threads. class Interface < Contrast::Agent::Reporting::Client::InterfaceBase # Execute startup routine and def startup with_monitor { reporter_client.startup!(reporting_connection) } end # @param event [Contrast::Agent::Reporting::ReportingEvent] # @return [Net::HTTPResponse] def send_event event with_monitor { reporter_client.send_event(event, reporting_connection) } end # return [Boolean] def sleep? with_monitor { reporter_client.sleep? } end # Retry once than discard the event. This is trigger on too many events of # same kind error. # # @param event [Contrast::Agent::Reporting::ReportingEvent] def handle_resend event with_monitor do reporter_client.handle_resend(event, reporting_connection) end end # Check to see if client exists and there is connection # # @return [Boolean def connected? with_monitor do return true if reporter_client && reporting_connection false end end # Check to see if statup connections are sent or not def startup_messages_sent? with_monitor { reporter_client.status.startup_messages_sent? } end # Check to see if event need to be resend # # @return [Boolean, nil] def resending? with_monitor { reporter_client.mode.status == reporter_client.mode.resending } end # Return timeout in ms def timeout with_monitor { reporter_client.timeout } || Contrast::Agent::Reporting::ResponseHandler::TIMEOUT end # @return headers [Contrast::Agent::Reporting::Headers] def headers with_monitor { reporter_client.headers } end # Response_handler # # @return [Contrast::Agent::Reporting::ResponseHandler] def response_handler with_monitor { reporter_client.response_handler } end def status with_monitor { reporter_client.status } end private # @return [Contrast::Agent::Reporting::ReporterClient] def reporter_client @_reporter_client ||= Contrast::Agent::Reporting::ReporterClient.new end # @return [Net::HTTP, nil] Return open connection or nil def reporting_connection @_reporting_connection ||= reporter_client.initialize_connection end end end module Telemetry # Interface to safely manage connections across threads. class Interface < Contrast::Agent::Reporting::Client::InterfaceBase URL = 'https://telemetry.ruby.contrastsecurity.com/' # Check to see if client exists and there is connection # # @return [Boolean def connected? with_monitor do return true if telemetry_client && telemetry_connection false end end # Starts telemetry request. def request_with_response event with_monitor do telemetry_client.handle_response(telemetry_client.send_request(event, telemetry_connection)) end end private def telemetry_client @_telemetry_client ||= Contrast::Agent::Telemetry::Client.new end def telemetry_connection @_telemetry_connection ||= telemetry_client.initialize_connection(URL) end end end end end end