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;
}
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" }));
}
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);
}
});
}
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);
}
});
}
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);
}
});
}
Aggregations