Skip to content

Commit

Permalink
change: don't expose public API by default & remove plugin interceptor (
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Jan 27, 2022
1 parent 615ee41 commit 427e926
Show file tree
Hide file tree
Showing 24 changed files with 703 additions and 1,009 deletions.
10 changes: 0 additions & 10 deletions apisix/admin/plugin_metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local error = error
local pcall = pcall
local require = require
local core = require("apisix.core")
local utils = require("apisix.admin.utils")
local api_router = require("apisix.api_router")

local injected_mark = "injected metadata_schema"
local _M = {
Expand Down Expand Up @@ -61,14 +59,6 @@ local function check_conf(plugin_name, conf)
end
local schema = plugin_object.metadata_schema

-- inject interceptors schema to each plugins
if schema.properties.interceptors
and api_router.interceptors_schema['$comment'] ~= schema.properties.interceptors['$comment']
then
error("'interceptors' can not be used as the name of metadata schema's field")
end
schema.properties.interceptors = api_router.interceptors_schema

core.log.info("schema: ", core.json.delay_encode(schema))
core.log.info("conf: ", core.json.delay_encode(conf))

Expand Down
77 changes: 4 additions & 73 deletions apisix/api_router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
--
local require = require
local router = require("apisix.utils.router")
local apisix_router = require("apisix.router")
local plugin_mod = require("apisix.plugin")
local ip_restriction = require("apisix.plugins.ip-restriction")
local core = require("apisix.core")
local ipairs = ipairs
local type = type
Expand All @@ -27,50 +25,6 @@ local type = type
local _M = {}
local match_opts = {}
local has_route_not_under_apisix
local interceptors = {
["ip-restriction"] = {
run = function (conf, ctx)
return ip_restriction.access(conf, ctx)
end,
schema = ip_restriction.schema,
}
}


_M.interceptors_schema = {
["$comment"] = "this is the mark for our interceptors schema",
type = "array",
items = {
type = "object",
minItems = 1,
properties = {
name = {
type = "string",
enum = {},
},
conf = {
type = "object",
}
},
required = {"name", "conf"},
dependencies = {
name = {
oneOf = {}
}
}
}
}
for name, attrs in pairs(interceptors) do
core.table.insert(_M.interceptors_schema.items.properties.name.enum, name)
core.table.insert(_M.interceptors_schema.items.dependencies.name.oneOf, {
properties = {
name = {
enum = {name},
},
conf = attrs.schema,
}
})
end


local fetch_api_router
Expand All @@ -84,7 +38,6 @@ function fetch_api_router()
for _, plugin in ipairs(plugin_mod.plugins) do
local api_fun = plugin.api
if api_fun then
local name = plugin.name
local api_routes = api_fun()
core.log.debug("fetched api routes: ",
core.json.delay_encode(api_routes, true))
Expand All @@ -108,30 +61,8 @@ function fetch_api_router()
core.table.insert(routes, {
methods = route.methods,
paths = route.uri,
handler = function (api_ctx, skip_global_rule)
local code, body

local metadata = plugin_mod.plugin_metadata(name)
if metadata and metadata.value.interceptors then
for _, rule in ipairs(metadata.value.interceptors) do
local f = interceptors[rule.name]
if f == nil then
core.log.error("unknown interceptor: ", rule.name)
else
code, body = f.run(rule.conf, api_ctx)
if code or body then
return core.response.exit(code, body)
end
end
end
end

if not skip_global_rule then
plugin_mod.run_global_rules(api_ctx,
apisix_router.global_rules, nil)
end

code, body = route.handler(api_ctx)
handler = function (api_ctx)
local code, body = route.handler(api_ctx)
if code or body then
core.response.exit(code, body)
end
Expand All @@ -156,7 +87,7 @@ function _M.has_route_not_under_apisix()
end


function _M.match(api_ctx, skip_global_rule)
function _M.match(api_ctx)
local api_router = core.lrucache.global("api_router", plugin_mod.load_times, fetch_api_router)
if not api_router then
core.log.error("failed to fetch valid api router")
Expand All @@ -166,7 +97,7 @@ function _M.match(api_ctx, skip_global_rule)
core.table.clear(match_opts)
match_opts.method = api_ctx.var.request_method

local ok = api_router:dispatch(api_ctx.var.uri, match_opts, api_ctx, skip_global_rule)
local ok = api_router:dispatch(api_ctx.var.uri, match_opts, api_ctx)
return ok
end

Expand Down
12 changes: 0 additions & 12 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,6 @@ function _M.http_access_phase()

local route = api_ctx.matched_route
if not route then
-- whether the public API run global rules is
-- controlled by the configuration file
if router.api.has_route_not_under_apisix() or
core.string.has_prefix(uri, "/apisix/")
then
local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api
local matched = router.api.match(api_ctx, skip)
if matched then
return
end
end

-- run global rule when there is no matching route
plugin.run_global_rules(api_ctx, router.global_rules, nil)

Expand Down
5 changes: 1 addition & 4 deletions apisix/plugins/public-api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ end


function _M.access(conf, ctx)
local local_conf = core.config.local_conf()

-- overwrite the uri in the ctx when the user has set the target uri
ctx.var.uri = conf.uri or ctx.var.uri
local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api

-- perform route matching
if router.api.match(ctx, skip) then
if router.api.match(ctx) then
return
end

Expand Down
Loading

0 comments on commit 427e926

Please sign in to comment.