Search in sources :

Example 1 with AuthorizationStack

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

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

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

the class RuntimeEnvironmentTest method testAuthorizationFlagDecode.

@Test
public void testAuthorizationFlagDecode() 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 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 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>OtherPlugin</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.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>AnotherPlugin</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.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>DifferentPlugin</string>\n" + "\t\t\t\t</void>\n" + "\t\t\t</object>\n" + "\t\t</void>\n" + "\t</void>\n" + " </object>\n" + "</java>";
    Configuration conf = Configuration.makeXMLStringAsConfiguration(confString);
    assertNotNull(conf.getPluginStack());
    AuthorizationStack pluginConfiguration = conf.getPluginStack();
    assertEquals(4, pluginConfiguration.getStack().size());
    assertTrue(pluginConfiguration.getStack().get(0).getFlag().isSufficient());
    assertEquals("Plugin", pluginConfiguration.getStack().get(0).getName());
    assertTrue(pluginConfiguration.getStack().get(1).getFlag().isRequired());
    assertEquals("OtherPlugin", pluginConfiguration.getStack().get(1).getName());
    assertTrue(pluginConfiguration.getStack().get(2).getFlag().isRequisite());
    assertEquals("AnotherPlugin", pluginConfiguration.getStack().get(2).getName());
    assertTrue(pluginConfiguration.getStack().get(3).getFlag().isRequired());
    assertEquals("DifferentPlugin", pluginConfiguration.getStack().get(3).getName());
}
Also used : AuthorizationStack(org.opengrok.indexer.authorization.AuthorizationStack) Test(org.junit.jupiter.api.Test)

Aggregations

AuthorizationStack (org.opengrok.indexer.authorization.AuthorizationStack)3 Test (org.junit.jupiter.api.Test)2 AuthorizationPlugin (org.opengrok.indexer.authorization.AuthorizationPlugin)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1