use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.
the class AuthorizationFrameworkReloadTest method testReloadCycle.
/**
* Sort of a stress test - call isAllowed() and reload() in parallel.
* This might uncover any snags with locking within AuthorizationFramework.
*/
@Test
public void testReloadCycle() {
String projectName = "project" + Math.random();
// Create authorization stack for single project.
AuthorizationStack stack = new AuthorizationStack(AuthControlFlag.REQUIRED, "stack for project " + projectName);
assertNotNull(stack);
stack.add(new AuthorizationPlugin(AuthControlFlag.REQUIRED, "opengrok.auth.plugin.FalsePlugin"));
stack.setForProjects(projectName);
AuthorizationFramework framework = new AuthorizationFramework(pluginDirectory.getPath(), stack);
// to avoid noise when loading classes of other tests
framework.setLoadClasses(false);
framework.reload();
// Perform simple sanity check before long run is entered. If this fails,
// it will be waste of time to continue with the test.
Project p = new Project(projectName);
DummyHttpServletRequest req = new DummyHttpServletRequest();
assertFalse(framework.isAllowed(req, p));
// Create a thread that does reload() every now and then.
runThread = true;
final int maxReloadSleep = 10;
Thread t = new Thread(() -> {
while (runThread) {
framework.reload();
try {
Thread.sleep((long) (Math.random() % maxReloadSleep) + 1);
} catch (InterruptedException ex) {
}
}
});
t.start();
// Process number or requests and check that framework decision is consistent.
for (int i = 0; i < 1000; i++) {
req = new DummyHttpServletRequest();
assertFalse(framework.isAllowed(req, p));
try {
// Should run more frequently than the thread performing reload().
Thread.sleep((long) (Math.random() % (maxReloadSleep / 3)) + 1);
} catch (InterruptedException ex) {
}
}
try {
// Terminate the thread.
runThread = false;
t.join();
} catch (InterruptedException ex) {
}
// Double check that at least one reload() was done.
long reloads = (long) Metrics.getRegistry().counter("authorization.stack.reload").count();
assertTrue(reloads > 0);
}
use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.
the class AuthorizationFrameworkReloadTest method testReloadSimple.
/**
* After {@code reload()} the session attributes should be invalidated.
* It is assumed that invalidation of HttpSession objects means that all
* the attributes will be unset.
*/
@Test
public void testReloadSimple() {
DummyHttpServletRequest req = new DummyHttpServletRequest();
AuthorizationFramework framework = new AuthorizationFramework(pluginDirectory.getPath());
// to avoid noise when loading classes of other tests
framework.setLoadClasses(false);
framework.reload();
// Ensure the framework was setup correctly.
assertNotNull(framework.getPluginDirectory());
assertEquals(pluginDirectory, framework.getPluginDirectory());
// Create pre-requisite objects - mainly the HTTP session with attribute.
Project p = new Project("project" + Math.random());
HttpSession session = req.getSession();
String attrName = "foo";
session.setAttribute(attrName, "bar");
assertNotNull(session.getAttribute(attrName));
// Reload the framework to increment the plugin generation version.
framework.reload();
// Let the framework check the request. This should invalidate the session
// since the version was incremented. In this test we are not interested
// in the actual result.
framework.isAllowed(req, p);
// Verify that the session no longer has the attribute.
assertNull(session.getAttribute(attrName));
}
use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.
the class AuthorizationEntityTest method testForGroupsAndForProjectsDiscovery.
@ParameterizedTest
@MethodSource("parameters")
public void testForGroupsAndForProjectsDiscovery(Function<Void, AuthorizationEntity> authEntityFactory) {
Group g1, g2, g3;
AuthorizationEntity authEntity;
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setProjectsEnabled(true);
env.getProjects().put("project 1", new Project("project 1"));
env.getProjects().put("project 2", new Project("project 2"));
env.getProjects().put("project 3", new Project("project 3"));
env.getProjects().put("project 4", new Project("project 4"));
env.getProjects().put("project 5", new Project("project 5"));
env.getProjects().put("project 6", new Project("project 6"));
env.getProjects().put("project 7", new Project("project 7"));
env.getProjects().put("project 8", new Project("project 8"));
env.getProjects().put("project 9", new Project("project 9"));
/**
* Structure<br>
* <pre>
* G1 + P1
* + P2
* + P3
* + G2
* + P4
* + P5
* + P6
* + P7
* G3 + P8
* + P9
* </pre>
*/
g1 = new Group();
g1.setName("group 1");
g1.addProject(env.getProjects().get("project 1"));
g1.addProject(env.getProjects().get("project 2"));
g1.addProject(env.getProjects().get("project 3"));
env.getGroups().add(g1);
g2 = new Group();
g2.setName("group 2");
g2.addProject(env.getProjects().get("project 4"));
g2.addProject(env.getProjects().get("project 5"));
g2.addProject(env.getProjects().get("project 6"));
g2.addProject(env.getProjects().get("project 7"));
g1.addGroup(g2);
env.getGroups().add(g2);
g3 = new Group();
g3.setName("group 3");
g3.addProject(env.getProjects().get("project 8"));
g3.addProject(env.getProjects().get("project 9"));
env.getGroups().add(g3);
// add g1 and all descendants their projects
authEntity = authEntityFactory.apply(null);
authEntity.setForGroups(new TreeSet<>());
authEntity.setForGroups("group 1");
authEntity.load(new TreeMap<>());
assertEquals(new TreeSet<>(Arrays.asList("group 1", "group 2")), authEntity.forGroups());
assertEquals(new TreeSet<>(Arrays.asList("project 1", "project 2", "project 3", "project 4", "project 5", "project 6", "project 7")), authEntity.forProjects());
// add group2, its parent g1 and g2 projects
authEntity = authEntityFactory.apply(null);
authEntity.setForGroups(new TreeSet<>());
authEntity.setForGroups("group 2");
authEntity.load(new TreeMap<>());
assertEquals(new TreeSet<>(Arrays.asList("group 1", "group 2")), authEntity.forGroups());
assertEquals(new TreeSet<>(Arrays.asList("project 4", "project 5", "project 6", "project 7")), authEntity.forProjects());
// add only g3 and its projects
authEntity = authEntityFactory.apply(null);
authEntity.setForGroups(new TreeSet<>());
authEntity.setForGroups("group 3");
authEntity.load(new TreeMap<>());
assertEquals(new TreeSet<>(Collections.singletonList("group 3")), authEntity.forGroups());
assertEquals(new TreeSet<>(Arrays.asList("project 8", "project 9")), authEntity.forProjects());
}
use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.
the class AuthorizationEntityTest method testForGroupsAndForProjectsDiscoveryInvalidProjectInGroup.
/**
* Listed projects in the group don't exist.
*/
@ParameterizedTest
@MethodSource("parameters")
public void testForGroupsAndForProjectsDiscoveryInvalidProjectInGroup(Function<Void, AuthorizationEntity> authEntityFactory) {
AuthorizationEntity authEntity = authEntityFactory.apply(null);
authEntity.setForGroups(new TreeSet<>(Arrays.asList("group 1", "group 2")));
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
Group g1 = new Group();
g1.setName("group 1");
g1.addProject(new Project("project 1"));
g1.addProject(new Project("project 2"));
g1.addProject(new Project("project 3"));
env.getGroups().add(g1);
authEntity.load(new TreeMap<>());
assertEquals(new TreeSet<>(Collections.singletonList("group 1")), authEntity.forGroups());
assertEquals(new TreeSet<>(), authEntity.forProjects());
}
use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.
the class TruePluginTest method shouldAllowRandomUserForAnyProject.
@Test
public void shouldAllowRandomUserForAnyProject() {
DummyHttpServletRequest req = new DummyHttpServletRequest();
req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomStringUtils.randomAlphanumeric(8)));
Project randomProject = new Project(RandomStringUtils.randomAlphanumeric(10));
boolean projectAllowed = plugin.isAllowed(req, randomProject);
assertTrue(projectAllowed, "should allow rando for random project 1");
randomProject = new Project(RandomStringUtils.randomAlphanumeric(10));
projectAllowed = plugin.isAllowed(req, randomProject);
assertTrue(projectAllowed, "should allow rando for random project 2");
}
Aggregations