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(redis): moving config to a new class #1781

Merged
merged 1 commit into from
Apr 2, 2024
Merged
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
Expand Up @@ -59,11 +59,8 @@ import org.springframework.context.annotation.Primary
import org.springframework.core.Ordered
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
import org.springframework.session.data.redis.config.ConfigureRedisAction
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration
import org.springframework.util.CollectionUtils
import org.springframework.web.client.RestTemplate
import redis.clients.jedis.JedisPool
import retrofit.Endpoint

import java.util.concurrent.ExecutorService
Expand All @@ -75,57 +72,21 @@ import static retrofit.Endpoints.newFixedEndpoint
@Configuration
@Slf4j
@Import([PluginsAutoConfiguration, DeckPluginConfiguration, PluginWebConfiguration])
class GateConfig extends RedisHttpSessionConfiguration {
class GateConfig {

private ServiceClientProvider serviceClientProvider
private ConfigureRedisAction configureRedisAction

@Value('${server.session.timeout-in-seconds:3600}')
void setSessionTimeout(int maxInactiveIntervalInSeconds) {
super.setMaxInactiveIntervalInSeconds(maxInactiveIntervalInSeconds)
}

@Autowired
void setServiceClientProvider(ServiceClientProvider serviceClientProvider) {
this.serviceClientProvider = serviceClientProvider
}

void setConfigureRedisAction(ConfigureRedisAction configureRedisAction){
this.configureRedisAction = configureRedisAction;
}

@Autowired
GateConfig(@Value('${server.session.timeout-in-seconds:3600}') int maxInactiveIntervalInSeconds) {
super.setMaxInactiveIntervalInSeconds(maxInactiveIntervalInSeconds)
}

/**
* This pool is used for the rate limit storage, as opposed to the JedisConnectionFactory, which
* is a separate pool used for Spring Boot's session management.
*/
@Bean
JedisPool jedis(@Value('${redis.connection:redis://localhost:6379}') String connection,
@Value('${redis.timeout:2000}') int timeout) {
return new JedisPool(new URI(connection), timeout)
}

@Bean
@ConditionalOnMissingBean(RestTemplate)
RestTemplate restTemplate() {
new RestTemplate()
}

/**
* Always disable the ConfigureRedisAction that Spring Boot uses internally. Instead we use one
* qualified with @ConnectionPostProcessor. See
* {@link PostConnectionConfiguringJedisConnectionFactory}.
* */
@Bean
@Primary
ConfigureRedisAction springBootConfigureRedisAction() {
return ConfigureRedisAction.NO_OP
}

@Bean
ExecutorService executorService() {
Executors.newCachedThreadPool()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@

package com.netflix.spinnaker.gate.config;

import com.netflix.spinnaker.gate.config.PostConnectionConfiguringJedisConnectionFactory.ConnectionPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.session.data.redis.config.ConfigureRedisAction;

@Configuration
@ConditionalOnProperty("redis.configuration.secure")
public class RedisConfigSecure {
public class RedisActionConfig {

/**
* Always disable the ConfigureRedisAction that Spring Boot uses internally. Instead we use one
* qualified with @ConnectionPostProcessor. See {@link
* PostConnectionConfiguringJedisConnectionFactory}.
*/
@Bean
@ConnectionPostProcessor
@Primary
public ConfigureRedisAction springBootConfigureRedisAction() {
return ConfigureRedisAction.NO_OP;
}

@Bean
@ConditionalOnProperty("redis.configuration.secure")
@PostConnectionConfiguringJedisConnectionFactory.ConnectionPostProcessor
public ConfigureRedisAction connectionPostProcessorConfigureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.netflix.spinnaker.gate.config;

import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
import redis.clients.jedis.JedisPool;

@Configuration
public class RedisConfig extends RedisHttpSessionConfiguration {

@Value("${server.session.timeout-in-seconds:3600}")
public void setSessionTimeout(int maxInactiveIntervalInSeconds) {
super.setMaxInactiveIntervalInSeconds(maxInactiveIntervalInSeconds);
}

@Autowired
public RedisConfig(
@Value("${server.session.timeout-in-seconds:3600}") int maxInactiveIntervalInSeconds) {
super.setMaxInactiveIntervalInSeconds(maxInactiveIntervalInSeconds);
}

/**
* This pool is used for the rate limit storage, as opposed to the JedisConnectionFactory, which
* is a separate pool used for Spring Boot's session management.
*/
@Bean
public JedisPool jedis(
@Value("${redis.connection:redis://localhost:6379}") String connection,
@Value("${redis.timeout:2000}") int timeout)
throws URISyntaxException {
return new JedisPool(new URI(connection), timeout);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 OpsMx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.gate.config;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.session.data.redis.config.ConfigureRedisAction;

class RedisConfigTest {

@Test
public void testCircularDependenciesException() {
ApplicationContextRunner applicationContextRunner =
new ApplicationContextRunner()
.withUserConfiguration(RedisConfig.class, RedisActionConfig.class)
.withBean(PostConnectionConfiguringJedisConnectionFactory.class);
assertDoesNotThrow(
() ->
applicationContextRunner.run(
ctx -> assertThat(ctx).hasSingleBean(ConfigureRedisAction.class)));
}

@Test
public void testCircularDependenciesExceptionSecure() {
ApplicationContextRunner applicationContextRunner =
new ApplicationContextRunner()
.withUserConfiguration(RedisConfig.class, RedisActionConfig.class)
.withBean(PostConnectionConfiguringJedisConnectionFactory.class)
.withPropertyValues("redis.configuration.secure", "true");

assertDoesNotThrow(
() ->
applicationContextRunner.run(
ctx -> assertThat(ctx).getBeans(ConfigureRedisAction.class).hasSize(2)));
}
}