diff --git a/.gitignore b/.gitignore index f8f383a..d136912 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ build/ ### Secrets ### src/main/resources/firebase-private-key.json src/main/resources/firebase-private-key_disable.json +/secrets/ \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index ce43a3b..6f92a2f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,7 @@ volumes: nominatim-data: postgres_data: + jwt_keys: services: backend: @@ -11,6 +12,11 @@ services: - "8080:8080" env_file: - .env + environment: + RSA_PRIVATE_KEY_PATH: /data/keys/jwt-private.pem + RSA_PUBLIC_KEY_PATH: /data/keys/jwt-public.pem + volumes: + - jwt_keys:/data/keys depends_on: - db restart: unless-stopped diff --git a/src/main/java/SuMoC/backend/security/utils/RSAKeyProperties.java b/src/main/java/SuMoC/backend/security/utils/RSAKeyProperties.java index 9011c58..3b27aa1 100644 --- a/src/main/java/SuMoC/backend/security/utils/RSAKeyProperties.java +++ b/src/main/java/SuMoC/backend/security/utils/RSAKeyProperties.java @@ -1,36 +1,96 @@ package SuMoC.backend.security.utils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; +import java.security.KeyFactory; import java.security.KeyPair; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; @Component public class RSAKeyProperties { - private RSAPublicKey publicKey; - private RSAPrivateKey privateKey; + private final RSAPublicKey publicKey; + private final RSAPrivateKey privateKey; - public RSAKeyProperties(){ - KeyPair pair = KeyGeneratorUtility.generateRsaKey(); - this.publicKey = (RSAPublicKey) pair.getPublic(); - this.privateKey = (RSAPrivateKey) pair.getPrivate(); + public RSAKeyProperties( + @Value("${RSA_PUBLIC_KEY_PATH:/data/keys/jwt-public.pem}") String publicKeyPath, + @Value("${RSA_PRIVATE_KEY_PATH:/data/keys/jwt-private.pem}") String privateKeyPath + ) { + try { + Path pubPath = Paths.get(publicKeyPath); + Path privPath = Paths.get(privateKeyPath); + + if (pubPath.getParent() != null) Files.createDirectories(pubPath.getParent()); + if (privPath.getParent() != null) Files.createDirectories(privPath.getParent()); + + if (Files.exists(pubPath) && Files.exists(privPath)) { + this.publicKey = readPublicKey(pubPath); + this.privateKey = readPrivateKey(privPath); + return; + } + + KeyPair pair = KeyGeneratorUtility.generateRsaKey(); + RSAPublicKey pub = (RSAPublicKey) pair.getPublic(); + RSAPrivateKey priv = (RSAPrivateKey) pair.getPrivate(); + + writePublicKey(pubPath, pub); + writePrivateKey(privPath, priv); + + this.publicKey = pub; + this.privateKey = priv; + + } catch (Exception e) { + throw new IllegalStateException("Failed to load or generate RSA keys", e); + } } - public RSAPublicKey getPublicKey() { - return publicKey; + public RSAPublicKey getPublicKey() { return publicKey; } + public RSAPrivateKey getPrivateKey() { return privateKey; } + + // ---------- Read ---------- + private static RSAPublicKey readPublicKey(Path path) throws Exception { + String pem = Files.readString(path, StandardCharsets.UTF_8) + .replace("-----BEGIN PUBLIC KEY-----", "") + .replace("-----END PUBLIC KEY-----", "") + .replaceAll("\\s", ""); + byte[] der = Base64.getDecoder().decode(pem); + return (RSAPublicKey) KeyFactory.getInstance("RSA") + .generatePublic(new X509EncodedKeySpec(der)); } - public void setPublicKey(RSAPublicKey publicKey) { - this.publicKey = publicKey; + private static RSAPrivateKey readPrivateKey(Path path) throws Exception { + String pem = Files.readString(path, StandardCharsets.UTF_8) + .replace("-----BEGIN PRIVATE KEY-----", "") + .replace("-----END PRIVATE KEY-----", "") + .replaceAll("\\s", ""); + byte[] der = Base64.getDecoder().decode(pem); + return (RSAPrivateKey) KeyFactory.getInstance("RSA") + .generatePrivate(new PKCS8EncodedKeySpec(der)); } - public RSAPrivateKey getPrivateKey() { - return privateKey; + // ---------- Write ---------- + private static void writePublicKey(Path path, RSAPublicKey key) throws Exception { + writePem(path, "PUBLIC KEY", key.getEncoded()); } - public void setPrivateKey(RSAPrivateKey privateKey) { - this.privateKey = privateKey; + private static void writePrivateKey(Path path, RSAPrivateKey key) throws Exception { + writePem(path, "PRIVATE KEY", key.getEncoded()); + } + + private static void writePem(Path path, String type, byte[] derBytes) throws Exception { + String base64 = Base64.getMimeEncoder(64, new byte[]{'\n'}).encodeToString(derBytes); + String pem = "-----BEGIN " + type + "-----\n" + + base64 + "\n" + + "-----END " + type + "-----\n"; + Path tmp = Paths.get(path.toString() + ".tmp"); + Files.writeString(tmp, pem, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + Files.move(tmp, path, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8d8181f..d6aff90 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,7 +3,7 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/sumoc spring.datasource.username=postgres spring.datasource.password=admin spring.datasource.driver-class-name=org.postgresql.Driver -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect logging.level.org.springframework.security=Trace