lib/contrast/utils/invalid_configuration_util.rb in contrast-agent-4.3.2 vs lib/contrast/utils/invalid_configuration_util.rb in contrast-agent-4.4.0

- old
+ new

@@ -24,32 +24,41 @@ # @param call_location [Thread::Backtrace::Location] the location where # the bad configuration was set def cs__report_finding rule_id, user_provided_options, call_location with_contrast_scope do finding = Contrast::Api::Dtm::Finding.new - finding.rule_id = rule_id - path = call_location.path - # just get the file name, not the full path - path = path.split(Contrast::Utils::ObjectShare::SLASH).last - session_id = user_provided_options[:key].to_s if user_provided_options - finding.version = Contrast::Agent::Assess::Policy::TriggerMethod::CURRENT_FINDING_VERSION - finding.properties[CS__SESSION_ID] = Contrast::Utils::StringUtils.force_utf8(session_id) - finding.properties[CS__PATH] = Contrast::Utils::StringUtils.force_utf8(path) - file_path = call_location.absolute_path - snippet = file_snippet(file_path, call_location) - finding.properties[CS__SNIPPET] = Contrast::Utils::StringUtils.force_utf8(snippet) - + finding.rule_id = rule_id + set_properties(finding, user_provided_options, call_location) hash = Contrast::Utils::HashDigest.generate_config_hash(finding) finding.hash_code = Contrast::Utils::StringUtils.force_utf8(hash) finding.preflight = Contrast::Utils::PreflightUtil.create_preflight(finding) Contrast::Agent::Assess::Policy::TriggerMethod.report_finding(finding) end rescue StandardError => e logger.error('Unable to build a finding', e, rule: rule_id) end private + + # Set the properties needed to report and subsequently render this finding on the finding given. + # + # @param finding [Contrast::Api::Dtm::Finding] the configuration finding to populate + # @param user_provided_options [Hash] the configuration value(s) which + # violated the rule + # @param call_location [Thread::Backtrace::Location] the location where + # the bad configuration was set + def set_properties finding, user_provided_options, call_location + path = call_location.path + # just get the file name, not the full path + path = path.split(Contrast::Utils::ObjectShare::SLASH).last + session_id = user_provided_options[:key].to_s if user_provided_options + finding.properties[CS__SESSION_ID] = Contrast::Utils::StringUtils.force_utf8(session_id) + finding.properties[CS__PATH] = Contrast::Utils::StringUtils.force_utf8(path) + file_path = call_location.absolute_path + snippet = file_snippet(file_path, call_location) + finding.properties[CS__SNIPPET] = Contrast::Utils::StringUtils.force_utf8(snippet) + end def file_snippet file_path, call_location idx = call_location&.lineno if file_path && idx && File.exist?(file_path) idx = idx > 5 ? idx - 5 : 0