Sha256: b28b5073994873407a4bbca1c6bcbd93f24a331295a92698cd837f92479272fa

Contents?: true

Size: 1.68 KB

Versions: 11

Compression:

Stored size: 1.68 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

require 'contrast/utils/string_utils'

module Contrast
  module Api
    module Decorators
      # Used to decorate the {Contrast::Api::Settings::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)

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
contrast-agent-4.4.1 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.4.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.3.2 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.3.1 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.3.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.2.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.1.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.0.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-3.16.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-3.15.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-3.14.0 lib/contrast/api/decorators/application_settings.rb