Browse Source

fix matchers and tests

tags/v0.14.3
Jonathan Cobb 4 years ago
parent
commit
ba575af685
7 changed files with 23 additions and 9 deletions
  1. +7
    -3
      bubble-server/src/main/java/bubble/resources/stream/FilterHttpResource.java
  2. +7
    -1
      bubble-server/src/main/java/bubble/service/cloud/StandardDeviceIdService.java
  3. +1
    -0
      bubble-server/src/main/resources/models/apps/analytics/bubbleApp_analytics_matchers.json
  4. +1
    -0
      bubble-server/src/test/resources/models/apps/user_block/hn_test/bubbleApp_userBlock_hn_matchers.json
  5. +2
    -0
      bubble-server/src/test/resources/models/apps/user_block/localhost/bubbleApp_userBlock_localhost_matchers.json
  6. +4
    -4
      bubble-server/src/test/resources/models/tests/filter/traffic_analytics.json
  7. +1
    -1
      bubble-server/src/test/resources/models/tests/filter/user_block/hn_request2.json

+ 7
- 3
bubble-server/src/main/java/bubble/resources/stream/FilterHttpResource.java View File

@@ -84,12 +84,12 @@ public class FilterHttpResource {
return json(json, FilterHttpRequest.class);
}

private Map<String, Account> accountCache = new ExpirationMap<>(MINUTES.toMillis(10));
private final Map<String, Account> accountCache = new ExpirationMap<>(MINUTES.toMillis(10));
public Account findCaller(String accountUuid) {
return accountCache.computeIfAbsent(accountUuid, uuid -> accountDAO.findByUuid(uuid));
}

private Map<String, Device> deviceCache = new ExpirationMap<>(MINUTES.toMillis(10));
private final Map<String, Device> deviceCache = new ExpirationMap<>(MINUTES.toMillis(10));
public Device findDevice(String deviceUuid) {
return deviceCache.computeIfAbsent(deviceUuid, uuid -> deviceDAO.findByUuid(uuid));
}
@@ -273,7 +273,7 @@ public class FilterHttpResource {
final FilterMatchersResponse response = new FilterMatchersResponse()
.setDecision(empty(retainMatchers) ? FilterMatchDecision.no_match : FilterMatchDecision.match)
.setRequest(filterRequest)
.setMatchers(new ArrayList<>(retainMatchers.values()));
.setMatchers(empty(retainMatchers) ? Collections.emptyList() : new ArrayList<>(retainMatchers.values()));

if (log.isDebugEnabled()) log.debug(prefix+"preprocess decision for "+filterRequest.getUrl()+": "+response+", retainMatchers="+names(retainMatchers.values()));

@@ -287,6 +287,8 @@ public class FilterHttpResource {
matchers = matchers.stream()
.filter(m -> appDAO.findByAccountAndId(accountUuid, m.getApp()).enabled()).collect(Collectors.toList());
if (log.isTraceEnabled()) log.trace(prefix+"after removing disabled apps, enabled matchers for fqdn: "+json(matchers, COMPACT_MAPPER));
if (matchers.isEmpty()) return matchers;

matchers = matchers.stream()
.filter(m -> {
final AppSite site = siteDAO.findByAccountAndAppAndId(accountUuid, m.getApp(), m.getSite());
@@ -297,6 +299,8 @@ public class FilterHttpResource {
return site.enabled();
}).collect(Collectors.toList());
if (log.isTraceEnabled()) log.trace(prefix+"after removing disabled sites, enabled matchers for fqdn: "+json(matchers, COMPACT_MAPPER));
if (matchers.isEmpty()) return matchers;

matchers = matchers.stream()
.filter(m -> {
final AppRule rule = ruleDAO.findByAccountAndAppAndId(accountUuid, m.getApp(), m.getRule());


+ 7
- 1
bubble-server/src/main/java/bubble/service/cloud/StandardDeviceIdService.java View File

@@ -11,6 +11,7 @@ import bubble.model.device.DeviceStatus;
import bubble.server.BubbleConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.cobbzilla.util.collection.ExpirationMap;
import org.cobbzilla.util.collection.SingletonList;
import org.cobbzilla.util.io.FileUtil;
import org.cobbzilla.util.io.FilenamePrefixFilter;
import org.cobbzilla.wizard.cache.redis.RedisService;
@@ -94,8 +95,13 @@ public class StandardDeviceIdService implements DeviceIdService {
});
}

public static final SingletonList<String> TEST_DEVICE_IP_LIST = new SingletonList<>("127.0.0.1");

@Override public List<String> findIpsByDevice(String deviceUuid) {
if (!WG_DEVICES_DIR.exists()) throw invalidEx("err.deviceDir.notFound");
if (!WG_DEVICES_DIR.exists()) {
if (configuration.testMode()) return TEST_DEVICE_IP_LIST;
throw invalidEx("err.deviceDir.notFound");
}
final File deviceFile = new File(WG_DEVICES_DIR, DEVICE_FILE_PREFIX+deviceUuid);
if (!deviceFile.exists() || deviceFile.length() == 0) return Collections.emptyList();
try {


+ 1
- 0
bubble-server/src/main/resources/models/apps/analytics/bubbleApp_analytics_matchers.json View File

@@ -4,6 +4,7 @@
"AppMatcher": [{
"name": "TrafficAnalyticsMatcher",
"template": true,
"requestCheck": true,
"site": "All_Sites",
"fqdn": "*",
"urlRegex": ".*",


+ 1
- 0
bubble-server/src/test/resources/models/apps/user_block/hn_test/bubbleApp_userBlock_hn_matchers.json View File

@@ -5,6 +5,7 @@
"name": "HNCommentMatcher",
"site": "HackerNews",
"template": true,
"requestCheck": true,
"fqdn": "news.ycombinator.com",
"urlRegex": "/item\\?id=\\d+",
"rule": "hn_user_blocker"


+ 2
- 0
bubble-server/src/test/resources/models/apps/user_block/localhost/bubbleApp_userBlock_localhost_matchers.json View File

@@ -6,6 +6,7 @@
"name": "LocalCommentMatcher",
"site": "HackerNews",
"template": true,
"requestCheck": true,
"fqdn": "127.0.0.1",
"urlRegex": "web_mock/simple_comments.html",
"rule": "local_user_blocker"
@@ -13,6 +14,7 @@
"name": "LocalHNCommentMatcher",
"site": "HackerNews",
"template": true,
"requestCheck": true,
"fqdn": "127.0.0.1",
"urlRegex": "web_mock/news.ycombinator.com/item_id_\\d+\\.html",
"rule": "hn_user_blocker"


+ 4
- 4
bubble-server/src/test/resources/models/tests/filter/traffic_analytics.json View File

@@ -5,7 +5,7 @@
"params": {
"fqdn": "foo.example.com",
"uri": "/bar",
"jsCheck": "json.getMatchers().length == 0"
"jsCheck": "json.getMatchers().length === 0"
}
},

@@ -15,7 +15,7 @@
"params": {
"fqdn": "foo.example.com",
"uri": "/baz",
"jsCheck": "json.getMatchers().length == 0"
"jsCheck": "json.getMatchers().length === 0"
}
},

@@ -25,7 +25,7 @@
"params": {
"fqdn": "bar.example.com",
"uri": "/qux",
"jsCheck": "json.getMatchers().length == 0"
"jsCheck": "json.getMatchers().length === 0"
}
},

@@ -34,7 +34,7 @@
"request": { "uri": "me/apps/TrafficAnalytics/data" },
"response": {
"check": [
{"condition": "json.length == 8"},
{"condition": "json.length === 8"},
{"condition": "_find(json, function (d) { return d.getKey().startsWith('foo.example.com@hourly') }) != null"},
{"condition": "_find(json, function (d) { return d.getKey().startsWith('foo.example.com@hourly') }).getData() == 2"},
{"condition": "_find(json, function (d) { return d.getKey().startsWith('bar.example.com@hourly') }) != null"},


+ 1
- 1
bubble-server/src/test/resources/models/tests/filter/user_block/hn_request2.json View File

@@ -2,7 +2,7 @@
{
"comment": "reload HN, expect more blocked users",
"request": {
"uri": "p/https/news.ycombinator.com/item?id=20783006"
"uri": "p/127.0.0.1:{{serverConfig.http.port}}/web_mock/news.ycombinator.com/item_id_20783006.html"
},
"response": {
"raw": true,


Loading…
Cancel
Save