Search in sources :

Example 1 with AuthorizationPlugin

use of org.opensolaris.opengrok.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.opensolaris.opengrok.configuration.Configuration\">\n" + "	<void property=\"pluginStack\">\n" + "		<void method=\"add\">\n" + "			<object id=\"first_plugin\" class=\"org.opensolaris.opengrok.authorization.AuthorizationPlugin\">\n" + "				<void property=\"flag\">\n" + "					<string>sufficient</string>\n" + "				</void>\n" + "				<void property=\"name\">\n" + "					<string>Plugin</string>\n" + "				</void>\n" + "			</object>\n" + "		</void>\n" + "		<void method=\"add\">\n" + "			<object id=\"first_stack\" class=\"org.opensolaris.opengrok.authorization.AuthorizationStack\">\n" + "				<void property=\"flag\">\n" + "					<string>required</string>\n" + "				</void>\n" + "				<void property=\"name\">\n" + "					<string>basic stack</string>\n" + "				</void>\n" + "                             <void property=\"stack\">" + "                                 <void method=\"add\">" + "	                 		<object class=\"org.opensolaris.opengrok.authorization.AuthorizationPlugin\">\n" + "	                 			<void property=\"flag\">\n" + "	                 				<string>required</string>\n" + "	                 			</void>\n" + "	                 			<void property=\"name\">\n" + "	                 				<string>NestedPlugin</string>\n" + "	                 			</void>\n" + "		                 	</object>\n" + "                                 </void>" + "                                 <void method=\"add\">" + "	                 		<object class=\"org.opensolaris.opengrok.authorization.AuthorizationPlugin\">\n" + "	                 			<void property=\"flag\">\n" + "	                 				<string>requisite</string>\n" + "	                 			</void>\n" + "	                 			<void property=\"name\">\n" + "	                 				<string>NestedPlugin</string>\n" + "	                 			</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>" + "		                 	</object>\n" + "                                 </void>" + "                             </void>" + "			</object>\n" + "		</void>\n" + "		<void method=\"add\">\n" + "			<object class=\"org.opensolaris.opengrok.authorization.AuthorizationPlugin\">\n" + "				<void property=\"flag\">\n" + "					<string>requisite</string>\n" + "				</void>\n" + "				<void property=\"name\">\n" + "					<string>Requisite</string>\n" + "				</void>\n" + "			</object>\n" + "		</void>\n" + "		<void method=\"add\">\n" + "			<object class=\"org.opensolaris.opengrok.authorization.AuthorizationStack\">\n" + "				<void property=\"flag\">\n" + "					<string>required</string>\n" + "				</void>\n" + "				<void property=\"name\">\n" + "					<string>advanced stack</string>\n" + "				</void>\n" + "                             <void property=\"stack\">" + "                                 <void method=\"add\">" + "	                 		<object class=\"org.opensolaris.opengrok.authorization.AuthorizationPlugin\">\n" + "	                 			<void property=\"flag\">\n" + "	                 				<string>required</string>\n" + "	                 			</void>\n" + "	                 			<void property=\"name\">\n" + "	                 				<string>NestedPlugin</string>\n" + "	                 			</void>\n" + "		                 	</object>\n" + "                                 </void>" + "                                 <void method=\"add\">" + "	                 		<object class=\"org.opensolaris.opengrok.authorization.AuthorizationPlugin\">\n" + "	                 			<void property=\"flag\">\n" + "	                 				<string>requisite</string>\n" + "	                 			</void>\n" + "	                 			<void property=\"name\">\n" + "	                 				<string>NestedPlugin</string>\n" + "	                 			</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>" + "		                 	</object>\n" + "                                 </void>" + "                             </void>" + "			</object>\n" + "		</void>\n" + "		<void method=\"add\">\n" + "			<object idref=\"first_stack\" />" + "		</void>\n" + "	</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.opensolaris.opengrok.authorization.AuthorizationStack) AuthorizationPlugin(org.opensolaris.opengrok.authorization.AuthorizationPlugin) Test(org.junit.Test)

Example 2 with AuthorizationPlugin

use of org.opensolaris.opengrok.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 == 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 {
        throw new UnsupportedOperationException("getSampleValue() for " + paramType + ", " + genType);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Set(java.util.Set) HashSet(java.util.HashSet) Filter(org.opensolaris.opengrok.index.Filter) AuthorizationStack(org.opensolaris.opengrok.authorization.AuthorizationStack) AuthorizationPlugin(org.opensolaris.opengrok.authorization.AuthorizationPlugin) IgnoredNames(org.opensolaris.opengrok.index.IgnoredNames) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with AuthorizationPlugin

use of org.opensolaris.opengrok.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(javax.servlet.http.HttpServletRequest) Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) AuthorizationFramework(org.opensolaris.opengrok.authorization.AuthorizationFramework) ArrayList(java.util.ArrayList) AuthorizationPlugin(org.opensolaris.opengrok.authorization.AuthorizationPlugin) TestPlugin(org.opensolaris.opengrok.authorization.TestPlugin) File(java.io.File) Test(org.junit.Test)

Aggregations

AuthorizationPlugin (org.opensolaris.opengrok.authorization.AuthorizationPlugin)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 AuthorizationStack (org.opensolaris.opengrok.authorization.AuthorizationStack)2 File (java.io.File)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 AuthorizationFramework (org.opensolaris.opengrok.authorization.AuthorizationFramework)1 TestPlugin (org.opensolaris.opengrok.authorization.TestPlugin)1 Project (org.opensolaris.opengrok.configuration.Project)1 RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)1 Filter (org.opensolaris.opengrok.index.Filter)1 IgnoredNames (org.opensolaris.opengrok.index.IgnoredNames)1