Skip to content

Commit

Permalink
added truststore path ans pass to the properties
Browse files Browse the repository at this point in the history
  • Loading branch information
SrdjanStevanetic committed Nov 8, 2023
1 parent 999ea98 commit 5354aa7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ public class TranslationAppConstants {
public static final String BUILD_INFO = "build";
public static final String APP_INFO = "app";
public static final String CONFIG_INFO = "config";

public static final String RUNTIME_ENV_TEST = "test";

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import eu.europeana.api.translation.config.TranslationServiceProvider;
import eu.europeana.api.translation.definitions.service.exception.LangDetectionServiceConfigurationException;
import eu.europeana.api.translation.definitions.service.exception.TranslationServiceConfigurationException;
import eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand Down Expand Up @@ -127,6 +128,7 @@ static void setProperties(DynamicPropertyRegistry registry) {
registry.add("translation.google.projectId", () -> "project-id-test");
registry.add("translation.google.usehttpclient", () -> "true");
registry.add("redis.connection.url", () -> "redis://localhost:" + redisPort + "/");
registry.add("translation.runtime", () -> TranslationAppConstants.RUNTIME_ENV_TEST);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
Expand All @@ -31,6 +30,7 @@
import eu.europeana.api.commons.oauth2.service.impl.EuropeanaClientDetailsService;
import eu.europeana.api.translation.definitions.service.exception.LangDetectionServiceConfigurationException;
import eu.europeana.api.translation.definitions.service.exception.TranslationServiceConfigurationException;
import eu.europeana.api.translation.definitions.vocabulary.TranslationAppConstants;
import eu.europeana.api.translation.model.RedisCacheTranslation;
import eu.europeana.api.translation.serialization.JsonRedisSerializer;
import eu.europeana.api.translation.service.google.DummyGLangDetectService;
Expand All @@ -47,7 +47,7 @@

@Configuration()
@PropertySource("classpath:translation.properties")
@PropertySource(value = "translation.user.properties", ignoreResourceNotFound = true)
@PropertySource(value = "classpath:translation.user.properties", ignoreResourceNotFound = true)
public class TranslationApiAutoconfig implements ApplicationListener<ApplicationStartedEvent> {

@Value("${translation.dummy.services:false}")
Expand All @@ -56,12 +56,9 @@ public class TranslationApiAutoconfig implements ApplicationListener<Application
private final TranslationConfig translationConfig;
TranslationServiceProvider translationServiceConfigProvider;
private final Logger logger = LogManager.getLogger(TranslationApiAutoconfig.class);
private ResourceLoader resourceLoader;


public TranslationApiAutoconfig(@Autowired TranslationConfig translationConfig, @Autowired ResourceLoader resourceLoader) {
public TranslationApiAutoconfig(@Autowired TranslationConfig translationConfig) {
this.translationConfig = translationConfig;
this.resourceLoader=resourceLoader;
}

@Bean(BeanNames.BEAN_CLIENT_DETAILS_SERVICE)
Expand Down Expand Up @@ -156,32 +153,34 @@ public TranslationServiceProvider getTranslationServiceProvider() {
*/
@Bean(BeanNames.BEAN_REDIS_CACHE_LETTUCE_CONNECTION_FACTORY)
public LettuceConnectionFactory redisStandAloneConnectionFactory() throws IOException {
LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder = LettuceClientConfiguration.builder();
boolean sslEnabled=true;
if (sslEnabled){
SslOptions sslOptions = SslOptions.builder()
.jdkSslProvider()
.truststore(new File("C:\\opt\\app\\config\\myTrustStore.jks"), "truststorepass")
.build();

// SslOptions sslOptions = SslOptions.builder()
// .trustManager(resourceLoader.getResource("file:/opt/app/config/redis-certificate.pem").getFile())
// .build();

ClientOptions clientOptions = ClientOptions
.builder()
.sslOptions(sslOptions)
.build();

lettuceClientConfigurationBuilder
.clientOptions(clientOptions)
.useSsl();
//in case of integration tests, we do not need the SSL certificate
if(TranslationAppConstants.RUNTIME_ENV_TEST.equals(translationConfig.getRuntimeEnv())) {
return new LettuceConnectionFactory(LettuceConnectionFactory.createRedisConfiguration(translationConfig.getRedisConnectionUrl()));
}
else {
LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder = LettuceClientConfiguration.builder();
boolean sslEnabled=true;
if (sslEnabled){
SslOptions sslOptions = SslOptions.builder()
.jdkSslProvider()
.truststore(new File(translationConfig.getTruststorePath()), translationConfig.getTruststorePass())
.build();

ClientOptions clientOptions = ClientOptions
.builder()
.sslOptions(sslOptions)
.build();

lettuceClientConfigurationBuilder
.clientOptions(clientOptions)
.useSsl();
}

LettuceClientConfiguration lettuceClientConfiguration = lettuceClientConfigurationBuilder.build();

RedisConfiguration redisConf = LettuceConnectionFactory.createRedisConfiguration(translationConfig.getRedisConnectionUrl());
return new LettuceConnectionFactory(redisConf, lettuceClientConfiguration);
}

LettuceClientConfiguration lettuceClientConfiguration = lettuceClientConfigurationBuilder.build();

RedisConfiguration redisConf = LettuceConnectionFactory.createRedisConfiguration(translationConfig.getRedisConnectionUrl());
return new LettuceConnectionFactory(redisConf, lettuceClientConfiguration);
}

@Bean(BeanNames.BEAN_REDIS_CACHE_TEMPLATE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ public class TranslationConfig{

@Value("${redis.connection.url}")
private String redisConnectionUrl;


@Value("${truststore.path}")
private String truststorePath;

@Value("${truststore.password}")
private String truststorePass;

@Value("${translation.runtime}")
private String runtimeEnv;

public TranslationConfig() {
LOG.info("Initializing TranslConfigProperties bean.");
}
Expand Down Expand Up @@ -113,7 +122,6 @@ public void verifyRequiredProperties() {
missingProps.add("europeana.apikey.serviceurl");
}


if (!missingProps.isEmpty()) {
throw new IllegalStateException(String.format(
"The following config properties are not set: %s", String.join("\n", missingProps)));
Expand All @@ -127,5 +135,17 @@ public boolean useGoogleHttpClient() {
public String getRedisConnectionUrl() {
return redisConnectionUrl;
}

public String getTruststorePath() {
return truststorePath;
}

public String getTruststorePass() {
return truststorePass;
}

public String getRuntimeEnv() {
return runtimeEnv;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ europeana.apikey.serviceurl=
# For using dummy services
#translation.dummy.services=false

redis.connection.url=
redis.connection.url=

truststore.path=
truststore.password=

translation.runtime=application

0 comments on commit 5354aa7

Please sign in to comment.