-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 2.x] Adding allowlist setting for ingest-useragent and inge…
…st-geoip processors (#15374) * Adding allowlist setting for ingest-useragent and ingest-geoip processors (#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]> * Fixing spotlesscheck Signed-off-by: Sarat Vemulapalli <[email protected]> * Fixing merge conflicts Signed-off-by: Sarat Vemulapalli <[email protected]> * Spotless Again Signed-off-by: Sarat Vemulapalli <[email protected]> --------- Signed-off-by: Sarat Vemulapalli <[email protected]>
- Loading branch information
1 parent
768905f
commit 1c53113
Showing
5 changed files
with
283 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
111 changes: 111 additions & 0 deletions
111
...-user-agent/src/test/java/org/opensearch/ingest/useragent/IngestUserAgentPluginTests.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,111 @@ | ||
/* | ||
* 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 IngestUserAgentPluginTests 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(IngestUserAgentPlugin.PROCESSORS_ALLOWLIST_SETTING.getKey(), invalidAllowList) | ||
.build(); | ||
try (IngestUserAgentPlugin plugin = new IngestUserAgentPlugin()) { | ||
IllegalArgumentException e = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> plugin.getProcessors(createParameters(settings)) | ||
); | ||
assertEquals( | ||
"Processor(s) " | ||
+ invalidAllowList | ||
+ " were defined in [" | ||
+ IngestUserAgentPlugin.PROCESSORS_ALLOWLIST_SETTING.getKey() | ||
+ "] but do not exist", | ||
e.getMessage() | ||
); | ||
} | ||
} | ||
|
||
public void testAllowListNotSpecified() throws IOException { | ||
settingsBuilder.remove(IngestUserAgentPlugin.PROCESSORS_ALLOWLIST_SETTING.getKey()); | ||
try (IngestUserAgentPlugin plugin = new IngestUserAgentPlugin()) { | ||
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(IngestUserAgentPlugin.PROCESSORS_ALLOWLIST_SETTING.getKey(), allowList).build(); | ||
try (IngestUserAgentPlugin plugin = new IngestUserAgentPlugin()) { | ||
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 | ||
); | ||
} | ||
} |