Sha256: 7b693e410aef0aefc615b59b065433fac97548cea5af0c2f844c3670fb18a173

Contents?: true

Size: 1.68 KB

Versions: 13

Compression:

Stored size: 1.68 KB

Contents

# Copyright (c) 2021 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

13 entries across 13 versions & 1 rubygems

Version Path
contrast-agent-4.14.1 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.14.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.13.1 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.13.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.12.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.11.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.10.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.9.1 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.9.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.8.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.7.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.6.0 lib/contrast/api/decorators/application_settings.rb
contrast-agent-4.5.0 lib/contrast/api/decorators/application_settings.rb