Search in sources :

Example 16 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanSetAttributeAnyOverload.

public void testCanSetAttributeAnyOverload() 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)", "admin");
    configuration.put("setFoo(boolean)", "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, "setFoo"));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 17 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanInvokeAnyOverload.

public void testCanInvokeAnyOverload() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanParameterInfo[] sig = new MBeanParameterInfo[] { new MBeanParameterInfo("arg1", "java.lang.String", "") };
    MBeanOperationInfo op = new MBeanOperationInfo("doit", "", sig, "int", MBeanOperationInfo.INFO);
    MBeanParameterInfo[] sig2 = new MBeanParameterInfo[] { new MBeanParameterInfo("arg1", "java.lang.String", ""), new MBeanParameterInfo("arg2", "java.lang.String", "") };
    MBeanOperationInfo op2 = new MBeanOperationInfo("doit", "", sig2, "int", MBeanOperationInfo.INFO);
    MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] { op, op2 }).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("doit(java.lang.String)", "admin");
    configuration.put("doit(java.lang.String,java.lang.String)", "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, "doit"));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 18 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanInvokeAnyOverload2.

public void testCanInvokeAnyOverload2() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanParameterInfo[] sig = new MBeanParameterInfo[] { new MBeanParameterInfo("arg1", "java.lang.String", "") };
    MBeanOperationInfo op = new MBeanOperationInfo("foit", "", sig, "int", MBeanOperationInfo.INFO);
    MBeanParameterInfo[] sig2 = new MBeanParameterInfo[] { new MBeanParameterInfo("arg1", "java.lang.String", ""), new MBeanParameterInfo("arg2", "java.lang.String", "") };
    MBeanOperationInfo op2 = new MBeanOperationInfo("doit", "", sig2, "int", MBeanOperationInfo.INFO);
    MBeanInfo info = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(info.getOperations()).andReturn(new MBeanOperationInfo[] { op, op2 }).anyTimes();
    EasyMock.expect(info.getAttributes()).andReturn(new MBeanAttributeInfo[] {}).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("foit(java.lang.String)", "viewer");
    configuration.put("doit(java.lang.String,java.lang.String)", "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, "doit"));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 19 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testRequiredRolesHierarchyWildcard3.

@SuppressWarnings("unchecked")
public void testRequiredRolesHierarchyWildcard3() throws Exception {
    Dictionary<String, Object> conf1 = new Hashtable<>();
    conf1.put("foo", "viewer");
    conf1.put(Constants.SERVICE_PID, "jmx.acl._.bar.Test");
    Dictionary<String, Object> conf2 = new Hashtable<>();
    conf2.put("foo", "editor");
    conf2.put(Constants.SERVICE_PID, "jmx.acl.foo._.Test");
    ConfigurationAdmin ca = getMockConfigAdmin2(conf1, conf2);
    assertEquals("Precondition", 2, ca.listConfigurations("(service.pid=jmx.acl*)").length);
    KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    ObjectName on1 = ObjectName.getInstance("foo.bar:type=Test");
    assertEquals("Should only return the most specific definition", Collections.singletonList("editor"), guard.getRequiredRoles(on1, "foo", new Object[] {}, new String[] {}));
    ObjectName on2 = ObjectName.getInstance("foo.tar:type=Test");
    assertEquals(Collections.singletonList("editor"), guard.getRequiredRoles(on2, "foo", new Object[] {}, new String[] {}));
    ObjectName on3 = ObjectName.getInstance("boo.bar:type=Test");
    assertEquals(Collections.singletonList("viewer"), guard.getRequiredRoles(on3, "foo", new Object[] {}, new String[] {}));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 20 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanInvokeMBeanSetter.

public void testCanInvokeMBeanSetter() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo attr = new MBeanAttributeInfo("A2", "java.lang.String", "", true, true, 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("get*", "admin");
    configuration.put("setA2", "viewer");
    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)

Aggregations

ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)211 Configuration (org.osgi.service.cm.Configuration)114 Test (org.junit.Test)65 Hashtable (java.util.Hashtable)57 ServiceReference (org.osgi.framework.ServiceReference)52 IOException (java.io.IOException)44 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 BundleContext (org.osgi.framework.BundleContext)25 Subject (javax.security.auth.Subject)24 Dictionary (java.util.Dictionary)23 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)21 Bundle (org.osgi.framework.Bundle)16 Properties (java.util.Properties)15 File (java.io.File)8 HashSet (java.util.HashSet)7 BundleException (org.osgi.framework.BundleException)7 Method (java.lang.reflect.Method)6 ObjectName (javax.management.ObjectName)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4