Search in sources :

Example 16 with DummyHttpServletRequest

use of org.opengrok.indexer.web.DummyHttpServletRequest 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);
}
Also used : Project(org.opengrok.indexer.configuration.Project) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 17 with DummyHttpServletRequest

use of org.opengrok.indexer.web.DummyHttpServletRequest 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));
}
Also used : Project(org.opengrok.indexer.configuration.Project) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) Test(org.junit.jupiter.api.Test)

Example 18 with DummyHttpServletRequest

use of org.opengrok.indexer.web.DummyHttpServletRequest 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");
}
Also used : Project(org.opengrok.indexer.configuration.Project) User(opengrok.auth.plugin.entity.User) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 19 with DummyHttpServletRequest

use of org.opengrok.indexer.web.DummyHttpServletRequest in project OpenGrok by OpenGrok.

the class TruePluginTest method shouldAllowRandomUserForAnyGroup.

@Test
public void shouldAllowRandomUserForAnyGroup() {
    DummyHttpServletRequest req = new DummyHttpServletRequest();
    req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomStringUtils.randomAlphanumeric(8)));
    Group randomGroup = new Group(RandomStringUtils.randomAlphanumeric(10));
    boolean projectAllowed = plugin.isAllowed(req, randomGroup);
    assertTrue(projectAllowed, "should allow rando for random group 1");
    randomGroup = new Group(RandomStringUtils.randomAlphanumeric(10));
    projectAllowed = plugin.isAllowed(req, randomGroup);
    assertTrue(projectAllowed, "should allow rando for random group 2");
}
Also used : Group(org.opengrok.indexer.configuration.Group) User(opengrok.auth.plugin.entity.User) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 20 with DummyHttpServletRequest

use of org.opengrok.indexer.web.DummyHttpServletRequest in project OpenGrok by OpenGrok.

the class UserWhiteListPluginTest method shouldNotAllowRandomUserForAnyProject.

@ParameterizedTest
@MethodSource("parameters")
public void shouldNotAllowRandomUserForAnyProject(String param) {
    init(param);
    plugin.load(validPluginParameters);
    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);
    assertFalse(projectAllowed, "should not allow random user for random project 1");
    randomProject = new Project(RandomStringUtils.randomAlphanumeric(10));
    projectAllowed = plugin.isAllowed(req, randomProject);
    assertFalse(projectAllowed, "should not allow random user for random project 2");
}
Also used : Project(org.opengrok.indexer.configuration.Project) User(opengrok.auth.plugin.entity.User) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

DummyHttpServletRequest (org.opengrok.indexer.web.DummyHttpServletRequest)27 Test (org.junit.jupiter.api.Test)22 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)8 User (opengrok.auth.plugin.entity.User)8 Project (org.opengrok.indexer.configuration.Project)8 Group (org.opengrok.indexer.configuration.Group)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 File (java.io.File)3 PluginClassLoader (org.opengrok.indexer.framework.PluginClassLoader)2 Cookie (jakarta.servlet.http.Cookie)1 HttpSession (jakarta.servlet.http.HttpSession)1 Response (jakarta.ws.rs.core.Response)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 EnabledOnOs (org.junit.jupiter.api.condition.EnabledOnOs)1 Configuration (org.opengrok.indexer.configuration.Configuration)1 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)1 Annotation (org.opengrok.indexer.history.Annotation)1 PageConfig (org.opengrok.web.PageConfig)1