Skip to content

Commit f064b71

Browse files
authored
Added config params for k8s (#1238)
* Added config params for k8s * Update docker config and rendering * Fix password comparison * More efficient default password check
1 parent f022df4 commit f064b71

File tree

11 files changed

+309
-40
lines changed

11 files changed

+309
-40
lines changed

docker/config-k8s.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,55 @@
7171
</https-listener>
7272
${IF:HIVEMQ_HTTPS_ENABLED}
7373
</listeners>
74+
${IF:HIVEMQ_LDAP_ENABLED}
75+
<ldap>
76+
<servers>
77+
<ldap-server>
78+
<host>${ENV:HIVEMQ_LDAP_SERVER1_HOST}</host>
79+
<port>${ENV:HIVEMQ_LDAP_SERVER1_PORT}</port>
80+
</ldap-server>
81+
${IF:HIVEMQ_LDAP_SERVER2_ENABLED}
82+
<ldap-server>
83+
<host>${ENV:HIVEMQ_LDAP_SERVER2_HOST}</host>
84+
<port>${ENV:HIVEMQ_LDAP_SERVER2_PORT}</port>
85+
</ldap-server>
86+
${IF:HIVEMQ_LDAP_SERVER2_ENABLED}
87+
${IF:HIVEMQ_LDAP_SERVER3_ENABLED}
88+
<ldap-server>
89+
<host>${ENV:HIVEMQ_LDAP_SERVER3_HOST}</host>
90+
<port>${ENV:HIVEMQ_LDAP_SERVER3_PORT}</port>
91+
</ldap-server>
92+
${IF:HIVEMQ_LDAP_SERVER3_ENABLED}
93+
</servers>
94+
95+
<tls-mode>${ENV:HIVEMQ_LDAP_TLS_MODE}</tls-mode>
96+
${IF:HIVEMQ_LDAP_TLS_TRUSTSTORE_ENABLED}
97+
<tls>
98+
<truststore-path>${ENV:HIVEMQ_LDAP_TRUSTSTORE_PATH}</truststore-path>
99+
<truststore-password>${ENV:HIVEMQ_LDAP_TRUSTSTORE_PASSWORD}</truststore-password>
100+
<truststore-type>JKS</truststore-type>
101+
</tls>
102+
${IF:HIVEMQ_LDAP_TLS_TRUSTSTORE_ENABLED}
103+
104+
<simple-bind>
105+
<rdns>${ENV:HIVEMQ_LDAP_SIMPLEBIND_RDNS}</rdns>
106+
<userPassword>${ENV:HIVEMQ_LDAP_SIMPLEBIND_PASSWORD}</userPassword>
107+
</simple-bind>
108+
109+
<uid-attribute>${ENV:HIVEMQ_LDAP_UID}</uid-attribute>
110+
<rdns>${ENV:HIVEMQ_LDAP_RDNS}</rdns>
111+
<directory-descent>${ENV:HIVEMQ_LDAP_DIRECTORY_DESCENT}</directory-descent>
112+
${IF:HIVEMQ_LDAP_OBJECT_CLASS_ENABLED}
113+
<required-object-class>${ENV:HIVEMQ_LDAP_OBJECT_CLASS}</required-object-class>
114+
${IF:HIVEMQ_LDAP_OBJECT_CLASS_ENABLED}
115+
116+
<max-connections>${ENV:HIVEMQ_LDAP_MAX_CONNECTION}</max-connections>
117+
<connect-timeout-millis>${ENV:HIVEMQ_LDAP_CONNECT_TIMEOUT_MS}</connect-timeout-millis>
118+
<response-timeout-millis>${ENV:HIVEMQ_LDAP_RESPONSE_TIMEOUT_MS}</response-timeout-millis>
119+
<search-timeout-seconds>${ENV:HIVEMQ_LDAP_SEARCH_TIMEOUT_S}</search-timeout-seconds>
120+
</ldap>
121+
${IF:HIVEMQ_LDAP_ENABLED}
122+
${IF:HIVEMQ_USERS_ENABLED}
74123
<users>
75124
<user>
76125
<username>${ENV:HIVEMQ_ADMIN_USER}</username>
@@ -80,6 +129,7 @@
80129
</roles>
81130
</user>
82131
</users>
132+
${IF:HIVEMQ_USERS_ENABLED}
83133
</admin-api>
84134
<persistence>
85135
<mode>${ENV:HIVEMQ_PERSISTENCE_MODE}</mode>

hivemq-edge/src/main/java/com/hivemq/api/auth/provider/impl/ldap/LdapConnectionProperties.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030

3131
import javax.net.ssl.SSLContext;
3232
import java.security.GeneralSecurityException;
33+
import java.util.Arrays;
3334
import java.util.List;
35+
import java.util.Objects;
3436

3537
import static com.hivemq.api.auth.ApiRoles.ADMIN;
3638
import static java.util.Arrays.stream;
@@ -83,17 +85,50 @@ public static LdapSimpleBind fromEntity(final @NotNull LdapSimpleBindEntity ldap
8385
ldapSimpleBindEntity.getRdns(),
8486
ldapSimpleBindEntity.getUserPassword());
8587
}
88+
89+
@Override
90+
public boolean equals(final Object o) {
91+
if (o == null || getClass() != o.getClass()) return false;
92+
final LdapSimpleBind that = (LdapSimpleBind) o;
93+
return Objects.equals(rdns(), that.rdns()) && Objects.equals(userPassword(), that.userPassword());
94+
}
95+
96+
@Override
97+
public int hashCode() {
98+
return Objects.hash(rdns(), userPassword());
99+
}
86100
}
87101

88102
/**
89103
* This class represents the simple bind credentials for an LDAP connection.
90104
*/
91105
public record LdapServers (@NotNull String[] hosts, int @NotNull [] ports){
106+
107+
/**
108+
* Compact constructor that makes defensive copies of the arrays to ensure immutability.
109+
*/
110+
public LdapServers {
111+
hosts = hosts.clone();
112+
ports = ports.clone();
113+
}
114+
92115
public static LdapServers fromEntity(final @NotNull List<LdapServerEntity> ldapServerEntities) {
93116
final String[] hosts = ldapServerEntities.stream().map(LdapServerEntity::getHost).toArray(String[]::new);
94117
final int[] ports = ldapServerEntities.stream().mapToInt(LdapServerEntity::getPort).toArray();
95118
return new LdapServers(hosts, ports);
96119
}
120+
121+
@Override
122+
public boolean equals(final Object o) {
123+
if (o == null || getClass() != o.getClass()) return false;
124+
final LdapServers that = (LdapServers) o;
125+
return Objects.deepEquals(hosts(), that.hosts()) && Objects.deepEquals(ports(), that.ports());
126+
}
127+
128+
@Override
129+
public int hashCode() {
130+
return Objects.hash(Arrays.hashCode(hosts()), Arrays.hashCode(ports()));
131+
}
97132
}
98133

99134
/**
@@ -106,6 +141,20 @@ public static TrustStore fromEntity(final @NotNull TrustStoreEntity trustStoreEn
106141
trustStoreEntity.getTrustStorePassword(),
107142
trustStoreEntity.getTrustStoreType());
108143
}
144+
145+
@Override
146+
public boolean equals(final Object o) {
147+
if (o == null || getClass() != o.getClass()) return false;
148+
final TrustStore that = (TrustStore) o;
149+
return Objects.equals(trustStorePath(), that.trustStorePath()) &&
150+
Objects.equals(trustStoreType(), that.trustStoreType()) &&
151+
Objects.equals(trustStorePassword(), that.trustStorePassword());
152+
}
153+
154+
@Override
155+
public int hashCode() {
156+
return Objects.hash(trustStorePath(), trustStorePassword(), trustStoreType());
157+
}
109158
}
110159

111160
/**
@@ -280,4 +329,42 @@ public static TrustStore fromEntity(final @NotNull TrustStoreEntity trustStoreEn
280329

281330
return options;
282331
}
332+
333+
@Override
334+
public boolean equals(final Object o) {
335+
if (o == null || getClass() != o.getClass()) return false;
336+
final LdapConnectionProperties that = (LdapConnectionProperties) o;
337+
return maxConnections() == that.maxConnections() &&
338+
connectTimeoutMillis() == that.connectTimeoutMillis() &&
339+
searchTimeoutSeconds() == that.searchTimeoutSeconds() &&
340+
responseTimeoutMillis() == that.responseTimeoutMillis() &&
341+
acceptAnyCertificateForTesting() == that.acceptAnyCertificateForTesting() &&
342+
Objects.equals(rdns(), that.rdns()) &&
343+
tlsMode() == that.tlsMode() &&
344+
Objects.equals(servers(), that.servers()) &&
345+
Objects.equals(uidAttribute(), that.uidAttribute()) &&
346+
Objects.equals(assignedRole(), that.assignedRole()) &&
347+
Objects.equals(trustStore(), that.trustStore()) &&
348+
Objects.equals(searchScope(), that.searchScope()) &&
349+
Objects.equals(requiredObjectClass(), that.requiredObjectClass()) &&
350+
Objects.equals(ldapSimpleBind(), that.ldapSimpleBind());
351+
}
352+
353+
@Override
354+
public int hashCode() {
355+
return Objects.hash(servers(),
356+
tlsMode(),
357+
trustStore(),
358+
connectTimeoutMillis(),
359+
responseTimeoutMillis(),
360+
maxConnections(),
361+
uidAttribute(),
362+
rdns(),
363+
requiredObjectClass(),
364+
searchScope(),
365+
searchTimeoutSeconds(),
366+
assignedRole(),
367+
acceptAnyCertificateForTesting(),
368+
ldapSimpleBind());
369+
}
283370
}

hivemq-edge/src/main/java/com/hivemq/api/utils/ApiUtils.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,33 @@
1616
package com.hivemq.api.utils;
1717

1818
import com.google.common.base.Preconditions;
19-
import com.hivemq.api.config.ApiListener;
2019
import com.hivemq.api.config.HttpsListener;
2120
import com.hivemq.configuration.service.ApiConfigurationService;
22-
import org.jetbrains.annotations.NotNull;
2321
import com.hivemq.http.core.UsernamePasswordRoles;
22+
import org.jetbrains.annotations.NotNull;
2423

2524
import java.net.InetAddress;
2625
import java.net.UnknownHostException;
2726
import java.util.List;
27+
import java.util.Objects;
2828

2929
/**
3030
* @author Simon L Johnson
3131
*/
3232
public class ApiUtils {
3333

34-
public static boolean hasDefaultUser(List<UsernamePasswordRoles> users){
34+
public static boolean hasDefaultUser(final List<UsernamePasswordRoles> users){
3535
if(!users.isEmpty()){
36-
return users.stream().filter(user -> (UsernamePasswordRoles.DEFAULT_USERNAME.equals(user.getUserName())
37-
&& UsernamePasswordRoles.DEFAULT_PASSWORD.equals(user.getPassword()))).count() > 0;
36+
return users.stream()
37+
.anyMatch(user -> (UsernamePasswordRoles.DEFAULT_USERNAME.equals(user.getUserName()) &&
38+
Objects.deepEquals(UsernamePasswordRoles.DEFAULT_PASSWORD_BYTES, user.getPassword())));
3839
}
3940
return false;
4041
}
4142

4243
public static String getWebContextRoot(final @NotNull ApiConfigurationService apiConfigurationService, final boolean trailingSlash){
4344

44-
List<ApiListener> listeners = apiConfigurationService.getListeners();
45+
final var listeners = apiConfigurationService.getListeners();
4546
if(listeners == null || listeners.isEmpty()){
4647
return null;
4748
}
@@ -50,10 +51,10 @@ public static String getWebContextRoot(final @NotNull ApiConfigurationService ap
5051
int port = 80;
5152
String host = null;
5253

53-
for(ApiListener listener : listeners){
54+
for(final var listener : listeners){
5455
try {
5556
host = getHostName(listener.getBindAddress());
56-
} catch(UnknownHostException e){
57+
} catch(final UnknownHostException e){
5758
host = listener.getBindAddress();
5859
}
5960
port = listener.getPort();
@@ -75,7 +76,7 @@ public static String getHostName(final @NotNull String name) throws UnknownHostE
7576
return "localhost";
7677
// return InetAddress.getLocalHost().getHostName();
7778
}
78-
InetAddress host = InetAddress.getByName(name);
79+
final var host = InetAddress.getByName(name);
7980
return host.getHostName();
8081
}
8182
}

hivemq-edge/src/main/java/com/hivemq/configuration/entity/api/ldap/LdapServerEntity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import jakarta.xml.bind.annotation.XmlElement;
66
import org.jetbrains.annotations.NotNull;
77

8+
import java.util.Objects;
9+
810
@XmlAccessorType(XmlAccessType.NONE)
911
@SuppressWarnings("NotNullFieldNotInitialized")
1012
public class LdapServerEntity {
@@ -35,4 +37,16 @@ public LdapServerEntity(final @NotNull String host, final int port) {
3537
public int getPort() {
3638
return port;
3739
}
40+
41+
@Override
42+
public boolean equals(final Object o) {
43+
if (o == null || getClass() != o.getClass()) return false;
44+
final LdapServerEntity that = (LdapServerEntity) o;
45+
return getPort() == that.getPort() && Objects.equals(getHost(), that.getHost());
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return Objects.hash(getHost(), getPort());
51+
}
3852
}

hivemq-edge/src/main/java/com/hivemq/configuration/entity/api/ldap/LdapSimpleBindEntity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import jakarta.xml.bind.annotation.XmlElement;
66
import org.jetbrains.annotations.NotNull;
77

8+
import java.util.Objects;
9+
810
/**
911
* This class represents the simple bind credentials for an LDAP connection.
1012
*/
@@ -33,4 +35,16 @@ public LdapSimpleBindEntity(final @NotNull String rdns, final @NotNull String pa
3335
public @NotNull String getUserPassword() {
3436
return userPassword;
3537
}
38+
39+
@Override
40+
public boolean equals(final Object o) {
41+
if (o == null || getClass() != o.getClass()) return false;
42+
final LdapSimpleBindEntity that = (LdapSimpleBindEntity) o;
43+
return Objects.equals(getRdns(), that.getRdns()) && Objects.equals(getUserPassword(), that.getUserPassword());
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
return Objects.hash(getRdns(), getUserPassword());
49+
}
3650
}

hivemq-edge/src/main/java/com/hivemq/http/core/UsernamePasswordRoles.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
import org.jetbrains.annotations.NotNull;
1919

20+
import java.nio.charset.StandardCharsets;
2021
import java.util.Set;
2122

2223
public class UsernamePasswordRoles {
2324

2425
public static final String DEFAULT_USERNAME = "admin";
2526
public static final String DEFAULT_PASSWORD = "hivemq";
27+
public static final byte[] DEFAULT_PASSWORD_BYTES = DEFAULT_PASSWORD.getBytes(StandardCharsets.UTF_8);
2628

2729
private String userName;
2830
private byte[] password;

hivemq-edge/src/main/java/com/hivemq/util/render/EnvVarUtil.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323

24-
import java.util.regex.Matcher;
2524
import java.util.regex.Pattern;
2625

2726
/**
@@ -44,7 +43,7 @@ public class EnvVarUtil {
4443
*/
4544
public static @Nullable String getValue(final @NotNull String name) {
4645
//also check java properties if system variable is not found
47-
final String systemProperty = System.getProperty(name);
46+
final var systemProperty = System.getProperty(name);
4847
if (systemProperty != null) {
4948
return systemProperty;
5049
}
@@ -60,10 +59,9 @@ public class EnvVarUtil {
6059
* @throws UnrecoverableException if a variable used in a placeholder is not set
6160
*/
6261
public static @NotNull String replaceEnvironmentVariablePlaceholders(final @NotNull String text) {
62+
final var resultString = new StringBuilder();
6363

64-
final StringBuffer resultString = new StringBuffer();
65-
66-
final Matcher matcher = Pattern.compile(ENV_VAR_PATTERN)
64+
final var matcher = Pattern.compile(ENV_VAR_PATTERN)
6765
.matcher(text);
6866

6967
while (matcher.find()) {
@@ -75,9 +73,9 @@ public class EnvVarUtil {
7573
continue;
7674
}
7775

78-
final String varName = matcher.group(1);
76+
final var varName = matcher.group(1);
7977

80-
final String replacement = getValue(varName);
78+
final var replacement = getValue(varName);
8179

8280
if (replacement == null) {
8381
log.error("Environment Variable {} for HiveMQ config.xml is not set.", varName);

0 commit comments

Comments
 (0)