Search in sources :

Example 1 with AuthorizationPlugin

use of org.opengrok.indexer.authorization.AuthorizationPlugin in project OpenGrok by OpenGrok.

the class ConfigurationHelp method getSampleValue.

private static Object getSampleValue(Method setter, Object defaultValue) {
    Class<?> paramType = setter.getParameterTypes()[0];
    Type genType = setter.getGenericParameterTypes()[0];
    if (setter.getName().equals("setBugPattern")) {
        return "Sample Bug \\#(\\d+)";
    } else if (setter.getName().equals("setReviewPattern")) {
        return "Sample Issue \\#(\\d+)";
    } else if (paramType == String.class) {
        return "user-specified-value";
    } else if (paramType == int.class) {
        return 1 + (int) defaultValue;
    } else if (paramType == long.class) {
        return 1 + (long) defaultValue;
    } else if (paramType == short.class) {
        return (short) (1 + (short) defaultValue);
    } else if (paramType == boolean.class) {
        if (defaultValue == null) {
            return null;
        }
        return !(boolean) defaultValue;
    } else if (paramType == double.class) {
        return 1 + (double) defaultValue;
    } else if (paramType == List.class) {
        return getSampleListValue(genType);
    } else if (paramType == Map.class) {
        return getSampleMapValue(genType);
    } else if (paramType == Set.class) {
        return getSampleSetValue(genType);
    } else if (paramType == AuthorizationStack.class) {
        AuthorizationStack astck = new AuthorizationStack(AuthControlFlag.REQUIRED, "user-specified-value");
        astck.add(new AuthorizationPlugin(AuthControlFlag.REQUISITE, "user-specified-value"));
        return astck;
    } else if (paramType == Filter.class) {
        Filter flt = new Filter();
        flt.add("user-specified-(patterns)*");
        flt.add("user-specified-filename");
        flt.add("user/specified/path");
        return flt;
    } else if (paramType == IgnoredNames.class) {
        IgnoredNames inm = new IgnoredNames();
        inm.add("f:user-specified-value");
        inm.add("d:user-specified-value");
        return inm;
    } else if (paramType.isEnum()) {
        for (Object value : paramType.getEnumConstants()) {
            if (!value.equals(defaultValue)) {
                return value;
            }
        }
        return null;
    } else if (paramType == SuggesterConfig.class) {
        return SuggesterConfig.getForHelp();
    } else if (paramType == StatsdConfig.class) {
        return StatsdConfig.getForHelp();
    } else {
        throw new UnsupportedOperationException("getSampleValue() for " + paramType + ", " + genType);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AuthorizationStack(org.opengrok.indexer.authorization.AuthorizationStack) AuthorizationPlugin(org.opengrok.indexer.authorization.AuthorizationPlugin) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 2 with AuthorizationPlugin

use of org.opengrok.indexer.authorization.AuthorizationPlugin in project OpenGrok by OpenGrok.

the class RuntimeEnvironmentTest method testAuthorizationStackDecode.

@Test
public void testAuthorizationStackDecode() throws IOException {
    String confString = "<?xml version='1.0' encoding='UTF-8'?>\n" + "<java class=\"java.beans.XMLDecoder\" version=\"1.8.0_121\">\n" + " <object class=\"org.opengrok.indexer.configuration.Configuration\">\n" + "\t<void property=\"pluginStack\">\n" + "\t\t<void method=\"add\">\n" + "\t\t\t<object id=\"first_plugin\" class=\"org.opengrok.indexer.authorization.AuthorizationPlugin\">\n" + "\t\t\t\t<void property=\"flag\">\n" + "\t\t\t\t\t<string>sufficient</string>\n" + "\t\t\t\t</void>\n" + "\t\t\t\t<void property=\"name\">\n" + "\t\t\t\t\t<string>Plugin</string>\n" + "\t\t\t\t</void>\n" + "\t\t\t</object>\n" + "\t\t</void>\n" + "\t\t<void method=\"add\">\n" + "\t\t\t<object id=\"first_stack\" class=\"org.opengrok.indexer.authorization.AuthorizationStack\">\n" + "\t\t\t\t<void property=\"flag\">\n" + "\t\t\t\t\t<string>required</string>\n" + "\t\t\t\t</void>\n" + "\t\t\t\t<void property=\"name\">\n" + "\t\t\t\t\t<string>basic stack</string>\n" + "\t\t\t\t</void>\n" + "                             <void property=\"stack\">" + "                                 <void method=\"add\">" + "\t                 \t\t<object class=\"org.opengrok.indexer.authorization.AuthorizationPlugin\">\n" + "\t                 \t\t\t<void property=\"flag\">\n" + "\t                 \t\t\t\t<string>required</string>\n" + "\t                 \t\t\t</void>\n" + "\t                 \t\t\t<void property=\"name\">\n" + "\t                 \t\t\t\t<string>NestedPlugin</string>\n" + "\t                 \t\t\t</void>\n" + "\t\t                 \t</object>\n" + "                                 </void>" + "                                 <void method=\"add\">" + "\t                 \t\t<object class=\"org.opengrok.indexer.authorization.AuthorizationPlugin\">\n" + "\t                 \t\t\t<void property=\"flag\">\n" + "\t                 \t\t\t\t<string>requisite</string>\n" + "\t                 \t\t\t</void>\n" + "\t                 \t\t\t<void property=\"name\">\n" + "\t                 \t\t\t\t<string>NestedPlugin</string>\n" + "\t                 \t\t\t</void>\n" + "                                             <void property=\"setup\">" + "                                                 <void method=\"put\">" + "                                                     <string>key</string>" + "                                                     <string>value</string>" + "                                                 </void>" + "                                                 <void method=\"put\">" + "                                                     <string>plugin</string>" + "                                                     <object idref=\"first_plugin\" />" + "                                                 </void>" + "                                             </void>" + "\t\t                 \t</object>\n" + "                                 </void>" + "                             </void>" + "\t\t\t</object>\n" + "\t\t</void>\n" + "\t\t<void method=\"add\">\n" + "\t\t\t<object class=\"org.opengrok.indexer.authorization.AuthorizationPlugin\">\n" + "\t\t\t\t<void property=\"flag\">\n" + "\t\t\t\t\t<string>requisite</string>\n" + "\t\t\t\t</void>\n" + "\t\t\t\t<void property=\"name\">\n" + "\t\t\t\t\t<string>Requisite</string>\n" + "\t\t\t\t</void>\n" + "\t\t\t</object>\n" + "\t\t</void>\n" + "\t\t<void method=\"add\">\n" + "\t\t\t<object class=\"org.opengrok.indexer.authorization.AuthorizationStack\">\n" + "\t\t\t\t<void property=\"flag\">\n" + "\t\t\t\t\t<string>required</string>\n" + "\t\t\t\t</void>\n" + "\t\t\t\t<void property=\"name\">\n" + "\t\t\t\t\t<string>advanced stack</string>\n" + "\t\t\t\t</void>\n" + "                             <void property=\"stack\">" + "                                 <void method=\"add\">" + "\t                 \t\t<object class=\"org.opengrok.indexer.authorization.AuthorizationPlugin\">\n" + "\t                 \t\t\t<void property=\"flag\">\n" + "\t                 \t\t\t\t<string>required</string>\n" + "\t                 \t\t\t</void>\n" + "\t                 \t\t\t<void property=\"name\">\n" + "\t                 \t\t\t\t<string>NestedPlugin</string>\n" + "\t                 \t\t\t</void>\n" + "\t\t                 \t</object>\n" + "                                 </void>" + "                                 <void method=\"add\">" + "\t                 \t\t<object class=\"org.opengrok.indexer.authorization.AuthorizationPlugin\">\n" + "\t                 \t\t\t<void property=\"flag\">\n" + "\t                 \t\t\t\t<string>requisite</string>\n" + "\t                 \t\t\t</void>\n" + "\t                 \t\t\t<void property=\"name\">\n" + "\t                 \t\t\t\t<string>NestedPlugin</string>\n" + "\t                 \t\t\t</void>\n" + "                                             <void property=\"setup\">" + "                                                 <void method=\"put\">" + "                                                     <string>key</string>" + "                                                     <string>other value</string>" + "                                                 </void>" + "                                                 <void method=\"put\">" + "                                                     <string>plugin</string>" + "                                                     <object idref=\"first_plugin\" />" + "                                                 </void>" + "                                             </void>" + "\t\t                 \t</object>\n" + "                                 </void>" + "                             </void>" + "\t\t\t</object>\n" + "\t\t</void>\n" + "\t\t<void method=\"add\">\n" + "\t\t\t<object idref=\"first_stack\" />" + "\t\t</void>\n" + "\t</void>\n" + " </object>\n" + "</java>";
    Configuration conf = Configuration.makeXMLStringAsConfiguration(confString);
    assertNotNull(conf.getPluginStack());
    AuthorizationStack pluginConfiguration = conf.getPluginStack();
    assertEquals(5, pluginConfiguration.getStack().size());
    // single plugins
    assertTrue(pluginConfiguration.getStack().get(0).getFlag().isSufficient());
    assertEquals("Plugin", pluginConfiguration.getStack().get(0).getName());
    assertTrue(pluginConfiguration.getStack().get(2).getFlag().isRequisite());
    assertEquals("Requisite", pluginConfiguration.getStack().get(2).getName());
    /**
     * Third element is a stack which defines two nested plugins.
     */
    assertTrue(pluginConfiguration.getStack().get(1) instanceof AuthorizationStack);
    AuthorizationStack stack = (AuthorizationStack) pluginConfiguration.getStack().get(1);
    assertTrue(stack.getFlag().isRequired());
    assertEquals("basic stack", stack.getName());
    assertEquals(2, stack.getStack().size());
    assertTrue(stack.getStack().get(0) instanceof AuthorizationPlugin);
    assertEquals("NestedPlugin", stack.getStack().get(0).getName());
    assertTrue(stack.getStack().get(0).isRequired());
    assertTrue(stack.getStack().get(1) instanceof AuthorizationPlugin);
    assertEquals("NestedPlugin", stack.getStack().get(1).getName());
    assertTrue(stack.getStack().get(1).isRequisite());
    AuthorizationPlugin plugin = (AuthorizationPlugin) stack.getStack().get(1);
    assertTrue(plugin.getSetup().containsKey("key"));
    assertEquals("value", plugin.getSetup().get("key"));
    assertTrue(plugin.getSetup().containsKey("plugin"));
    assertTrue(plugin.getSetup().get("plugin") instanceof AuthorizationPlugin);
    assertEquals(pluginConfiguration.getStack().get(0), plugin.getSetup().get("plugin"));
    /**
     * Fourth element is a stack slightly changed from the previous stack.
     * Only the setup for the particular plugin is changed.
     */
    assertTrue(pluginConfiguration.getStack().get(3) instanceof AuthorizationStack);
    stack = (AuthorizationStack) pluginConfiguration.getStack().get(3);
    assertTrue(stack.getFlag().isRequired());
    assertEquals("advanced stack", stack.getName());
    assertEquals(2, stack.getStack().size());
    assertTrue(stack.getStack().get(0) instanceof AuthorizationPlugin);
    assertEquals("NestedPlugin", stack.getStack().get(0).getName());
    assertTrue(stack.getStack().get(0).isRequired());
    assertTrue(stack.getStack().get(1) instanceof AuthorizationPlugin);
    assertEquals("NestedPlugin", stack.getStack().get(1).getName());
    assertTrue(stack.getStack().get(1).isRequisite());
    plugin = (AuthorizationPlugin) stack.getStack().get(1);
    assertTrue(plugin.getSetup().containsKey("key"));
    assertEquals("other value", plugin.getSetup().get("key"));
    assertTrue(plugin.getSetup().containsKey("plugin"));
    assertTrue(plugin.getSetup().get("plugin") instanceof AuthorizationPlugin);
    assertEquals(pluginConfiguration.getStack().get(0), plugin.getSetup().get("plugin"));
    /**
     * Fifth element is a direct copy of the first stack.
     */
    assertTrue(pluginConfiguration.getStack().get(4) instanceof AuthorizationStack);
    stack = (AuthorizationStack) pluginConfiguration.getStack().get(4);
    assertTrue(stack.getFlag().isRequired());
    assertEquals("basic stack", stack.getName());
    assertEquals(2, stack.getStack().size());
    assertTrue(stack.getStack().get(0) instanceof AuthorizationPlugin);
    assertEquals("NestedPlugin", stack.getStack().get(0).getName());
    assertTrue(stack.getStack().get(0).isRequired());
    assertTrue(stack.getStack().get(1) instanceof AuthorizationPlugin);
    assertEquals("NestedPlugin", stack.getStack().get(1).getName());
    assertTrue(stack.getStack().get(1).isRequisite());
    plugin = (AuthorizationPlugin) stack.getStack().get(1);
    assertTrue(plugin.getSetup().containsKey("key"));
    assertEquals("value", plugin.getSetup().get("key"));
    assertTrue(plugin.getSetup().containsKey("plugin"));
    assertTrue(plugin.getSetup().get("plugin") instanceof AuthorizationPlugin);
    assertEquals(pluginConfiguration.getStack().get(0), plugin.getSetup().get("plugin"));
}
Also used : AuthorizationStack(org.opengrok.indexer.authorization.AuthorizationStack) AuthorizationPlugin(org.opengrok.indexer.authorization.AuthorizationPlugin) Test(org.junit.jupiter.api.Test)

Example 3 with AuthorizationPlugin

use of org.opengrok.indexer.authorization.AuthorizationPlugin 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

AuthorizationPlugin (org.opengrok.indexer.authorization.AuthorizationPlugin)3 Test (org.junit.jupiter.api.Test)2 AuthorizationStack (org.opengrok.indexer.authorization.AuthorizationStack)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 File (java.io.File)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AuthorizationFramework (org.opengrok.indexer.authorization.AuthorizationFramework)1 TestPlugin (org.opengrok.indexer.authorization.TestPlugin)1 Project (org.opengrok.indexer.configuration.Project)1 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)1 DummyHttpServletRequest (org.opengrok.indexer.web.DummyHttpServletRequest)1