Skip to content

Commit 3fb6b71

Browse files
svenzikmrts
authored andcommitted
Add configuration for allowing http connection during development
WE2-967 Signed-off-by: Sven Mitt <[email protected]>
1 parent 937c55e commit 3fb6b71

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

example/src/main/java/eu/webeid/example/config/YAMLConfig.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
package eu.webeid.example.config;
2424

25+
import java.net.InetAddress;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
28+
import java.net.UnknownHostException;
29+
import org.apache.commons.lang3.StringUtils;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
2532
import org.springframework.beans.factory.annotation.Value;
2633
import org.springframework.boot.context.properties.ConfigurationProperties;
2734
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -32,6 +39,8 @@
3239
@ConfigurationProperties(prefix = "web-eid-auth-token.validation")
3340
public class YAMLConfig {
3441

42+
private static final Logger LOG = LoggerFactory.getLogger(YAMLConfig.class);
43+
3544
@Value("local-origin")
3645
private String localOrigin;
3746

@@ -49,6 +58,22 @@ public String getLocalOrigin() {
4958
}
5059

5160
public void setLocalOrigin(String localOrigin) {
61+
if (StringUtils.endsWith(localOrigin, "/")) {
62+
throw new IllegalArgumentException("Configuration parameter local-origin cannot end with '/': " + localOrigin);
63+
}
64+
if (StringUtils.startsWith(localOrigin, "http:")) {
65+
try {
66+
if (InetAddress.getByName(new URI(localOrigin).getHost()).isLoopbackAddress()) {
67+
this.localOrigin = localOrigin.replaceFirst("^http:", "https:");
68+
LOG.warn("Configuration contains http protocol {}, which is not supported. Replacing it with secure {}", localOrigin, this.localOrigin);
69+
return;
70+
}
71+
} catch (URISyntaxException e) {
72+
LOG.error("Configuration parameter origin-local does not contain an URL: {}", localOrigin, e);
73+
} catch (UnknownHostException e) {
74+
LOG.error("Unable to determine if origin-local {} is loopback address", localOrigin, e);
75+
}
76+
}
5277
this.localOrigin = localOrigin;
5378
}
5479

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package eu.webeid.example.config;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import org.apache.commons.lang3.StringUtils;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.ValueSource;
11+
12+
class YAMLConfigTest {
13+
14+
@ValueSource(strings = {
15+
"http://localhost",
16+
"http://localhost:8080",
17+
"http://127.0.0.1",
18+
"http://127.0.0.1:8080",
19+
"http://::1",
20+
"http://[::1]:8080"
21+
})
22+
@ParameterizedTest
23+
void givenLocalOriginHttpLoopbackAddress_whenParsingLocalOrigin_thenItIsReplacedWithHttps(String origin) {
24+
YAMLConfig yamlConfig = new YAMLConfig();
25+
yamlConfig.setLocalOrigin(origin);
26+
assertThat(yamlConfig.getLocalOrigin()).isEqualTo(origin.replaceFirst("^http:", "https:"));
27+
}
28+
29+
@ValueSource(strings = {
30+
"https://localhost",
31+
"https://localhost:8080",
32+
"https://127.0.0.1",
33+
"https://127.0.0.1:8080",
34+
"https://::1",
35+
"https://[::1]:8080",
36+
})
37+
@ParameterizedTest
38+
void givenLocalOriginHttpsLoopbackAddress_whenParsingLocalOrigin_thenOriginalIsKept(String origin) {
39+
YAMLConfig yamlConfig = new YAMLConfig();
40+
yamlConfig.setLocalOrigin(origin);
41+
assertThat(yamlConfig.getLocalOrigin()).isEqualTo(origin);
42+
}
43+
44+
@ValueSource(strings = {
45+
"http://somename.app",
46+
"http://somename.app:8080",
47+
"http://8.8.8.8",
48+
"http://8.8.8.8:8080",
49+
"http://[2001:4860:4860::8888]",
50+
"http://[2001:4860:4860::8888]:8080",
51+
})
52+
@ParameterizedTest
53+
void givenLocalOriginHttpNonLoopbackAddress_whenParsingLocalOrigin_thenOriginalIsKept(String origin) {
54+
YAMLConfig yamlConfig = new YAMLConfig();
55+
yamlConfig.setLocalOrigin(origin);
56+
assertThat(yamlConfig.getLocalOrigin()).isEqualTo(origin);
57+
}
58+
59+
@ValueSource(strings = {
60+
"https://localhost/",
61+
"https://localhost:8080/"
62+
})
63+
@ParameterizedTest
64+
void givenLocalOriginThatEndsWithSlash_whenParsingLocalOrigin_thenExceptionIsThrown(String origin) {
65+
YAMLConfig yamlConfig = new YAMLConfig();
66+
assertThatThrownBy(() -> yamlConfig.setLocalOrigin(origin))
67+
.hasMessage("Configuration parameter local-origin cannot end with '/': " + origin);
68+
}
69+
}

0 commit comments

Comments
 (0)