Skip to content

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

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

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

Conversation

@Zheaoli

@Zheaoli Zheaoli commented Sep 3, 2021

Copy link
Copy Markdown
Member

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

Zheaoli commented Sep 4, 2021

Copy link
Copy Markdown
Member Author

#4441

@nic-chen nic-chen left a comment

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.

need test cases

Comment thread apisix/stream/router/ip_port.lua Outdated
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?

Comment thread t/stream-plugin/mqtt-proxy.t Outdated


=== 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

Comment thread rockspec/apisix-master-0.rockspec Outdated
"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

Comment thread t/stream-node/sanity.t Outdated



=== 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

Comment thread t/stream-node/sanity.t Outdated
--- 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
Comment thread apisix/stream/router/ip_port.lua Outdated

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
Comment thread apisix/stream/router/ip_port.lua Outdated

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,

Comment thread apisix/stream/router/ip_port.lua Outdated
Comment thread apisix/stream/router/ip_port.lua Outdated
Comment thread apisix/stream/router/ip_port.lua Outdated
@Zheaoli
Zheaoli force-pushed the use-ipmatcher branch 2 times, most recently from d311852 to 0720a4a Compare September 12, 2021 14:01
Comment thread t/stream-node/sanity.t



=== 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 <[email protected]>
Signed-off-by: spacewander <[email protected]>
@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