Skip to content

Commit

Permalink
use NIO Spring Resource Abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
svencc committed Sep 16, 2023
1 parent d2dbb14 commit c0eb5b0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion runConfigurations/RECOM.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="RECOM" type="Application" factoryName="Application">
<option name="ALTERNATIVE_JRE_PATH" value="temurin-17" />
<option name="ALTERNATIVE_JRE_PATH" value="$USER_HOME$/.sdkman/candidates/java/17.0.5-tem" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="MAIN_CLASS_NAME" value="com.recom.Application" />
<module name="recom-backend" />
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/recom/property/RECOMSecurityProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class RECOMSecurityProperties {
private Duration jwtExpirationTime;
private Optional<String> keyPath;

public Optional<String> getKeyPath() {
public Optional<Path> getKeyPath() {
if (keyPath.isPresent() && !keyPath.get().isEmpty()) {
return Optional.of(keyPath.get());
return Optional.of(Path.of(keyPath.get()));
} else {
return Optional.empty();
}
Expand Down
59 changes: 33 additions & 26 deletions src/main/java/com/recom/security/rsa/RSAKeyGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;

import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -43,7 +46,7 @@ public RSAKey rsaKeyFactory() throws Exception {
}

@NonNull
private RSAKey hydrateKeyThroughFilesystem(@NonNull final String keyPath) throws Exception {
private RSAKey hydrateKeyThroughFilesystem(@NonNull final Path keyPath) throws Exception {
if (keypairExists(keyPath)) {
log.info("| +- Keys exist");

Expand Down Expand Up @@ -89,74 +92,78 @@ private KeyPair generateKeypair() throws NoSuchAlgorithmException {
return generator.generateKeyPair();
}

private boolean keypairExists(@NonNull final String keyPath) {
final File publicKeyFile = new File(Paths.get(keyPath, KeyType.PUBLIC.name().toLowerCase()).toString());
final File privateKeyFile = new File(Paths.get(keyPath, KeyType.PRIVATE.name().toLowerCase()).toString());
private boolean keypairExists(@NonNull final Path keyPath) {
final File publicKeyFile = new File(Paths.get(keyPath.toString(), KeyType.PUBLIC.name().toLowerCase()).toString());
final File privateKeyFile = new File(Paths.get(keyPath.toString(), KeyType.PRIVATE.name().toLowerCase()).toString());

return publicKeyFile.exists() && privateKeyFile.exists();
}

@NonNull
private PublicKey loadPublicKeyFromFile(@NonNull final String filePath) throws IOException, GeneralSecurityException {
final Path keyPath = Paths.get(filePath, KeyType.PUBLIC.name().toLowerCase());
log.info("| +- Load public key: '{}'", keyPath);
private PublicKey loadPublicKeyFromFile(@NonNull final Path filePath) throws IOException, GeneralSecurityException {
final Path keyPath = Paths.get(filePath.toString(), KeyType.PUBLIC.name().toLowerCase());
log.info("| +- Load public key: '{}'", keyPath.toAbsolutePath());

final byte[] keyBytes = Files.readAllBytes(keyPath);
final FileSystemResource fileSystemResource = new FileSystemResource(keyPath);
final byte[] keyBytes = fileSystemResource.getContentAsByteArray();
final X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
final KeyFactory keyFactory = KeyFactory.getInstance(Algorithm.RSA.name());

return keyFactory.generatePublic(spec);
}

@NonNull
private PrivateKey loadPrivateKeyFromFile(@NonNull final String filePath) throws IOException, GeneralSecurityException {
final Path keyPath = Paths.get(filePath, KeyType.PRIVATE.name().toLowerCase());
log.info("| +- Load private key: '{}'", keyPath);
private PrivateKey loadPrivateKeyFromFile(@NonNull final Path filePath) throws IOException, GeneralSecurityException {
final Path keyPath = Paths.get(filePath.toString(), KeyType.PRIVATE.name().toLowerCase());
log.info("| +- Load private key: '{}'", keyPath.toAbsolutePath());

final byte[] keyBytes = Files.readAllBytes(keyPath);
final FileSystemResource fileSystemResource = new FileSystemResource(keyPath);
final byte[] keyBytes = fileSystemResource.getContentAsByteArray();
final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
final KeyFactory keyFactory = KeyFactory.getInstance(Algorithm.RSA.name());

return keyFactory.generatePrivate(spec);
}

@NonNull
private String loadUUIDFromFile(@NonNull final String filePath) throws IOException, GeneralSecurityException {
final Path keyPath = Paths.get(filePath, "uuid");
log.info("| +- Load uuid: '{}'", keyPath);
private String loadUUIDFromFile(@NonNull final Path filePath) throws IOException, GeneralSecurityException {
final Path keyPath = Paths.get(filePath.toString(), "uuid");
log.info("| +- Load uuid: '{}'", keyPath.toAbsolutePath());

final byte[] uuidBytes = Files.readAllBytes(keyPath);

return new String(uuidBytes);
}

private void persistKeyToFile(
@NonNull final String keyPath,
@NonNull final Path keyPath,
@NonNull final KeyType keyType,
final byte[] keyBytes
) throws IOException {
final String filePath = Paths.get(keyPath, keyType.name().toLowerCase()).toString();
final Path filePath = Paths.get(keyPath.toString(), keyType.name().toLowerCase());

new File(filePath).getParentFile().mkdirs();
final FileSystemResource fileSystemResource = new FileSystemResource(filePath);
fileSystemResource.getFile().getParentFile().mkdirs();

try (FileOutputStream fos = new FileOutputStream(filePath)) {
log.info("| +- Save {} key: '{}'", keyType.name().toLowerCase(), filePath);
try (final OutputStream fos = fileSystemResource.getOutputStream()) {
log.info("| +- Save {} key: '{}'", keyType.name().toLowerCase(), filePath.toAbsolutePath());
fos.write(keyBytes);
} catch (IOException e) {
throw new IOException(e);
}
}

private void persistUUIDToFile(
@NonNull final String keyPath,
@NonNull final Path keyPath,
@NonNull final String uuid
) throws IOException {
final String filePath = Paths.get(keyPath, "uuid").toString();
final Path filePath = Paths.get(keyPath.toString(), "uuid");

new File(filePath).getParentFile().mkdirs();
final FileSystemResource fileSystemResource = new FileSystemResource(filePath);
fileSystemResource.getFile().getParentFile().mkdir();

try (FileOutputStream fos = new FileOutputStream(filePath)) {
log.info("| +- Save uuid: '{}'", filePath);
try (final OutputStream fos = fileSystemResource.getOutputStream()) {
log.info("| +- Save uuid: '{}'", filePath.toAbsolutePath());
fos.write(uuid.getBytes());
} catch (IOException e) {
throw new IOException(e);
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application-local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ logging.level.com.vladmihalcea.hibernate.type.util.LogUtils=DEBUG

recom.security.jwt-issuer=RECOM DEV Backend
recom.security.jwt-expiration-time=5m
recom.security.key-path=C:/RECOMKey
#recom.security.key-path=C:/RECOMKey
recom.security.key-path=RECOMKey

0 comments on commit c0eb5b0

Please sign in to comment.