Skip to content

Commit eec3005

Browse files
committed
Add client auth config validation
1 parent 6aff66c commit eec3005

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/main/java/io/specto/hoverfly/junit/core/config/HoverflyConfigValidator.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ HoverflyConfiguration validate(HoverflyConfiguration hoverflyConfig) {
4545
boolean isKeyBlank = StringUtils.isBlank(hoverflyConfig.getSslKeyPath());
4646
boolean isCertBlank = StringUtils.isBlank(hoverflyConfig.getSslCertificatePath());
4747
if (isKeyBlank && !isCertBlank || !isKeyBlank && isCertBlank) {
48-
throw new IllegalArgumentException("Both SSL key and certificate files are required to override the default Hoverfly SSL.");
48+
throw new IllegalArgumentException("Both ca cert and key files are required to override the default Hoverfly ca cert.");
4949
}
50+
51+
// Validate client auth cert and key
52+
boolean isClientKeyBlank = StringUtils.isBlank(hoverflyConfig.getClientKeyPath());
53+
boolean isClientCertBlank = StringUtils.isBlank(hoverflyConfig.getClientCertPath());
54+
if (isClientKeyBlank && !isClientCertBlank || !isClientKeyBlank && isClientCertBlank) {
55+
throw new IllegalArgumentException("Both client cert and key files are required to enable mutual TLS authentication.");
56+
}
57+
5058
// Validate proxy port
5159
if (hoverflyConfig.getProxyPort() == 0) {
5260
hoverflyConfig.setProxyPort(findUnusedPort());

src/test/java/io/specto/hoverfly/junit/core/config/HoverflyConfigValidatorTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,34 @@ public void shouldAssignPortForLocalHoverflyInstanceIfNotConfigured() {
4242
@Test
4343
public void shouldThrowExceptionIfOnlySslKeyIsConfigured() {
4444

45-
assertThatThrownBy(() -> localConfigs().sslKeyPath("ssl/ca.key").build())
45+
assertThatThrownBy(() -> localConfigs().caCert("", "ssl/ca.key").build())
4646
.isInstanceOf(IllegalArgumentException.class)
47-
.hasMessageContaining("Both SSL key and certificate files are required to override the default Hoverfly SSL");
47+
.hasMessageContaining("Both ca cert and key files are required to override the default Hoverfly ca cert.");
4848
}
4949

5050
@Test
5151
public void shouldThrowExceptionIfOnlySslCertIsConfigured() {
5252

53-
assertThatThrownBy(() -> localConfigs().sslCertificatePath("ssl/ca.crt").build())
53+
assertThatThrownBy(() -> localConfigs().caCert("ssl/ca.crt", "").build())
5454
.isInstanceOf(IllegalArgumentException.class)
55-
.hasMessageContaining("Both SSL key and certificate files are required to override the default Hoverfly SSL");
55+
.hasMessageContaining("Both ca cert and key files are required to override the default Hoverfly ca cert.");
56+
}
57+
58+
59+
@Test
60+
public void shouldThrowExceptionIfOnlyClientKeyIsConfigured() {
61+
62+
assertThatThrownBy(() -> localConfigs().clientAuth("", "ssl/ca.key").build())
63+
.isInstanceOf(IllegalArgumentException.class)
64+
.hasMessageContaining("Both client cert and key files are required to enable mutual TLS authentication.");
65+
}
5666

67+
@Test
68+
public void shouldThrowExceptionIfOnlyClientCertIsConfigured() {
69+
70+
assertThatThrownBy(() -> localConfigs().clientAuth("ssl/ca.crt", "").build())
71+
.isInstanceOf(IllegalArgumentException.class)
72+
.hasMessageContaining("Both client cert and key files are required to enable mutual TLS authentication.");
5773
}
5874

5975
@Test

0 commit comments

Comments
 (0)