You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultAdminCredentialsVerifierImpl.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.authentication;
  21. import org.picocontainer.Startable;
  22. import org.sonar.api.utils.log.Logger;
  23. import org.sonar.api.utils.log.Loggers;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.user.UserDto;
  27. import org.sonar.server.authentication.event.AuthenticationEvent;
  28. import org.sonar.server.authentication.event.AuthenticationException;
  29. import org.sonar.server.notification.NotificationManager;
  30. import static org.sonar.server.log.ServerProcessLogging.STARTUP_LOGGER_NAME;
  31. import static org.sonar.server.property.InternalProperties.DEFAULT_ADMIN_CREDENTIAL_USAGE_EMAIL;
  32. /**
  33. * Detect usage of an active admin account with default credential in order to ask this account to reset its password during authentication.
  34. */
  35. public class DefaultAdminCredentialsVerifierImpl implements Startable, DefaultAdminCredentialsVerifier {
  36. private static final Logger LOGGER = Loggers.get(STARTUP_LOGGER_NAME);
  37. private final DbClient dbClient;
  38. private final CredentialsLocalAuthentication localAuthentication;
  39. private final NotificationManager notificationManager;
  40. public DefaultAdminCredentialsVerifierImpl(DbClient dbClient, CredentialsLocalAuthentication localAuthentication, NotificationManager notificationManager) {
  41. this.dbClient = dbClient;
  42. this.localAuthentication = localAuthentication;
  43. this.notificationManager = notificationManager;
  44. }
  45. @Override
  46. public void start() {
  47. try (DbSession session = dbClient.openSession(false)) {
  48. UserDto admin = getAdminUser(session);
  49. if (admin == null || !isDefaultCredentialUser(session, admin)) {
  50. return;
  51. }
  52. addWarningInSonarDotLog();
  53. dbClient.userDao().update(session, admin.setResetPassword(true));
  54. sendEmailToAdmins(session);
  55. session.commit();
  56. }
  57. }
  58. public boolean hasDefaultCredentialUser() {
  59. try (DbSession session = dbClient.openSession(false)) {
  60. UserDto admin = getAdminUser(session);
  61. if (admin == null) {
  62. return false;
  63. } else {
  64. return isDefaultCredentialUser(session, admin);
  65. }
  66. }
  67. }
  68. private UserDto getAdminUser(DbSession session) {
  69. return dbClient.userDao().selectActiveUserByLogin(session, "admin");
  70. }
  71. private static void addWarningInSonarDotLog() {
  72. String highlighter = "####################################################################################################################";
  73. String msg = "Default Administrator credentials are still being used. Make sure to change the password or deactivate the account.";
  74. LOGGER.warn(highlighter);
  75. LOGGER.warn(msg);
  76. LOGGER.warn(highlighter);
  77. }
  78. private boolean isDefaultCredentialUser(DbSession dbSession, UserDto user) {
  79. try {
  80. localAuthentication.authenticate(dbSession, user, "admin", AuthenticationEvent.Method.BASIC);
  81. return true;
  82. } catch (AuthenticationException ex) {
  83. return false;
  84. }
  85. }
  86. private void sendEmailToAdmins(DbSession session) {
  87. if (dbClient.internalPropertiesDao().selectByKey(session, DEFAULT_ADMIN_CREDENTIAL_USAGE_EMAIL)
  88. .map(Boolean::parseBoolean)
  89. .orElse(false)) {
  90. return;
  91. }
  92. notificationManager.scheduleForSending(new DefaultAdminCredentialsVerifierNotification());
  93. dbClient.internalPropertiesDao().save(session, DEFAULT_ADMIN_CREDENTIAL_USAGE_EMAIL, Boolean.TRUE.toString());
  94. }
  95. @Override
  96. public void stop() {
  97. // Nothing to do
  98. }
  99. }