Skip to content

Commit 91f6592

Browse files
committed
Allowed to deactivate certificate validation
1 parent 3c082d4 commit 91f6592

File tree

7 files changed

+43
-13
lines changed

7 files changed

+43
-13
lines changed

modules/hivemq-edge-module-opcua/src/main/java/com/hivemq/edge/adapters/opcua/client/ParsedConfig.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static Result<ParsedConfig, String> fromConfig(final OpcUaSpecificAdapter
5757
CertificateValidator certValidator = null;
5858
if (tlsEnabled) {
5959
final var truststore = adapterConfig.getTls().truststore();
60-
final var certOptional = getTrustedCerts(truststore).map(ParsedConfig::createServerCertificateValidator);
60+
final var certOptional = getTrustedCerts(truststore).map(trustedCerts -> createServerCertificateValidator(trustedCerts, adapterConfig.getTls().noChecks()));
6161
if (certOptional.isEmpty()) {
6262
return Failure.of("Failed to create certificate validator, check truststore configuration");
6363
}
@@ -124,10 +124,16 @@ public static Result<ParsedConfig, String> fromConfig(final OpcUaSpecificAdapter
124124
return Optional.of(KeystoreUtil.getCertificatesFromDefaultTruststore());
125125
}
126126

127-
private static @NotNull CertificateValidator createServerCertificateValidator(final @NotNull List<X509Certificate> trustedCerts) {
128-
return new DefaultClientCertificateValidator(new CertificateTrustListManager(trustedCerts),
129-
Set.of(ValidationCheck.VALIDITY, ValidationCheck.REVOCATION, ValidationCheck.REVOCATION_LISTS),
130-
new MemoryCertificateQuarantine());
127+
private static @NotNull CertificateValidator createServerCertificateValidator(final @NotNull List<X509Certificate> trustedCerts, final boolean noChecks) {
128+
if(noChecks) {
129+
return new DefaultClientCertificateValidator(new CertificateTrustListManager(trustedCerts),
130+
Set.of(),
131+
new MemoryCertificateQuarantine());
132+
} else {
133+
return new DefaultClientCertificateValidator(new CertificateTrustListManager(trustedCerts),
134+
Set.of(ValidationCheck.VALIDITY, ValidationCheck.REVOCATION, ValidationCheck.REVOCATION_LISTS),
135+
new MemoryCertificateQuarantine());
136+
}
131137
}
132138

133139
private static @NotNull Optional<KeystoreUtil.KeyPairWithChain> getKeyPairWithChain(final @NotNull Keystore keystore) {

modules/hivemq-edge-module-opcua/src/main/java/com/hivemq/edge/adapters/opcua/config/OpcUaSpecificAdapterConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public OpcUaSpecificAdapterConfig(
9090
this.overrideUri = requireNonNullElse(overrideUri, false);
9191
this.applicationUri = (applicationUri != null && !applicationUri.isBlank()) ? applicationUri : null;
9292
this.auth = auth;
93-
this.tls = requireNonNullElse(tls, new Tls(false, null, null));
93+
this.tls = requireNonNullElse(tls, new Tls(false, false, null, null));
9494
this.opcuaToMqttConfig =
9595
Objects.requireNonNullElseGet(opcuaToMqttConfig, () -> new OpcUaToMqttConfig(1, 1000));
9696
this.security = requireNonNullElse(security, new Security(Constants.DEFAULT_SECURITY_POLICY));
@@ -125,6 +125,8 @@ public OpcUaSpecificAdapterConfig(
125125
return applicationUri;
126126
}
127127

128+
129+
128130
@Override
129131
public boolean equals(final @Nullable Object o) {
130132
if (o == null || getClass() != o.getClass()) {

modules/hivemq-edge-module-opcua/src/main/java/com/hivemq/edge/adapters/opcua/config/Tls.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public record Tls (@JsonProperty("enabled")
3131
defaultValue = "false")
3232
boolean enabled,
3333

34+
@JsonProperty("noChecks")
35+
@ModuleConfigField(title = "Disable certificate validation",
36+
description = "Allows to disable the validation of a certificate",
37+
defaultValue = "false")
38+
boolean noChecks,
39+
3440
@JsonProperty("keystore")
3541
@JsonInclude(NON_NULL)
3642
@ModuleConfigField(title = "Keystore",
@@ -46,4 +52,19 @@ public record Tls (@JsonProperty("enabled")
4652
@JsonCreator
4753
public Tls{
4854
}
55+
56+
@Override
57+
public boolean equals(final Object o) {
58+
if (o == null || getClass() != o.getClass()) return false;
59+
final Tls tls = (Tls) o;
60+
return enabled() == tls.enabled() &&
61+
noChecks() == tls.noChecks() &&
62+
Objects.equals(keystore(), tls.keystore()) &&
63+
Objects.equals(truststore(), tls.truststore());
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(enabled(), noChecks(), keystore(), truststore());
69+
}
4970
}

modules/hivemq-edge-module-opcua/src/test/java/com/hivemq/edge/adapters/opcua/OpcUaEndpointFilterTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void whenSingleEndpointConfigSet_thenPickCorrectEndpoint() {
5656
false,
5757
null,
5858
null,
59-
new Tls(true, new Keystore("path", null, null), null),
59+
new Tls(true, false, new Keystore("path", null, null), null),
6060
null,
6161
null);
6262

@@ -115,7 +115,7 @@ public void whenMessageSecurityModeSpecified_thenFilterByMode() {
115115
false,
116116
null,
117117
null,
118-
new Tls(true, new Keystore("path", null, null), null),
118+
new Tls(true, false, new Keystore("path", null, null), null),
119119
null,
120120
null);
121121

@@ -152,7 +152,7 @@ public void whenNoMessageSecurityModeSpecified_thenAcceptAnyMode() {
152152
false,
153153
null,
154154
null,
155-
new Tls(true, new Keystore("path", null, null), null),
155+
new Tls(true, false, new Keystore("path", null, null), null),
156156
null,
157157
null);
158158

@@ -181,7 +181,7 @@ public void whenWrongMessageSecurityMode_thenNoEndpointSelected() {
181181
false,
182182
null,
183183
null,
184-
new Tls(true, new Keystore("path", null, null), null),
184+
new Tls(true, false, new Keystore("path", null, null), null),
185185
null,
186186
null);
187187

modules/hivemq-edge-module-opcua/src/test/java/com/hivemq/edge/adapters/opcua/OpcUaProtocolAdapterAuthTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void whenBasicAuthAndNoSubscriptions_thenConnectSuccessfully() {
145145
@Timeout(30)
146146
public void whenTlsAndNoSubscriptions_thenConnectSuccessfully() {
147147
final Security security = new Security(SecPolicy.NONE);
148-
final Tls tls = new Tls(true, null, null);
148+
final Tls tls = new Tls(true, false, null, null);
149149
final OpcUaSpecificAdapterConfig config = new OpcUaSpecificAdapterConfig(
150150
opcUaServerExtension.getServerUri(),
151151
false,
@@ -174,7 +174,7 @@ public void whenCertAuthAndNoSubscriptions_thenConnectSuccessfully() throws Exce
174174
final KeyChain root = KeyChain.createKeyChain("root");
175175

176176
final var keystore = root.wrapInKeyStoreWithPrivateKey("keystore", "root", "password", "password");
177-
final Tls tls = new Tls(true, new Keystore(keystore.getAbsolutePath(), "password", "password"), null);
177+
final Tls tls = new Tls(true, false, new Keystore(keystore.getAbsolutePath(), "password", "password"), null);
178178
final OpcUaSpecificAdapterConfig config = new OpcUaSpecificAdapterConfig(
179179
opcUaServerExtension.getServerUri(),
180180
false,

modules/hivemq-edge-module-opcua/src/test/java/com/hivemq/edge/adapters/opcua/client/ParsedConfigTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private OpcUaSpecificAdapterConfig createAdapterConfig(
357357
? new Truststore(truststorePath, KEYSTORE_PASSWORD)
358358
: null;
359359

360-
final Tls tls = new Tls(tlsEnabled, keystore, truststore);
360+
final Tls tls = new Tls(tlsEnabled, false, keystore, truststore);
361361
final Security security = new Security(SecPolicy.NONE);
362362
final OpcUaToMqttConfig opcUaToMqttConfig = new OpcUaToMqttConfig(1, 1000);
363363

modules/hivemq-edge-module-opcua/src/test/java/com/hivemq/edge/adapters/opcua/config/OpcUaProtocolAdapterConfigTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ public void unconvertConfigObject_full_valid() {
180180
null,
181181
new Auth(new BasicAuth("my-username", "my-password"), new X509Auth(true)),
182182
new Tls(true,
183+
false,
183184
new Keystore("my/keystore/path", "keystore-password", "private-key-password"),
184185
new Truststore("my/truststore/path", "truststore-password")),
185186
new OpcUaToMqttConfig(1, 1000),

0 commit comments

Comments
 (0)