Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add workflow plugin(mvp) #7760

Merged
merged 6 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions apisix/plugins/workflow.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local core = require("apisix.core")
local expr = require("resty.expr.v1")
local ipairs = ipairs
local tonumber = tonumber

local schema = {
type = "object",
properties = {
rules = {
type = "array",
items = {
type = "object",
properties = {
case = {
type = "array",
items = {
type = "array",
},
minItems = 1,
},
actions = {
type = "array",
items = {
type = "array",
minItems = 1
}
}
},
required = {"case", "actions"}
}
}
}
}

local plugin_name = "workflow"

local _M = {
version = 0.1,
priority = 1006,
name = plugin_name,
schema = schema
}


local return_schema = {
type = "object",
properties = {
code = {
type = "integer",
minimum = 100,
maximum = 599
}
},
required = {"code"}
}


local function exit(conf)
local code = tonumber(conf.code)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is tonumber necessary after the schema check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need, fix it on next PR.

return code, {error_msg = "rejected by workflow"}
end


local support_action = {
["return"] = {
handler = exit,
schema = return_schema,
},
}


function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end

for _, rule in ipairs(conf.rules) do
local ok, err = expr.new(rule.case)
if not ok then
return false, "failed to validate the 'case' expression: " .. err
end

local actions = rule.actions
for _, action in ipairs(actions) do

if not support_action[action[1]] then
return false, "unsupported action: " .. action[1]
end

local ok, err = core.schema.check(support_action[action[1]].schema, action[2])
if not ok then
return false, "failed to validate the '" .. action[1] .. "' action: " .. err
end
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
end
end

return true
end


function _M.access(conf, ctx)
local match_result
for _, rule in ipairs(conf.rules) do
local expr, _ = expr.new(rule.case)
match_result = expr:eval(ctx.var)
if match_result then
-- only one action is currently supported
local action = rule.actions[1]
return support_action[action[1]].handler(action[2])
end
end
end


return _M
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ plugins: # plugin list (sorted by priority)
- proxy-mirror # priority: 1010
- proxy-cache # priority: 1009
- proxy-rewrite # priority: 1008
- workflow # priority: 1006
- api-breaker # priority: 1005
- limit-conn # priority: 1003
- limit-count # priority: 1002
Expand Down
1 change: 1 addition & 0 deletions t/admin/plugins.t
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ authz-keycloak
proxy-mirror
proxy-cache
proxy-rewrite
workflow
api-breaker
limit-conn
limit-count
Expand Down
Loading