forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding allowlist setting for ingest-useragent and ingest-geoip proces…
…sors (opensearch-project#15325) * Adding allowlist setting for user-agent, geo-ip and updated tests for ingest-common. Signed-off-by: Sarat Vemulapalli <[email protected]> * Remove duplicate test in ingest-common Signed-off-by: Sarat Vemulapalli <[email protected]> * Adding changelog Signed-off-by: Sarat Vemulapalli <[email protected]> --------- Signed-off-by: Sarat Vemulapalli <[email protected]>
- Loading branch information
1 parent
b6b0403
commit d5a6c0b
Showing
5 changed files
with
286 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
114 changes: 114 additions & 0 deletions
114
...agent/src/test/java/org/opensearch/ingest/useragent/IngestUserAgentModulePluginTests.java
This file contains 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,114 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.ingest.useragent; | ||
|
||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.env.TestEnvironment; | ||
import org.opensearch.ingest.Processor; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.junit.Before; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class IngestUserAgentModulePluginTests extends OpenSearchTestCase { | ||
private Settings.Builder settingsBuilder; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
Path configDir = createTempDir(); | ||
Path userAgentConfigDir = configDir.resolve("ingest-user-agent"); | ||
Files.createDirectories(userAgentConfigDir); | ||
settingsBuilder = Settings.builder().put("ingest-user-agent", configDir).put("path.home", configDir); | ||
|
||
// Copy file, leaving out the device parsers at the end | ||
String regexWithoutDevicesFilename = "regexes_without_devices.yml"; | ||
try ( | ||
BufferedReader reader = new BufferedReader( | ||
new InputStreamReader(UserAgentProcessor.class.getResourceAsStream("/regexes.yml"), StandardCharsets.UTF_8) | ||
); | ||
BufferedWriter writer = Files.newBufferedWriter(userAgentConfigDir.resolve(regexWithoutDevicesFilename)); | ||
) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
if (line.startsWith("device_parsers:")) { | ||
break; | ||
} | ||
|
||
writer.write(line); | ||
writer.newLine(); | ||
} | ||
} | ||
} | ||
|
||
public void testAllowList() throws IOException { | ||
runAllowListTest(List.of()); | ||
runAllowListTest(List.of("user_agent")); | ||
} | ||
|
||
public void testInvalidAllowList() throws IOException { | ||
List<String> invalidAllowList = List.of("set"); | ||
final Settings settings = settingsBuilder.putList( | ||
IngestUserAgentModulePlugin.PROCESSORS_ALLOWLIST_SETTING.getKey(), | ||
invalidAllowList | ||
).build(); | ||
try (IngestUserAgentModulePlugin plugin = new IngestUserAgentModulePlugin()) { | ||
IllegalArgumentException e = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> plugin.getProcessors(createParameters(settings)) | ||
); | ||
assertEquals( | ||
"Processor(s) " | ||
+ invalidAllowList | ||
+ " were defined in [" | ||
+ IngestUserAgentModulePlugin.PROCESSORS_ALLOWLIST_SETTING.getKey() | ||
+ "] but do not exist", | ||
e.getMessage() | ||
); | ||
} | ||
} | ||
|
||
public void testAllowListNotSpecified() throws IOException { | ||
settingsBuilder.remove(IngestUserAgentModulePlugin.PROCESSORS_ALLOWLIST_SETTING.getKey()); | ||
try (IngestUserAgentModulePlugin plugin = new IngestUserAgentModulePlugin()) { | ||
final Set<String> expected = Set.of("user_agent"); | ||
assertEquals(expected, plugin.getProcessors(createParameters(settingsBuilder.build())).keySet()); | ||
} | ||
} | ||
|
||
private void runAllowListTest(List<String> allowList) throws IOException { | ||
final Settings settings = settingsBuilder.putList(IngestUserAgentModulePlugin.PROCESSORS_ALLOWLIST_SETTING.getKey(), allowList) | ||
.build(); | ||
try (IngestUserAgentModulePlugin plugin = new IngestUserAgentModulePlugin()) { | ||
assertEquals(Set.copyOf(allowList), plugin.getProcessors(createParameters(settings)).keySet()); | ||
} | ||
} | ||
|
||
private static Processor.Parameters createParameters(Settings settings) { | ||
return new Processor.Parameters( | ||
TestEnvironment.newEnvironment(settings), | ||
null, | ||
null, | ||
null, | ||
() -> 0L, | ||
(a, b) -> null, | ||
null, | ||
null, | ||
$ -> {}, | ||
null | ||
); | ||
} | ||
} |