From ad677d8797ece3130a2c9b52085f326748d53337 Mon Sep 17 00:00:00 2001 From: Tim203 Date: Tue, 23 Mar 2021 01:50:48 +0100 Subject: [PATCH] Generate new key when converting a pre-rewrite config. Bumped Geyser --- .../floodgate/config/loader/ConfigLoader.java | 56 ++++++++++--------- .../config/updater/ConfigUpdater.java | 27 +++++++-- pom.xml | 2 +- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/common/src/main/java/org/geysermc/floodgate/config/loader/ConfigLoader.java b/common/src/main/java/org/geysermc/floodgate/config/loader/ConfigLoader.java index 287b711b..a47efc67 100644 --- a/common/src/main/java/org/geysermc/floodgate/config/loader/ConfigLoader.java +++ b/common/src/main/java/org/geysermc/floodgate/config/loader/ConfigLoader.java @@ -72,7 +72,7 @@ public final class ConfigLoader { try { // check and update if the config is outdated if (!newConfig) { - updater.update(defaultConfigName); + updater.update(this, defaultConfigName); } FloodgateConfig config = ConfigInitializer.initializeFrom( @@ -93,31 +93,7 @@ public final class ConfigLoader { Path keyPath = dataFolder.resolve(configInstance.getKeyFileName()); // don't assume that the key always exists with the existence of a config if (!Files.exists(keyPath)) { - try { - Key key = keyProducer.produce(); - cipher.init(key); - - String test = "abcdefghijklmnopqrstuvwxyz0123456789"; - byte[] encrypted = cipher.encryptFromString(test); - String decrypted = cipher.decryptToString(encrypted); - - if (!test.equals(decrypted)) { - logger.error("Whoops, we tested the generated Floodgate keys but " + - "the decrypted test message doesn't match the original.\n" + - "Original message: " + test + "." + - "Decrypted message: " + decrypted + ".\n" + - "The encrypted message itself: " + new String(encrypted) - ); - throw new RuntimeException( - "Tested the generated public and private key but, " + - "the decrypted message doesn't match the original!" - ); - } - - Files.write(keyPath, key.getEncoded()); - } catch (Exception exception) { - logger.error("Error while creating key", exception); - } + generateKey(keyPath); } try { @@ -131,4 +107,32 @@ public final class ConfigLoader { return configInstance; } + + public void generateKey(Path keyPath) { + try { + Key key = keyProducer.produce(); + cipher.init(key); + + String test = "abcdefghijklmnopqrstuvwxyz0123456789"; + byte[] encrypted = cipher.encryptFromString(test); + String decrypted = cipher.decryptToString(encrypted); + + if (!test.equals(decrypted)) { + logger.error("Whoops, we tested the generated Floodgate keys but " + + "the decrypted test message doesn't match the original.\n" + + "Original message: " + test + "." + + "Decrypted message: " + decrypted + ".\n" + + "The encrypted message itself: " + new String(encrypted) + ); + throw new RuntimeException( + "Tested the generated public and private key but, " + + "the decrypted message doesn't match the original!" + ); + } + + Files.write(keyPath, key.getEncoded()); + } catch (Exception exception) { + logger.error("Error while creating key", exception); + } + } } diff --git a/common/src/main/java/org/geysermc/floodgate/config/updater/ConfigUpdater.java b/common/src/main/java/org/geysermc/floodgate/config/updater/ConfigUpdater.java index 76523e16..397245ee 100644 --- a/common/src/main/java/org/geysermc/floodgate/config/updater/ConfigUpdater.java +++ b/common/src/main/java/org/geysermc/floodgate/config/updater/ConfigUpdater.java @@ -36,6 +36,7 @@ import java.util.HashMap; import java.util.Map; import lombok.RequiredArgsConstructor; import org.geysermc.floodgate.api.logger.FloodgateLogger; +import org.geysermc.floodgate.config.loader.ConfigLoader; import org.yaml.snakeyaml.Yaml; @RequiredArgsConstructor @@ -45,7 +46,7 @@ public final class ConfigUpdater { private final ConfigFileUpdater fileUpdater; private final FloodgateLogger logger; - public void update(String defaultConfigLocation) { + public void update(ConfigLoader loader, String defaultConfigLocation) { Path configLocation = dataFolder.resolve("config.yml"); Map config; @@ -79,13 +80,27 @@ public final class ConfigUpdater { return; } } else { - logger.warn("You're using a pre-rewrite config file, please note that Floodgate will " + - "throw an exception if you didn't already update your Floodgate key" + - "(across all your servers, including Geyser). " + - "We'll still try to update the config," + - "but please regenerate the keys if it failed before asking for support."); + logger.warn("We've detected a pre-rewrite config file, please note that Floodgate " + + "doesn't not work properly if you don't update your Floodgate key used on " + + "all your servers (including Geyser). We'll try to update your Floodgate " + + "config now and we'll also generate a new Floodgate key for you, but if " + + "you're running a network or if you're running a Spigot server with " + + "Geyser Standalone please update as you'll no longer be able to connect."); renames.put("enabled", "enable"); //todo make dump system and add a boolean 'found-legacy-key' or something like that renames.put("allowed", "allow-linking"); + + // relocate the old key so that they can restore it if it was a new key + Path keyFilePath = dataFolder.resolve((String) config.get("key-file-name")); + if (Files.exists(keyFilePath)) { + try { + Files.copy(keyFilePath, dataFolder.resolve("old-key.pem")); + } catch (IOException exception) { + throw new RuntimeException( + "Failed to relocate the old key to make place for a new key", + exception); + } + } + loader.generateKey(keyFilePath); } try { diff --git a/pom.xml b/pom.xml index 4e790b45..1b167870 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ - 1.2.0-SNAPSHOT + 1.3.0-SNAPSHOT 1.0-SNAPSHOT 1.13-R0.1-SNAPSHOT 1.15-SNAPSHOT