public static void writeConfigToZookeeper(ConfigurationType configType, byte[] configData, String zookeeperUrl) throws Exception { writeConfigToZookeeper(configType, Optional.empty(), configData, zookeeperUrl); }
public static void writeConfigToZookeeper(String name, Map<String, Object> config, String zookeeperUrl) throws Exception { writeConfigToZookeeper(Constants.ZOOKEEPER_TOPOLOGY_ROOT + "/" + name, JSONUtils.INSTANCE.toJSONPretty(config), zookeeperUrl); }
public static void writeConfigToZookeeper(ConfigurationType configType, Optional<String> configName, byte[] configData, String zookeeperUrl) throws Exception { writeConfigToZookeeper(getConfigZKPath(configType, configName), configData, zookeeperUrl); }
/** * Reads Json data for the specified config type and config name (if applicable) from zookeeper, * applies the patch from patchData, and writes it back to Zookeeper in a pretty print format. * Patching JSON flattens existing formatting, so this will keep configs readable. The * curatorclient should be started already. * * @param configurationType GLOBAL, PARSER, etc. * @param configName e.g. bro, yaf, snort * @param patchData a JSON patch in the format specified by RFC 6902 * @param client access to zookeeeper */ public static void applyConfigPatchToZookeeper( ConfigurationType configurationType, Optional<String> configName, byte[] patchData, CuratorFramework client) throws Exception { byte[] configData = readConfigBytesFromZookeeper(configurationType, configName, client); byte[] prettyPatchedConfig = JSONUtils.INSTANCE.applyPatch(patchData, configData); // ensure the patch produces a valid result; otherwise exception thrown during deserialization String prettyPatchedConfigStr = new String(prettyPatchedConfig); configurationType.deserialize(prettyPatchedConfigStr); writeConfigToZookeeper(configurationType, configName, prettyPatchedConfig, client); }
@Test public void modifiesSingleParserConfiguration() throws Exception { // write parser configuration ConfigurationType type = ConfigurationType.PARSER; String parserName = "a-happy-metron-parser"; byte[] config = JSONUtils.INSTANCE.toJSONPretty(someParserConfig); ConfigurationsUtils.writeConfigToZookeeper(type, Optional.of(parserName), config, zookeeperUrl); // validate the modified parser configuration byte[] actual = ConfigurationsUtils.readConfigBytesFromZookeeper(type, Optional.of(parserName), zookeeperUrl); assertThat(actual, equalTo(JSONUtils.INSTANCE.toJSONPretty(someParserConfig))); }
@Test public void patchesParserConfigurationViaPatchJSON() throws Exception { // setup zookeeper with a configuration final ConfigurationType type = ConfigurationType.PARSER; final String parserName = "patched-metron-parser"; byte[] config = JSONUtils.INSTANCE.toJSONPretty(someParserConfig); ConfigurationsUtils.writeConfigToZookeeper(type, Optional.of(parserName), config, zookeeperUrl); // patch the configuration byte[] patch = JSONUtils.INSTANCE.toJSONPretty(patchParserConfig); ConfigurationsUtils.applyConfigPatchToZookeeper(type, Optional.of(parserName), patch, zookeeperUrl); // validate the patched configuration byte[] actual = ConfigurationsUtils.readConfigBytesFromZookeeper(type, Optional.of(parserName), zookeeperUrl); byte[] expected = JSONUtils.INSTANCE.toJSONPretty(modifiedParserConfig); assertThat(actual, equalTo(expected)); }
@Test public void modifiedGlobalConfiguration() throws Exception { // write global configuration ConfigurationType type = ConfigurationType.GLOBAL; ConfigurationsUtils.writeConfigToZookeeper(type, JSONUtils.INSTANCE.toJSONPretty(someParserConfig), zookeeperUrl); // validate the modified global configuration byte[] actual = ConfigurationsUtils.readConfigBytesFromZookeeper(type, zookeeperUrl); assertThat(actual, equalTo(JSONUtils.INSTANCE.toJSONPretty(someParserConfig))); }
@Test public void test() throws Exception { Assert.assertTrue(expectedGlobalConfig.length > 0); ConfigurationsUtils.writeGlobalConfigToZookeeper(expectedGlobalConfig, zookeeperUrl); byte[] actualGlobalConfigBytes = ConfigurationsUtils.readGlobalConfigBytesFromZookeeper(client); Assert.assertTrue(Arrays.equals(expectedGlobalConfig, actualGlobalConfigBytes)); Assert.assertTrue(expectedSensorParserConfigMap.size() > 0); String testSensorType = "yaf"; byte[] expectedSensorParserConfigBytes = expectedSensorParserConfigMap.get(testSensorType); ConfigurationsUtils.writeSensorParserConfigToZookeeper(testSensorType, expectedSensorParserConfigBytes, zookeeperUrl); byte[] actualSensorParserConfigBytes = ConfigurationsUtils.readSensorParserConfigBytesFromZookeeper(testSensorType, client); Assert.assertTrue(Arrays.equals(expectedSensorParserConfigBytes, actualSensorParserConfigBytes)); Assert.assertTrue(expectedSensorEnrichmentConfigMap.size() > 0); byte[] expectedSensorEnrichmentConfigBytes = expectedSensorEnrichmentConfigMap.get(testSensorType); ConfigurationsUtils.writeSensorEnrichmentConfigToZookeeper(testSensorType, expectedSensorEnrichmentConfigBytes, zookeeperUrl); byte[] actualSensorEnrichmentConfigBytes = ConfigurationsUtils.readSensorEnrichmentConfigBytesFromZookeeper(testSensorType, client); Assert.assertTrue(Arrays.equals(expectedSensorEnrichmentConfigBytes, actualSensorEnrichmentConfigBytes)); String name = "testConfig"; Map<String, Object> testConfig = new HashMap<>(); testConfig.put("stringField", "value"); testConfig.put("intField", 1); testConfig.put("doubleField", 1.1); ConfigurationsUtils.writeConfigToZookeeper(name, testConfig, zookeeperUrl); byte[] readConfigBytes = ConfigurationsUtils.readConfigBytesFromZookeeper(name, client); Assert.assertTrue(Arrays.equals(JSONUtils.INSTANCE.toJSONPretty(testConfig), readConfigBytes)); }
/** * Note: the current configuration structure mixes abstractions based on the configuration type * and requires testing each type. GLOBAL is a actually representative of the final node name, * whereas the other types, e.g. PARSER, represent a directory/path and not a ZK node where values * are stored. The semantics are similar but slightly different. */ @Test public void patchesGlobalConfigurationViaPatchJSON() throws Exception { // setup zookeeper with a configuration final ConfigurationType type = ConfigurationType.GLOBAL; byte[] config = JSONUtils.INSTANCE.toJSONPretty(someGlobalConfig); ConfigurationsUtils.writeConfigToZookeeper(type, config, zookeeperUrl); // patch the configuration byte[] patch = JSONUtils.INSTANCE.toJSONPretty(patchGlobalConfig); ConfigurationsUtils.applyConfigPatchToZookeeper(type, patch, zookeeperUrl); // validate the patched configuration byte[] actual = ConfigurationsUtils.readConfigBytesFromZookeeper(type, zookeeperUrl); byte[] expected = JSONUtils.INSTANCE.toJSONPretty(modifiedGlobalConfig); assertThat(actual, equalTo(expected)); }