Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: apache/apisix
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: wistefan/apisix
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 13 commits
  • 6 files changed
  • 2 contributors

Commits on Jan 15, 2025

  1. Copy the full SHA
    2ae895f View commit details
  2. Copy the full SHA
    a85ea8c View commit details
  3. fix linting issues

    wistefan committed Jan 15, 2025
    Copy the full SHA
    aa67cb5 View commit details
  4. fix tests

    wistefan committed Jan 15, 2025
    Copy the full SHA
    20525b6 View commit details
  5. linting fixes

    wistefan committed Jan 15, 2025
    Copy the full SHA
    27afbdd View commit details
  6. fix linting

    wistefan committed Jan 15, 2025
    Copy the full SHA
    ea3304a View commit details
  7. add doc

    wistefan committed Jan 15, 2025
    Copy the full SHA
    1c4619e View commit details
  8. clean

    wistefan committed Jan 15, 2025
    Copy the full SHA
    73ee9da View commit details
  9. still lintign

    wistefan committed Jan 15, 2025
    Copy the full SHA
    5e89c8f View commit details
  10. fix linter issues

    wistefan committed Jan 15, 2025
    Copy the full SHA
    be7e116 View commit details
  11. change the error call

    wistefan committed Jan 15, 2025
    Copy the full SHA
    0f5ca70 View commit details
  12. Update apisix/plugins/opa/helper.lua

    Co-authored-by: Ming Wen <moonbingbing@gmail.com>
    wistefan and moonming committed Jan 15, 2025
    Copy the full SHA
    ff110a5 View commit details
  13. Update apisix/plugins/opa/helper.lua

    Co-authored-by: Ming Wen <moonbingbing@gmail.com>
    wistefan and moonming committed Jan 15, 2025
    Copy the full SHA
    60b1efe View commit details
Showing with 154 additions and 3 deletions.
  1. +1 −0 apisix/plugins/opa.lua
  2. +28 −2 apisix/plugins/opa/helper.lua
  3. +4 −1 ci/pod/docker-compose.plugin.yml
  4. +29 −0 ci/pod/opa/with_body.rego
  5. +1 −0 docs/en/latest/plugins/opa.md
  6. +91 −0 t/plugin/opa3.t
1 change: 1 addition & 0 deletions apisix/plugins/opa.lua
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ local schema = {
with_route = {type = "boolean", default = false},
with_service = {type = "boolean", default = false},
with_consumer = {type = "boolean", default = false},
with_body = {type = "boolean", default = false}
},
required = {"host", "policy"}
}
30 changes: 28 additions & 2 deletions apisix/plugins/opa/helper.lua
Original file line number Diff line number Diff line change
@@ -34,9 +34,25 @@ local function build_var(conf, ctx)
}
end

local function get_body_for_request()
local original_body, err = core.request.get_body()
if err ~= nil then
return nil, "failed to get request body: " .. err
end
if original_body == nil then
return nil
end
-- decode to prevent double encoded json objects
local body, err = core.json.decode(original_body)
if err ~= nil then
-- if its not json, the body can just be added
body = original_body
end
return body
end

local function build_http_request(conf, ctx)
return {
local http = {
scheme = core.request.get_scheme(ctx),
method = core.request.get_method(),
host = core.request.get_host(ctx),
@@ -45,8 +61,18 @@ local function build_http_request(conf, ctx)
headers = core.request.headers(ctx),
query = core.request.get_uri_args(ctx),
}
end

if conf.with_body then
local body, err = get_body_for_request()
if err then
core.log.warn(err)
else
http.body = body
end
end

return http
end

local function build_http_route(conf, ctx, remove_upstream)
local route = core.table.deepcopy(ctx.matched_route).value
5 changes: 4 additions & 1 deletion ci/pod/docker-compose.plugin.yml
Original file line number Diff line number Diff line change
@@ -182,11 +182,14 @@ services:
restart: unless-stopped
ports:
- 8181:8181
command: run -s /example.rego /echo.rego /data.json /with_route.rego
command: run -s /example.rego /echo.rego /data.json /with_route.rego /with_body.rego
volumes:
- type: bind
source: ./ci/pod/opa/with_route.rego
target: /with_route.rego
- type: bind
source: ./ci/pod/opa/with_body.rego
target: /with_body.rego
- type: bind
source: ./ci/pod/opa/example.rego
target: /example.rego
29 changes: 29 additions & 0 deletions ci/pod/opa/with_body.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# 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.
#
package with_body

import input.request

default allow = false

allow {
request.method == "POST"
}

allow {
request.body
}
1 change: 1 addition & 0 deletions docs/en/latest/plugins/opa.md
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ The `opa` Plugin can be used to integrate with [Open Policy Agent (OPA)](https:/
| with_route | boolean | False | false | | When set to true, sends information about the current Route. |
| with_service | boolean | False | false | | When set to true, sends information about the current Service. |
| with_consumer | boolean | False | false | | When set to true, sends information about the current Consumer. Note that this may send sensitive information like the API key. Make sure to turn it on only when you are sure it is safe. |
| with_body | boolean | False | false | | When set to true, sends the request body. |

## Data definition

91 changes: 91 additions & 0 deletions t/plugin/opa3.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#
# 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.
#
use t::APISIX 'no_plan';

repeat_each(1);
no_long_string();
no_root_location();

add_block_preprocessor(sub {
my ($block) = @_;

if (!defined $block->request) {
$block->set_value("request", "GET /t");
}
});

run_tests();

__DATA__
=== TEST 1: setup route with plugin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["POST"],
"plugins": {
"opa": {
"host": "http://127.0.0.1:8181",
"policy": "with_body",
"with_body": true
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uris": ["/hello", "/test"]
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 2: hit route (with empty request)
--- request
POST /hello
--- response_body
hello world
=== TEST 3: hit route (with json request)
--- request
POST /hello
{
"hello": "world"
}
--- response_body
hello world
=== TEST 4: hit route (with non-json request)
--- request
POST /hello
hello world
--- response_body
hello world