Skip to content

Commit 4c848f7

Browse files
authored
Account for multivalue lookups in doRefresh and getPreview (#23436)
* Account for multivalue lookups in doRefresh and getPreview * unit tests * refresh unit test * add pr to changelog
1 parent 189168f commit 4c848f7

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

changelog/unreleased/issue-4203.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ type = "added"
22
message = "Added option for multi-value lookups to CSV File data adapters."
33

44
issues = ["4203"]
5-
pulls = ["23346"]
5+
pulls = ["23346", "23436"]

graylog2-server/src/main/java/org/graylog2/lookup/adapters/CSVFileDataAdapter.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ protected void doRefresh(LookupCachePurge cachePurge) throws Exception {
163163
}
164164

165165
LOG.debug("CSV file {} has changed, updating data", config.path());
166-
setLookupRefFromCSV();
166+
if (!config.isMultiValueLookup()) {
167+
setLookupRefFromCSV();
168+
} else {
169+
setMultiValueLookupRefFromCSV();
170+
}
167171
cachePurge.purgeAll();
168172
// If the file has been moved, then moved back, the fileInfo might have been disconnected.
169173
// In this case, create a new fileInfo.
@@ -417,14 +421,27 @@ public LookupPreview getPreview(int size) {
417421
return cidrLookupRef.get().getPreview(size);
418422
} else {
419423
final Map<Object, Object> result = new HashMap<>();
420-
final Map<String, String> lookup = lookupRef.get();
421-
for (Map.Entry<String, String> entries : lookup.entrySet()) {
422-
if (result.size() == size) {
423-
break;
424+
final long totalSize;
425+
if (!config.isMultiValueLookup()) {
426+
final Map<String, String> lookup = lookupRef.get();
427+
totalSize = lookup.size();
428+
for (Map.Entry<String, String> entries : lookup.entrySet()) {
429+
if (result.size() == size) {
430+
break;
431+
}
432+
result.put(entries.getKey(), entries.getValue());
433+
}
434+
} else {
435+
final Map<String, Map<Object, Object>> multiLookup = multiValueLookupRef.get();
436+
totalSize = multiLookup.size();
437+
for (Map.Entry<String, Map<Object, Object>> entries : multiLookup.entrySet()) {
438+
if (result.size() == size) {
439+
break;
440+
}
441+
result.put(entries.getKey(), toSingleValue(entries.getValue()));
424442
}
425-
result.put(entries.getKey(), entries.getValue());
426443
}
427-
return new LookupPreview(lookup.size(), result);
444+
return new LookupPreview(totalSize, result);
428445
}
429446
}
430447

graylog2-server/src/test/java/org/graylog2/lookup/adapters/CSVFileDataAdapterTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.io.Resources;
2222
import org.graylog2.lookup.AllowedAuxiliaryPathChecker;
2323
import org.graylog2.plugin.lookup.LookupCachePurge;
24+
import org.graylog2.plugin.lookup.LookupPreview;
2425
import org.graylog2.plugin.lookup.LookupResult;
2526
import org.junit.Rule;
2627
import org.junit.Test;
@@ -236,6 +237,30 @@ public void testConfigValidationFailsIfValueColumnMissingForNonMultiValueLookup(
236237
.contains("Value column must be set unless multi-value lookup is enabled.");
237238
}
238239

240+
@Test
241+
public void testGetPreview() throws Exception {
242+
final Config config = baseConfig();
243+
csvFileDataAdapter = spy(new CSVFileDataAdapter("id", "name", config, new MetricRegistry(), pathChecker));
244+
when(pathChecker.fileIsInAllowedPath(isA(Path.class))).thenReturn(true);
245+
csvFileDataAdapter.doStart();
246+
247+
LookupPreview previewResult = csvFileDataAdapter.getPreview(1);
248+
assertThat(previewResult).satisfies(r -> {
249+
assertThat(r.supported()).isTrue();
250+
assertThat(r.total()).isEqualTo(2);
251+
assertThat(r.results()).hasSize(1);
252+
});
253+
254+
previewResult = csvFileDataAdapter.getPreview(3);
255+
assertThat(previewResult).satisfies(r -> {
256+
assertThat(r.supported()).isTrue();
257+
assertThat(r.total()).isEqualTo(2);
258+
assertThat(r.results()).hasSize(2);
259+
assertThat(r.results().get("foo")).isEqualTo("23");
260+
assertThat(r.results().get("bar")).isEqualTo("42");
261+
});
262+
}
263+
239264
@Test
240265
public void testCIDRLookups() throws Exception {
241266
final Config config = cidrLookupConfig();
@@ -292,6 +317,23 @@ public void testMultiValueLookups() throws Exception {
292317
assertThat(csvFileDataAdapter.doGet("999999")).isEqualTo(LookupResult.empty());
293318
}
294319

320+
@Test
321+
public void refreshMultiValueSuccess() throws Exception {
322+
csvFileDataAdapter = spy(new CSVFileDataAdapter("id", "name", multiValueConfig(), new MetricRegistry(), pathChecker));
323+
when(pathChecker.fileIsInAllowedPath(isA(Path.class))).thenReturn(true);
324+
csvFileDataAdapter.doStart();
325+
csvFileDataAdapter.doRefresh(cachePurge);
326+
assertFalse(csvFileDataAdapter.getError().isPresent());
327+
328+
LookupResult result1 = csvFileDataAdapter.doGet("000001");
329+
assertThat(result1.multiValue())
330+
.containsEntry("first_name", "Adam")
331+
.containsEntry("last_name", "Alpha")
332+
.containsEntry("username", "aalpha")
333+
.containsEntry("phone", "123-4567")
334+
.containsEntry("address", "123 Sleepy Hollow Lane");
335+
}
336+
295337
@Test
296338
public void testMultiValueCIDRLookups() throws Exception {
297339
csvFileDataAdapter = spy(new CSVFileDataAdapter("id", "name", multiValueCidrConfig(), new MetricRegistry(), pathChecker));
@@ -325,6 +367,27 @@ public void testMultiValueCIDRLookups() throws Exception {
325367
assertThat(csvFileDataAdapter.doGet("8.8.8.8")).isEqualTo(LookupResult.empty());
326368
}
327369

370+
@Test
371+
public void testMultiValuePreview() throws Exception {
372+
csvFileDataAdapter = spy(new CSVFileDataAdapter("id", "name", multiValueConfig(), new MetricRegistry(), pathChecker));
373+
when(pathChecker.fileIsInAllowedPath(isA(Path.class))).thenReturn(true);
374+
csvFileDataAdapter.doStart();
375+
376+
LookupPreview previewResult = csvFileDataAdapter.getPreview(3);
377+
assertThat(previewResult).satisfies(r -> {
378+
assertThat(r.supported()).isTrue();
379+
assertThat(r.total()).isEqualTo(10);
380+
assertThat(r.results()).hasSize(3);
381+
});
382+
383+
previewResult = csvFileDataAdapter.getPreview(15);
384+
assertThat(previewResult).satisfies(r -> {
385+
assertThat(r.supported()).isTrue();
386+
assertThat(r.total()).isEqualTo(10);
387+
assertThat(r.results()).hasSize(10);
388+
});
389+
}
390+
328391
private Config baseConfig() {
329392
return Config.builder()
330393
.type(NAME)

0 commit comments

Comments
 (0)