Search in sources :

Example 21 with Group

use of org.opengrok.indexer.configuration.Group 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 22 with Group

use of org.opengrok.indexer.configuration.Group 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 23 with Group

use of org.opengrok.indexer.configuration.Group in project OpenGrok by OpenGrok.

the class PluginClassLoaderTest method testTruePlugin.

@Test
public void testTruePlugin() {
    PluginClassLoader instance = new PluginClassLoader(pluginDirectory);
    Class<?> clazz = loadClass(instance, "opengrok.auth.plugin.TruePlugin");
    IAuthorizationPlugin plugin = getNewInstance(clazz);
    Group g = new Group("group1");
    Project p = new Project("project1");
    assertTrue(plugin.isAllowed(new DummyHttpServletRequest(), g));
    assertTrue(plugin.isAllowed(new DummyHttpServletRequest(), p));
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) PluginClassLoader(org.opengrok.indexer.framework.PluginClassLoader) Test(org.junit.jupiter.api.Test)

Example 24 with Group

use of org.opengrok.indexer.configuration.Group in project OpenGrok by OpenGrok.

the class AuthorizationEntity method processTargetGroupsAndProjects.

/**
 * Discover all targeted groups and projects for every group given by
 * {@link #forGroups()}.
 *
 * <ul>
 * <li>add to the {@link #forGroups()} all groups which are descendant
 * groups to the group</li>
 * <li>add to the {@link #forGroups()} all groups which are parent groups to
 * the group</li>
 * <li>add to the {@link #forProjects()} all projects and repositories which
 * are in the descendant groups or in the group itself</li>
 * <li>issue a warning for non-existent groups</li>
 * <li>issue a warning for non-existent projects</li>
 * </ul>
 */
protected void processTargetGroupsAndProjects() {
    Set<String> groups = new TreeSet<>();
    for (String x : forGroups()) {
        /**
         * Full group discovery takes place here. All projects/repositories
         * in the group are added into "forProjects" and all subgroups
         * (including projects/repositories) and parent groups (excluding
         * the projects/repositories) are added into "forGroups".
         *
         * If the group does not exist then a warning is issued.
         */
        Group g;
        if ((g = Group.getByName(x)) != null) {
            forProjects().addAll(g.getAllProjects().stream().map(Project::getName).collect(Collectors.toSet()));
            groups.addAll(g.getRelatedGroups().stream().map(Group::getName).collect(Collectors.toSet()));
            groups.add(x);
        } else {
            LOGGER.log(Level.WARNING, "Configured group \"{0}\" in forGroups section" + " for name \"{1}\" does not exist", new Object[] { x, getName() });
        }
    }
    setForGroups(groups);
    forProjects().removeIf((t) -> {
        /**
         * Check the existence of the projects and issue a warning if there
         * is no such project.
         */
        Project p;
        if ((p = Project.getByName(t)) == null) {
            LOGGER.log(Level.WARNING, "Configured project \"{0}\" in forProjects" + " section for name \"{1}\" does not exist", new Object[] { t, getName() });
            return true;
        }
        return false;
    });
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) TreeSet(java.util.TreeSet)

Example 25 with Group

use of org.opengrok.indexer.configuration.Group in project OpenGrok by OpenGrok.

the class ProjectHelperTest method testGetAllGroupedAllowedGroup.

/**
 * Test of getAllGrouped method, of class ProjectHelper.
 */
@Test
public void testGetAllGroupedAllowedGroup() {
    for (Group g : RuntimeEnvironment.getInstance().getGroups()) {
        if (g.getName().startsWith("allowed_group_0")) {
            Set<Project> result = helper.getAllGrouped(g);
            assertEquals(4, result.size());
            for (Project p : result) {
                assertTrue(p.getName().startsWith("allowed_"));
            }
        }
    }
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) Test(org.junit.jupiter.api.Test)

Aggregations

Group (org.opengrok.indexer.configuration.Group)33 Test (org.junit.jupiter.api.Test)19 Project (org.opengrok.indexer.configuration.Project)19 DummyHttpServletRequest (org.opengrok.indexer.web.DummyHttpServletRequest)7 User (opengrok.auth.plugin.entity.User)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 TreeSet (java.util.TreeSet)3 Entity (jakarta.ws.rs.client.Entity)2 GenericType (jakarta.ws.rs.core.GenericType)2 Response (jakarta.ws.rs.core.Response)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2