Fix migration in case of empty dir

This commit is contained in:
Max Dor
2019-02-13 23:11:01 +01:00
parent 77dc75d383
commit 2f7e5e4025

View File

@@ -54,16 +54,9 @@ public class FileKeyStore implements KeyStore {
base = new File(path).getAbsoluteFile().toString(); base = new File(path).getAbsoluteFile().toString();
File f = new File(base); File f = new File(base);
if (!f.exists()) { if (f.exists() && f.isFile()) {
if (!f.mkdir()) {
throw new RuntimeException("Unable to create key store at " + f.toString());
}
} else {
if (!f.isFile()) {
log.debug("Key store is already in directory format");
} else {
try { try {
log.info("Found old key store format, migrating..."); log.info("Found old key store format at {}, migrating...", base);
File oldStorePath = new File(f.toString() + ".backup-before-migration"); File oldStorePath = new File(f.toString() + ".backup-before-migration");
FileUtils.moveFile(f, oldStorePath); FileUtils.moveFile(f, oldStorePath);
FileUtils.forceMkdir(f); FileUtils.forceMkdir(f);
@@ -71,7 +64,7 @@ public class FileKeyStore implements KeyStore {
String privKey = new KeyFileStore(oldStorePath.toString()).load().orElse(""); String privKey = new KeyFileStore(oldStorePath.toString()).load().orElse("");
if (StringUtils.isBlank(privKey)) { if (StringUtils.isBlank(privKey)) {
throw new IllegalStateException("Signing key file is empty. Either fix or delete"); log.info("Empty file, nothing to migrate");
} else { } else {
// We ensure this is valid Base64 data before migrating // We ensure this is valid Base64 data before migrating
Base64.decodeBase64(privKey); Base64.decodeBase64(privKey);
@@ -84,7 +77,8 @@ public class FileKeyStore implements KeyStore {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Unable to migrate store from old single file format to new directory format", e); throw new RuntimeException("Unable to migrate store from old single file format to new directory format", e);
} }
} } else {
log.info("Key store is already in directory format");
} }
if (!f.isDirectory()) { if (!f.isDirectory()) {
@@ -137,7 +131,7 @@ public class FileKeyStore implements KeyStore {
File algoDir = Paths.get(base, toDirName(type)).toFile(); File algoDir = Paths.get(base, toDirName(type)).toFile();
File[] algos = algoDir.listFiles(); File[] algos = algoDir.listFiles();
if (Objects.isNull(algos)) { if (Objects.isNull(algos)) {
throw new IllegalStateException("Cannot list stored key algorithms: was expecting " + algoDir.toString() + " to be a directory"); return keyIds;
} }
for (File algo : algos) { for (File algo : algos) {