-
Notifications
You must be signed in to change notification settings - Fork 9
feat: enable for native api entrypoint connect phase #103
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
Draft
mukul-tyagi08
wants to merge
1
commit into
alpha
Choose a base branch
from
APIM-12433-enable-for-native-api
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,14 +19,18 @@ | |||||
| import static java.util.stream.Collectors.toList; | ||||||
|
|
||||||
| import io.gravitee.common.http.HttpStatusCode; | ||||||
| import io.gravitee.el.TemplateEngine; | ||||||
| import io.gravitee.gateway.api.ExecutionContext; | ||||||
| import io.gravitee.gateway.api.Request; | ||||||
| import io.gravitee.gateway.api.http.HttpHeaderNames; | ||||||
| import io.gravitee.gateway.reactive.api.exception.InterruptConnectionException; | ||||||
| import io.gravitee.gateway.reactive.api.policy.kafka.KafkaPolicy; | ||||||
| import io.gravitee.policy.api.PolicyChain; | ||||||
| import io.gravitee.policy.api.PolicyResult; | ||||||
| import io.gravitee.policy.api.annotations.OnRequest; | ||||||
| import io.netty.handler.ipfilter.IpFilterRuleType; | ||||||
| import io.netty.handler.ipfilter.IpSubnetFilterRule; | ||||||
| import io.reactivex.rxjava3.core.Completable; | ||||||
| import io.vertx.core.CompositeFuture; | ||||||
| import io.vertx.core.Future; | ||||||
| import io.vertx.core.Promise; | ||||||
|
|
@@ -47,7 +51,7 @@ | |||||
| * @author Azize ELAMRANI (azize.elamrani at graviteesource.com) | ||||||
| * @author GraviteeSource Team | ||||||
| */ | ||||||
| public class IPFilteringPolicy { | ||||||
| public class IPFilteringPolicy implements KafkaPolicy { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| private static final Logger LOGGER = LoggerFactory.getLogger(IPFilteringPolicy.class); | ||||||
|
|
||||||
|
|
@@ -62,13 +66,18 @@ public IPFilteringPolicy(IPFilteringPolicyConfiguration configuration) { | |||||
| this.configuration = configuration; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public String id() { | ||||||
| return "ip-filtering"; | ||||||
| } | ||||||
|
|
||||||
| @OnRequest | ||||||
| public void onRequest(ExecutionContext executionContext, PolicyChain policyChain) { | ||||||
| final List<String> ips = extractIps(executionContext); | ||||||
| final List<Future> futures = new ArrayList<>(); | ||||||
|
|
||||||
| var blackList = computeList(executionContext, configuration.getBlacklistIps()); | ||||||
| var whiteList = computeList(executionContext, configuration.getWhitelistIps()); | ||||||
| var blackList = computeList(executionContext.getTemplateEngine(), configuration.getBlacklistIps()); | ||||||
| var whiteList = computeList(executionContext.getTemplateEngine(), configuration.getWhitelistIps()); | ||||||
|
|
||||||
| if (!blackList.isEmpty()) { | ||||||
| final List<String> filteredIps = new ArrayList<>(); | ||||||
|
|
@@ -113,6 +122,84 @@ public void onRequest(ExecutionContext executionContext, PolicyChain policyChain | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Executes IP filtering during the ENTRYPOINT_CONNECT phase for Native Kafka APIs. | ||||||
| * This happens BEFORE authentication, allowing early rejection of unauthorized IPs. | ||||||
| * | ||||||
| * Note: DNS lookups for hostnames are NOT supported in this phase for performance reasons. | ||||||
| * Only static IPs and CIDR ranges are evaluated. Use IPs instead of hostnames for ENTRYPOINT_CONNECT phase. | ||||||
| * | ||||||
| * @param ctx the entrypoint connect context | ||||||
| * @return a Completable that completes successfully if the IP is allowed, or throws InterruptConnectionException if denied | ||||||
| */ | ||||||
| @Override | ||||||
| public Completable onEntrypointConnect(io.gravitee.gateway.reactive.api.context.EntrypointConnectContext ctx) { | ||||||
| return Completable.defer(() -> { | ||||||
| try { | ||||||
| final String remoteAddress = extractIpFromNativeContext(ctx); | ||||||
|
|
||||||
| final Set<String> blacklistIps = computeList(ctx.getTemplateEngine(), configuration.getBlacklistIps()); | ||||||
| final Set<String> whitelistIps = computeList(ctx.getTemplateEngine(), configuration.getWhitelistIps()); | ||||||
|
|
||||||
| final List<String> blacklistFilteredIps = new ArrayList<>(); | ||||||
| final List<String> blacklistFilteredHosts = new ArrayList<>(); | ||||||
| processFilteredLists(blacklistIps, blacklistFilteredIps, blacklistFilteredHosts); | ||||||
|
|
||||||
| final List<String> whitelistFilteredIps = new ArrayList<>(); | ||||||
| final List<String> whitelistFilteredHosts = new ArrayList<>(); | ||||||
| processFilteredLists(whitelistIps, whitelistFilteredIps, whitelistFilteredHosts); | ||||||
|
|
||||||
| // Log warning if hostnames are configured (not supported in ENTRYPOINT_CONNECT) | ||||||
| if (!blacklistFilteredHosts.isEmpty() || !whitelistFilteredHosts.isEmpty()) { | ||||||
| LOGGER.warn( | ||||||
| "[IP Filtering] Hostnames in whitelist/blacklist are not supported in ENTRYPOINT_CONNECT phase. " + | ||||||
| "Only IP addresses and CIDR ranges will be evaluated. Configured hostnames will be ignored: blacklist={}, whitelist={}", | ||||||
| blacklistFilteredHosts, | ||||||
| whitelistFilteredHosts | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| if (isFiltered(remoteAddress, blacklistFilteredIps)) { | ||||||
| String reason = "IP " + remoteAddress + " is blacklisted and not allowed to connect"; | ||||||
| ctx.interrupt(reason); | ||||||
| return Completable.error(new InterruptConnectionException(reason)); | ||||||
| } | ||||||
|
|
||||||
| if (!whitelistFilteredIps.isEmpty()) { | ||||||
| boolean matched = isFiltered(remoteAddress, whitelistFilteredIps); | ||||||
| if (!matched) { | ||||||
| String reason = "IP " + remoteAddress + " is not whitelisted"; | ||||||
| ctx.interrupt(reason); | ||||||
| return Completable.error(new InterruptConnectionException(reason)); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return Completable.complete(); | ||||||
| } catch (InterruptConnectionException e) { | ||||||
| return Completable.error(e); | ||||||
| } catch (Exception e) { | ||||||
| LOGGER.error("[IP Filtering] Error during ENTRYPOINT_CONNECT phase", e); | ||||||
| return Completable.error(e); | ||||||
| } | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| private String extractIpFromNativeContext(io.gravitee.gateway.reactive.api.context.EntrypointConnectContext ctx) { | ||||||
| if (configuration.isUseCustomIPAddress()) { | ||||||
| String customIPAddress = ctx.getTemplateEngine().getValue(configuration.getCustomIPAddress(), String.class); | ||||||
| if (customIPAddress == null || customIPAddress.trim().isEmpty()) { | ||||||
| return ctx.remoteAddress(); | ||||||
| } | ||||||
| return Arrays | ||||||
| .stream(customIPAddress.split(",")) | ||||||
| .map(String::trim) | ||||||
| .filter(ip -> !ip.isEmpty()) | ||||||
| .findFirst() // Take first IP if comma-separated (Native Kafka = 1 connection = 1 source IP) | ||||||
| .orElse(ctx.remoteAddress()); | ||||||
| } | ||||||
| return ctx.remoteAddress(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @param filteredHosts A list of hosts that should be blocked | ||||||
| * @param futures | ||||||
|
|
@@ -276,13 +363,13 @@ private static boolean isIPv4(String ip) throws UnknownHostException { | |||||
| } | ||||||
|
|
||||||
| @SuppressWarnings({ "removal" }) | ||||||
| private static Set<String> computeList(ExecutionContext ctx, List<String> givenList) { | ||||||
| private static Set<String> computeList(TemplateEngine templateEngine, List<String> givenList) { | ||||||
| if (givenList == null) { | ||||||
| return Set.of(); | ||||||
| } | ||||||
| return givenList | ||||||
| .stream() | ||||||
| .map(given -> ctx.getTemplateEngine().getValue(given, String.class)) | ||||||
| .map(given -> templateEngine.getValue(given, String.class)) | ||||||
| .map(k -> k != null && !k.isEmpty() ? k.split(",") : new String[] {}) | ||||||
| .flatMap(Arrays::stream) | ||||||
| .collect(Collectors.toSet()); | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ http_proxy=REQUEST | |
| http_message=REQUEST | ||
| mcp_proxy=REQUEST | ||
| llm_proxy=REQUEST | ||
| native_kafka=ENTRYPOINT_CONNECT | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will replace after gravitee-io/gravitee-gateway-api#321 gets merged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it's not a BC. Because I'm not sure if the policy still works for an apim 3.13.0 for example 🤔
Since this dependency is provided, won't there be an error in the NativePolicy import, in version of apim without it ?
I'm not really sure, but I have my doubts 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it is