From 0d7f97af5e3eff8282494e48ad26cf6813efcbee Mon Sep 17 00:00:00 2001 From: Lukas Laaser Date: Fri, 23 Jan 2026 10:05:46 +0100 Subject: [PATCH] Bugfix: keysave should work now on docker and device --- docker-compose.yml | 6 ++--- .../security/utils/RSAKeyProperties.java | 25 ++++++++++--------- .../java/SuMoC/backend/user/models/User.java | 1 + 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 917ec49..5d7f3b5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,10 +13,10 @@ services: env_file: - .env environment: - RSA_PRIVATE_KEY_PATH: ./keys/jwt-private.pem - RSA_PUBLIC_KEY_PATH: ./keys/jwt-public.pem + RSA_PRIVATE_KEY_PATH: /data/keys/jwt-private.pem + RSA_PUBLIC_KEY_PATH: /data/keys/jwt-public.pem volumes: - - jwt_keys:./keys + - 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 e492317..84da7ba 100644 --- a/src/main/java/SuMoC/backend/security/utils/RSAKeyProperties.java +++ b/src/main/java/SuMoC/backend/security/utils/RSAKeyProperties.java @@ -20,15 +20,14 @@ public class RSAKeyProperties { private final RSAPrivateKey privateKey; public RSAKeyProperties( - @Value("${RSA_PUBLIC_KEY_PATH:./keys/jwt-public.pem}") String publicKeyPath, - @Value("${RSA_PRIVATE_KEY_PATH:./keys/jwt-private.pem}") String privateKeyPath + @Value("${security.rsa.public-key-path}") String publicKeyPath, + @Value("${security.rsa.private-key-path}") 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()); + Files.createDirectories(pubPath.getParent()); if (Files.exists(pubPath) && Files.exists(privPath)) { this.publicKey = readPublicKey(pubPath); @@ -36,21 +35,23 @@ public class RSAKeyProperties { return; } + if (!Files.isWritable(pubPath.getParent())) { + throw new IllegalStateException("Key directory not writable: " + pubPath.getParent()); + } + KeyPair pair = KeyGeneratorUtility.generateRsaKey(); - RSAPublicKey pub = (RSAPublicKey) pair.getPublic(); - RSAPrivateKey priv = (RSAPrivateKey) pair.getPrivate(); + this.publicKey = (RSAPublicKey) pair.getPublic(); + this.privateKey = (RSAPrivateKey) pair.getPrivate(); - writePublicKey(pubPath, pub); - writePrivateKey(privPath, priv); - - this.publicKey = pub; - this.privateKey = priv; + writePublicKey(pubPath, this.publicKey); + writePrivateKey(privPath, this.privateKey); } catch (Exception e) { throw new IllegalStateException("Failed to load or generate RSA keys", e); } } + public RSAPublicKey getPublicKey() { return publicKey; } public RSAPrivateKey getPrivateKey() { return privateKey; } @@ -91,6 +92,6 @@ public class RSAKeyProperties { "-----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); + Files.move(tmp, path, StandardCopyOption.REPLACE_EXISTING); } } diff --git a/src/main/java/SuMoC/backend/user/models/User.java b/src/main/java/SuMoC/backend/user/models/User.java index 0865f11..307415e 100644 --- a/src/main/java/SuMoC/backend/user/models/User.java +++ b/src/main/java/SuMoC/backend/user/models/User.java @@ -12,6 +12,7 @@ import lombok.Setter; import java.util.HashSet; import java.util.Set; +import java.util.UUID; @Entity @Table(name = "users")