Sha256: 47c5724e1e52fccd9f052f85ab013972e80c825db27b424d09069b79fbe88b25

Contents?: true

Size: 1.26 KB

Versions: 41

Compression:

Stored size: 1.26 KB

Contents

require 'ipaddr'
module Hydra
  class IpBasedGroups
    def self.for(remote_ip)
      groups.select { |group| group.include_ip?(remote_ip) }.map(&:name)
    end

    class Group
      attr_accessor :name
      # @param [Hash] h
      def initialize(h)
        @name = h.fetch('name')
        @subnet_strings = h.fetch('subnets')
      end

      def include_ip?(ip_string)
        ip = IPAddr.new(ip_string)
        subnets.any? { |subnet| subnet.include?(ip) }
      end

      private

        def subnets
          @subnets ||= @subnet_strings.map { |s| IPAddr.new(s) }
        end
    end

      def self.groups
        load_groups.fetch('groups').map { |h| Group.new(h) }
      end

      def self.filename
        'config/hydra_ip_range.yml'
      end

      def self.load_groups
        require 'yaml'

        file = File.join(Rails.root, filename)

        unless File.exists?(file)
          raise "ip-range configuration file not found. Expected: #{file}."
        end

        begin
          yml = YAML::load_file(file)
        rescue
          raise("#{filename} was found, but could not be parsed.\n")
        end
        unless yml.is_a? Hash
          raise("#{filename} was found, but was blank or malformed.\n")
        end

        yml.fetch(Rails.env)
      end

  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
hydra-access-controls-12.0.2 lib/hydra/ip_based_groups.rb
hydra-access-controls-11.0.7 lib/hydra/ip_based_groups.rb
hydra-access-controls-12.0.1 lib/hydra/ip_based_groups.rb
hydra-access-controls-11.0.6 lib/hydra/ip_based_groups.rb
hydra-access-controls-12.0.0 lib/hydra/ip_based_groups.rb
hydra-access-controls-11.0.1 lib/hydra/ip_based_groups.rb
hydra-access-controls-11.0.0 lib/hydra/ip_based_groups.rb
hydra-access-controls-11.0.0.rc2 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.7.0 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.6.2 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.6.1 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.6.0 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.5.1 lib/hydra/ip_based_groups.rb
hydra-access-controls-11.0.0.rc1 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.5.0 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.4.0 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.4.0.rc2 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.4.0.rc1 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.3.4 lib/hydra/ip_based_groups.rb
hydra-access-controls-10.3.3 lib/hydra/ip_based_groups.rb