Search in sources :

Example 11 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanGetAttributeAnyOverload2.

public void testCanGetAttributeAnyOverload2() throws Exception {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo attr = new MBeanAttributeInfo("Foo", "int", "", 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("getFoo(java.lang.String)", "viewer");
    configuration.put("getFoo()", "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, "getFoo"));
            return null;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    });
}
Also used : Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 12 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testRequiredRolesRegExpNobody.

public void testRequiredRolesRegExpNobody() throws Exception {
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("testit(java.lang.String)[/ab/]", "");
    configuration.put("test*", "tester");
    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, "testit", new Object[] { "ab" }, new String[] { "java.lang.String" }));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 13 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testRequiredRolesHierarchyWildcard2.

@SuppressWarnings("unchecked")
public void testRequiredRolesHierarchyWildcard2() throws Exception {
    Dictionary<String, Object> conf1 = new Hashtable<>();
    conf1.put("foo", "viewer");
    conf1.put(Constants.SERVICE_PID, "jmx.acl.foo.bar.Test");
    Dictionary<String, Object> conf2 = new Hashtable<>();
    conf2.put("foo", "editor");
    conf2.put(Constants.SERVICE_PID, "jmx.acl._.bar.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("viewer"), guard.getRequiredRoles(on1, "foo", new Object[] {}, new String[] {}));
    ObjectName on2 = ObjectName.getInstance("tar.bar:type=Test");
    assertEquals("Should return definition from wildcard PID", Collections.singletonList("editor"), guard.getRequiredRoles(on2, "foo", new Object[] {}, new String[] {}));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 14 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testRequiredRolesHierarchyWildcard1.

@SuppressWarnings("unchecked")
public void testRequiredRolesHierarchyWildcard1() 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.bar.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("tar.bar:type=Test");
    assertEquals("Should return definition from wildcard PID", Collections.singletonList("viewer"), guard.getRequiredRoles(on2, "foo", new Object[] {}, new String[] {}));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 15 with ConfigurationAdmin

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

the class KarafMBeanServerGuardTest method testCanGetAttributeAnyOverload3.

public void testCanGetAttributeAnyOverload3() 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)", "admin");
    configuration.put("getFoo()", "admin");
    configuration.put("isFoo()", "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, "isFoo"));
            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