|
36 | 36 |
|
37 | 37 | import org.opensearch.common.network.InetAddresses;
|
38 | 38 | import org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache;
|
| 39 | +import org.opensearch.common.settings.Settings; |
| 40 | +import org.opensearch.env.TestEnvironment; |
| 41 | +import org.opensearch.ingest.Processor; |
39 | 42 | import org.opensearch.test.OpenSearchTestCase;
|
| 43 | +import org.opensearch.test.StreamsUtils; |
| 44 | + |
| 45 | +import java.io.ByteArrayInputStream; |
| 46 | +import java.io.IOException; |
| 47 | +import java.io.UncheckedIOException; |
| 48 | +import java.nio.file.Files; |
| 49 | +import java.nio.file.Path; |
| 50 | +import java.util.List; |
| 51 | +import java.util.Set; |
40 | 52 |
|
41 | 53 | import static org.mockito.Mockito.mock;
|
42 | 54 |
|
@@ -77,4 +89,87 @@ public void testInvalidInit() {
|
77 | 89 | IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new GeoIpCache(-1));
|
78 | 90 | assertEquals("geoip max cache size must be 0 or greater", ex.getMessage());
|
79 | 91 | }
|
| 92 | + |
| 93 | + public void testAllowList() throws IOException { |
| 94 | + runAllowListTest(List.of()); |
| 95 | + runAllowListTest(List.of("geoip")); |
| 96 | + } |
| 97 | + |
| 98 | + public void testInvalidAllowList() throws IOException { |
| 99 | + List<String> invalidAllowList = List.of("set"); |
| 100 | + Settings.Builder settingsBuilder = Settings.builder() |
| 101 | + .putList(IngestGeoIpPlugin.PROCESSORS_ALLOWLIST_SETTING.getKey(), invalidAllowList); |
| 102 | + createDb(settingsBuilder); |
| 103 | + try (IngestGeoIpPlugin plugin = new IngestGeoIpPlugin()) { |
| 104 | + IllegalArgumentException e = expectThrows( |
| 105 | + IllegalArgumentException.class, |
| 106 | + () -> plugin.getProcessors(createParameters(settingsBuilder.build())) |
| 107 | + ); |
| 108 | + assertEquals( |
| 109 | + "Processor(s) " |
| 110 | + + invalidAllowList |
| 111 | + + " were defined in [" |
| 112 | + + IngestGeoIpPlugin.PROCESSORS_ALLOWLIST_SETTING.getKey() |
| 113 | + + "] but do not exist", |
| 114 | + e.getMessage() |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public void testAllowListNotSpecified() throws IOException { |
| 120 | + Settings.Builder settingsBuilder = Settings.builder(); |
| 121 | + settingsBuilder.remove(IngestGeoIpPlugin.PROCESSORS_ALLOWLIST_SETTING.getKey()); |
| 122 | + createDb(settingsBuilder); |
| 123 | + try (IngestGeoIpPlugin plugin = new IngestGeoIpPlugin()) { |
| 124 | + final Set<String> expected = Set.of("geoip"); |
| 125 | + assertEquals(expected, plugin.getProcessors(createParameters(settingsBuilder.build())).keySet()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void runAllowListTest(List<String> allowList) throws IOException { |
| 130 | + Settings.Builder settingsBuilder = Settings.builder(); |
| 131 | + createDb(settingsBuilder); |
| 132 | + final Settings settings = settingsBuilder.putList(IngestGeoIpPlugin.PROCESSORS_ALLOWLIST_SETTING.getKey(), allowList).build(); |
| 133 | + try (IngestGeoIpPlugin plugin = new IngestGeoIpPlugin()) { |
| 134 | + assertEquals(Set.copyOf(allowList), plugin.getProcessors(createParameters(settings)).keySet()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private void createDb(Settings.Builder settingsBuilder) throws IOException { |
| 139 | + Path configDir = createTempDir(); |
| 140 | + Path userAgentConfigDir = configDir.resolve("ingest-geoip"); |
| 141 | + Files.createDirectories(userAgentConfigDir); |
| 142 | + settingsBuilder.put("ingest.geoip.database_path", configDir).put("path.home", configDir); |
| 143 | + try { |
| 144 | + Files.copy( |
| 145 | + new ByteArrayInputStream(StreamsUtils.copyToBytesFromClasspath("/GeoLite2-City.mmdb")), |
| 146 | + configDir.resolve("GeoLite2-City.mmdb") |
| 147 | + ); |
| 148 | + Files.copy( |
| 149 | + new ByteArrayInputStream(StreamsUtils.copyToBytesFromClasspath("/GeoLite2-Country.mmdb")), |
| 150 | + configDir.resolve("GeoLite2-Country.mmdb") |
| 151 | + ); |
| 152 | + Files.copy( |
| 153 | + new ByteArrayInputStream(StreamsUtils.copyToBytesFromClasspath("/GeoLite2-ASN.mmdb")), |
| 154 | + configDir.resolve("GeoLite2-ASN.mmdb") |
| 155 | + ); |
| 156 | + } catch (IOException e) { |
| 157 | + throw new UncheckedIOException(e); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private static Processor.Parameters createParameters(Settings settings) { |
| 162 | + return new Processor.Parameters( |
| 163 | + TestEnvironment.newEnvironment(settings), |
| 164 | + null, |
| 165 | + null, |
| 166 | + null, |
| 167 | + () -> 0L, |
| 168 | + (a, b) -> null, |
| 169 | + null, |
| 170 | + null, |
| 171 | + $ -> {}, |
| 172 | + null |
| 173 | + ); |
| 174 | + } |
80 | 175 | }
|
0 commit comments