Skip to content

Commit

Permalink
wip - plugins schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito committed Sep 20, 2018
1 parent 30ea9c0 commit 8cfd127
Show file tree
Hide file tree
Showing 31 changed files with 836 additions and 1,101 deletions.
10 changes: 2 additions & 8 deletions kong/db/schema/entities/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ local positive_int_or_zero = Schema.define {
}


local header_name = Schema.define {
type = "string",
custom_validator = utils.validate_header_name,
}


local healthchecks_defaults = {
active = {
timeout = 1,
Expand Down Expand Up @@ -130,8 +124,8 @@ local r = {
{ name = { type = "string", required = true, unique = true, custom_validator = validate_name }, },
{ hash_on = hash_on },
{ hash_fallback = hash_on },
{ hash_on_header = header_name, },
{ hash_fallback_header = header_name, },
{ hash_on_header = typedefs.header_name, },
{ hash_fallback_header = typedefs.header_name, },
{ hash_on_cookie = { type = "string", custom_validator = utils.validate_cookie_name }, },
{ hash_on_cookie_path = typedefs.path{ default = "/", }, },
{ slots = { type = "integer", default = 10000, between = { 10, 2^16 }, }, },
Expand Down
30 changes: 30 additions & 0 deletions kong/db/schema/typedefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-- @module kong.db.schema.typedefs
local utils = require "kong.tools.utils"
local Schema = require("kong.db.schema")
local socket_url = require("socket.url")


local match = string.match
Expand Down Expand Up @@ -57,6 +58,25 @@ local function validate_name(name)
end


local function validate_url(url)
local parsed_url, err = socket_url.parse(url)

if not parsed_url then
return nil, "could not parse url. " .. err
end

if not parsed_url.host then
return nil, "missing host in url"
end

if not parsed_url.scheme then
return nil, "missing scheme in url"
end

return true
end


local typedefs = {}


Expand Down Expand Up @@ -98,6 +118,16 @@ typedefs.path = Schema.define {
custom_validator = validate_path,
}

typedefs.url = Schema.define {
type = "string",
custom_validator = validate_url,
}

typedefs.header_name = Schema.define {
type = "string",
custom_validator = utils.validate_header_name,
}


typedefs.timeout = Schema.define {
type = "integer",
Expand Down
31 changes: 18 additions & 13 deletions kong/plugins/acl/schema.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
local Errors = require "kong.dao.errors"
local typedefs = require "kong.db.schema.typedefs"


return {
no_consumer = true,
name = "acl",
fields = {
whitelist = { type = "array" },
blacklist = { type = "array" },
hide_groups_header = { type = "boolean", default = false },
{ consumer = typedefs.no_consumer },
{ config = {
type = "record",
nullable = false,
fields = {
{ whitelist = { type = "array", elements = { type = "string" }, }, },
{ blacklist = { type = "array", elements = { type = "string" }, }, },
{ hide_groups_header = { type = "boolean", default = false }, },
}
}
}
},
entity_checks = {
{ only_one_of = { "config.whitelist", "config.blacklist" }, },
{ at_least_one_of = { "config.whitelist", "config.blacklist" }, },
},
self_check = function(schema, plugin_t, dao, is_update)
if next(plugin_t.whitelist or {}) and next(plugin_t.blacklist or {}) then
return false, Errors.schema "You cannot set both a whitelist and a blacklist"
elseif not (next(plugin_t.whitelist or {}) or next(plugin_t.blacklist or {})) then
return false, Errors.schema "You must set at least a whitelist or blacklist"
end
return true
end
}
138 changes: 43 additions & 95 deletions kong/plugins/aws-lambda/schema.lua
Original file line number Diff line number Diff line change
@@ -1,101 +1,49 @@
local function check_status(status)
if status and (status < 100 or status > 999) then
return false, "unhandled_status must be within 100 - 999."
end
local REGIONS = {
"ap-northeast-1", "ap-northeast-2",
"ap-south-1",
"ap-southeast-1", "ap-southeast-2",
"ca-central-1",
"eu-central-1",
"eu-west-1", "eu-west-2",
"sa-east-1",
"us-east-1", "us-east-2",
"us-gov-west-1",
"us-west-1", "us-west-2",
}

return true
end

return {
name = "aws-lambda",
fields = {
timeout = {
type = "number",
default = 60000,
required = true,
},
keepalive = {
type = "number",
default = 60000,
required = true,
},
aws_key = {
type = "string",
required = true,
},
aws_secret = {
type = "string",
required = true,
},
aws_region = {
type = "string",
required = true,
enum = {
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
"us-gov-west-1",
"ap-northeast-1",
"ap-northeast-2",
"ap-southeast-1",
"ap-southeast-2",
"ap-south-1",
"ca-central-1",
"eu-central-1",
"eu-west-1",
"eu-west-2",
"sa-east-1",
},
},
function_name = {
type= "string",
required = true,
},
qualifier = {
type = "string",
},
invocation_type = {
type = "string",
required = true,
default = "RequestResponse",
enum = {
"RequestResponse",
"Event",
"DryRun",
}
},
log_type = {
type = "string",
required = true,
default = "Tail",
enum = {
"Tail",
"None",
}
},
port = {
type = "number",
default = 443,
},
unhandled_status = {
type = "number",
func = check_status,
},
forward_request_method = {
type = "boolean",
default = false,
},
forward_request_uri = {
type = "boolean",
default = false,
},
forward_request_headers = {
type = "boolean",
default = false,
},
forward_request_body = {
type = "boolean",
default = false,
},
{ config = {
type = "record",
nullable = false,
fields = {
{ timeout = { type = "number", required = true, default = 60000 }, },
{ keepalive = { type = "number", required = true, default = 60000 }, },
{ aws_key = { type = "string", required = true }, },
{ aws_secret = { type = "string", required = true }, },
{ aws_region = { type = "string", required = true, one_of = REGIONS }, },
{ function_name = { type= "string", required = true }, },
{ qualifier = { type = "string" }, },
{ invocation_type = {
type = "string",
required = true,
default = "RequestResponse",
one_of = { "RequestResponse", "Event", "DryRun" },
}, },
{ log_type = {
type = "string",
required = true,
default = "Tail",
one_of = { "Tail", "None" },
}, },
{ port = { type = "integer", default = 443 }, },
{ unhandled_status = { type = "integer", between = { 100, 999 }, }, },
{ forward_request_method = { type = "boolean", default = false }, },
{ forward_request_uri = { type = "boolean", default = false }, },
{ forward_request_headers = { type = "boolean", default = false }, },
{ forward_request_body = { type = "boolean", default = false }, },
}, }, },
},
}
24 changes: 11 additions & 13 deletions kong/plugins/basic-auth/schema.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
local utils = require "kong.tools.utils"

local function check_user(anonymous)
if anonymous == "" or utils.is_valid_uuid(anonymous) then
return true
end

return false, "the anonymous user must be empty or a valid uuid"
end
local typedefs = require "kong.db.schema.typedefs"

return {
no_consumer = true,
name = "basic-auth",
fields = {
anonymous = {type = "string", default = "", func = check_user},
hide_credentials = {type = "boolean", default = false}
}
{ consumer = typedefs.no_consumer },
{ config = {
type = "record",
nullable = false,
fields = {
{ anonymous = { type = "string", uuid = true, len_min = 0, default = "" }, },
{ hide_credentials = { type = "boolean", default = false }, },
}, }, },
},
}
59 changes: 19 additions & 40 deletions kong/plugins/bot-detection/schema.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
local re_match = ngx.re.match

local check_regex = function(value)
if value then
for _, rule in ipairs(value) do
local _, err = re_match("just a string to test", rule)
if err then
return false, "value '" .. rule .. "' is not a valid regex"
end
end
end
return true
end
local typedefs = require "kong.db.schema.typedefs"

return {
no_consumer = true,
name = "bot-detection",
fields = {
whitelist = {
type = "array",
func = check_regex,
new_type = {
type = "array",
elements = {
type = "string",
match = ".*",
is_regex = true,
},
default = {},
}
},
blacklist = {
type = "array",
func = check_regex,
new_type = {
type = "array",
elements = {
type = "string",
is_regex = true,
},
default = {},
}
},
}
{ consumer = typedefs.no_consumer },
{ config = {
type = "record",
nullable = false,
fields = {
{ whitelist = {
type = "array",
elements = { type = "string", is_regex = true, match = ".*" },
default = {},
}, },
{ blacklist = {
type = "array",
elements = { type = "string", is_regex = true },
default = {},
}, },
}, }, },
},
}
24 changes: 11 additions & 13 deletions kong/plugins/correlation-id/schema.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
return {
name = "correlation-id",
fields = {
header_name = {
type = "string",
default = "Kong-Request-ID"
config = {
type = "record",
nullable = false,
fields = {
{ header_name = { type = "string", default = "Kong-Request-ID" }, },
{ generator = { type = "string", default = "uuid#counter",
one_of = { "uuid", "uuid#counter", "tracker" }, }, },
{ echo_downstream = { type = "boolean", default = false, }, },
},
},
generator = {
type = "string",
default = "uuid#counter",
enum = {"uuid", "uuid#counter", "tracker"}
},
echo_downstream = {
type = "boolean",
default = false
}
}
},
}
Loading

0 comments on commit 8cfd127

Please sign in to comment.