Check that createLogHandler() isn't called more than once.

This would result in multiple log files encrypted with the same key.
This commit is contained in:
akwizgran
2018-10-23 15:16:29 +01:00
parent dfb581ef12
commit 7ce91066f5
2 changed files with 5 additions and 0 deletions

View File

@@ -24,6 +24,8 @@ public interface PersistentLogManager {
/**
* Creates and returns a persistent log handler that stores its logs in
* the given directory.
* <p>
* This method should only be called once.
*/
Handler createLogHandler(File dir) throws IOException;

View File

@@ -28,6 +28,7 @@ import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Logger;
@@ -63,6 +64,7 @@ class PersistentLogManagerImpl implements PersistentLogManager,
private final StreamWriterFactory streamWriterFactory;
private final Formatter formatter;
private final SecretKey logKey;
private final AtomicBoolean handlerCreated = new AtomicBoolean(false);
@Nullable
private volatile SecretKey oldLogKey = null;
@@ -103,6 +105,7 @@ class PersistentLogManagerImpl implements PersistentLogManager,
@Override
public Handler createLogHandler(File dir) throws IOException {
if (handlerCreated.getAndSet(true)) throw new IllegalStateException();
File logFile = new File(dir, LOG_FILE);
File oldLogFile = new File(dir, OLD_LOG_FILE);
if (oldLogFile.exists() && !oldLogFile.delete())