-
Notifications
You must be signed in to change notification settings - Fork 7
traffic calculator with hardcoded baseline #251
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
Merged
Ian-Nara
merged 38 commits into
ian-UID2-6337-asynchronous-full-queue-process
from
ian-UID2-6146-update-traffic-calculator-to-hardcoded-baseline
Dec 6, 2025
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
8391dd1
add traffic calculator
Ian-Nara 2f426ad
update from review
Ian-Nara 7d18fbd
add unit tests
Ian-Nara 726feda
allow custom eval window
Ian-Nara 1d8f050
update comment
Ian-Nara 4bc5e45
switch to configmap for traffic config
Ian-Nara 72a99d0
update to all k8s
Ian-Nara 027f576
update config validations
Ian-Nara 58bd298
small rename
Ian-Nara b3cbdfd
test fix
Ian-Nara a802e31
undo accidental change
Ian-Nara e26a64f
whitespace
Ian-Nara fd13b69
whitespace
Ian-Nara 14a6c1f
update traffic baseline to hardcoded
Ian-Nara 843f41f
naming improvements
Ian-Nara 1477e2f
naming improvements
Ian-Nara d75bc0d
naming improvements
Ian-Nara d7db764
small comment/name update
Ian-Nara a178540
naming update
Ian-Nara df7ab91
naming update
Ian-Nara 38c86b3
add newest delta file logic
Ian-Nara 160c5de
Merge branch 'ian-UID2-6337-asynchronous-full-queue-process' into ian…
Ian-Nara 962b7cc
update comments
Ian-Nara 16dfe53
add traffic filter
Ian-Nara b520a7a
Merge branch 'ian-UID2-6337-asynchronous-full-queue-process' into ian…
Ian-Nara b7116f9
Merge branch 'ian-UID2-6337-asynchronous-full-queue-process' into ian…
Ian-Nara 6f4cf76
merge
Ian-Nara 458d70a
Merge branch 'ian-UID2-6146-update-traffic-calculator-to-hardcoded-ba…
Ian-Nara f6b7f46
update traffic calculator
Ian-Nara a93b051
address comments
Ian-Nara 019f426
undo accidental commit
Ian-Nara 0265ae2
Merge branch 'ian-UID2-6337-asynchronous-full-queue-process' into ian…
Ian-Nara f671dd4
Merge branch 'ian-UID2-6146-update-traffic-calculator-to-hardcoded-ba…
Ian-Nara cabdce5
Temporarily Suppress libpng CVE-2025-64720 and CVE-2025-65018 (#255)
caroline-ttd 47f63b0
Merge branch 'main' into ian-UID2-6337-asynchronous-full-queue-process
Ian-Nara 213eb5f
Merge branch 'ian-UID2-6337-asynchronous-full-queue-process' into ian…
Ian-Nara 64f1e61
Merge branch 'ian-UID2-6146-update-traffic-calculator-to-hardcoded-ba…
Ian-Nara 8531ec5
Merge pull request #253 from IABTechLab/ian-UID2-6151-add-traffic-fil…
Ian-Nara 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
Some comments aren't visible on the classic Files Changed page.
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
167 changes: 167 additions & 0 deletions
167
src/main/java/com/uid2/optout/vertx/OptOutTrafficFilter.java
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 |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package com.uid2.optout.vertx; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Collections; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.nio.charset.StandardCharsets; | ||
| import io.vertx.core.json.JsonObject; | ||
| import io.vertx.core.json.JsonArray; | ||
|
|
||
| public class OptOutTrafficFilter { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(OptOutTrafficFilter.class); | ||
|
|
||
| private final String trafficFilterConfigPath; | ||
| List<TrafficFilterRule> filterRules; | ||
|
|
||
| /** | ||
| * Traffic filter rule defining a time range and a list of IP addresses to exclude | ||
| */ | ||
| private static class TrafficFilterRule { | ||
| private final List<Long> range; | ||
| private final List<String> ipAddresses; | ||
|
|
||
| TrafficFilterRule(List<Long> range, List<String> ipAddresses) { | ||
| this.range = range; | ||
| this.ipAddresses = ipAddresses; | ||
| } | ||
|
|
||
| public long getRangeStart() { | ||
| return range.get(0); | ||
| } | ||
| public long getRangeEnd() { | ||
| return range.get(1); | ||
| } | ||
| public List<String> getIpAddresses() { | ||
| return ipAddresses; | ||
| } | ||
| } | ||
|
|
||
| public static class MalformedTrafficFilterConfigException extends Exception { | ||
| public MalformedTrafficFilterConfigException(String message) { | ||
| super(message); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Constructor for OptOutTrafficFilter | ||
| * | ||
| * @param trafficFilterConfigPath S3 path for traffic filter config | ||
| * @throws MalformedTrafficFilterConfigException if the traffic filter config is invalid | ||
| */ | ||
| public OptOutTrafficFilter(String trafficFilterConfigPath) throws MalformedTrafficFilterConfigException { | ||
| this.trafficFilterConfigPath = trafficFilterConfigPath; | ||
| // Initial filter rules load | ||
| this.filterRules = Collections.emptyList(); // start empty | ||
| reloadTrafficFilterConfig(); // load ConfigMap | ||
|
|
||
| LOGGER.info("OptOutTrafficFilter initialized: filterRules={}", | ||
| filterRules.size()); | ||
| } | ||
|
|
||
| /** | ||
| * Reload traffic filter config from ConfigMap. | ||
| * Expected format: | ||
| * { | ||
| * "blacklist_requests": [ | ||
| * {range: [startTimestamp, endTimestamp], IPs: ["ip1"]}, | ||
| * {range: [startTimestamp, endTimestamp], IPs: ["ip1", "ip2"]}, | ||
| * {range: [startTimestamp, endTimestamp], IPs: ["ip1", "ip3"]}, | ||
| * ] | ||
| * } | ||
| * | ||
| * Can be called periodically to pick up config changes without restarting. | ||
| */ | ||
| public void reloadTrafficFilterConfig() throws MalformedTrafficFilterConfigException { | ||
| LOGGER.info("Loading traffic filter config from ConfigMap"); | ||
| try (InputStream is = Files.newInputStream(Paths.get(trafficFilterConfigPath))) { | ||
| String content = new String(is.readAllBytes(), StandardCharsets.UTF_8); | ||
| JsonObject filterConfigJson = new JsonObject(content); | ||
|
|
||
| this.filterRules = parseFilterRules(filterConfigJson); | ||
|
|
||
| LOGGER.info("Successfully loaded traffic filter config from ConfigMap: filterRules={}", | ||
| filterRules.size()); | ||
|
|
||
| } catch (Exception e) { | ||
| LOGGER.warn("No traffic filter config found at: {}", trafficFilterConfigPath, e); | ||
| throw new MalformedTrafficFilterConfigException(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parse request filtering rules from JSON config | ||
| */ | ||
| List<TrafficFilterRule> parseFilterRules(JsonObject config) throws MalformedTrafficFilterConfigException { | ||
| List<TrafficFilterRule> rules = new ArrayList<>(); | ||
| try { | ||
| JsonArray blacklistRequests = config.getJsonArray("blacklist_requests"); | ||
| if (blacklistRequests == null) { | ||
| LOGGER.error("Invalid traffic filter config: blacklist_requests is null"); | ||
| throw new MalformedTrafficFilterConfigException("Invalid traffic filter config: blacklist_requests is null"); | ||
| } | ||
| for (int i = 0; i < blacklistRequests.size(); i++) { | ||
| JsonObject ruleJson = blacklistRequests.getJsonObject(i); | ||
|
|
||
| // parse range | ||
| var rangeJson = ruleJson.getJsonArray("range"); | ||
| List<Long> range = new ArrayList<>(); | ||
| if (rangeJson != null && rangeJson.size() == 2) { | ||
| long start = rangeJson.getLong(0); | ||
| long end = rangeJson.getLong(1); | ||
|
|
||
| if (start >= end) { | ||
| LOGGER.error("Invalid traffic filter rule: range start must be less than end: {}", ruleJson.encode()); | ||
| throw new MalformedTrafficFilterConfigException("Invalid traffic filter rule: range start must be less than end"); | ||
| } | ||
| range.add(start); | ||
| range.add(end); | ||
| } | ||
|
|
||
| // parse IPs | ||
| var ipAddressesJson = ruleJson.getJsonArray("IPs"); | ||
| List<String> ipAddresses = new ArrayList<>(); | ||
| if (ipAddressesJson != null) { | ||
| for (int j = 0; j < ipAddressesJson.size(); j++) { | ||
| ipAddresses.add(ipAddressesJson.getString(j)); | ||
| } | ||
| } | ||
|
|
||
| // log error and throw exception if rule is invalid | ||
| if (range.size() != 2 || ipAddresses.size() == 0 || range.get(1) - range.get(0) > 86400) { // range must be 24 hours or less | ||
| LOGGER.error("Invalid traffic filter rule: {}", ruleJson.encode()); | ||
| throw new MalformedTrafficFilterConfigException("Invalid traffic filter rule"); | ||
| } | ||
|
|
||
| TrafficFilterRule rule = new TrafficFilterRule(range, ipAddresses); | ||
|
|
||
| LOGGER.info("Loaded traffic filter rule: range=[{}, {}], IPs={}", rule.getRangeStart(), rule.getRangeEnd(), rule.getIpAddresses()); | ||
| rules.add(rule); | ||
| } | ||
| return rules; | ||
| } catch (Exception e) { | ||
| LOGGER.error("Failed to parse traffic filter rules: config={}, error={}", config.encode(), e.getMessage()); | ||
| throw new MalformedTrafficFilterConfigException(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public boolean isBlacklisted(SqsParsedMessage message) { | ||
| long timestamp = message.getTimestamp(); | ||
| String clientIp = message.getClientIp(); | ||
|
|
||
| for (TrafficFilterRule rule : filterRules) { | ||
| if(timestamp >= rule.getRangeStart() && timestamp <= rule.getRangeEnd()) { | ||
| if(rule.getIpAddresses().contains(clientIp)) { | ||
| return true; | ||
| } | ||
| }; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.