Search in sources :

Example 6 with KarafMBeanServerGuard

use of org.apache.karaf.management.KarafMBeanServerGuard in project karaf by apache.

the class JMXSecurityMBeanImplTestCase method testCanInvokeMBean.

public void testCanInvokeMBean() throws Exception {
    MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.replay(mbs);
    String objectName = "foo.bar.testing:type=SomeMBean";
    KarafMBeanServerGuard testGuard = EasyMock.createMock(KarafMBeanServerGuard.class);
    EasyMock.expect(testGuard.canInvoke(null, mbs, new ObjectName(objectName))).andReturn(true);
    EasyMock.replay(testGuard);
    JMXSecurityMBeanImpl mb = new JMXSecurityMBeanImpl();
    mb.setMBeanServer(mbs);
    mb.setGuard(testGuard);
    assertTrue(mb.canInvoke(objectName));
}
Also used : KarafMBeanServerGuard(org.apache.karaf.management.KarafMBeanServerGuard) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 7 with KarafMBeanServerGuard

use of org.apache.karaf.management.KarafMBeanServerGuard in project karaf by apache.

the class JMXSecurityMBeanImplTestCase method testCanInvokeMBeanThrowsException.

public void testCanInvokeMBeanThrowsException() throws Exception {
    try {
        MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
        EasyMock.replay(mbs);
        String objectName = "foo.bar.testing:type=SomeMBean";
        KarafMBeanServerGuard testGuard = EasyMock.createMock(KarafMBeanServerGuard.class);
        EasyMock.expect(testGuard.canInvoke(null, mbs, new ObjectName(objectName))).andThrow(new IOException());
        EasyMock.replay(testGuard);
        JMXSecurityMBeanImpl mb = new JMXSecurityMBeanImpl();
        mb.setMBeanServer(mbs);
        mb.setGuard(testGuard);
        mb.canInvoke(objectName);
        fail("Should have thrown an exception");
    } catch (IOException ioe) {
    // good!
    }
}
Also used : KarafMBeanServerGuard(org.apache.karaf.management.KarafMBeanServerGuard) IOException(java.io.IOException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 8 with KarafMBeanServerGuard

use of org.apache.karaf.management.KarafMBeanServerGuard in project karaf by apache.

the class JMXSecurityMBeanImplTestCase method testCanInvokeMethod.

public void testCanInvokeMethod() throws Exception {
    MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.replay(mbs);
    String objectName = "foo.bar.testing:type=SomeMBean";
    KarafMBeanServerGuard testGuard = EasyMock.createMock(KarafMBeanServerGuard.class);
    String[] la = new String[] { "long" };
    String[] sa = new String[] { "java.lang.String" };
    String[] sa2 = new String[] { "java.lang.String", "java.lang.String" };
    EasyMock.expect(testGuard.canInvoke(null, mbs, new ObjectName(objectName), "testMethod", la)).andReturn(true);
    EasyMock.expect(testGuard.canInvoke(null, mbs, new ObjectName(objectName), "testMethod", sa)).andReturn(true);
    EasyMock.expect(testGuard.canInvoke(null, mbs, new ObjectName(objectName), "otherMethod", sa2)).andReturn(false);
    EasyMock.replay(testGuard);
    JMXSecurityMBeanImpl mb = new JMXSecurityMBeanImpl();
    mb.setMBeanServer(mbs);
    mb.setGuard(testGuard);
    assertTrue(mb.canInvoke(objectName, "testMethod", la));
    assertTrue(mb.canInvoke(objectName, "testMethod", sa));
    assertFalse(mb.canInvoke(objectName, "otherMethod", sa2));
}
Also used : KarafMBeanServerGuard(org.apache.karaf.management.KarafMBeanServerGuard) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 9 with KarafMBeanServerGuard

use of org.apache.karaf.management.KarafMBeanServerGuard in project karaf by apache.

the class JMXSecurityMBeanImplTestCase method testCanInvokeBulkWithDuplicateMethods.

public void testCanInvokeBulkWithDuplicateMethods() throws Exception {
    MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.replay(mbs);
    ConfigurationAdmin testConfigAdmin = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(testConfigAdmin.listConfigurations(EasyMock.eq("(service.pid=jmx.acl*)"))).andReturn(new Configuration[0]).anyTimes();
    EasyMock.expect(testConfigAdmin.listConfigurations(EasyMock.eq("(service.pid=jmx.acl.whitelist)"))).andReturn(new Configuration[0]).once();
    EasyMock.replay(testConfigAdmin);
    KarafMBeanServerGuard testGuard = EasyMock.createMock(KarafMBeanServerGuard.class);
    String objectName = "foo.bar.testing:type=SomeMBean";
    final String[] la = new String[] { "long" };
    final String[] sa = new String[] { "java.lang.String" };
    EasyMock.expect(testGuard.getConfigAdmin()).andReturn(testConfigAdmin).anyTimes();
    EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName)), EasyMock.eq("duplicateMethod1"), EasyMock.aryEq(la))).andReturn(true).anyTimes();
    EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName)), EasyMock.eq("duplicateMethod1"), EasyMock.aryEq(sa))).andReturn(false).anyTimes();
    EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName)), EasyMock.eq("duplicateMethod2"))).andReturn(true).anyTimes();
    EasyMock.replay(testGuard);
    JMXSecurityMBeanImpl mb = new JMXSecurityMBeanImpl();
    mb.setMBeanServer(mbs);
    mb.setGuard(testGuard);
    Map<String, List<String>> query = new HashMap<>();
    query.put(objectName, Arrays.asList("duplicateMethod1(long)", "duplicateMethod1(java.lang.String)", "duplicateMethod1(long)", "duplicateMethod2", "duplicateMethod2"));
    TabularData result = mb.canInvoke(query);
    assertEquals(3, result.size());
    CompositeData cd = result.get(new Object[] { objectName, "duplicateMethod1(long)" });
    assertEquals(objectName, cd.get("ObjectName"));
    assertEquals("duplicateMethod1(long)", cd.get("Method"));
    assertEquals(true, cd.get("CanInvoke"));
    CompositeData cd2 = result.get(new Object[] { objectName, "duplicateMethod1(java.lang.String)" });
    assertEquals(objectName, cd2.get("ObjectName"));
    assertEquals("duplicateMethod1(java.lang.String)", cd2.get("Method"));
    assertEquals(false, cd2.get("CanInvoke"));
    CompositeData cd3 = result.get(new Object[] { objectName, "duplicateMethod2" });
    assertEquals(objectName, cd3.get("ObjectName"));
    assertEquals("duplicateMethod2", cd3.get("Method"));
    assertEquals(true, cd3.get("CanInvoke"));
}
Also used : Configuration(org.osgi.service.cm.Configuration) KarafMBeanServerGuard(org.apache.karaf.management.KarafMBeanServerGuard) CompositeData(javax.management.openmbean.CompositeData) ObjectName(javax.management.ObjectName) TabularData(javax.management.openmbean.TabularData) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) MBeanServer(javax.management.MBeanServer)

Aggregations

MBeanServer (javax.management.MBeanServer)9 KarafMBeanServerGuard (org.apache.karaf.management.KarafMBeanServerGuard)9 ObjectName (javax.management.ObjectName)8 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 CompositeData (javax.management.openmbean.CompositeData)3 TabularData (javax.management.openmbean.TabularData)3 Configuration (org.osgi.service.cm.Configuration)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)1 KeystoreInstance (org.apache.karaf.jaas.config.KeystoreInstance)1 KeystoreManager (org.apache.karaf.jaas.config.KeystoreManager)1 ConnectorServerFactory (org.apache.karaf.management.ConnectorServerFactory)1 JaasAuthenticator (org.apache.karaf.management.JaasAuthenticator)1 MBeanServerFactory (org.apache.karaf.management.MBeanServerFactory)1 RmiRegistryFactory (org.apache.karaf.management.RmiRegistryFactory)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1