Search in sources :

Example 81 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method getMockConfigAdmin2.

private ConfigurationAdmin getMockConfigAdmin2(Dictionary<String, Object>... configurations) throws IOException, InvalidSyntaxException {
    List<Configuration> allConfigs = new ArrayList<>();
    for (Dictionary<String, Object> configuration : configurations) {
        Configuration conf = EasyMock.createMock(Configuration.class);
        EasyMock.expect(conf.getPid()).andReturn((String) configuration.get(Constants.SERVICE_PID)).anyTimes();
        EasyMock.expect(conf.getProperties()).andReturn(configuration).anyTimes();
        EasyMock.replay(conf);
        allConfigs.add(conf);
    }
    ConfigurationAdmin ca = EasyMock.createMock(ConfigurationAdmin.class);
    for (Configuration c : allConfigs) {
        EasyMock.expect(ca.getConfiguration(c.getPid(), null)).andReturn(c).anyTimes();
    }
    EasyMock.expect(ca.listConfigurations(EasyMock.eq("(service.pid=jmx.acl*)"))).andReturn(allConfigs.toArray(new Configuration[] {})).anyTimes();
    EasyMock.expect(ca.listConfigurations(EasyMock.eq("(service.pid=jmx.acl.whitelist)"))).andReturn(allConfigs.toArray(new Configuration[] {})).anyTimes();
    EasyMock.replay(ca);
    return ca;
}
Also used : Configuration(org.osgi.service.cm.Configuration) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 82 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testRequiredRolesExact.

public void testRequiredRolesExact() 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");
    configuration.put("testit(java.lang.String) [\"ab\"]", "manager");
    configuration.put("testit(java.lang.String)[\"a b\" ]", "admin");
    configuration.put("testit(java.lang.String)[ \"cd\"]  ", "tester");
    configuration.put("testit(java.lang.String)[\"cd/\"]", "monkey");
    configuration.put("testit(java.lang.String)[\"cd\"\"]", "donkey");
    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("admin"), guard.getRequiredRoles(on, "testit", new Object[] { " a b " }, new String[] { "java.lang.String" }));
    assertEquals("The arguments are trimmed before checking", Collections.singletonList("admin"), guard.getRequiredRoles(on, "testit", new Object[] { "a b" }, new String[] { "java.lang.String" }));
    assertEquals(Collections.singletonList("tester"), guard.getRequiredRoles(on, "testit", new Object[] { "cd" }, new String[] { "java.lang.String" }));
    assertEquals(Collections.singletonList("monkey"), guard.getRequiredRoles(on, "testit", new Object[] { "cd/" }, new String[] { "java.lang.String" }));
    assertEquals(Collections.singletonList("donkey"), guard.getRequiredRoles(on, "testit", new Object[] { "cd\"" }, new String[] { "java.lang.String" }));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 83 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanSetAttributeAnyOverload2.

public void testCanSetAttributeAnyOverload2() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo attr = new MBeanAttributeInfo("Foo", "boolean", "", true, true, true);
    MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] {}).anyTimes();
    EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] { attr }).anyTimes();
    EasyMock.replay(info);
    final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(info).anyTimes();
    EasyMock.replay(mbs);
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("setFoo(java.lang.String)", "viewer");
    configuration.put("setFoo(boolean)", "admin");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("viewer");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            assertFalse(guard.canInvoke(mbs, on, "setFoo"));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 84 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanGetAttributeAnyOverload4.

public void testCanGetAttributeAnyOverload4() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo attr = new MBeanAttributeInfo("Foo", "boolean", "", true, true, true);
    MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] {}).anyTimes();
    EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] { attr }).anyTimes();
    EasyMock.replay(info);
    final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(info).anyTimes();
    EasyMock.replay(mbs);
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("getFoo(java.lang.String)", "viewer");
    configuration.put("getFoo()", "viewer");
    configuration.put("isFoo()", "admin");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("viewer");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            assertFalse(guard.canInvoke(mbs, on, "isFoo"));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 85 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanInvokeMethod.

public void testCanInvokeMethod() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    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(null, on, "dodo", new String[] { "java.lang.String" }));
            assertTrue(guard.canInvoke(null, on, "doit", new String[] { "java.lang.String", "java.lang.String" }));
            assertTrue(guard.canInvoke(null, on, "doit", new String[] { "java.lang.String" }));
            assertFalse(guard.canInvoke(null, on, "doit", new String[] { "int" }));
            assertFalse(guard.canInvoke(null, on, "doit", new 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)

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