Skip to content

Commit 687e133

Browse files
author
Vladimir Kotal
committed
actually invalidate session attributes on plugin reload
fixes #1826
1 parent a1d7060 commit 687e133

19 files changed

+406
-175
lines changed

build.xml

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

11031103
<!-- Build the plugins. -->
1104-
<target name="plugins" depends="jar">
1104+
<target name="plugins" depends="jar" description="create jar of all authorization plugins for distribution">
11051105
<mkdir dir="${build.dir}/plugins"/>
11061106
<javac srcdir="plugins/"
11071107
destdir="${build.dir}/plugins"

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

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import opengrok.auth.plugin.ldap.AbstractLdapProvider;
3333
import opengrok.auth.plugin.ldap.FakeLdapFacade;
3434
import opengrok.auth.plugin.ldap.LdapFacade;
35+
import org.opensolaris.opengrok.authorization.AuthorizationFramework;
3536
import org.opensolaris.opengrok.authorization.IAuthorizationPlugin;
3637
import org.opensolaris.opengrok.configuration.Group;
3738
import org.opensolaris.opengrok.configuration.Project;
@@ -42,7 +43,6 @@
4243
* <ul>
4344
* <li>controlling the established session</li>
4445
* <li>controlling if the session belongs to the user</li>
45-
* <li>controlling plug-in version</li>
4646
* </ul>
4747
*
4848
* <p>
@@ -63,9 +63,9 @@ abstract public class AbstractLdapPlugin implements IAuthorizationPlugin {
6363
protected static final String CONFIGURATION_PARAM = "configuration";
6464
protected static final String FAKE_PARAM = "fake";
6565

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";
66+
private final static String SESSION_PREFIX = "opengrok-abstract-ldap-plugin-";
67+
protected String SESSION_USERNAME = SESSION_PREFIX + "username";
68+
protected String SESSION_ESTABLISHED = SESSION_PREFIX + "session-established";
6969

7070
/**
7171
* Configuration for the LDAP servers.
@@ -86,7 +86,6 @@ abstract public class AbstractLdapPlugin implements IAuthorizationPlugin {
8686
public AbstractLdapPlugin() {
8787
SESSION_USERNAME += "-" + nextId;
8888
SESSION_ESTABLISHED += "-" + nextId;
89-
SESSION_VERSION += "-" + nextId;
9089
nextId++;
9190
}
9291

@@ -197,11 +196,8 @@ public AbstractLdapProvider getLdapProvider() {
197196
* @return true if it does; false otherwise
198197
*/
199198
protected boolean isSameUser(String sessionUsername, String authUser) {
200-
if (sessionUsername != null
201-
&& sessionUsername.equals(authUser)) {
202-
return true;
203-
}
204-
return false;
199+
return sessionUsername != null
200+
&& sessionUsername.equals(authUser);
205201
}
206202

207203
/**
@@ -214,7 +210,6 @@ protected boolean isSameUser(String sessionUsername, String authUser) {
214210
protected boolean sessionExists(HttpServletRequest req) {
215211
return req != null && req.getSession() != null
216212
&& req.getSession().getAttribute(SESSION_ESTABLISHED) != null
217-
&& req.getSession().getAttribute(SESSION_VERSION) != null
218213
&& req.getSession().getAttribute(SESSION_USERNAME) != null;
219214
}
220215

@@ -234,39 +229,40 @@ protected boolean sessionExists(HttpServletRequest req) {
234229
@SuppressWarnings("unchecked")
235230
private void ensureSessionExists(HttpServletRequest req) {
236231
User user;
232+
237233
if (req.getSession() == null) {
238234
// old/invalid request (should not happen)
239235
return;
240236
}
241-
237+
238+
// The cast to User should not be problem as this object is stored
239+
// in the request itself (as opposed to in the session).
242240
if ((user = (User) req.getAttribute(UserPlugin.REQUEST_ATTR)) == null) {
243-
updateSession(req, null, false, getPluginVersion());
241+
updateSession(req, null, false);
244242
return;
245243
}
246244

247245
if (sessionExists(req)
248246
// we've already filled the groups and projects
249247
&& (boolean) req.getSession().getAttribute(SESSION_ESTABLISHED)
250248
// 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)) {
249+
&& isSameUser((String) req.getSession().getAttribute(SESSION_USERNAME), user.getUsername())) {
254250
/**
255251
* The session is already filled so no need to
256252
* {@link #updateSession()}
257253
*/
258254
return;
259255
}
260256

261-
updateSession(req, user.getUsername(), false, getPluginVersion());
257+
updateSession(req, user.getUsername(), false);
262258

263259
if (ldap == null) {
264260
return;
265261
}
266262

267263
fillSession(req, user);
268264

269-
updateSession(req, user.getUsername(), true, getPluginVersion());
265+
updateSession(req, user.getUsername(), true);
270266
}
271267

272268
/**
@@ -275,44 +271,12 @@ && isSameUser((String) req.getSession().getAttribute(SESSION_USERNAME), user.get
275271
* @param req the request
276272
* @param username new username
277273
* @param established new value for established
278-
* @param sessionV new value for session version
279274
*/
280275
protected void updateSession(HttpServletRequest req,
281276
String username,
282-
boolean established,
283-
int sessionV) {
277+
boolean established) {
284278
setSessionEstablished(req, established);
285279
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);
316280
}
317281

318282
/**
@@ -335,15 +299,6 @@ protected void setSessionUsername(HttpServletRequest req, String value) {
335299
req.getSession().setAttribute(SESSION_USERNAME, value);
336300
}
337301

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-
347302
@Override
348303
public boolean isAllowed(HttpServletRequest request, Project project) {
349304
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: 2 additions & 33 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;
@@ -80,7 +81,6 @@ public void setUp() {
8081
plugin.load(parameters);
8182

8283
framework = new AuthorizationFramework(null);
83-
framework.setPluginVersion(1);
8484
}
8585

8686
private void prepareRequest(String username, String mail, String... ous) {
@@ -90,7 +90,6 @@ private void prepareRequest(String username, String mail, String... ous) {
9090
new TreeSet<>(Arrays.asList(ous))));
9191
plugin.setSessionEstablished(dummyRequest, true);
9292
plugin.setSessionUsername(dummyRequest, username);
93-
plugin.setSessionVersion(dummyRequest, 1);
9493
}
9594

9695
private Project makeProject(String name) {
@@ -136,36 +135,6 @@ public void testIsAllowed() {
136135

137136
prepareRequest("00A", "[email protected]", "MI6", "MI7");
138137

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-
169138
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Random Project")));
170139
Assert.assertTrue(plugin.isAllowed(dummyRequest, makeProject("Project 1")));
171140
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)