Search in sources :

Example 86 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testSetAttribute.

public void testSetAttribute() throws Throwable {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo a1 = new MBeanAttributeInfo("Something", "java.lang.String", "Something Attribute", true, true, false);
    MBeanAttributeInfo a2 = new MBeanAttributeInfo("Value", "long", "Value Attribute", true, true, false);
    MBeanAttributeInfo a3 = new MBeanAttributeInfo("Other", "boolean", "Other Attribute", true, true, false);
    MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[] { a1, a2, a3 };
    MBeanInfo mbeanInfo = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(mbeanInfo.getAttributes()).andReturn(attrs).anyTimes();
    EasyMock.replay(mbeanInfo);
    final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(mbeanInfo).anyTimes();
    EasyMock.replay(mbs);
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("setSomething", "editor");
    configuration.put("setValue*", "admin");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("editor", "admin");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            Method im = MBeanServer.class.getMethod("setAttribute", ObjectName.class, Attribute.class);
            guard.invoke(mbs, im, new Object[] { on, new Attribute("Something", "v1") });
            guard.invoke(mbs, im, new Object[] { on, new Attribute("Value", 42L) });
            try {
                guard.invoke(mbs, im, new Object[] { on, new Attribute("Other", Boolean.TRUE) });
                fail("Should not have allowed the invocation");
            } catch (SecurityException se) {
            }
            try {
                guard.invoke(mbs, im, new Object[] { on, new Attribute("NonExistent", "v4") });
                fail("Should not have found the MBean Declaration");
            } catch (IllegalStateException ise) {
            }
            return null;
        } catch (Throwable ex) {
            throw new RuntimeException(ex);
        }
    });
}
Also used : Method(java.lang.reflect.Method) Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 87 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testInvoke.

public void testInvoke() throws Throwable {
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("someMethod", "editor");
    configuration.put("someOtherMethod", "viewer");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("editor", "admin");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            Method im = MBeanServer.class.getMethod("invoke", ObjectName.class, String.class, Object[].class, String[].class);
            ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
            guard.invoke(null, im, new Object[] { on, "someMethod", new Object[] { "test" }, new String[] { "java.lang.String" } });
            try {
                guard.invoke(null, im, new Object[] { on, "someOtherMethod", new Object[] {}, new String[] {} });
                fail("Should not have allowed the invocation");
            } catch (SecurityException se) {
            }
            try {
                guard.invoke(null, im, new Object[] { on, "somemethingElse", new Object[] {}, new String[] {} });
                fail("Should not have allowed the invocation");
            } catch (SecurityException se) {
            }
            return null;
        } catch (Throwable ex) {
            throw new RuntimeException(ex);
        }
    });
}
Also used : Method(java.lang.reflect.Method) Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 88 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testRequiredRolesRegExp2.

public void testRequiredRolesRegExp2() throws Exception {
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("foo(java.lang.String,java.lang.String)[/a/,/b/]", "editor");
    configuration.put("foo(java.lang.String,java.lang.String)[/[bc]/ , /[^b]/]", "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, "foo", new Object[] { "a", "b" }, new String[] { "java.lang.String", "java.lang.String" }));
    assertEquals(Collections.singletonList("viewer"), guard.getRequiredRoles(on, "foo", new Object[] { "b", "a" }, new String[] { "java.lang.String", "java.lang.String" }));
    assertEquals(Collections.singletonList("viewer"), guard.getRequiredRoles(on, "foo", new Object[] { "c", "c" }, new String[] { "java.lang.String", "java.lang.String" }));
    assertEquals(Collections.emptyList(), guard.getRequiredRoles(on, "foo", new Object[] { "b", "b" }, new String[] { "java.lang.String", "java.lang.String" }));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 89 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanInvokeMBeanGetter3.

public void testCanInvokeMBeanGetter3() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo attr = new MBeanAttributeInfo("A1", "boolean", "", true, false, false);
    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("getA1", "viewer");
    configuration.put("is*", "admin");
    configuration.put("*", "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(mbs, on));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 90 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testRequiredRolesExactNobody.

public void testRequiredRolesExactNobody() throws Exception {
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("foo(java.lang.String)[\"a\"]", "");
    configuration.put("foo(java.lang.String)[\"aa\"]", "#hello");
    configuration.put("foo", "test");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    assertEquals(Collections.emptyList(), guard.getRequiredRoles(on, "foo", new Object[] { "a" }, new String[] { "java.lang.String" }));
    assertEquals(Collections.emptyList(), guard.getRequiredRoles(on, "foo", new Object[] { "aa" }, new 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