Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/main/java/org/codelibs/fess/ds/git/GitDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,22 @@ private Throwable redactCredentialsInChain(final Throwable throwable, final int
return sanitized;
}

/**
* Returns whether fess has already stored a failure URL for each document of a rejected bulk request. The exception
* of such a request says so with {@code isRecorded()}, and fess then stores nothing else for it. The method is
* looked up by name, so that builds of fess whose exceptions do not have it can still be used.
*
* @param t The exception as it was thrown.
* @return true if no failure URL is to be stored for it.
*/
private boolean isRecordedRejection(final Throwable t) {
try {
return Boolean.TRUE.equals(t.getClass().getMethod("isRecorded").invoke(t));
} catch (final ReflectiveOperationException e) {
return false;
}
}

/**
* Returns the file name from the given path.
*
Expand Down Expand Up @@ -1137,7 +1153,7 @@ protected void processFile(final DataConfig dataConfig, final IndexUpdateCallbac
// chain before it is logged or persisted. The original objects are still used below for the
// instanceof/errorName checks, which only read class names (never sensitive).
final Throwable redactedException = redactCredentialsInChain(e);
logger.warn("Crawling Access Exception at : {}", dataMap, redactedException);
logger.warn("Crawling Access Exception at : {}:{}", redactUrl(uri), path, redactedException);

Throwable target = e;
if (target instanceof MultipleCrawlingAccessException) {
Expand Down Expand Up @@ -1170,10 +1186,13 @@ protected void processFile(final DataConfig dataConfig, final IndexUpdateCallbac
} catch (final Throwable t) {
// Scrub any credential-bearing Git URL out of the message chain before logging/persisting it.
final Throwable redactedThrowable = redactCredentialsInChain(t);
logger.warn("Crawling Access Exception at : {}", dataMap, redactedThrowable);
final String url = redactUrl(uri) + ":" + path;
final FailureUrlService failureUrlService = ComponentUtil.getComponent(FailureUrlService.class);
failureUrlService.store(dataConfig, t.getClass().getCanonicalName(), url, redactedThrowable);
logger.warn("Crawling Access Exception at : {}:{}", redactUrl(uri), path, redactedThrowable);
// asked of the original: the redacted copy no longer says that nothing more is to be stored
if (!isRecordedRejection(t)) {
final String url = redactUrl(uri) + ":" + path;
final FailureUrlService failureUrlService = ComponentUtil.getComponent(FailureUrlService.class);
failureUrlService.store(dataConfig, t.getClass().getCanonicalName(), url, redactedThrowable);
}

final long readInterval = (Long) configMap.get(READ_INTERVAL);
if (readInterval > 0) {
Expand Down
111 changes: 109 additions & 2 deletions src/test/java/org/codelibs/fess/ds/git/GitDataStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.RefSpec;
import org.codelibs.fess.app.service.FailureUrlService;
import org.codelibs.fess.crawler.exception.CrawlingAccessException;
import org.codelibs.fess.opensearch.config.exentity.CrawlingConfig;
import org.codelibs.fess.opensearch.config.exentity.FailureUrl;

public class GitDataStoreTest extends UnitDsTestCase {

Expand Down Expand Up @@ -1602,6 +1606,101 @@ public void test_processFile_stripsCredentialsFromResultMap() throws Exception {
assertFalse(resultMap.values().stream().anyMatch(v -> "s3cr3t-token".equals(v)));
}

@Test
public void test_processFile_logsTheFailureUrlOfAFailedFileAndNoneOfItsValues() throws Exception {
for (final RuntimeException failure : new RuntimeException[] { new IllegalStateException("rejected"),
new CrawlingAccessException("not accessible") }) {
final List<String> failureUrls = new ArrayList<>();
registerFailureUrlRecorder(failureUrls, new ArrayList<>());
final Map<String, Object> defaultDataMap = new HashMap<>();
defaultDataMap.put("content", "SECRET-MARKER");

final List<LogEvent> warnings = runProcessFileFailing("https://github.com/codelibs/fess.git", failure, defaultDataMap);

assertEquals(1, failureUrls.size());
assertEquals("https://github.com/codelibs/fess.git:a.txt", failureUrls.get(0));
assertEquals(1, warnings.size());
assertEquals("Crawling Access Exception at : https://github.com/codelibs/fess.git:a.txt",
warnings.get(0).getMessage().getFormattedMessage());
assertTrue(warnings.get(0).getThrown() == failure);
}
}

@Test
public void test_processFile_storesNoFailureUrlForARecordedRejection() throws Exception {
// the message needs a redaction, so the copy that is logged and stored is no longer this exception
final String message = "failed to parse [ftp://appuser@files.example.com/a]";
for (final boolean recorded : new boolean[] { true, false }) {
final List<String> failureUrls = new ArrayList<>();
final List<Throwable> throwables = new ArrayList<>();
registerFailureUrlRecorder(failureUrls, throwables);

final List<LogEvent> warnings = runProcessFileFailing("https://github.com/codelibs/fess.git",
new RejectionException(message, recorded), new HashMap<>());

assertEquals(recorded ? 0 : 1, failureUrls.size());
assertEquals(1, warnings.size());
assertFalse(warnings.get(0).getThrown().getMessage().contains("appuser"));
assertFalse(throwables.stream().anyMatch(t -> t.getMessage().contains("appuser")));
}
}

/** Stands in for the exception of fess that tells whether the failure URLs of rejected documents are stored. */
public static class RejectionException extends RuntimeException {
private static final long serialVersionUID = 1L;

private final boolean recorded;

public RejectionException(final String message, final boolean recorded) {
super(message);
this.recorded = recorded;
}

public boolean isRecorded() {
return recorded;
}
}

/** Registers a FailureUrlService that only remembers the URLs and throwables it is asked to store. */
private void registerFailureUrlRecorder(final List<String> urls, final List<Throwable> throwables) {
ComponentUtil.register(new FailureUrlService() {
@Override
public FailureUrl store(final CrawlingConfig crawlingConfig, final String errorName, final String url, final Throwable e) {
urls.add(url);
throwables.add(e);
return null;
}
}, FailureUrlService.class.getCanonicalName());
}

/** Drives processFile with a callback that throws the failure, and returns the warnings the data store logged. */
private List<LogEvent> runProcessFileFailing(final String uri, final RuntimeException failure, final Map<String, Object> defaultDataMap)
throws Exception {
final List<LogEvent> events = new ArrayList<>();
final AbstractAppender appender = new AbstractAppender("warnCapture", null, null, true, Property.EMPTY_ARRAY) {
@Override
public void append(final LogEvent event) {
if (event.getLevel().isMoreSpecificThan(Level.WARN)) {
events.add(event.toImmutable());
}
}
};
appender.start();
final org.apache.logging.log4j.core.Logger coreLogger =
(org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager.getLogger(GitDataStore.class);
final Level originalLevel = coreLogger.getLevel();
coreLogger.addAppender(appender);
coreLogger.setLevel(Level.WARN);
try {
runProcessFile(uri, new DataStoreParams(), new AtomicReference<>(), defaultDataMap, failure);
} finally {
coreLogger.setLevel(originalLevel);
coreLogger.removeAppender(appender);
appender.stop();
}
return events;
}

/**
* Drives {@link GitDataStore#processFile} against a fresh single-file local repository, capturing the
* {@code resultMap} handed to {@code convertValue}. The credential-bearing {@code uri} is deliberately
Expand All @@ -1612,6 +1711,12 @@ public void test_processFile_stripsCredentialsFromResultMap() throws Exception {
*/
private Map<String, Object> runProcessFileCapturingResultMap(final String uri, final DataStoreParams paramMap,
final AtomicReference<Map<String, Object>> capturedResultMap) throws Exception {
return runProcessFile(uri, paramMap, capturedResultMap, new HashMap<>(), null);
}

private Map<String, Object> runProcessFile(final String uri, final DataStoreParams paramMap,
final AtomicReference<Map<String, Object>> capturedResultMap, final Map<String, Object> defaultDataMap,
final RuntimeException storeFailure) throws Exception {
// The real processFile pipeline uses CrawlerStatsHelper (which in turn uses SystemHelper); register
// initialized instances so it can run inside this unit-test container.
ComponentUtil.register(new SystemHelper(), "systemHelper");
Expand Down Expand Up @@ -1665,7 +1770,9 @@ protected Object convertValue(final String scriptType, final String template, fi
final IndexUpdateCallback callback = new IndexUpdateCallback() {
@Override
public void store(final DataStoreParams paramMap, final Map<String, Object> dataMap) {
// no-op
if (storeFailure != null) {
throw storeFailure;
}
}

@Override
Expand All @@ -1687,7 +1794,7 @@ public void commit() {
final Map<String, String> scriptMap = new HashMap<>();
scriptMap.put("url", "url"); // one entry so convertValue is invoked with the resultMap

dataStore.processFile(null, callback, paramMap, scriptMap, new HashMap<>(), configMap);
dataStore.processFile(null, callback, paramMap, scriptMap, defaultDataMap, configMap);
return configMap;
}
}
Expand Down
Loading