# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true cs__scoped_require 'contrast/utils/string_utils' module Contrast module Api module Decorators # Used to decorate the ApplicationSettings protobuf model to handle setting translation module ApplicationSettings MODE_ALLOWLIST = [ Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION, Contrast::Api::Settings::ProtectionRule::Mode::BLOCK, Contrast::Api::Settings::ProtectionRule::Mode::MONITOR, Contrast::Api::Settings::ProtectionRule::Mode::BLOCK_AT_PERIMETER ].cs__freeze # Convert protobuf exlcusions into Agent exclusions def translated_exclusions exclusions.map { |exc| Contrast::Agent::ExclusionMatcher.new(exc) } end # Convert protobuf protect rule settings into a hash that settings state understands def translated_protection_mode_by_id protection_rules = self.protection_rules.map { |pr, _hash| [pr.id, pr.mode] } # [[RULE_ID, MODE]] protection_rules.select! { |(_id, mode)| MODE_ALLOWLIST.include? mode } # [REMOVE IF MODE INVALID] protection_rules.to_h # {RULE_ID => MODE} end def application_state_translation { modes_by_id: translated_protection_mode_by_id, exclusion_matchers: translated_exclusions, disabled_assess_rules: disabled_assess_rules } end end end end end Contrast::Api::Settings::ApplicationSettings.include(Contrast::Api::Decorators::ApplicationSettings)