public Updater(String path, Consumer<IpFilterConfig> setter) { this.path = path; this.setter = setter; config = new IpAddressDeterministicRuleEngineConfig(); unmarshaller = new ThriftUnmarshaller(); }
@Override public void process(UpdateType updateType, InetAddress address, RuleType ruleType) { if (updateType == UpdateType.Add) { if (ruleType == RuleType.blacklist) { rules.mutateIpRules().blacklistIp(address); } else if (ruleType == RuleType.whitelist) { rules.mutateIpRules().whitelistIp(address); } } else if (updateType == UpdateType.Remove) { rules.mutateIpRules().remove(address); } }
public void update(byte[] data) { config.clear(); unmarshaller.unmarshall(config, data); setter.accept(new IpFilterConfig(config.getBlacklistIps(), config.getWhitelistIps())); } }
public void populateIpRules(Map<InetAddress, RuleType> rules) { for (InetAddress address : ipRules.get().getBlacklistIps()) { rules.put(address, RuleType.blacklist); } for (InetAddress address : ipRules.get().getWhitelistIps()) { rules.put(address, RuleType.whitelist); } }
@Test public void testUpdater() throws UnknownHostException { ThriftMarshaller marshaller = new ThriftMarshaller(); IpAddressDeterministicRuleEngineConfig rules = new IpAddressDeterministicRuleEngineConfig(); rules.blacklistIp(InetAddress.getByName("127.0.0.1")); IpFilterConfig.Updater updater = new IpFilterConfig.Updater("path", this::setIpFilterConfig); updater.update(marshaller.marshall(rules)); IpFilterConfig expected = new IpFilterConfig(rules.getBlacklistIps(), rules.getWhitelistIps()); assertEquals(expected, config); }
server.start(); IpAddressDeterministicRuleEngineConfig config = new IpAddressDeterministicRuleEngineConfig(); config.blacklistIp(InetAddress.getByName("127.0.0.1")); config.blacklistIp(InetAddress.getByName("::1")); config.whitelistIp(InetAddress.getByName("0.0.0.0")); IpAddressDeterministicRuleEngineConfig read = new IpAddressDeterministicRuleEngineConfig(); unmarshaller.unmarshall(read, data);
public void unmarshall(IpAddressDeterministicRuleEngineConfig config, byte[] data) { IpRuleset message = new IpRuleset(); deserialize(message, data); try { for (ByteBuffer address : message.getBlacklistIps()) { config.blacklistIp(InetAddress.getByAddress(getBytes(address))); } for (ByteBuffer address : message.getWhitelistIps()) { config.whitelistIp(InetAddress.getByAddress(getBytes(address))); } } catch (UnknownHostException e) { throw new RuntimeException(e); } } }
public boolean equals(Ruleset other) { return other.ipRules.get().equals(ipRules.get()) && other.http1Rules.get().equals(http1Rules.get()); } }
public long write(WriteProvider writer) { long recordsWritten = 0; if (ipRules.marked()) { writer.write(ipFilterPath, ipRules.get()); recordsWritten += ipRules.get().size(); ipRules.clearMark(); } if (http1Rules.marked()) { writer.write(http1FilterPath, http1Rules.get()); recordsWritten += http1Rules.get().size(); http1Rules.clearMark(); } return recordsWritten; }
server.start(); IpAddressDeterministicRuleEngineConfig config = new IpAddressDeterministicRuleEngineConfig(); config.blacklistIp(InetAddress.getByName("127.0.0.1")); config.blacklistIp(InetAddress.getByName("::1")); config.whitelistIp(InetAddress.getByName("0.0.0.0")); ZooKeeperReadProvider provider = new ZooKeeperReadProvider(unmarshaller, client); IpAddressDeterministicRuleEngineConfig read = new IpAddressDeterministicRuleEngineConfig(); provider.read(path, read);
public byte[] marshall(IpAddressDeterministicRuleEngineConfig config) { IpRuleset message = new IpRuleset(); message.setBlacklistIps(new HashSet<ByteBuffer>()); for (InetAddress address : config.getBlacklistIps()) { message.addToBlacklistIps(ByteBuffer.wrap(address.getAddress())); } message.setWhitelistIps(new HashSet<ByteBuffer>()); for (InetAddress address : config.getWhitelistIps()) { message.addToWhitelistIps(ByteBuffer.wrap(address.getAddress())); } return serialize(message); } }