Skip to content

feat(stream_route): support CIDR in ip match#4980

Merged
spacewander merged 4 commits intoapache:masterfrom
Zheaoli:use-ipmatcher
Sep 17, 2021
Merged

feat(stream_route): support CIDR in ip match#4980
spacewander merged 4 commits intoapache:masterfrom
Zheaoli:use-ipmatcher

Conversation

@Zheaoli
Copy link
Copy Markdown
Member

@Zheaoli Zheaoli commented Sep 3, 2021

What this PR does / why we need it:

Pre-submission checklist:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

@Zheaoli Zheaoli changed the title [WIP] replace ip match tools by using ipmatcher fix[stream_route]: replace ip match tools by using ipmatcher Sep 4, 2021
@Zheaoli Zheaoli changed the title fix[stream_route]: replace ip match tools by using ipmatcher fix(stream_route): replace ip match tools by using ipmatcher Sep 4, 2021
@Zheaoli
Copy link
Copy Markdown
Member Author

Zheaoli commented Sep 4, 2021

#4441

Copy link
Copy Markdown
Member

@nic-chen nic-chen left a comment

Choose a reason for hiding this comment

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

need test cases

route.value.remote_addr ~= vars.remote_addr then
return false
if route.value.remote_addr then
local ip = remote_addr_match_cache[route.value.remote_addr]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. It's dangerous to use a module-level table to cache the match result, since it may cause the memory leak;
  2. The IP match overheads are not so high, the caching is unnecessary;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Make sense, I'll update mycode

if route.value.remote_addr and
route.value.remote_addr ~= vars.remote_addr then
return false
if route.value.remote_addr then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Better to put the init in the create_router instead of the match

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sorry I don't get your means, you mean remove the match function?



=== TEST 9: set route with invalid host
=== TEST 9: set route with IP CIDR
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

New test should be added from the bottom of the file. And it should be in stream-node/sanity.t

"ext-plugin-proto = 0.3.0",
"casbin = 1.26.0",
"api7-snowflake = 2.0-1",
"lua-resty-ipmatcher = 0.6.1"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't add lua-resty-ipmatcher twice




=== TEST 11: set stream route (id: 1) which uses upstream_id
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The name should show the difference from the others, like CIDR

--- stream_response
hello world
--- no_error_log
[error] No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Add blank line at the end

@Zheaoli Zheaoli force-pushed the use-ipmatcher branch 5 times, most recently from 8d29fc1 to b53220c Compare September 6, 2021 16:48

if not item.value.remote_addr_matcher and item.value.remote_addr then
local ip = create_matcher(item.value.remote_addr)
if ip then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can write a new checker in

checker = plugin_checker,
to ensure the matcher can be always created?

We can use the methods in https://github.com/apache/apisix/blob/master/apisix/core/ip.lua to do this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can use validate_cidr_or_ip to validate.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it's unnecessary to use validate_cidr_or_ip here, we just need to check the remote_addr and server_addr is nil or not. the ipmatcher will help to validate the address when create a new match object by using new method

@Zheaoli Zheaoli force-pushed the use-ipmatcher branch 2 times, most recently from d601ef4 to 6f27129 Compare September 8, 2021 09:33

local route = item.value

if not item.value.remote_addr_matcher and validate_address(item.value.remote_addr) then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add the check to

checker = plugin_checker,

@Zheaoli Zheaoli force-pushed the use-ipmatcher branch 2 times, most recently from d311852 to 0720a4a Compare September 12, 2021 14:01



=== TEST 11: set stream route (id: 1) which uses upstream_id and remote address with IP CIDR
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's add a test that the conf is rejected by validate_cidr_or_ip

t
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
@spacewander spacewander changed the title fix(stream_route): replace ip match tools by using ipmatcher feat(stream_route): support CIDR in ip match Sep 17, 2021
@spacewander spacewander merged commit 1a072f4 into apache:master Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants