Check for existing TextEncryptor and create a TextEncryptorLocator
This addresses the corner case where encrypt.key is provided so a TextEncryptor is available in the bootstrap context. Previously it was not used and the /encrypt endpoints wouldn't work. Fixes gh-760
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
package org.springframework.cloud.config.server.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -32,6 +34,7 @@ import org.springframework.cloud.config.server.encryption.TextEncryptorLocator;
|
||||
import org.springframework.cloud.context.encrypt.EncryptorFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.security.crypto.encrypt.Encryptors;
|
||||
import org.springframework.security.crypto.encrypt.TextEncryptor;
|
||||
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
|
||||
@@ -50,30 +53,9 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(KeyProperties.class)
|
||||
@Import({SingleTextEncryptorConfiguration.class, DefaultTextEncryptorConfiguration.class})
|
||||
public class EncryptionAutoConfiguration {
|
||||
|
||||
@ConditionalOnMissingBean(TextEncryptor.class)
|
||||
protected static class DefaultTextEncryptorConfiguration {
|
||||
|
||||
@Autowired
|
||||
private KeyProperties key;
|
||||
|
||||
@Autowired(required = false)
|
||||
private TextEncryptorLocator locator;
|
||||
|
||||
@Bean
|
||||
public TextEncryptor defaultTextEncryptor() {
|
||||
if (locator != null) {
|
||||
return new LocatorTextEncryptor(locator);
|
||||
}
|
||||
if (StringUtils.hasText(this.key.getKey())) {
|
||||
return new EncryptorFactory().create(this.key.getKey());
|
||||
}
|
||||
return Encryptors.noOpText();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnProperty(value = "spring.cloud.config.server.encrypt.enabled", matchIfMissing = true)
|
||||
protected static class EncryptorConfiguration {
|
||||
@@ -120,3 +102,42 @@ public class EncryptionAutoConfiguration {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ConditionalOnBean(TextEncryptor.class)
|
||||
@ConditionalOnMissingBean(TextEncryptorLocator.class)
|
||||
@Configuration
|
||||
class SingleTextEncryptorConfiguration {
|
||||
|
||||
@Autowired
|
||||
private TextEncryptor encryptor;
|
||||
|
||||
@Bean
|
||||
public SingleTextEncryptorLocator textEncryptorLocator() {
|
||||
return new SingleTextEncryptorLocator(encryptor);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ConditionalOnMissingBean(TextEncryptor.class)
|
||||
@Configuration
|
||||
class DefaultTextEncryptorConfiguration {
|
||||
|
||||
@Autowired
|
||||
private KeyProperties key;
|
||||
|
||||
@Autowired(required = false)
|
||||
private TextEncryptorLocator locator;
|
||||
|
||||
@Bean
|
||||
public TextEncryptor defaultTextEncryptor() {
|
||||
if (locator != null) {
|
||||
return new LocatorTextEncryptor(locator);
|
||||
}
|
||||
if (StringUtils.hasText(this.key.getKey())) {
|
||||
return new EncryptorFactory().create(this.key.getKey());
|
||||
}
|
||||
return Encryptors.noOpText();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2016-2017 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.encryption;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.cloud.config.server.ConfigServerApplication;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SymmetricEncryptionIntegrationTests {
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = {
|
||||
ConfigServerApplication.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
properties = "encrypt.key=foobar")
|
||||
@ActiveProfiles({ "test", "native" })
|
||||
@DirtiesContext
|
||||
public static class ConfigSymmetricEncryptionIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate testRestTemplate;
|
||||
|
||||
@Test
|
||||
public void symmetricEncryptionEnabled() throws Exception {
|
||||
ResponseEntity<String> entity = testRestTemplate
|
||||
.getForEntity("/encrypt/status", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { ConfigServerApplication.class}, properties = "spring.cloud.bootstrap.name:symmetric-key-bootstrap", webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles({ "test", "native" })
|
||||
@DirtiesContext
|
||||
public static class BootstrapConfigSymmetricEncryptionIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate testRestTemplate;
|
||||
|
||||
@Test
|
||||
public void symmetricEncryptionBootstrapConfig() throws Exception {
|
||||
ResponseEntity<String> entity = testRestTemplate
|
||||
.getForEntity("/encrypt/status", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user