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.

KerberosTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.auth.ldap;
  21. import java.io.File;
  22. import javax.servlet.http.HttpServletRequest;
  23. import org.junit.Assert;
  24. import org.junit.ClassRule;
  25. import org.junit.Test;
  26. import org.mockito.Mockito;
  27. import org.sonar.api.config.internal.MapSettings;
  28. import org.sonar.api.security.Authenticator;
  29. import org.sonar.api.security.ExternalGroupsProvider;
  30. import org.sonar.auth.ldap.server.LdapServer;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. public class KerberosTest {
  33. static {
  34. System.setProperty("java.security.krb5.conf", new File("target/krb5.conf").getAbsolutePath());
  35. }
  36. @ClassRule
  37. public static LdapServer server = new LdapServer("/krb.ldif");
  38. @Test
  39. public void test() {
  40. MapSettings settings = configure();
  41. LdapRealm ldapRealm = new LdapRealm(new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery()));
  42. ldapRealm.init();
  43. assertThat(ldapRealm.doGetAuthenticator().doAuthenticate(new Authenticator.Context("Godin@EXAMPLE.ORG", "wrong_user_password", Mockito.mock(HttpServletRequest.class))))
  44. .isFalse();
  45. assertThat(ldapRealm.doGetAuthenticator().doAuthenticate(new Authenticator.Context("Godin@EXAMPLE.ORG", "user_password", Mockito.mock(HttpServletRequest.class)))).isTrue();
  46. // Using default realm from krb5.conf:
  47. assertThat(ldapRealm.doGetAuthenticator().doAuthenticate(new Authenticator.Context("Godin", "user_password", Mockito.mock(HttpServletRequest.class)))).isTrue();
  48. assertThat(ldapRealm.getGroupsProvider().doGetGroups(new ExternalGroupsProvider.Context("godin", Mockito.mock(HttpServletRequest.class)))).containsOnly("sonar-users");
  49. }
  50. @Test
  51. public void wrong_bind_password() {
  52. MapSettings settings = configure()
  53. .setProperty("ldap.bindPassword", "wrong_bind_password");
  54. LdapRealm ldapRealm = new LdapRealm(new LdapSettingsManager(settings.asConfig(), new LdapAutodiscovery()));
  55. try {
  56. ldapRealm.init();
  57. Assert.fail();
  58. } catch (LdapException e) {
  59. assertThat(e.getMessage()).isEqualTo("Unable to open LDAP connection");
  60. }
  61. }
  62. private static MapSettings configure() {
  63. return new MapSettings()
  64. .setProperty("ldap.url", server.getUrl())
  65. .setProperty("ldap.authentication", LdapContextFactory.AUTH_METHOD_GSSAPI)
  66. .setProperty("ldap.bindDn", "SonarQube@EXAMPLE.ORG")
  67. .setProperty("ldap.bindPassword", "bind_password")
  68. .setProperty("ldap.user.baseDn", "ou=users,dc=example,dc=org")
  69. .setProperty("ldap.group.baseDn", "ou=groups,dc=example,dc=org")
  70. .setProperty("ldap.group.request", "(&(objectClass=groupOfUniqueNames)(uniqueMember={dn}))");
  71. }
  72. }