Skip to content

Commit 5a4d4aa

Browse files
dsyerryanjbaxter
authored andcommitted
Add support for switching off profile validation (#3227)
Using the already documented spring.profiles.validate flag from Spring Boot. Signed-off-by: Dave Syer <david.syer@broadcom.com>
1 parent 93770ee commit 5a4d4aa

6 files changed

Lines changed: 64 additions & 4 deletions

File tree

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerEncryptionConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
package org.springframework.cloud.config.server.config;
1818

1919
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.boot.context.properties.bind.Binder;
2021
import org.springframework.cloud.config.server.encryption.EncryptionController;
2122
import org.springframework.cloud.config.server.encryption.TextEncryptorLocator;
2223
import org.springframework.context.annotation.Bean;
2324
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.core.env.Environment;
2426

2527
/**
2628
* @author Bartosz Wojtkiewicz
@@ -36,11 +38,18 @@ public class ConfigServerEncryptionConfiguration {
3638
@Autowired
3739
private ConfigServerProperties properties;
3840

41+
@Autowired
42+
private Environment environment;
43+
3944
@Bean
4045
public EncryptionController encryptionController() {
4146
EncryptionController controller = new EncryptionController(this.encryptor);
4247
controller.setDefaultApplicationName(this.properties.getDefaultApplicationName());
4348
controller.setDefaultProfile(this.properties.getDefaultProfile());
49+
boolean validateProfiles = Binder.get(this.environment)
50+
.bind("spring.profiles.validate", Boolean.class)
51+
.orElse(true);
52+
controller.setValidateProfiles(validateProfiles);
4453
return controller;
4554
}
4655

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerMvcConfiguration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2929
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
30+
import org.springframework.boot.context.properties.bind.Binder;
3031
import org.springframework.cloud.config.server.encryption.EnvironmentEncryptor;
3132
import org.springframework.cloud.config.server.encryption.ResourceEncryptor;
3233
import org.springframework.cloud.config.server.environment.EnvironmentController;
@@ -38,6 +39,7 @@
3839
import org.springframework.cloud.context.config.annotation.RefreshScope;
3940
import org.springframework.context.annotation.Bean;
4041
import org.springframework.context.annotation.Configuration;
42+
import org.springframework.core.env.Environment;
4143
import org.springframework.http.MediaType;
4244
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
4345
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -74,6 +76,14 @@ static class EnvironmentControllerConfiguration {
7476
@Autowired(required = false)
7577
private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
7678

79+
private boolean validateProfiles = true;
80+
81+
EnvironmentControllerConfiguration(Environment environment) {
82+
this.validateProfiles = Binder.get(environment)
83+
.bind("spring.profiles.validate", Boolean.class)
84+
.orElse(true);
85+
}
86+
7787
@Bean
7888
public EnvironmentController environmentController(EnvironmentRepository envRepository,
7989
ConfigServerProperties server) {
@@ -86,6 +96,7 @@ protected EnvironmentController delegateController(EnvironmentRepository envRepo
8696
this.objectMapper);
8797
controller.setStripDocumentFromYaml(server.isStripDocumentFromYaml());
8898
controller.setAcceptEmpty(server.isAcceptEmpty());
99+
controller.setValidateProfiles(this.validateProfiles);
89100
return controller;
90101
}
91102

@@ -97,6 +108,7 @@ public ResourceController resourceController(ResourceRepository repository, Envi
97108
this.resourceEncryptorMap);
98109
controller.setEncryptEnabled(server.getEncrypt().isEnabled());
99110
controller.setPlainTextEncryptEnabled(server.getEncrypt().isPlainTextEncrypt());
111+
controller.setValidateProfiles(this.validateProfiles);
100112
return controller;
101113
}
102114

@@ -119,6 +131,10 @@ private EnvironmentRepository encrypted(EnvironmentRepository envRepository, Con
119131
@ConditionalOnBean(org.springframework.cloud.context.scope.refresh.RefreshScope.class)
120132
static class RefreshableEnvironmentControllerConfiguration extends EnvironmentControllerConfiguration {
121133

134+
RefreshableEnvironmentControllerConfiguration(Environment environment) {
135+
super(environment);
136+
}
137+
122138
@Override
123139
@Bean
124140
@RefreshScope

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/encryption/EncryptionController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class EncryptionController {
6666

6767
private String defaultProfile = "default";
6868

69+
private boolean validateProfiles = true;
70+
6971
public EncryptionController(TextEncryptorLocator encryptorLocator) {
7072
this.encryptorLocator = encryptorLocator;
7173
}
@@ -151,7 +153,7 @@ private TextEncryptor getEncryptor(String name, String profiles, String data) {
151153
if (isInvalidEncodedLocation(name)) {
152154
throw new InvalidEnvironmentRequestException("Invalid request");
153155
}
154-
if (isInvalidProfiles(profiles)) {
156+
if (this.validateProfiles && isInvalidProfiles(profiles)) {
155157
throw new InvalidEnvironmentRequestException("Invalid request");
156158
}
157159

@@ -266,6 +268,10 @@ public ResponseEntity<Map<String, Object>> invalidCipher() {
266268
return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
267269
}
268270

271+
public void setValidateProfiles(boolean validateProfiles) {
272+
this.validateProfiles = validateProfiles;
273+
}
274+
269275
}
270276

271277
@SuppressWarnings("serial")

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public class EnvironmentController {
7979

8080
private boolean acceptEmpty = true;
8181

82+
private boolean validateProfiles = true;
83+
8284
public EnvironmentController(EnvironmentRepository repository) {
8385
this(repository, new ObjectMapper());
8486
}
@@ -105,6 +107,15 @@ public void setAcceptEmpty(boolean acceptEmpty) {
105107
this.acceptEmpty = acceptEmpty;
106108
}
107109

110+
/**
111+
* Flag to indicate that spring profiles are to be validated (default true). If set to
112+
* false, then profiles with invalid characters (e.g. '-') will throw an exception.
113+
* @param validateProfiles the flag to set
114+
*/
115+
public void setValidateProfiles(boolean validateProfiles) {
116+
this.validateProfiles = validateProfiles;
117+
}
118+
108119
@GetMapping(path = "/{name}/{profiles:(?!.*\\b\\.(?:ya?ml|properties|json)\\b).*}",
109120
produces = MediaType.APPLICATION_JSON_VALUE)
110121
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles) {
@@ -132,7 +143,7 @@ public Environment getEnvironment(String name, String profiles, String label, bo
132143
try {
133144
name = normalize(name);
134145
label = normalize(label);
135-
if (isInvalidProfiles(profiles)) {
146+
if (this.validateProfiles && isInvalidProfiles(profiles)) {
136147
throw new InvalidEnvironmentRequestException("Invalid request");
137148
}
138149
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin);

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public class ResourceController {
8282

8383
private boolean plainTextEncryptEnabled = false;
8484

85+
private boolean validateProfiles = true;
86+
8587
public ResourceController(ResourceRepository resourceRepository, EnvironmentRepository environmentRepository,
8688
Map<String, ResourceEncryptor> resourceEncryptorMap) {
8789
this.resourceRepository = resourceRepository;
@@ -104,6 +106,15 @@ public void setPlainTextEncryptEnabled(boolean plainTextEncryptEnabled) {
104106
this.plainTextEncryptEnabled = plainTextEncryptEnabled;
105107
}
106108

109+
/**
110+
* Flag to indicate that spring profiles are to be validated (default true). If set to
111+
* false, then profiles with invalid characters (e.g. '-') will throw an exception.
112+
* @param validateProfiles the flag to set
113+
*/
114+
public void setValidateProfiles(boolean validateProfiles) {
115+
this.validateProfiles = validateProfiles;
116+
}
117+
107118
@GetMapping("/{name}/{profile}/{label}/**")
108119
public String retrieve(@PathVariable String name, @PathVariable String profile, @PathVariable String label,
109120
ServletWebRequest request, @RequestParam(defaultValue = "true") boolean resolvePlaceholders,
@@ -145,7 +156,7 @@ synchronized String retrieve(ServletWebRequest request, String name, String prof
145156
boolean resolvePlaceholders, String acceptedCharset) throws IOException {
146157
name = normalize(name);
147158
label = normalize(label);
148-
if (isInvalidProfiles(profile)) {
159+
if (this.validateProfiles && isInvalidProfiles(profile)) {
149160
throw new InvalidEnvironmentRequestException("Invalid request");
150161
}
151162
path = normalize(path);
@@ -227,7 +238,7 @@ private synchronized byte[] binary(ServletWebRequest request, String name, Strin
227238
String path) throws IOException {
228239
name = normalize(name);
229240
label = normalize(label);
230-
if (isInvalidProfiles(profile)) {
241+
if (this.validateProfiles && isInvalidProfiles(profile)) {
231242
throw new InvalidEnvironmentRequestException("Invalid request");
232243
}
233244
path = normalize(path);

spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.web.util.pattern.PathPatternParser;
4545

4646
import static org.assertj.core.api.Assertions.assertThat;
47+
import static org.assertj.core.api.Assertions.assertThatNoException;
4748
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4849
import static org.assertj.core.api.Assertions.entry;
4950
import static org.mockito.ArgumentMatchers.any;
@@ -580,6 +581,12 @@ public void invalidProfileTests() {
580581
.isInstanceOf(InvalidEnvironmentRequestException.class);
581582
}
582583

584+
@Test
585+
public void invalidProfileTestsDisabled() {
586+
this.controller.setValidateProfiles(false);
587+
assertThatNoException().isThrownBy(() -> this.controller.labelled("application", "bar,..,foo", "label"));
588+
}
589+
583590
abstract class MockMvcTestCases {
584591

585592
protected MockMvc mvc;

0 commit comments

Comments
 (0)