Sha256: 4687dc3569b9309e7715ba773e53fcce0a12fd3ccff934e6ce5a1f3c3c5c71e7

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require "logger"

module Aserto
  class Config
    class << self
      def default_logger
        logger = Logger.new($stdout)
        logger.progname = "aserto"
        logger
      end

      def validate!
        error_message = ""
        REQUIRED_OPTIONS.each do |option|
          if !instance_variable_defined?(:"@#{option}") ||
             instance_variable_get(:"@#{option}") == ""
            error_message += "Missing required option: #{option}\n"
          end
        end
        raise error_message if error_message != ""
      end
    end

    DEFAULT_ATTRS = {
      authorizer_api_key: "",
      tenant_id: "",
      client: nil,
      service_url: "localhost:8282",
      decision: "allowed",
      disabled_for: [{}],
      enabled: true,
      identity_mapping: {
        type: :none
      },
      logger: Config.default_logger,
      policy_name: "",
      instance_label: "",
      policy_root: "",
      cert_path: "",
      on_unauthorized: lambda do |_env|
        return [403, {}, ["Forbidden"]]
      end
    }.freeze

    OPTIONS = DEFAULT_ATTRS.keys.freeze

    REQUIRED_OPTIONS = OPTIONS - %i[
      service_url decision disabled_for identity_mapping enabled logger
    ].freeze

    private_constant :DEFAULT_ATTRS, :OPTIONS, :REQUIRED_OPTIONS

    attr_accessor(*OPTIONS)

    def initialize(options)
      OPTIONS.each do |key|
        send("#{key}=", options[key] || DEFAULT_ATTRS[key])
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aserto-0.30.1 lib/aserto/config.rb
aserto-0.30.0 lib/aserto/config.rb
aserto-0.20.6 lib/aserto/config.rb