Skip to content

Commit 3006c48

Browse files
author
Vladimir Kotal
committed
convert checked LdapException to unchecked AuthorizationException
1 parent 27a7f86 commit 3006c48

File tree

9 files changed

+84
-29
lines changed

9 files changed

+84
-29
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
24+
package org.opengrok.indexer.authorization;
25+
26+
public class AuthorizationException extends RuntimeException {
27+
28+
private static final long serialVersionUID = -1;
29+
30+
public AuthorizationException(Throwable ex) {
31+
super(ex);
32+
}
33+
34+
AuthorizationException(String msg) {
35+
super(msg);
36+
}
37+
38+
AuthorizationException(String msg, Throwable ex) {
39+
super(msg, ex);
40+
}
41+
}

opengrok-indexer/src/main/java/org/opengrok/indexer/authorization/AuthorizationStack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ protected boolean processStack(Nameable entity,
257257
overallDecision = true;
258258
break;
259259
}
260-
} catch (LdapException ex) {
260+
} catch (AuthorizationException ex) {
261261
// Propagate up so that proper HTTP error can be given.
262-
LOGGER.log(Level.FINEST, "got LDAP exception: " + ex.getMessage());
262+
LOGGER.log(Level.FINEST, "got authorization exception: " + ex.getMessage());
263263
throw ex;
264264
} catch (Throwable ex) {
265265
LOGGER.log(Level.WARNING,

opengrok-indexer/src/main/java/org/opengrok/indexer/framework/PluginClassLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class PluginClassLoader extends ClassLoader {
5353
"org.opengrok.indexer.configuration.RuntimeEnvironment",
5454
"org.opengrok.indexer.authorization.IAuthorizationPlugin",
5555
"org.opengrok.indexer.authorization.plugins.*",
56-
"org.opengrok.indexer.authorization.LdapError",
56+
"org.opengrok.indexer.authorization.AuthorizationException",
5757
"org.opengrok.indexer.util.*",
5858
"org.opengrok.indexer.logger.*"
5959
};

plugins/src/opengrok/auth/plugin/LdapAttrPlugin.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import javax.servlet.http.HttpServletRequest;
3535
import opengrok.auth.entity.LdapUser;
3636
import opengrok.auth.plugin.entity.User;
37+
import opengrok.auth.plugin.ldap.LdapException;
38+
import org.opengrok.indexer.authorization.AuthorizationException;
3739
import org.opengrok.indexer.configuration.Group;
3840
import org.opengrok.indexer.configuration.Project;
3941

@@ -107,8 +109,12 @@ public void fillSession(HttpServletRequest req, User user) {
107109
if (attributeValues != null) {
108110
sessionAllowed = attributeValues.stream().anyMatch((t) -> whitelist.contains(t));
109111
} else {
110-
if ((records = getLdapProvider().lookupLdapContent(user, new String[]{ldapAttr})) == null) {
111-
return;
112+
try {
113+
if ((records = getLdapProvider().lookupLdapContent(user, new String[]{ldapAttr})) == null) {
114+
return;
115+
}
116+
} catch (LdapException ex) {
117+
throw new AuthorizationException(ex);
112118
}
113119

114120
if (records.isEmpty() || (attributeValues = records.get(ldapAttr)) == null) {

plugins/src/opengrok/auth/plugin/LdapFilterPlugin.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import javax.servlet.http.HttpServletRequest;
3232
import opengrok.auth.entity.LdapUser;
3333
import opengrok.auth.plugin.entity.User;
34+
import opengrok.auth.plugin.ldap.LdapException;
35+
import org.opengrok.indexer.authorization.AuthorizationException;
3436
import org.opengrok.indexer.configuration.Group;
3537
import org.opengrok.indexer.configuration.Project;
3638

@@ -85,9 +87,13 @@ public void fillSession(HttpServletRequest req, User user) {
8587
String expandedFilter = expandFilter(ldapFilter, ldapUser, user);
8688
LOGGER.log(Level.FINER, "expanded filter for user {0} into ''{1}''",
8789
new Object[]{user, expandedFilter});
88-
if ((records = getLdapProvider().lookupLdapContent(null, expandedFilter, dn)) == null) {
89-
LOGGER.log(Level.FINER, "failed to get content for user from LDAP server");
90-
return;
90+
try {
91+
if ((records = getLdapProvider().lookupLdapContent(null, expandedFilter, dn)) == null) {
92+
LOGGER.log(Level.FINER, "failed to get content for user from LDAP server");
93+
return;
94+
}
95+
} catch (LdapException ex) {
96+
throw new AuthorizationException(ex);
9197
}
9298

9399
LOGGER.log(Level.FINER, "got {0} records", records.size());

plugins/src/opengrok/auth/plugin/LdapUserPlugin.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import javax.servlet.http.HttpServletRequest;
3333
import opengrok.auth.entity.LdapUser;
3434
import opengrok.auth.plugin.entity.User;
35+
import opengrok.auth.plugin.ldap.LdapException;
36+
import org.opengrok.indexer.authorization.AuthorizationException;
3537
import org.opengrok.indexer.configuration.Group;
3638
import org.opengrok.indexer.configuration.Project;
3739
import org.opengrok.indexer.util.StringUtils;
@@ -133,11 +135,15 @@ public void fillSession(HttpServletRequest req, User user) {
133135
}
134136

135137
String filter = getFilter(user);
136-
if ((records = getLdapProvider().lookupLdapContent(null, filter, attributes)) == null) {
137-
LOGGER.log(Level.WARNING, "failed to get LDAP attributes ''{3}'' for user ''{0}'' " +
138-
"with filter ''{1}''",
139-
new Object[]{user, filter, String.join(", ", attributes)});
140-
return;
138+
try {
139+
if ((records = getLdapProvider().lookupLdapContent(null, filter, attributes)) == null) {
140+
LOGGER.log(Level.WARNING, "failed to get LDAP attributes ''{3}'' for user ''{0}'' " +
141+
"with filter ''{1}''",
142+
new Object[]{user, filter, String.join(", ", attributes)});
143+
return;
144+
}
145+
} catch (LdapException ex) {
146+
throw new AuthorizationException(ex);
141147
}
142148

143149
if (records.isEmpty()) {

plugins/src/opengrok/auth/plugin/ldap/AbstractLdapProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public abstract class AbstractLdapProvider {
3737
* @see #lookupLdapContent(opengrok.auth.plugin.entity.User,
3838
* java.lang.String)
3939
*/
40-
public Map<String, Set<String>> lookupLdapContent(User user) {
40+
public Map<String, Set<String>> lookupLdapContent(User user) throws LdapException {
4141
// calling the lookupLdapContent(user, filter)
4242
return lookupLdapContent(user, (String) null);
4343
}
@@ -52,7 +52,7 @@ public Map<String, Set<String>> lookupLdapContent(User user) {
5252
* @see #lookupLdapContent(opengrok.auth.plugin.entity.User,
5353
* java.lang.String, java.lang.String[])
5454
*/
55-
public Map<String, Set<String>> lookupLdapContent(User user, String filter) {
55+
public Map<String, Set<String>> lookupLdapContent(User user, String filter) throws LdapException {
5656
return lookupLdapContent(user, filter, null);
5757
}
5858

@@ -66,7 +66,7 @@ public Map<String, Set<String>> lookupLdapContent(User user, String filter) {
6666
* @see #lookupLdapContent(opengrok.auth.plugin.entity.User,
6767
* java.lang.String, java.lang.String[])
6868
*/
69-
public Map<String, Set<String>> lookupLdapContent(User user, String[] values) {
69+
public Map<String, Set<String>> lookupLdapContent(User user, String[] values) throws LdapException {
7070
return lookupLdapContent(user, null, values);
7171
}
7272

@@ -78,7 +78,7 @@ public Map<String, Set<String>> lookupLdapContent(User user, String[] values) {
7878
* @param values match these LDAP value
7979
* @return set of important attributes for the user
8080
*/
81-
public abstract Map<String, Set<String>> lookupLdapContent(User user, String filter, String[] values);
81+
public abstract Map<String, Set<String>> lookupLdapContent(User user, String filter, String[] values) throws LdapException;
8282

8383
/**
8484
* @return if the provider is correctly configured
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,19 @@
2121
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323

24-
package org.opengrok.indexer.authorization;
24+
package opengrok.auth.plugin.ldap;
2525

2626
/**
2727
* Unchecked exception to be thrown when LDAP server pool is down.
2828
*/
29-
public class LdapException extends RuntimeException {
29+
public class LdapException extends Exception {
3030
public static final long serialVersionUID = -1;
3131

32-
public LdapException() {
33-
super();
34-
}
35-
36-
public LdapException(String str) {
32+
LdapException(String str) {
3733
super(str);
3834
}
3935

40-
public LdapException(String str, Throwable ex) {
36+
LdapException(String str, Throwable ex) {
4137
super(str, ex);
4238
}
4339
}

plugins/src/opengrok/auth/plugin/ldap/LdapFacade.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import opengrok.auth.plugin.entity.User;
4646
import opengrok.auth.plugin.util.WebHook;
4747
import opengrok.auth.plugin.util.WebHooks;
48-
import org.opengrok.indexer.authorization.LdapException;
4948
import opengrok.auth.plugin.util.RestfulClient;
5049

5150
public class LdapFacade extends AbstractLdapProvider {
@@ -266,7 +265,7 @@ public boolean isConfigured() {
266265
* @return set of strings describing the user's attributes
267266
*/
268267
@Override
269-
public Map<String, Set<String>> lookupLdapContent(User user, String filter, String[] values) {
268+
public Map<String, Set<String>> lookupLdapContent(User user, String filter, String[] values) throws LdapException {
270269

271270
return lookup(
272271
user != null ? user.getUsername() : getSearchBase(),
@@ -299,7 +298,7 @@ public SearchControls getSearchControls() {
299298
*
300299
* @return results transformed with mapper
301300
*/
302-
private <T> T lookup(String dn, String filter, String[] attributes, AttributeMapper<T> mapper) {
301+
private <T> T lookup(String dn, String filter, String[] attributes, AttributeMapper<T> mapper) throws LdapException {
303302
return lookup(dn, filter, attributes, mapper, 0);
304303
}
305304

@@ -314,8 +313,9 @@ private <T> T lookup(String dn, String filter, String[] attributes, AttributeMap
314313
* @param fail current count of failures
315314
*
316315
* @return results transformed with mapper or {@code null} on failure
316+
* @throws LdapException LDAP exception
317317
*/
318-
private <T> T lookup(String dn, String filter, String[] attributes, AttributeMapper<T> mapper, int fail) {
318+
private <T> T lookup(String dn, String filter, String[] attributes, AttributeMapper<T> mapper, int fail) throws LdapException {
319319

320320
if (errorTimestamp > 0 && errorTimestamp + interval > System.currentTimeMillis()) {
321321
if (!reported) {

0 commit comments

Comments
 (0)