@Override public void process( UpdateType updateType, Http1DeterministicRuleEngineConfig.Rule http1Rule, RuleType ruleType) { if (updateType == UpdateType.Add) { if (ruleType == RuleType.blacklist) { rules.mutateHttp1Rules().blacklistRule(http1Rule); } else if (ruleType == RuleType.whitelist) { rules.mutateHttp1Rules().whitelistRule(http1Rule); } } else if (updateType == UpdateType.Remove) { rules.mutateHttp1Rules().remove(http1Rule); } } }
public void unmarshall(Http1DeterministicRuleEngineConfig config, byte[] data) { Http1Ruleset message = new Http1Ruleset(); deserialize(message, data); for (Http1Rule rule : message.getBlacklistRules()) { config.blacklistRule(build(rule)); } for (Http1Rule rule : message.getWhitelistRules()) { config.whitelistRule(build(rule)); } }
@Test public void testWriteHttp1DeterministicRuleEngineConfig() throws Exception { try (TestingServer server = new TestingServer()) { server.start(); Http1DeterministicRuleEngineConfig config = new Http1DeterministicRuleEngineConfig(); HashMultimap<String, String> headers = HashMultimap.create(); headers.put("User-Agent", "Bad-actor: 1.0"); Http1DeterministicRuleEngineConfig.Rule bad = new Http1DeterministicRuleEngineConfig.Rule( HttpMethod.GET, "/path/to/failure", HttpVersion.HTTP_1_0, headers); Http1DeterministicRuleEngineConfig.Rule good = new Http1DeterministicRuleEngineConfig.Rule(null, null, null, null); config.blacklistRule(bad); config.whitelistRule(good); ThriftMarshaller marshaller = new ThriftMarshaller(); RetryPolicy retryPolicy = new RetryOneTime(1); try (CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy)) { client.start(); String path = "/some/path/to/nodes/http1Rules"; ZooKeeperWriteProvider provider = new ZooKeeperWriteProvider(marshaller, client); provider.write(path, config); byte[] data = client.getData().forPath(path); ThriftUnmarshaller unmarshaller = new ThriftUnmarshaller(); Http1DeterministicRuleEngineConfig read = new Http1DeterministicRuleEngineConfig(); unmarshaller.unmarshall(read, data); assertEquals(config, read); } } }
@Test public void testUpdater() throws UnknownHostException { ThriftMarshaller marshaller = new ThriftMarshaller(); Http1DeterministicRuleEngineConfig rules = new Http1DeterministicRuleEngineConfig(); HashMultimap<String, String> headers = HashMultimap.create(); headers.put("User-Agent", "Bad-actor: 1.0"); Http1DeterministicRuleEngineConfig.Rule bad = new Http1DeterministicRuleEngineConfig.Rule( HttpMethod.GET, "/path/to/failure", HttpVersion.HTTP_1_0, headers); rules.blacklistRule(bad); Http1FilterConfig.Updater updater = new Http1FilterConfig.Updater("path", this::setHttp1FilterConfig); updater.update(marshaller.marshall(rules)); Http1FilterConfig expected = new Http1FilterConfig(rules.getBlacklistRules()); assertEquals(expected, config); }