Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(resources): update configuration properties with records #525

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package {{package}}.kms;

import org.springframework.beans.factory.annotation.Value;
import {{package}}.kms.config.model.KmsConnectionProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import software.amazon.awssdk.core.SdkBytes;
Expand All @@ -14,25 +15,21 @@ import software.amazon.awssdk.services.kms.model.EncryptionAlgorithmSpec;
import java.util.Base64;

@Component
@RequiredArgsConstructor
public class KmsAdapter // implements ModelRepository from domain
{
private final String keyId;
private final KmsAsyncClient kmsAsyncClient;

public KmsAdapter(@Value("${adapters.aws.kms.keyId}") String keyId, KmsAsyncClient kmsAsyncClient) {
this.keyId = keyId;
this.kmsAsyncClient = kmsAsyncClient;
}
private final KmsConnectionProperties kmsProperties;

public Mono<byte[]> decrypt(String secretKey) {
return Mono.fromFuture(kmsAsyncClient.decrypt(getDecryptRequest(secretKey, keyId)))
return Mono.fromFuture(kmsAsyncClient.decrypt(getDecryptRequest(secretKey, kmsProperties.keyId())))
.map(DecryptResponse::plaintext)
.map(SdkBytes::asByteArray)
.switchIfEmpty(Mono.error(new Throwable("Error decrypt secret")));
}

public Mono<String> encrypt(byte[] secretKey) {
return Mono.fromFuture(kmsAsyncClient.encrypt(getEncryptRequest(secretKey, keyId)))
return Mono.fromFuture(kmsAsyncClient.encrypt(getEncryptRequest(secretKey, kmsProperties.keyId())))
.map(EncryptResponse::ciphertextBlob)
.map(SdkBytes::asByteArray)
.map(Base64.getEncoder()::encodeToString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class KmsConfig {
private KmsAsyncClientBuilder getBuilder(KmsConnectionProperties kmsProperties, MetricPublisher publisher) {
return KmsAsyncClient.builder()
.overrideConfiguration(o -> o.addMetricPublisher(publisher))
.region(Region.of(kmsProperties.getRegion()));
.region(Region.of(kmsProperties.region()));
}
{{/metrics}}
{{^metrics}}
Expand All @@ -53,7 +53,7 @@ public class KmsConfig {
}

private KmsAsyncClientBuilder getBuilder(KmsConnectionProperties kmsProperties) {
return KmsAsyncClient.builder().region(Region.of(kmsProperties.getRegion()));
return KmsAsyncClient.builder().region(Region.of(kmsProperties.region()));
}
{{/metrics}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
package {{package}}.kms.config.model;

import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

{{#lombok}}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
{{/lombok}}
@Configuration
@ConfigurationProperties(prefix = "adapters.aws.kms")
public class KmsConnectionProperties {

String host;
String region;
String protocol;
Integer port;
String keyId;
{{^lombok}}
public KmsConnectionProperties(String host, String region, String protocol, Integer port, String keyId ) {
this.host = host;
this.region = region;
this.protocol = protocol;
this.port = port;
this.keyId = keyId;
}
{{/lombok}}
public record KmsConnectionProperties(
String host,
String region,
String protocol,
Integer port,
String keyId) {

public String getEndpoint() {
return String.format("%s://%s:%s", protocol, host, port);
}

}
20 changes: 10 additions & 10 deletions src/main/resources/driven-adapter/kms/kms-adapter.java.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package {{package}}.kms;

import org.springframework.beans.factory.annotation.Value;
import {{package}}.kms.config.model.KmsConnectionProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.kms.KmsAsyncClient;
Expand All @@ -12,27 +13,26 @@ import java.util.Base64;
import java.util.concurrent.ExecutionException;

@Component
@RequiredArgsConstructor
public class KmsAdapter // implements ModelRepository from domain
{
private final String keyId;
private final KmsAsyncClient kmsAsyncClient;

public KmsAdapter(@Value("${adapters.aws.kms.keyId}") String keyId, KmsAsyncClient kmsAsyncClient) {
this.keyId = keyId;
this.kmsAsyncClient = kmsAsyncClient;
}
private final KmsConnectionProperties kmsProperties;

public byte[] decrypt(String secretKey) throws ExecutionException, InterruptedException {
return kmsAsyncClient.decrypt(getDecryptRequest(secretKey, keyId))
return kmsAsyncClient.decrypt(getDecryptRequest(secretKey, kmsProperties.keyId()))
.get()
.plaintext()
.asByteArray();
}

public String encrypt(byte[] secretKey) throws ExecutionException, InterruptedException {
return Base64.getEncoder().encodeToString(kmsAsyncClient.encrypt(getEncryptRequest(secretKey, keyId))
return Base64.getEncoder().encodeToString(
kmsAsyncClient.encrypt(getEncryptRequest(secretKey, kmsProperties.keyId()))
.get()
.ciphertextBlob().asByteArray());
.ciphertextBlob()
.asByteArray()
);
}

private DecryptRequest getDecryptRequest(String secretKey, String keyId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class KmsConfig {
private KmsAsyncClientBuilder getBuilder(KmsConnectionProperties kmsProperties, MetricPublisher publisher) {
return KmsAsyncClient.builder()
.overrideConfiguration(o -> o.addMetricPublisher(publisher))
.region(Region.of(kmsProperties.getRegion()));
.region(Region.of(kmsProperties.region()));
}
{{/metrics}}
{{^metrics}}
Expand All @@ -53,7 +53,7 @@ public class KmsConfig {
}

private KmsAsyncClientBuilder getBuilder(KmsConnectionProperties kmsProperties) {
return KmsAsyncClient.builder().region(Region.of(kmsProperties.getRegion()));
return KmsAsyncClient.builder().region(Region.of(kmsProperties.region()));
}
{{/metrics}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
package {{package}}.kms.config.model;

import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

{{#lombok}}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
{{/lombok}}
@Configuration
@ConfigurationProperties(prefix = "adapters.aws.kms")
public class KmsConnectionProperties {

String host;
String region;
String protocol;
Integer port;
String keyId;
{{^lombok}}
public KmsConnectionProperties(String host, String region, String protocol, Integer port, String keyId ) {
this.host = host;
this.region = region;
this.protocol = protocol;
this.port = port;
this.keyId = keyId;
}
{{/lombok}}
public record KmsConnectionProperties(
String host,
String region,
String protocol,
Integer port,
String keyId) {

public String getEndpoint() {
return String.format("%s://%s:%s", protocol, host, port);
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package {{package}}.r2dbc.config;

import java.time.Duration;
import io.r2dbc.pool.ConnectionPool;
import io.r2dbc.pool.ConnectionPoolConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class PostgreSQLConnectionPool {
// TODO: change pool connection properties based on your resources.
Expand All @@ -17,28 +17,15 @@ public class PostgreSQLConnectionPool {
public static final int MAX_IDLE_TIME = 30;

@Bean
public ConnectionPool getConnectionConfig() {
// TODO: change these properties for yours
PostgresqlConnectionProperties pgProperties = new PostgresqlConnectionProperties();
pgProperties.setDatabase("postgres");
pgProperties.setHost("localhost");
pgProperties.setPort(5432);
pgProperties.setUsername("postgres");
pgProperties.setPassword("secret");
pgProperties.setSchema("public");

return buildConnectionConfiguration(pgProperties);
}

private ConnectionPool buildConnectionConfiguration(PostgresqlConnectionProperties properties) {
public ConnectionPool getConnectionConfig(PostgresqlConnectionProperties properties) {
PostgresqlConnectionConfiguration dbConfiguration = PostgresqlConnectionConfiguration.builder()
.host(properties.getHost())
.port(properties.getPort())
.database(properties.getDatabase())
.schema(properties.getSchema())
.username(properties.getUsername())
.password(properties.getPassword())
.build();
.host(properties.host())
.port(properties.port())
.database(properties.database())
.schema(properties.schema())
.username(properties.username())
.password(properties.password())
.build();

ConnectionPoolConfiguration poolConfiguration = ConnectionPoolConfiguration.builder()
.connectionFactory(new PostgresqlConnectionFactory(dbConfiguration))
Expand All @@ -51,4 +38,4 @@ public class PostgreSQLConnectionPool {

return new ConnectionPool(poolConfiguration);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
package {{package}}.r2dbc.config;

import {{package}}.r2dbc.config.PostgreSQLConnectionPool;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

class PostgreSQLConnectionPoolTest {

// TODO: change four you own tests
@InjectMocks
private PostgreSQLConnectionPool connectionPool;

@Mock
private PostgresqlConnectionProperties properties;


@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);

when(properties.host()).thenReturn("localhost");
when(properties.port()).thenReturn(5432);
when(properties.database()).thenReturn("dbName");
when(properties.schema()).thenReturn("schema");
when(properties.username()).thenReturn("username");
when(properties.password()).thenReturn("password");
}

@Test
void getConnectionConfig() {
PostgreSQLConnectionPool postgreSQLConnectionPool= new PostgreSQLConnectionPool();
Assertions.assertNotNull(postgreSQLConnectionPool.getConnectionConfig());
void getConnectionConfigSuccess() {
assertNotNull(connectionPool.getConnectionConfig(properties));
}
}
Loading
Loading