Skip to content

Commit 4d35d68

Browse files
Fix url in password reset email (#12078)
1 parent 8c2ba2b commit 4d35d68

File tree

4 files changed

+64
-17
lines changed

4 files changed

+64
-17
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ public class ServerDaemon implements Daemon {
7171
private static final String BIND_INTERFACE = "bind.interface";
7272
private static final String CONTEXT_PATH = "context.path";
7373
private static final String SESSION_TIMEOUT = "session.timeout";
74-
private static final String HTTP_ENABLE = "http.enable";
75-
private static final String HTTP_PORT = "http.port";
76-
private static final String HTTPS_ENABLE = "https.enable";
77-
private static final String HTTPS_PORT = "https.port";
78-
private static final String KEYSTORE_FILE = "https.keystore";
7974
private static final String KEYSTORE_PASSWORD = "https.keystore.password";
8075
private static final String WEBAPP_DIR = "webapp.dir";
8176
private static final String ACCESS_LOG = "access.log";
@@ -137,11 +132,11 @@ public void init(final DaemonContext context) {
137132
}
138133
setBindInterface(properties.getProperty(BIND_INTERFACE, null));
139134
setContextPath(properties.getProperty(CONTEXT_PATH, "/client"));
140-
setHttpEnable(Boolean.valueOf(properties.getProperty(HTTP_ENABLE, "true")));
141-
setHttpPort(Integer.valueOf(properties.getProperty(HTTP_PORT, "8080")));
142-
setHttpsEnable(Boolean.valueOf(properties.getProperty(HTTPS_ENABLE, "false")));
143-
setHttpsPort(Integer.valueOf(properties.getProperty(HTTPS_PORT, "8443")));
144-
setKeystoreFile(properties.getProperty(KEYSTORE_FILE));
135+
setHttpEnable(Boolean.valueOf(properties.getProperty(ServerProperties.HTTP_ENABLE, "true")));
136+
setHttpPort(Integer.valueOf(properties.getProperty(ServerProperties.HTTP_PORT, "8080")));
137+
setHttpsEnable(Boolean.valueOf(properties.getProperty(ServerProperties.HTTPS_ENABLE, "false")));
138+
setHttpsPort(Integer.valueOf(properties.getProperty(ServerProperties.HTTPS_PORT, "8443")));
139+
setKeystoreFile(properties.getProperty(ServerProperties.KEYSTORE_FILE));
145140
setKeystorePassword(properties.getProperty(KEYSTORE_PASSWORD));
146141
setWebAppLocation(properties.getProperty(WEBAPP_DIR));
147142
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 for reset password links sent to the user via email", true,
81+
"Domain URL (along with scheme - http:// or https:// and port 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: 22 additions & 4 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;
@@ -48,6 +49,7 @@
4849
import java.util.Set;
4950
import java.util.UUID;
5051

52+
import static org.apache.cloudstack.config.ApiServiceConfiguration.ManagementServerAddresses;
5153
import static org.apache.cloudstack.resourcedetail.UserDetailVO.PasswordResetToken;
5254
import static org.apache.cloudstack.resourcedetail.UserDetailVO.PasswordResetTokenExpiryDate;
5355

@@ -68,7 +70,7 @@ public class UserPasswordResetManagerImpl extends ManagerBase implements UserPas
6870
new ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, String.class,
6971
"user.password.reset.mail.template", "Hello {{username}}!\n" +
7072
"You have requested to reset your password. Please click the following link to reset your password:\n" +
71-
"{{{domainUrl}}}{{{resetLink}}}\n" +
73+
"{{{resetLink}}}\n" +
7274
"If you did not request a password reset, please ignore this email.\n" +
7375
"\n" +
7476
"Regards,\n" +
@@ -179,10 +181,26 @@ public void setResetTokenAndSend(UserAccount userAccount) {
179181
final String email = userAccount.getEmail();
180182
final String username = userAccount.getUsername();
181183
final String subject = "Password Reset Request";
182-
final String domainUrl = UserPasswordResetDomainURL.value();
184+
String domainUrl = UserPasswordResetDomainURL.value();
185+
if (StringUtils.isBlank(domainUrl)) {
186+
String mgmtServerAddr = ManagementServerAddresses.value().split(",")[0];
187+
if (ServerProperties.isHttpsEnabled()) {
188+
domainUrl = "https://" + mgmtServerAddr + ":" + ServerProperties.getHttpsPort();
189+
} else {
190+
domainUrl = "http://" + mgmtServerAddr + ":" + ServerProperties.getHttpPort();
191+
}
192+
} else if (!domainUrl.startsWith("http://") && !domainUrl.startsWith("https://")) {
193+
if (ServerProperties.isHttpsEnabled()) {
194+
domainUrl = "https://" + domainUrl;
195+
} else {
196+
domainUrl = "http://" + domainUrl;
197+
}
198+
}
199+
200+
domainUrl = domainUrl.replaceAll("/+$", "");
183201

184-
String resetLink = String.format("/client/#/user/resetPassword?username=%s&token=%s",
185-
username, resetToken);
202+
String resetLink = String.format("%s/client/#/user/resetPassword?username=%s&token=%s",
203+
domainUrl, username, resetToken);
186204
String content = getMessageBody(userAccount, resetToken, resetLink);
187205

188206
SMTPMailProperties mailProperties = new SMTPMailProperties();

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,33 @@
1717
package com.cloud.utils.server;
1818

1919
import com.cloud.utils.crypt.EncryptionSecretKeyChecker;
20+
import com.cloud.utils.StringUtils;
2021
import org.apache.commons.io.IOUtils;
2122
import org.apache.logging.log4j.Logger;
2223
import org.apache.logging.log4j.LogManager;
2324

25+
import java.io.File;
2426
import java.io.IOException;
2527
import java.io.InputStream;
2628
import java.util.Properties;
2729

2830
public class ServerProperties {
2931
protected Logger logger = LogManager.getLogger(getClass());
3032

33+
public static final String HTTP_ENABLE = "http.enable";
34+
public static final String HTTP_PORT = "http.port";
35+
public static final String HTTPS_ENABLE = "https.enable";
36+
public static final String HTTPS_PORT = "https.port";
37+
public static final String KEYSTORE_FILE = "https.keystore";
38+
public static final String PASSWORD_ENCRYPTION_TYPE = "password.encryption.type";
39+
3140
private static Properties properties = new Properties();
3241
private static boolean loaded = false;
33-
public static final String passwordEncryptionType = "password.encryption.type";
42+
43+
private static int httpPort = 8080;
44+
45+
private static boolean httpsEnable = false;
46+
private static int httpsPort = 8443;
3447

3548
public synchronized static Properties getServerProperties(InputStream inputStream) {
3649
if (!loaded) {
@@ -39,7 +52,7 @@ public synchronized static Properties getServerProperties(InputStream inputStrea
3952
serverProps.load(inputStream);
4053

4154
EncryptionSecretKeyChecker checker = new EncryptionSecretKeyChecker();
42-
checker.check(serverProps, passwordEncryptionType);
55+
checker.check(serverProps, PASSWORD_ENCRYPTION_TYPE);
4356

4457
if (EncryptionSecretKeyChecker.useEncryption()) {
4558
EncryptionSecretKeyChecker.decryptAnyProperties(serverProps);
@@ -50,10 +63,29 @@ public synchronized static Properties getServerProperties(InputStream inputStrea
5063
IOUtils.closeQuietly(inputStream);
5164
}
5265

66+
httpPort = Integer.parseInt(serverProps.getProperty(ServerProperties.HTTP_PORT, "8080"));
67+
68+
boolean httpsEnabled = Boolean.parseBoolean(serverProps.getProperty(ServerProperties.HTTPS_ENABLE, "false"));
69+
String keystoreFile = serverProps.getProperty(KEYSTORE_FILE);
70+
httpsEnable = httpsEnabled && StringUtils.isNotEmpty(keystoreFile) && new File(keystoreFile).exists();
71+
httpsPort = Integer.parseInt(serverProps.getProperty(ServerProperties.HTTPS_PORT, "8443"));
72+
5373
properties = serverProps;
5474
loaded = true;
5575
}
5676

5777
return properties;
5878
}
79+
80+
public static int getHttpPort() {
81+
return httpPort;
82+
}
83+
84+
public static boolean isHttpsEnabled() {
85+
return httpsEnable;
86+
}
87+
88+
public static int getHttpsPort() {
89+
return httpsPort;
90+
}
5991
}

0 commit comments

Comments
 (0)