<commit_before>require 'net/http'
require 'tmpdir'
require 'yaml'
require 'resolv'

module Puppet::Parser::Functions
  newfunction(:rir_allocations, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
    
    Returns IP ranges allocated to the various Regional Internet Registries
    around the world by type (ipv4 vs ipv6) and their geographical country
    assignment.

    This function is very usefu as a replacement for GeoIP modules as instead
    of requiring per-app support, you can simply use Puppet to take the output
    from this function and generate the designed configuration or ACLs.

    As the data from the RIRs only gets updated daily, this function caches the
    results and only refreshes every 24 hours. If a refresh fails (eg network
    issue) it falls back to serving stale cache so there's no sudden change of
    configuration as long as the cache files remain on disk (generally in /tmp)

    Note that unlike a GeoIP provider there's no curation taking place, we
    assume the country an IP range has been allocated to, is where it is being
    used - but this is not always true, so your milage may vary.

    The registries are:
    AFRNIC    : Africa Region
    APNIC     : Asia/Pacific Region
    ARIN      : North America Region
    LACNIC    : Latin America and some Caribbean Islands
    RIPE NCC  : Europe, the Middle East, and Central Asia
    ENDHEREDOC


    # RIRs & their data
    rirs = { 'afrinic'  => 'http://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest',
             'apnic'    => 'http://ftp.apnic.net/stats/apnic/delegated-apnic-latest',
             'arin'     => 'http://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest',
             'lacnic'   => ' http://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest',
             'ripe-ncc' => 'http://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-latest'
    }

    # Make sure an RIR is selected (Mandatory) and that it is valid.
    unless args[0] == nil
      selected_rir = args[0]

      if rirs[selected_rir] == nil
        raise Puppet::ParseError, 'You must select an RIR to return ranges for EG: rir_allocations(\'apnic\', \'ipv6\')'
      end
    else
      raise Puppet::ParseError, 'You must select an RIR to return ranges for EG: rir_allocations(\'apnic\', \'ipv6\')'
    end

    unless args[1] == nil
      selected_inet = args[1]

      if selected_inet != 'ipv4' and selected_inet != 'ipv6'
        raise Puppet::ParseError, 'You must select either IPv4 or IPv6 to return ranges for EG: rir_allocations(\'apnic\', \'ipv6\')'
      end
    else
      raise Puppet::ParseError, 'You must select either IPv4 or IPv6 to return ranges for EG: rir_allocations(\'apnic\', \'ipv6\')'
    end

    unless args[2] == nil
      selected_country = args[2]
      
      unless selected_country.match(/^[A-Z]{2}$/)
        raise Puppet::ParseError, 'Country is optional and must be a 2char upper case code. EG: rir_allocations(\'apnic\', \'ipv6\'. \'NZ\')'
      end
    else
      # Country is the only optional
      selected_country = nil
    end


    # Run through each registry and add it to the output
    debug("RIR: Processing data for RIR "+ selected_rir)

    data_cached      = nil<commit_msg>Upcase/downcase to avoid user annoyance<commit_after>require 'net/http'
require 'tmpdir'
require 'yaml'
require 'resolv'

module Puppet::Parser::Functions
  newfunction(:rir_allocations, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
    
    Returns IP ranges allocated to the various Regional Internet Registries
    around the world by type (ipv4 vs ipv6) and their geographical country
    assignment.

    This function is very usefu as a replacement for GeoIP modules as instead
    of requiring per-app support, you can simply use Puppet to take the output
    from this function and generate the designed configuration or ACLs.

    As the data from the RIRs only gets updated daily, this function caches the
    results and only refreshes every 24 hours. If a refresh fails (eg network
    issue) it falls back to serving stale cache so there's no sudden change of
    configuration as long as the cache files remain on disk (generally in /tmp)

    Note that unlike a GeoIP provider there's no curation taking place, we
    assume the country an IP range has been allocated to, is where it is being
    used - but this is not always true, so your milage may vary.

    The registries are:
    AFRNIC    : Africa Region
    APNIC     : Asia/Pacific Region
    ARIN      : North America Region
    LACNIC    : Latin America and some Caribbean Islands
    RIPE NCC  : Europe, the Middle East, and Central Asia
    ENDHEREDOC


    # RIRs & their data
    rirs = { 'afrinic'  => 'http://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest',
             'apnic'    => 'http://ftp.apnic.net/stats/apnic/delegated-apnic-latest',
             'arin'     => 'http://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest',
             'lacnic'   => ' http://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest',
             'ripe-ncc' => 'http://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-latest'
    }

    # Make sure an RIR is selected (Mandatory) and that it is valid.
    unless args[0] == nil
      selected_rir = args[0].downcase

      if rirs[selected_rir] == nil
        raise Puppet::ParseError, 'You must select an RIR to return ranges for EG: rir_allocations(\'apnic\', \'ipv6\')'
      end
    else
      raise Puppet::ParseError, 'You must select an RIR to return ranges for EG: rir_allocations(\'apnic\', \'ipv6\')'
    end

    unless args[1] == nil
      selected_inet = args[1].downcase

      if selected_inet != 'ipv4' and selected_inet != 'ipv6'
        raise Puppet::ParseError, 'You must select either IPv4 or IPv6 to return ranges for EG: rir_allocations(\'apnic\', \'ipv6\')'
      end
    else
      raise Puppet::ParseError, 'You must select either IPv4 or IPv6 to return ranges for EG: rir_allocations(\'apnic\', \'ipv6\')'
    end

    unless args[2] == nil
      selected_country = args[2].upcase
      
      unless selected_country.match(/^[A-Z]{2}$/)
        raise Puppet::ParseError, 'Country is optional and must be a 2char upper case code. EG: rir_allocations(\'apnic\', \'ipv6\', \'NZ\')'
      end
    else
      # Country is the only optional
      selected_country = nil
    end


    # Run through each registry and add it to the output
    debug("RIR: Processing data for RIR "+ selected_rir)

    data_cached      = nil
{'fraction_non_alphanumeric': 0.0951506807404008, 'fraction_numerical': 0.007036867064402631, 'mean_word_length': 2.8595041322314048, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7312174', 'n_tokens_mistral': 2082, 'n_tokens_neox': 1992, 'n_words': 727}