aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-auth-ldap/src/it/java/org/sonar/auth/ldap/DefaultLdapAuthenticatorIT.java
blob: 87d797e9df293ac03fd0d160e79c27df0793adc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.auth.ldap;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.server.http.HttpRequest;
import org.sonar.auth.ldap.server.LdapServer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class DefaultLdapAuthenticatorIT {

  /**
   * A reference to the original ldif file
   */
  private static final String USERS_EXAMPLE_ORG_LDIF = "/users.example.org.ldif";
  /**
   * A reference to an additional ldif file.
   */
  private static final String USERS_INFOSUPPORT_COM_LDIF = "/users.infosupport.com.ldif";
  @RegisterExtension
  private static final LdapServer exampleServer = new LdapServer(USERS_EXAMPLE_ORG_LDIF);
  @RegisterExtension
  private static final LdapServer infosupportServer = new LdapServer(USERS_INFOSUPPORT_COM_LDIF, "infosupport.com", "dc=infosupport," +
    "dc=com");

  @Test
  void testNoConnection() {
    exampleServer.disableAnonymousAccess();
    try {
      LdapSettingsManager settingsManager = new LdapSettingsManager(
        LdapSettingsFactory.generateAuthenticationSettings(exampleServer, null, LdapContextFactory.AUTH_METHOD_SIMPLE).asConfig());
      DefaultLdapAuthenticator authenticator = new DefaultLdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());
      boolean isAuthenticationSuccessful = authenticator.doAuthenticate(createContext("godin", "secret1")).isSuccess();
      assertThat(isAuthenticationSuccessful).isTrue();
    } finally {
      exampleServer.enableAnonymousAccess();
    }
  }

  @Test
  void testSimple() {
    LdapSettingsManager settingsManager = new LdapSettingsManager(
      LdapSettingsFactory.generateAuthenticationSettings(exampleServer, null, LdapContextFactory.AUTH_METHOD_SIMPLE).asConfig());
    DefaultLdapAuthenticator authenticator = new DefaultLdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());

    LdapAuthenticationResult user1Success = authenticator.doAuthenticate(createContext("godin", "secret1"));
    assertThat(user1Success.isSuccess()).isTrue();
    assertThat(user1Success.getServerKey()).isEqualTo("default");

    assertThat(authenticator.doAuthenticate(createContext("godin", "wrong")).isSuccess()).isFalse();

    LdapAuthenticationResult user2Success = authenticator.doAuthenticate(createContext("tester", "secret2"));
    assertThat(user2Success.isSuccess()).isTrue();
    assertThat(user2Success.getServerKey()).isEqualTo("default");

    assertThat(authenticator.doAuthenticate(createContext("tester", "wrong")).isSuccess()).isFalse();

    assertThat(authenticator.doAuthenticate(createContext("notfound", "wrong")).isSuccess()).isFalse();
    // SONARPLUGINS-2493
    assertThat(authenticator.doAuthenticate(createContext("godin", "")).isSuccess()).isFalse();
    assertThat(authenticator.doAuthenticate(createContext("godin", null)).isSuccess()).isFalse();
  }

  @Test
  void testSimpleMultiLdap() {
    LdapSettingsManager settingsManager = new LdapSettingsManager(
      LdapSettingsFactory.generateAuthenticationSettings(exampleServer, infosupportServer, LdapContextFactory.AUTH_METHOD_SIMPLE).asConfig());
    DefaultLdapAuthenticator authenticator = new DefaultLdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());

    LdapAuthenticationResult user1Success = authenticator.doAuthenticate(createContext("godin", "secret1"));
    assertThat(user1Success.isSuccess()).isTrue();
    assertThat(user1Success.getServerKey()).isEqualTo("example");
    assertThat(authenticator.doAuthenticate(createContext("godin", "wrong")).isSuccess()).isFalse();

    LdapAuthenticationResult user2Server1Success = authenticator.doAuthenticate(createContext("tester", "secret2"));
    assertThat(user2Server1Success.isSuccess()).isTrue();
    assertThat(user2Server1Success.getServerKey()).isEqualTo("example");

    LdapAuthenticationResult user2Server2Success = authenticator.doAuthenticate(createContext("tester", "secret3"));
    assertThat(user2Server2Success.isSuccess()).isTrue();
    assertThat(user2Server2Success.getServerKey()).isEqualTo("infosupport");

    assertThat(authenticator.doAuthenticate(createContext("tester", "wrong")).isSuccess()).isFalse();

    assertThat(authenticator.doAuthenticate(createContext("notfound", "wrong")).isSuccess()).isFalse();
    // SONARPLUGINS-2493
    assertThat(authenticator.doAuthenticate(createContext("godin", "")).isSuccess()).isFalse();
    assertThat(authenticator.doAuthenticate(createContext("godin", null)).isSuccess()).isFalse();

    // SONARPLUGINS-2793
    LdapAuthenticationResult user3Success = authenticator.doAuthenticate(createContext("robby", "secret1"));
    assertThat(user3Success.isSuccess()).isTrue();
    assertThat(user3Success.getServerKey()).isEqualTo("infosupport");
    assertThat(authenticator.doAuthenticate(createContext("robby", "wrong")).isSuccess()).isFalse();
  }

  @Test
  void testSasl() {
    MapSettings mapSettings = LdapSettingsFactory.generateAuthenticationSettings(exampleServer, null, LdapContextFactory.AUTH_METHOD_DIGEST_MD5);
    //set sasl QoP properties as per https://docs.oracle.com/javase/jndi/tutorial/ldap/security/digest.html
    mapSettings.setProperty("ldap.saslQop", "auth")
      .setProperty("ldap.saslStrength", "high")
      .setProperty("ldap.saslMaxbuf", "16384");
    LdapSettingsManager settingsManager = new LdapSettingsManager(mapSettings.asConfig());
    DefaultLdapAuthenticator authenticator = new DefaultLdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());

    LdapAuthenticationResult user1Success = authenticator.doAuthenticate(createContext("godin", "secret1"));
    assertThat(user1Success.isSuccess()).isTrue();
    assertThat(user1Success.getServerKey()).isEqualTo("default");

    assertThat(authenticator.doAuthenticate(createContext("godin", "wrong")).isSuccess()).isFalse();

    LdapAuthenticationResult user2Success = authenticator.doAuthenticate(createContext("tester", "secret2"));
    assertThat(user2Success.isSuccess()).isTrue();
    assertThat(user2Success.getServerKey()).isEqualTo("default");

    assertThat(authenticator.doAuthenticate(createContext("tester", "wrong")).isSuccess()).isFalse();

    assertThat(authenticator.doAuthenticate(createContext("notfound", "wrong")).isSuccess()).isFalse();
  }

  @Test
  void testSaslMultipleLdap() {
    LdapSettingsManager settingsManager = new LdapSettingsManager(
      LdapSettingsFactory.generateAuthenticationSettings(exampleServer, infosupportServer, LdapContextFactory.AUTH_METHOD_CRAM_MD5).asConfig());
    DefaultLdapAuthenticator authenticator = new DefaultLdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());

    LdapAuthenticationResult user1Success = authenticator.doAuthenticate(createContext("godin", "secret1"));
    assertThat(user1Success.isSuccess()).isTrue();
    assertThat(authenticator.doAuthenticate(createContext("godin", "wrong")).isSuccess()).isFalse();

    LdapAuthenticationResult user2Server1Success = authenticator.doAuthenticate(createContext("tester", "secret2"));
    assertThat(user2Server1Success.isSuccess()).isTrue();
    assertThat(user2Server1Success.getServerKey()).isEqualTo("example");

    LdapAuthenticationResult user2Server2Success = authenticator.doAuthenticate(createContext("tester", "secret3"));
    assertThat(user2Server2Success.isSuccess()).isTrue();
    assertThat(user2Server2Success.getServerKey()).isEqualTo("infosupport");

    assertThat(authenticator.doAuthenticate(createContext("tester", "wrong")).isSuccess()).isFalse();

    assertThat(authenticator.doAuthenticate(createContext("notfound", "wrong")).isSuccess()).isFalse();

    LdapAuthenticationResult user3Success = authenticator.doAuthenticate(createContext("robby", "secret1"));
    assertThat(user3Success.isSuccess()).isTrue();

    assertThat(authenticator.doAuthenticate(createContext("robby", "wrong")).isSuccess()).isFalse();
  }

  private static LdapAuthenticator.Context createContext(String username, String password) {
    return new LdapAuthenticator.Context(username, password, mock(HttpRequest.class));
  }

}