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 geoblock tower middleware #4

Merged
merged 7 commits into from
Sep 26, 2023
Merged

feat: add geoblock tower middleware #4

merged 7 commits into from
Sep 26, 2023

Conversation

xav
Copy link
Contributor

@xav xav commented Sep 25, 2023

Description

Add Tower middleware for blocking requests based on clients' IP origin.

How Has This Been Tested?

example

Due Diligence

  • Breaking change
  • Requires a documentation update
  • Requires a e2e/integration test update

Copy link
Collaborator

@heilhead heilhead left a comment

Choose a reason for hiding this comment

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

Looks good, but I would change Vec -> HashSet and preformat the blocked country list to match the format of the returned country ISO code (upper- or lower-case).

Another thing, which is a bit out of scope of this PR: I think it would be nice if this struct returned borrowed strings (as the maxmind/geoip2 API returns them), and leave it up to the consumer whether to convert them to owned strings. Otherwise we're cloning them for no good reason, since we only need to check if there's a match.

crates/geoblock/Cargo.toml Outdated Show resolved Hide resolved
crates/geoblock/Cargo.toml Outdated Show resolved Hide resolved
crates/geoblock/src/errors.rs Outdated Show resolved Hide resolved
crates/geoblock/src/lib.rs Outdated Show resolved Hide resolved
crates/geoblock/src/lib.rs Show resolved Hide resolved
crates/geoblock/src/lib.rs Outdated Show resolved Hide resolved
if IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)) == caller {
GeoData {
continent: Some("Asia".to_string().into()),
country: Some("Derkaderkastan".to_string().into()),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hold on, this seems incorrect. The data returned by the actual API would be an ISO code. Can you come up with an example that resolves an actual IP to an actual GeoData?

Copy link
Member

@xDarksome xDarksome left a comment

Choose a reason for hiding this comment

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

Also, I think we should return 500 for all error cases, except "BlockedCountry" (which is 401)

crates/geoblock/Cargo.toml Outdated Show resolved Hide resolved
crates/geoblock/src/lib.rs Outdated Show resolved Hide resolved
crates/geoblock/src/lib.rs Show resolved Hide resolved
crates/geoblock/src/errors.rs Outdated Show resolved Hide resolved
crates/geoblock/src/lib.rs Outdated Show resolved Hide resolved
crates/geoblock/src/lib.rs Show resolved Hide resolved
@xav xav requested review from xDarksome and heilhead September 25, 2023 14:41
crates/geoblock/src/lib.rs Outdated Show resolved Hide resolved
crates/geoblock/src/lib.rs Outdated Show resolved Hide resolved
crates/geoblock/src/lib.rs Show resolved Hide resolved
crates/geoblock/src/lib.rs Show resolved Hide resolved
@xDarksome
Copy link
Member

Btw, there are clippy warnings in tests

@heilhead
Copy link
Collaborator

@xDarksome For some reason can't reply to your comment, but I agree that the error is too detailed. Though it's not that big of a deal, since these errors should be rather rare.

@xav xav requested review from heilhead and xDarksome September 25, 2023 22:23
Copy link
Member

@xDarksome xDarksome left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

There are still some clippy warnings in the tests though

Copy link
Collaborator

@heilhead heilhead left a comment

Choose a reason for hiding this comment

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

Looks good, though I think we should update the tests too to use country codes.

@xav xav merged commit 03ccdfd into main Sep 26, 2023
@xav xav deleted the feat/geoblock branch September 26, 2023 11:18
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.

3 participants