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