Search in sources :

Example 71 with Project

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

Example 72 with Project

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));
}
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 73 with Project

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());
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 74 with Project

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());
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 75 with Project

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");
}
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)

Aggregations

Project (org.opengrok.indexer.configuration.Project)88 Test (org.junit.jupiter.api.Test)42 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)27 File (java.io.File)22 Group (org.opengrok.indexer.configuration.Group)20 RepositoryInfo (org.opengrok.indexer.history.RepositoryInfo)17 ArrayList (java.util.ArrayList)16 TreeSet (java.util.TreeSet)11 IOException (java.io.IOException)10 DummyHttpServletRequest (org.opengrok.indexer.web.DummyHttpServletRequest)10 List (java.util.List)8 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)7 Path (jakarta.ws.rs.Path)7 HistoryGuru (org.opengrok.indexer.history.HistoryGuru)7 Path (java.nio.file.Path)6 Map (java.util.Map)6 Paths (java.nio.file.Paths)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 MercurialRepositoryTest (org.opengrok.indexer.history.MercurialRepositoryTest)5