Skip to content

Commit

Permalink
make auth nullable again to avoid empty strings in the config that ca…
Browse files Browse the repository at this point in the history
…n not be parsed back.
  • Loading branch information
DC2-DanielKrueger committed Dec 11, 2024
1 parent 7610e3d commit 1884c56
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.hivemq.edge.adapters.opcua.client;

import com.google.common.collect.ImmutableList;
import com.hivemq.edge.adapters.opcua.config.Auth;
import com.hivemq.edge.adapters.opcua.config.BasicAuth;
import com.hivemq.edge.adapters.opcua.config.Keystore;
import com.hivemq.edge.adapters.opcua.config.OpcUaSpecificAdapterConfig;
Expand Down Expand Up @@ -50,8 +51,8 @@ public class OpcUaClientConfigurator implements Function<OpcUaClientConfigBuilde
private final @NotNull OpcUaSpecificAdapterConfig adapterConfig;
private final @NotNull String adapterId;

public OpcUaClientConfigurator(final @NotNull OpcUaSpecificAdapterConfig adapterConfig,
final @NotNull String adapterId) {
public OpcUaClientConfigurator(
final @NotNull OpcUaSpecificAdapterConfig adapterConfig, final @NotNull String adapterId) {
this.adapterConfig = adapterConfig;
this.adapterId = adapterId;
}
Expand Down Expand Up @@ -112,10 +113,9 @@ public OpcUaClientConfigurator(final @NotNull OpcUaSpecificAdapterConfig adapter

private boolean checkAuthEnabled() {
//check that at least one auth method (Basic or X509) is enabled
return adapterConfig.getAuth() != null &&
(adapterConfig.getAuth().getBasicAuth() != null ||
(adapterConfig.getAuth().getX509Auth() != null &&
adapterConfig.getAuth().getX509Auth().isEnabled()));
final Auth auth = adapterConfig.getAuth();
return auth != null &&
(auth.getBasicAuth() != null || (auth.getX509Auth() != null && auth.getX509Auth().isEnabled()));
}

private void configureIdentityProvider(
Expand All @@ -124,16 +124,20 @@ private void configureIdentityProvider(
final @Nullable KeystoreUtil.KeyPairWithChain keyPairWithChain) {

final ImmutableList.Builder<IdentityProvider> identityProviderBuilder = ImmutableList.builder();
final X509Auth x509Auth = adapterConfig.getAuth().getX509Auth();
final boolean x509AuthEnabled = x509Auth != null && x509Auth.isEnabled();
if (x509AuthEnabled && tlsEnabled && keyPairWithChain != null) {
identityProviderBuilder.add(new X509IdentityProvider(Arrays.asList(keyPairWithChain.getCertificateChain()),
keyPairWithChain.getPrivateKey()));
}
final Auth auth = adapterConfig.getAuth();

if (auth != null) {
final X509Auth x509Auth = auth.getX509Auth();
final boolean x509AuthEnabled = x509Auth != null && x509Auth.isEnabled();
if (x509AuthEnabled && tlsEnabled && keyPairWithChain != null) {
identityProviderBuilder.add(new X509IdentityProvider(Arrays.asList(keyPairWithChain.getCertificateChain()),
keyPairWithChain.getPrivateKey()));
}

if (adapterConfig.getAuth().getBasicAuth() != null) {
final BasicAuth basicAuth = adapterConfig.getAuth().getBasicAuth();
identityProviderBuilder.add(new UsernameProvider(basicAuth.getUsername(), basicAuth.getPassword()));
if (auth.getBasicAuth() != null) {
final BasicAuth basicAuth = auth.getBasicAuth();
identityProviderBuilder.add(new UsernameProvider(basicAuth.getUsername(), basicAuth.getPassword()));
}
}

final ImmutableList<IdentityProvider> identityProviders = identityProviderBuilder.build();
Expand All @@ -145,7 +149,7 @@ private void configureIdentityProvider(
}

@NotNull
private DefaultClientCertificateValidator createServerCertificateValidator(@NotNull Tls tlsConfig) {
private DefaultClientCertificateValidator createServerCertificateValidator(@NotNull final Tls tlsConfig) {
final List<X509Certificate> trustedCerts;
final boolean truststoreAvailable = checkTruststoreAvailable(tlsConfig);
if (truststoreAvailable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public class OpcUaSpecificAdapterConfig implements ProtocolSpecificAdapterConfig
defaultValue = "false")
private final boolean overrideUri;

// @JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("auth")
private final @NotNull Auth auth;
@JsonInclude(JsonInclude.Include.NON_NULL)
private final @Nullable Auth auth;

@JsonProperty("tls")
private final @NotNull Tls tls;
Expand Down Expand Up @@ -94,7 +94,7 @@ public OpcUaSpecificAdapterConfig(
return uri;
}

public @NotNull Auth getAuth() {
public @Nullable Auth getAuth() {
return auth;
}

Expand Down

0 comments on commit 1884c56

Please sign in to comment.