<commit_before>local typedefs = require "kong.db.schema.typedefs"


local ORDERED_PERIODS = { "second", "minute", "hour", "day", "month", "year" }


local function validate_periods_order(limit)
  for i, lower_period in ipairs(ORDERED_PERIODS) do
    local v1 = limit[lower_period]
    if type(v1) == "number" then
      for j = i + 1, #ORDERED_PERIODS do
        local upper_period = ORDERED_PERIODS[j]
        local v2 = limit[upper_period]
        if type(v2) == "number" and v2 < v1 then
          return nil, string.format("the limit for %s(%.1f) cannot be lower than the limit for %s(%.1f)",
                                    upper_period, v2, lower_period, v1)
        end
      end
    end
  end

  return true
end


return {
  name = "response-ratelimiting",
  fields = {
    { protocols = typedefs.protocols_http },
    { config = {
        type = "record",
        fields = {
          { header_name = { type = "string", default = "x-kong-limit" }, },
          { limit_by = { type = "string",
                         default = "consumer",
                         one_of = { "consumer", "credential", "ip" },
          }, },
          { policy = { type = "string",
                       default = "cluster",
                       one_of = { "local", "cluster", "redis" },
          }, },
          { fault_tolerant = { type = "boolean", required = true, default = true }, },
          { redis_host = typedefs.host },
          { redis_port = typedefs.port({ default = 6379 }), },
          { redis_password = { type = "string", len_min = 0 }, },<commit_msg>feat(response-ratelimiting) add role based policy with sane default and validation<commit_after>local typedefs = require "kong.db.schema.typedefs"


local ORDERED_PERIODS = { "second", "minute", "hour", "day", "month", "year" }


local function validate_periods_order(limit)
  for i, lower_period in ipairs(ORDERED_PERIODS) do
    local v1 = limit[lower_period]
    if type(v1) == "number" then
      for j = i + 1, #ORDERED_PERIODS do
        local upper_period = ORDERED_PERIODS[j]
        local v2 = limit[upper_period]
        if type(v2) == "number" and v2 < v1 then
          return nil, string.format("the limit for %s(%.1f) cannot be lower than the limit for %s(%.1f)",
                                    upper_period, v2, lower_period, v1)
        end
      end
    end
  end

  return true
end


local function is_dbless()
  local _, database, role = pcall(function()
    return kong.configuration.database,
           kong.configuration.role
  end)

  return database == "off" or role == "control_plane"
end


local policy
if is_dbless() then
  policy = {
    type = "string",
    default = "local",
    one_of = {
      "local",
      "redis",
    },
  }

else
  policy = {
    type = "string",
    default = "cluster",
    one_of = {
      "local",
      "cluster",
      "redis",
    },
  }
end


return {
  name = "response-ratelimiting",
  fields = {
    { protocols = typedefs.protocols_http },
    { config = {
        type = "record",
        fields = {
          { header_name = { type = "string", default = "x-kong-limit" }, },
          { limit_by = { type = "string",
                         default = "consumer",
                         one_of = { "consumer", "credential", "ip" },
          }, },
          { policy = policy },
          { fault_tolerant = { type = "boolean", required = true, default = true }, },
          { redis_host = typedefs.host },
          { redis_port = typedefs.port({ default = 6379 }), },
          { redis_password = { type = "string", len_min = 0 }, },
{'fraction_non_alphanumeric': 0.13301728945900726, 'fraction_numerical': 0.008923591745677636, 'mean_word_length': 2.0398305084745765, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4883025', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1120, 'n_words': 292}