| 1 | package net.minecraft.server.jsonrpc.security;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import io.netty.handler.ssl.SslContext;
|
| 5 | import io.netty.handler.ssl.SslContextBuilder;
|
| 6 | import java.io.File;
|
| 7 | import java.io.FileInputStream;
|
| 8 | import java.io.InputStream;
|
| 9 | import java.security.KeyStore;
|
| 10 | import javax.net.ssl.KeyManagerFactory;
|
| 11 | import javax.net.ssl.TrustManagerFactory;
|
| 12 | import org.slf4j.Logger;
|
| 13 |
|
| 14 | public class JsonRpcSslContextProvider {
|
| 15 | private static final String PASSWORD_ENV_VARIABLE_KEY = "MINECRAFT_MANAGEMENT_TLS_KEYSTORE_PASSWORD";
|
| 16 | private static final String PASSWORD_SYSTEM_PROPERTY_KEY = "management.tls.keystore.password";
|
| 17 | private static final Logger log = LogUtils.getLogger();
|
| 18 |
|
| 19 | public static SslContext createFrom(final String keystorePath, final String keystorePasswordFromServerProperties) throws Exception {
|
| 20 | if (keystorePath.isEmpty()) {
|
| 21 | throw new IllegalArgumentException("TLS is enabled but keystore is not configured");
|
| 22 | } else {
|
| 23 | File file = new File(keystorePath);
|
| 24 | if (file.exists() && file.isFile()) {
|
| 25 | String keystorePassword = getKeystorePassword(keystorePasswordFromServerProperties);
|
| 26 | return loadKeystoreFromPath(file, keystorePassword);
|
| 27 | } else {
|
| 28 | throw new IllegalArgumentException("Supplied keystore is not a file or does not exist: '" + keystorePath + "'");
|
| 29 | }
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | private static String getKeystorePassword(final String keystorePasswordFromServerProperties) {
|
| 34 | String keystorePassword = System.getenv().get("MINECRAFT_MANAGEMENT_TLS_KEYSTORE_PASSWORD");
|
| 35 | if (keystorePassword != null) {
|
| 36 | return keystorePassword;
|
| 37 | }
|
| 38 |
|
| 39 | String systemPropertyKeystorePassword = System.getProperty("management.tls.keystore.password", null);
|
| 40 | return systemPropertyKeystorePassword != null ? systemPropertyKeystorePassword : keystorePasswordFromServerProperties;
|
| 41 | }
|
| 42 |
|
| 43 | private static SslContext loadKeystoreFromPath(final File keyStoreFile, final String password) throws Exception {
|
| 44 | KeyStore keyStore = KeyStore.getInstance("PKCS12");
|
| 45 |
|
| 46 | try (InputStream keystoreStream = new FileInputStream(keyStoreFile)) {
|
| 47 | keyStore.load(keystoreStream, password.toCharArray());
|
| 48 | }
|
| 49 |
|
| 50 | KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
| 51 | keyManagerFactory.init(keyStore, password.toCharArray());
|
| 52 | TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
| 53 | trustManagerFactory.init(keyStore);
|
| 54 | return SslContextBuilder.forServer(keyManagerFactory).trustManager(trustManagerFactory).build();
|
| 55 | }
|
| 56 |
|
| 57 | public static void printInstructions() {
|
| 58 | log.info("To use TLS for the management server, please follow these steps:");
|
| 59 | log.info("1. Set the server property 'management-server-tls-enabled' to 'true' to enable TLS");
|
| 60 | log.info("2. Create a keystore file of type PKCS12 containing your server certificate and private key");
|
| 61 | log.info("3. Set the server property 'management-server-tls-keystore' to the path of your keystore file");
|
| 62 | log.info(
|
| 63 | "4. Set the keystore password via the environment variable 'MINECRAFT_MANAGEMENT_TLS_KEYSTORE_PASSWORD', or system property 'management.tls.keystore.password', or server property 'management-server-tls-keystore-password'"
|
| 64 | );
|
| 65 | log.info("5. Restart the server to apply the changes.");
|
| 66 | }
|
| 67 | }
|
| 68 |
|