Search in sources :

Example 91 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testRequiredRolesRegExp.

public void testRequiredRolesRegExp() throws Exception {
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("  testit   (java.lang.String)  [  /ab/]", "manager");
    configuration.put("testit(java.lang.String)[/c\"d/]", "tester");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    assertEquals(Collections.singletonList("manager"), guard.getRequiredRoles(on, "testit", new Object[] { "ab" }, new String[] { "java.lang.String" }));
    assertEquals(Collections.singletonList("manager"), guard.getRequiredRoles(on, "testit", new Object[] { "    ab  " }, new String[] { "java.lang.String" }));
    assertEquals(Collections.emptyList(), guard.getRequiredRoles(on, "testit", new Object[] { " a b " }, new String[] { "java.lang.String" }));
    assertEquals(Collections.singletonList("tester"), guard.getRequiredRoles(on, "testit", new Object[] { " c\"d " }, new String[] { "java.lang.String" }));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 92 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testRequiredRolesNumeric.

public void testRequiredRolesNumeric() throws Exception {
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("bar(int)[\"17\"]", "editor #this is the editor rule");
    configuration.put("bar", "viewer");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    assertEquals(Collections.singletonList("editor"), guard.getRequiredRoles(on, "bar", new Object[] { new Integer(17) }, new String[] { "int" }));
    assertEquals(Collections.singletonList("viewer"), guard.getRequiredRoles(on, "bar", new Object[] { new Integer(18) }, new String[] { "int" }));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 93 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testCanInvokeMethod2.

public void testCanInvokeMethod2() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("doit(java.lang.String)[/11/]", "viewer");
    configuration.put("doit(java.lang.String)", "admin");
    configuration.put("doit(java.lang.String,java.lang.String)", "admin");
    configuration.put("doit(int)[\"12\"]", "viewer");
    configuration.put("doit", "viewer");
    configuration.put("do*", "admin");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("viewer");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            assertTrue(guard.canInvoke(null, on, "doit", new String[] { "java.lang.String" }));
            assertTrue(guard.canInvoke(null, on, "doit", new String[] {}));
            assertTrue(guard.canInvoke(null, on, "doit", new String[] { "int" }));
            assertFalse(guard.canInvoke(null, on, "doit", new String[] { "java.lang.String", "java.lang.String" }));
            assertFalse(guard.canInvoke(null, on, "dodo", new String[] { "java.lang.String" }));
            assertFalse(guard.canInvoke(null, on, "uuuh", new String[] { "java.lang.String" }));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Subject(javax.security.auth.Subject)

Example 94 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testCanInvokeMBean.

public void testCanInvokeMBean() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    final ObjectName on2 = ObjectName.getInstance("foo.bar:type=Toast");
    MBeanParameterInfo[] sig = new MBeanParameterInfo[] { new MBeanParameterInfo("arg1", "java.lang.String", "") };
    MBeanOperationInfo op = new MBeanOperationInfo("doit", "", sig, "int", MBeanOperationInfo.INFO);
    MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] { op }).anyTimes();
    EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] {}).anyTimes();
    EasyMock.replay(info);
    MBeanInfo info2 = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(info2.getOperations()).andReturn(new MBeanOperationInfo[] {}).anyTimes();
    EasyMock.expect(info2.getAttributes()).andReturn(new MBeanAttributeInfo[] {}).anyTimes();
    EasyMock.replay(info2);
    final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(info).anyTimes();
    EasyMock.expect(mbs.getMBeanInfo(on2)).andReturn(info2).anyTimes();
    EasyMock.replay(mbs);
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("doit(java.lang.String)[/11/]", "admin");
    configuration.put("doit(java.lang.String)", "viewer");
    configuration.put("doit(java.lang.String,java.lang.String)", "viewer");
    configuration.put("doit(int)[\"12\"]", "admin");
    configuration.put("doit", "admin");
    configuration.put("do*", "viewer");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("viewer");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            assertTrue(guard.canInvoke(mbs, on));
            assertFalse(guard.canInvoke(mbs, on2));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 95 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testRequiredRolesSignature.

public void testRequiredRolesSignature() throws Exception {
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("testit", "master");
    configuration.put("testit(java.lang.String)", "viewer");
    configuration.put("testit(java.lang.String, java.lang.String)", "editor");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    assertEquals(Collections.singletonList("editor"), guard.getRequiredRoles(on, "testit", new Object[] { "test", "toast" }, new String[] { "java.lang.String", "java.lang.String" }));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Aggregations

ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)108 Configuration (org.osgi.service.cm.Configuration)42 Subject (javax.security.auth.Subject)24 Test (org.junit.Test)19 Hashtable (java.util.Hashtable)14 BundleContext (org.osgi.framework.BundleContext)14 ServiceReference (org.osgi.framework.ServiceReference)13 IOException (java.io.IOException)9 Dictionary (java.util.Dictionary)8 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 Method (java.lang.reflect.Method)6 Bundle (org.osgi.framework.Bundle)6 File (java.io.File)4 Properties (java.util.Properties)4 MBeanServer (javax.management.MBeanServer)4 ObjectName (javax.management.ObjectName)4 KarafMBeanServerGuard (org.apache.karaf.management.KarafMBeanServerGuard)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 CompositeData (javax.management.openmbean.CompositeData)3