Search in sources :

Example 76 with Project

use of org.opengrok.indexer.configuration.Project 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)

Example 77 with Project

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

the class LdapUserPluginTest method testNegativeCache.

@Test
void testNegativeCache() throws LdapException {
    AbstractLdapProvider mockprovider = mock(LdapFacade.class);
    when(mockprovider.lookupLdapContent(isNull(), isNull(), any(String[].class))).thenReturn(null);
    Map<String, Object> params = getParamsMap();
    params.put(LdapUserPlugin.ATTRIBUTES, "mail");
    params.put(LdapUserPlugin.USE_DN, false);
    LdapUserPlugin origPlugin = new LdapUserPlugin();
    LdapUserPlugin plugin = Mockito.spy(origPlugin);
    plugin.load(params, mockprovider);
    assertSame(mockprovider, plugin.getLdapProvider());
    HttpServletRequest dummyRequest = new DummyHttpServletRequestLdap();
    User user = new User("foo@example.com", "id");
    dummyRequest.setAttribute(UserPlugin.REQUEST_ATTR, new User("foo", "123"));
    plugin.fillSession(dummyRequest, user);
    assertNotNull(dummyRequest.getSession().getAttribute(SESSION_ATTR));
    assertFalse(plugin.isAllowed(dummyRequest, new Project("foo")));
    assertFalse(plugin.isAllowed(dummyRequest, new Group("bar")));
    // Make sure that the session was filled so that the second call to isAllowed() did not fill it again.
    verify(plugin, times(2)).updateSession(eq(dummyRequest), anyString(), anyBoolean());
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) DummyHttpServletRequestLdap(opengrok.auth.plugin.util.DummyHttpServletRequestLdap) Project(org.opengrok.indexer.configuration.Project) Group(org.opengrok.indexer.configuration.Group) LdapUser(opengrok.auth.entity.LdapUser) User(opengrok.auth.plugin.entity.User) AbstractLdapProvider(opengrok.auth.plugin.ldap.AbstractLdapProvider) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 78 with Project

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

the class UserPluginTest method testTimeoutedUser.

@Test
public void testTimeoutedUser() {
    HttpServletRequest req;
    assertFalse(plugin.isAllowed(req = createRequest("007", true), new Group()));
    assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
    assertFalse(plugin.isAllowed(req = createRequest("008", true), new Project()));
    assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
    assertFalse(plugin.isAllowed(req = createRequest("009", true), createGroup("some group")));
    assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
    assertFalse(plugin.isAllowed(req = createRequest("00A", true), createProject("some project")));
    assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) Test(org.junit.jupiter.api.Test)

Example 79 with Project

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

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

the class PageConfigTest method testGetResourceFileList.

/**
 * Testing the root of /xref for authorization filtering.
 */
@Test
public void testGetResourceFileList() {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    // backup original values
    String oldSourceRootPath = env.getSourceRootPath();
    AuthorizationFramework oldAuthorizationFramework = env.getAuthorizationFramework();
    Map<String, Project> oldProjects = env.getProjects();
    // Set up the source root directory containing some projects.
    env.setSourceRoot(repository.getSourceRoot());
    env.setProjectsEnabled(true);
    // Enable projects.
    for (String file : new File(repository.getSourceRoot()).list()) {
        Project proj = new Project(file);
        proj.setIndexed(true);
        env.getProjects().put(file, proj);
    }
    HttpServletRequest req = createRequest("/source", "/xref", "");
    PageConfig cfg = PageConfig.get(req);
    List<String> allFiles = new ArrayList<>(cfg.getResourceFileList());
    /**
     * Check if there are some files (the "5" here is just a sufficient
     * value for now which won't break any future repository tests) without
     * any authorization.
     */
    assertTrue(allFiles.size() > 5);
    assertTrue(allFiles.contains("git"));
    assertTrue(allFiles.contains("mercurial"));
    /**
     * Now set up the same projects with authorization plugin enabling only
     * some of them.
     * <pre>
     *  - disabling "git"
     *  - disabling "mercurial"
     * </pre>
     */
    env.setAuthorizationFramework(new AuthorizationFramework());
    env.getAuthorizationFramework().reload();
    env.getAuthorizationFramework().getStack().add(new AuthorizationPlugin(AuthControlFlag.REQUIRED, new TestPlugin() {

        @Override
        public boolean isAllowed(HttpServletRequest request, Project project) {
            return !project.getName().startsWith("git") && !project.getName().startsWith("mercurial");
        }
    }));
    req = createRequest("/source", "/xref", "");
    cfg = PageConfig.get(req);
    List<String> filteredFiles = new ArrayList<>(cfg.getResourceFileList());
    // list subtraction - retains only disabled files
    allFiles.removeAll(filteredFiles);
    assertEquals(2, allFiles.size());
    assertTrue(allFiles.contains("git"));
    assertTrue(allFiles.contains("mercurial"));
    // restore original values
    env.setAuthorizationFramework(oldAuthorizationFramework);
    env.setSourceRoot(oldSourceRootPath);
    env.setProjects(oldProjects);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) Project(org.opengrok.indexer.configuration.Project) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) AuthorizationFramework(org.opengrok.indexer.authorization.AuthorizationFramework) ArrayList(java.util.ArrayList) AuthorizationPlugin(org.opengrok.indexer.authorization.AuthorizationPlugin) TestPlugin(org.opengrok.indexer.authorization.TestPlugin) File(java.io.File) 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