Skip to content

Commit 911bb60

Browse files
Updated http scheme for reset password url based on https.enabled in server.properties file
1 parent 859f765 commit 911bb60

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

client/src/main/java/org/apache/cloudstack/ServerDaemon.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ public class ServerDaemon implements Daemon {
7676
private static final String SESSION_TIMEOUT = "session.timeout";
7777
private static final String HTTP_ENABLE = "http.enable";
7878
private static final String HTTP_PORT = "http.port";
79-
private static final String HTTPS_ENABLE = "https.enable";
8079
private static final String HTTPS_PORT = "https.port";
81-
private static final String KEYSTORE_FILE = "https.keystore";
8280
private static final String KEYSTORE_PASSWORD = "https.keystore.password";
8381
private static final String WEBAPP_DIR = "webapp.dir";
8482
private static final String ACCESS_LOG = "access.log";
@@ -142,9 +140,9 @@ public void init(final DaemonContext context) {
142140
setContextPath(properties.getProperty(CONTEXT_PATH, "/client"));
143141
setHttpEnable(Boolean.valueOf(properties.getProperty(HTTP_ENABLE, "true")));
144142
setHttpPort(Integer.valueOf(properties.getProperty(HTTP_PORT, "8080")));
145-
setHttpsEnable(Boolean.valueOf(properties.getProperty(HTTPS_ENABLE, "false")));
143+
setHttpsEnable(Boolean.valueOf(properties.getProperty(ServerProperties.HTTPS_ENABLE, "false")));
146144
setHttpsPort(Integer.valueOf(properties.getProperty(HTTPS_PORT, "8443")));
147-
setKeystoreFile(properties.getProperty(KEYSTORE_FILE));
145+
setKeystoreFile(properties.getProperty(ServerProperties.KEYSTORE_FILE));
148146
setKeystorePassword(properties.getProperty(KEYSTORE_PASSWORD));
149147
setWebAppLocation(properties.getProperty(WEBAPP_DIR));
150148
setAccessLogFile(properties.getProperty(ACCESS_LOG, "access.log"));

server/src/main/java/org/apache/cloudstack/user/UserPasswordResetManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ public interface UserPasswordResetManager {
7878

7979
ConfigKey<String> UserPasswordResetDomainURL = new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED,
8080
String.class, "user.password.reset.mail.domain.url", null,
81-
"Domain URL (along with http:// or https:// as applicable) for reset password links sent to the user via email", true,
81+
"Domain URL (along with http:// or https:// as applicable) for reset password links sent to the user via email. " +
82+
"If this is not set, CloudStack would determine the domain url based on the first management server from 'host' setting " +
83+
"and http scheme based on the https.enabled flag from server.properties file in the management server.", true,
8284
ConfigKey.Scope.Global);
8385

8486
void setResetTokenAndSend(UserAccount userAccount);

server/src/main/java/org/apache/cloudstack/user/UserPasswordResetManagerImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.cloud.user.dao.UserDao;
2424
import com.cloud.utils.StringUtils;
2525
import com.cloud.utils.component.ManagerBase;
26+
import com.cloud.utils.server.ServerProperties;
2627
import com.github.mustachejava.DefaultMustacheFactory;
2728
import com.github.mustachejava.Mustache;
2829
import com.github.mustachejava.MustacheFactory;
@@ -182,12 +183,16 @@ public void setResetTokenAndSend(UserAccount userAccount) {
182183
final String subject = "Password Reset Request";
183184
String domainUrl = UserPasswordResetDomainURL.value();
184185
if (StringUtils.isBlank(domainUrl)) {
185-
domainUrl = ManagementServerAddresses.value().split(",")[0];
186+
domainUrl = ManagementServerAddresses.value().split(",")[0];
186187
}
187188
domainUrl = domainUrl.replaceAll("/+$", "");
188189

189190
if (!domainUrl.startsWith("http://") && !domainUrl.startsWith("https://")) {
190-
domainUrl = "http://" + domainUrl;
191+
if (ServerProperties.isHttpsEnabled()) {
192+
domainUrl = "https://" + domainUrl;
193+
} else {
194+
domainUrl = "http://" + domainUrl;
195+
}
191196
}
192197
String resetLink = String.format("%s/client/#/user/resetPassword?username=%s&token=%s",
193198
domainUrl, username, resetToken);

utils/src/main/java/com/cloud/utils/server/ServerProperties.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,28 @@
1616
// under the License.
1717
package com.cloud.utils.server;
1818

19+
import com.cloud.utils.PropertiesUtil;
1920
import com.cloud.utils.crypt.EncryptionSecretKeyChecker;
21+
import com.cloud.utils.StringUtils;
2022
import org.apache.commons.io.IOUtils;
2123
import org.apache.logging.log4j.Logger;
2224
import org.apache.logging.log4j.LogManager;
2325

26+
import java.io.File;
27+
import java.io.FileInputStream;
2428
import java.io.IOException;
2529
import java.io.InputStream;
2630
import java.util.Properties;
2731

2832
public class ServerProperties {
2933
protected Logger logger = LogManager.getLogger(getClass());
3034

35+
public static final String HTTPS_ENABLE = "https.enable";
36+
public static final String KEYSTORE_FILE = "https.keystore";
37+
public static final String PASSWORD_ENCRYPTION_TYPE = "password.encryption.type";
38+
3139
private static Properties properties = new Properties();
3240
private static boolean loaded = false;
33-
public static final String passwordEncryptionType = "password.encryption.type";
3441

3542
public synchronized static Properties getServerProperties(InputStream inputStream) {
3643
if (!loaded) {
@@ -39,7 +46,7 @@ public synchronized static Properties getServerProperties(InputStream inputStrea
3946
serverProps.load(inputStream);
4047

4148
EncryptionSecretKeyChecker checker = new EncryptionSecretKeyChecker();
42-
checker.check(serverProps, passwordEncryptionType);
49+
checker.check(serverProps, PASSWORD_ENCRYPTION_TYPE);
4350

4451
if (EncryptionSecretKeyChecker.useEncryption()) {
4552
EncryptionSecretKeyChecker.decryptAnyProperties(serverProps);
@@ -56,4 +63,25 @@ public synchronized static Properties getServerProperties(InputStream inputStrea
5663

5764
return properties;
5865
}
66+
67+
public static boolean isHttpsEnabled() {
68+
final File confFile = PropertiesUtil.findConfigFile("server.properties");
69+
if (confFile == null) {
70+
return false;
71+
}
72+
73+
try {
74+
InputStream is = new FileInputStream(confFile);
75+
final Properties properties = ServerProperties.getServerProperties(is);
76+
if (properties == null) {
77+
return false;
78+
}
79+
80+
boolean httpsEnable = Boolean.parseBoolean(properties.getProperty(HTTPS_ENABLE, "false"));
81+
String keystoreFile = properties.getProperty(KEYSTORE_FILE);
82+
return httpsEnable && StringUtils.isNotEmpty(keystoreFile) && new File(keystoreFile).exists();
83+
} catch (final IOException e) {
84+
return false;
85+
}
86+
}
5987
}

0 commit comments

Comments
 (0)