Factor out encryption into a repository (not controller)

This commit is contained in:
Dave Syer
2015-10-01 10:02:38 +01:00
parent 309d1fbfbc
commit b024788be9
11 changed files with 217 additions and 96 deletions

View File

@@ -26,7 +26,6 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.EnvironmentControllerIntegrationTests.ControllerConfiguration;
import org.springframework.cloud.config.server.encryption.CipherEnvironmentEncryptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@@ -57,6 +56,7 @@ public class EnvironmentControllerIntegrationTests {
@Before
public void init() {
Mockito.reset(this.repository);
Mockito.when(this.repository.getDefaultLabel()).thenReturn("master");
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@@ -135,8 +135,7 @@ public class EnvironmentControllerIntegrationTests {
@Bean
public EnvironmentController controller() {
return new EnvironmentController(environmentRepository(),
new CipherEnvironmentEncryptor(null));
return new EnvironmentController(environmentRepository());
}
}

View File

@@ -30,7 +30,6 @@ import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.encryption.CipherEnvironmentEncryptor;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@@ -55,8 +54,7 @@ public class EnvironmentControllerTests {
@Before
public void init() {
Mockito.when(this.repository.getDefaultLabel()).thenReturn("master");
this.controller = new EnvironmentController(this.repository,
new CipherEnvironmentEncryptor(null));
this.controller = new EnvironmentController(this.repository);
}
@Test
@@ -256,26 +254,4 @@ public class EnvironmentControllerTests {
MockMvcResultMatchers.status().isBadRequest());
}
@Test
public void allowOverrideFalse() throws Exception {
this.controller.setOverrides(Collections.singletonMap("foo", "bar"));
Map<String, Object> map = new HashMap<String, Object>();
map.put("a.b.c", "d");
this.environment.add(new PropertySource("one", map));
Mockito.when(this.repository.findOne("foo", "bar", "master")).thenReturn(this.environment);
assertEquals("{foo=bar}", this.controller.defaultLabel("foo", "bar")
.getPropertySources().get(0).getSource().toString());
}
@Test
public void overrideWithEscapedPlaceholders() throws Exception {
this.controller.setOverrides(Collections.singletonMap("foo", "$\\{bar}"));
Map<String, Object> map = new HashMap<String, Object>();
map.put("bar", "foo");
this.environment.add(new PropertySource("one", map));
Mockito.when(this.repository.findOne("foo", "bar", "master")).thenReturn(this.environment);
assertEquals("{foo=${bar}}", this.controller.defaultLabel("foo", "bar")
.getPropertySources().get(0).getSource().toString());
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* 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 org.springframework.cloud.config.server;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
/**
* @author Dave Syer
* @author Roy Clarkson
*/
public class EnvironmentEncryptorEnvironmentRepositoryTests {
@Rule
public ExpectedException expected = ExpectedException.none();
private EnvironmentRepository repository = Mockito.mock(EnvironmentRepository.class);
private EnvironmentEncryptorEnvironmentRepository controller;
private Environment environment = new Environment("foo", "master");
@Before
public void init() {
Mockito.when(this.repository.getDefaultLabel()).thenReturn("master");
this.controller = new EnvironmentEncryptorEnvironmentRepository(this.repository);
}
@Test
public void allowOverrideFalse() throws Exception {
this.controller.setOverrides(Collections.singletonMap("foo", "bar"));
Map<String, Object> map = new HashMap<String, Object>();
map.put("a.b.c", "d");
this.environment.add(new PropertySource("one", map));
Mockito.when(this.repository.findOne("foo", "bar", "master")).thenReturn(this.environment);
assertEquals("{foo=bar}", this.controller.findOne("foo", "bar", "master")
.getPropertySources().get(0).getSource().toString());
}
@Test
public void overrideWithEscapedPlaceholders() throws Exception {
this.controller.setOverrides(Collections.singletonMap("foo", "$\\{bar}"));
Map<String, Object> map = new HashMap<String, Object>();
map.put("bar", "foo");
this.environment.add(new PropertySource("one", map));
Mockito.when(this.repository.findOne("foo", "bar", "master")).thenReturn(this.environment);
assertEquals("{foo=${bar}}", this.controller.findOne("foo", "bar", "master")
.getPropertySources().get(0).getSource().toString());
}
}

View File

@@ -51,7 +51,7 @@ public class ResourceControllerTests {
this.repository = new GenericResourceRepository(this.environmentRepository);
this.repository.setResourceLoader(this.context);
this.controller = new ResourceController(this.repository,
new EnvironmentController(this.environmentRepository));
this.environmentRepository);
this.context.close();
}