Only reuse SMTP authentication data for testing endpoint when the same auth, host, port and user are passed

Closes #39486

Signed-off-by: rmartinc <rmartinc@redhat.com>
(cherry picked from commit 598154bc5839934569a78d8ee1ec8c1af8fc4142)
This commit is contained in:
rmartinc 2025-05-20 10:17:18 +02:00 committed by Marek Posolda
parent 0ad29a0844
commit 7ac6096269
2 changed files with 18 additions and 3 deletions

View File

@ -27,6 +27,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -1011,24 +1012,33 @@ public class RealmAdminResource {
@Tag(name = KeycloakOpenAPI.Admin.Tags.REALMS_ADMIN)
@Operation()
public Response testSMTPConnection(Map<String, String> settings) throws Exception {
auth.realm().requireManageRealm();
try {
UserModel user = auth.adminAuth().getUser();
if (user.getEmail() == null) {
throw ErrorResponse.error("Logged in user does not have an e-mail.", Response.Status.INTERNAL_SERVER_ERROR);
}
if (ComponentRepresentation.SECRET_VALUE.equals(settings.get("password"))) {
if (ComponentRepresentation.SECRET_VALUE.equals(settings.get("password"))
&& reuseConfiguredAuthenticationForSmtp(settings)) {
settings.put("password", realm.getSmtpConfig().get("password"));
}
session.getProvider(EmailTemplateProvider.class).sendSmtpTestEmail(settings, user);
} catch (Exception e) {
e.printStackTrace();
logger.errorf("Failed to send email \n %s", e.getCause());
logger.errorf(e, "Failed to send email \n %s", e.getCause());
throw ErrorResponse.error("Failed to send email", Response.Status.INTERNAL_SERVER_ERROR);
}
return Response.noContent().build();
}
private boolean reuseConfiguredAuthenticationForSmtp(Map<String, String> settings) {
// just reuse the configured authentication if the same authenticator, host, port and user are passed
return Boolean.parseBoolean(settings.get("auth")) && Boolean.parseBoolean(realm.getSmtpConfig().get("auth"))
&& Objects.equals(settings.getOrDefault("host", ""), realm.getSmtpConfig().getOrDefault("host", ""))
&& Objects.equals(settings.getOrDefault("port", "25"), realm.getSmtpConfig().getOrDefault("port", "25"))
&& Objects.equals(settings.getOrDefault("user", ""), realm.getSmtpConfig().getOrDefault("user", ""));
}
@Path("identity-provider")
public IdentityProvidersResource getIdentityProviderResource() {
return new IdentityProvidersResource(realm, session, this.auth, adminEvent);

View File

@ -128,6 +128,11 @@ public class SMTPConnectionTest extends AbstractKeycloakTest {
Response response = realm.testSMTPConnection(settings("127.0.0.1", "3025", "auto@keycloak.org", "true", null, null,
"admin@localhost", SECRET_VALUE));
assertStatus(response, 204);
// no reuse password if the server is different (localhost) to the saved one (127.0.0.1)
response = realm.testSMTPConnection(settings("localhost", "3025", "auto@keycloak.org", "true", null, null,
"admin@localhost", SECRET_VALUE));
assertStatus(response, 500);
} finally {
// Revert SMTP back
realmRep.setSmtpServer(oldSmtp);