Skip to content

Commit b6008ab

Browse files
authored
Merge pull request #1928 from vladak/plugins_session_invalidate
Plugins session invalidate
2 parents 07a41d5 + 22080cc commit b6008ab

23 files changed

+538
-231
lines changed

build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
11281128
</target>
11291129

11301130
<!-- Build the plugins. -->
1131-
<target name="plugins" depends="jar">
1131+
<target name="plugins" depends="jar" description="create jar of all authorization plugins for distribution">
11321132
<mkdir dir="${build.dir}/plugins"/>
11331133
<javac srcdir="plugins/"
11341134
destdir="${build.dir}/plugins"

plugins/LdapPlugin/src/opengrok/auth/plugin/AbstractLdapPlugin.java

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@
3535
import org.opensolaris.opengrok.authorization.IAuthorizationPlugin;
3636
import org.opensolaris.opengrok.configuration.Group;
3737
import org.opensolaris.opengrok.configuration.Project;
38-
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
3938

4039
/**
4140
* Abstract class for all plug-ins working with LDAP. Takes care of
4241
* <ul>
4342
* <li>controlling the established session</li>
4443
* <li>controlling if the session belongs to the user</li>
45-
* <li>controlling plug-in version</li>
4644
* </ul>
4745
*
4846
* <p>
@@ -63,9 +61,9 @@ abstract public class AbstractLdapPlugin implements IAuthorizationPlugin {
6361
protected static final String CONFIGURATION_PARAM = "configuration";
6462
protected static final String FAKE_PARAM = "fake";
6563

66-
protected String SESSION_USERNAME = "opengrok-group-plugin-username";
67-
protected String SESSION_ESTABLISHED = "opengrok-group-plugin-session-established";
68-
protected String SESSION_VERSION = "opengrok-group-plugin-session-version";
64+
private final static String SESSION_PREFIX = "opengrok-abstract-ldap-plugin-";
65+
protected String SESSION_USERNAME = SESSION_PREFIX + "username";
66+
protected String SESSION_ESTABLISHED = SESSION_PREFIX + "session-established";
6967

7068
/**
7169
* Configuration for the LDAP servers.
@@ -86,7 +84,6 @@ abstract public class AbstractLdapPlugin implements IAuthorizationPlugin {
8684
public AbstractLdapPlugin() {
8785
SESSION_USERNAME += "-" + nextId;
8886
SESSION_ESTABLISHED += "-" + nextId;
89-
SESSION_VERSION += "-" + nextId;
9087
nextId++;
9188
}
9289

@@ -197,11 +194,8 @@ public AbstractLdapProvider getLdapProvider() {
197194
* @return true if it does; false otherwise
198195
*/
199196
protected boolean isSameUser(String sessionUsername, String authUser) {
200-
if (sessionUsername != null
201-
&& sessionUsername.equals(authUser)) {
202-
return true;
203-
}
204-
return false;
197+
return sessionUsername != null
198+
&& sessionUsername.equals(authUser);
205199
}
206200

207201
/**
@@ -214,7 +208,6 @@ protected boolean isSameUser(String sessionUsername, String authUser) {
214208
protected boolean sessionExists(HttpServletRequest req) {
215209
return req != null && req.getSession() != null
216210
&& req.getSession().getAttribute(SESSION_ESTABLISHED) != null
217-
&& req.getSession().getAttribute(SESSION_VERSION) != null
218211
&& req.getSession().getAttribute(SESSION_USERNAME) != null;
219212
}
220213

@@ -234,39 +227,40 @@ protected boolean sessionExists(HttpServletRequest req) {
234227
@SuppressWarnings("unchecked")
235228
private void ensureSessionExists(HttpServletRequest req) {
236229
User user;
230+
237231
if (req.getSession() == null) {
238232
// old/invalid request (should not happen)
239233
return;
240234
}
241-
235+
236+
// The cast to User should not be problem as this object is stored
237+
// in the request itself (as opposed to in the session).
242238
if ((user = (User) req.getAttribute(UserPlugin.REQUEST_ATTR)) == null) {
243-
updateSession(req, null, false, getPluginVersion());
239+
updateSession(req, null, false);
244240
return;
245241
}
246242

247243
if (sessionExists(req)
248244
// we've already filled the groups and projects
249245
&& (boolean) req.getSession().getAttribute(SESSION_ESTABLISHED)
250246
// the session belongs to the user from the request
251-
&& isSameUser((String) req.getSession().getAttribute(SESSION_USERNAME), user.getUsername())
252-
// and this is not a case when we want to renew all sessions
253-
&& !isSessionInvalidated(req)) {
247+
&& isSameUser((String) req.getSession().getAttribute(SESSION_USERNAME), user.getUsername())) {
254248
/**
255249
* The session is already filled so no need to
256250
* {@link #updateSession()}
257251
*/
258252
return;
259253
}
260254

261-
updateSession(req, user.getUsername(), false, getPluginVersion());
255+
updateSession(req, user.getUsername(), false);
262256

263257
if (ldap == null) {
264258
return;
265259
}
266260

267261
fillSession(req, user);
268262

269-
updateSession(req, user.getUsername(), true, getPluginVersion());
263+
updateSession(req, user.getUsername(), true);
270264
}
271265

272266
/**
@@ -275,44 +269,12 @@ && isSameUser((String) req.getSession().getAttribute(SESSION_USERNAME), user.get
275269
* @param req the request
276270
* @param username new username
277271
* @param established new value for established
278-
* @param sessionV new value for session version
279272
*/
280273
protected void updateSession(HttpServletRequest req,
281274
String username,
282-
boolean established,
283-
int sessionV) {
275+
boolean established) {
284276
setSessionEstablished(req, established);
285277
setSessionUsername(req, username);
286-
setSessionVersion(req, sessionV);
287-
}
288-
289-
/**
290-
* Is this session marked as invalid?
291-
*
292-
* @param req the request
293-
* @return true if it is; false otherwise
294-
*/
295-
protected boolean isSessionInvalidated(HttpServletRequest req) {
296-
Integer version;
297-
if ((version = (Integer) req.getAttribute(SESSION_VERSION)) != null) {
298-
return version != getPluginVersion();
299-
}
300-
if ((version = (Integer) req.getSession().getAttribute(SESSION_VERSION)) != null) {
301-
req.setAttribute(SESSION_VERSION, version);
302-
return version != getPluginVersion();
303-
}
304-
return true;
305-
}
306-
307-
/**
308-
* Set session version into the session.
309-
*
310-
* @param req request containing the session
311-
* @param value the value
312-
*/
313-
protected void setSessionVersion(HttpServletRequest req, Integer value) {
314-
req.getSession().setAttribute(SESSION_VERSION, value);
315-
req.setAttribute(SESSION_VERSION, value);
316278
}
317279

318280
/**
@@ -335,15 +297,6 @@ protected void setSessionUsername(HttpServletRequest req, String value) {
335297
req.getSession().setAttribute(SESSION_USERNAME, value);
336298
}
337299

338-
/**
339-
* Return the current plug-in version tracked by the authorization framework.
340-
*
341-
* @return the version
342-
*/
343-
protected static int getPluginVersion() {
344-
return RuntimeEnvironment.getInstance().getPluginVersion();
345-
}
346-
347300
@Override
348301
public boolean isAllowed(HttpServletRequest request, Project project) {
349302
ensureSessionExists(request);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void fillSession(HttpServletRequest req, User user) {
127127
protected void updateSession(HttpServletRequest req, boolean allowed) {
128128
req.getSession().setAttribute(SESSION_ALLOWED, allowed);
129129
}
130-
130+
131131
@Override
132132
public boolean checkEntity(HttpServletRequest request, Project project) {
133133
return ((Boolean) request.getSession().getAttribute(SESSION_ALLOWED));

plugins/LdapPlugin/test/opengrok/auth/plugin/LdapAttrTest.java renamed to plugins/LdapPlugin/test/opengrok/auth/plugin/LdapAttrPluginTest.java

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.TreeMap;
3434
import java.util.TreeSet;
3535
import javax.servlet.http.HttpServletRequest;
36+
import javax.servlet.http.HttpSession;
3637
import opengrok.auth.entity.LdapUser;
3738
import opengrok.auth.plugin.entity.User;
3839
import opengrok.auth.plugin.util.DummyHttpServletRequestLdap;
@@ -45,7 +46,7 @@
4546
import org.opensolaris.opengrok.configuration.Group;
4647
import org.opensolaris.opengrok.configuration.Project;
4748

48-
public class LdapAttrTest {
49+
public class LdapAttrPluginTest {
4950

5051
private HttpServletRequest dummyRequest;
5152
private LdapAttrPlugin plugin;
@@ -79,8 +80,8 @@ public void setUp() {
7980

8081
plugin.load(parameters);
8182

82-
framework = new AuthorizationFramework(null);
83-
framework.setPluginVersion(1);
83+
framework = new AuthorizationFramework();
84+
framework.reload();
8485
}
8586

8687
private void prepareRequest(String username, String mail, String... ous) {
@@ -90,7 +91,6 @@ private void prepareRequest(String username, String mail, String... ous) {
9091
new TreeSet<>(Arrays.asList(ous))));
9192
plugin.setSessionEstablished(dummyRequest, true);
9293
plugin.setSessionUsername(dummyRequest, username);
93-
plugin.setSessionVersion(dummyRequest, 1);
9494
}
9595

9696
private Project makeProject(String name) {
@@ -136,36 +136,6 @@ public void testIsAllowed() {
136136

137137
prepareRequest("00A", "[email protected]", "MI6", "MI7");
138138

139-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Random Project")));
140-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Project 1")));
141-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 1")));
142-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 2")));
143-
144-
}
145-
146-
@Test
147-
public void testInvalidateSession() {
148-
/**
149-
* whitelist[mail] => [[email protected], [email protected], just_a_text]
150-
*/
151-
prepareRequest("007", "[email protected]", "MI6", "MI7");
152-
153-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Random Project")));
154-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Project 1")));
155-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 1")));
156-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 2")));
157-
158-
framework.increasePluginVersion();
159-
prepareRequest("007", "[email protected]", "MI6", "MI7");
160-
161-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Random Project")));
162-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Project 1")));
163-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 1")));
164-
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 2")));
165-
166-
plugin.setSessionVersion(dummyRequest, 2);
167-
prepareRequest("007", "[email protected]", "MI6", "MI7");
168-
169139
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Random Project")));
170140
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Project 1")));
171141
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeGroup("Group 1")));

plugins/LdapPlugin/test/opengrok/auth/plugin/LdapFilterTest.java renamed to plugins/LdapPlugin/test/opengrok/auth/plugin/LdapFilterPluginTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import static org.junit.Assert.assertEquals;
3333

34-
public class LdapFilterTest {
34+
public class LdapFilterPluginTest {
3535

3636
private LdapFilterPlugin plugin;
3737

plugins/LdapPlugin/test/opengrok/auth/plugin/util/DummyHttpServletRequestLdap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import javax.servlet.http.HttpSessionContext;
4141
import opengrok.auth.plugin.UserPlugin;
4242
import opengrok.auth.plugin.entity.User;
43+
import org.opensolaris.opengrok.util.RandomString;
4344

4445
public class DummyHttpServletRequestLdap implements HttpServletRequest {
4546

@@ -60,7 +61,7 @@ public String getId() {
6061
if ((user = (User) getAttribute(UserPlugin.REQUEST_ATTR)) != null) {
6162
return user.getUsername();
6263
}
63-
return Strings.generate(5);
64+
return RandomString.generate(5);
6465
}
6566

6667
@Override

0 commit comments

Comments
 (0)