use of org.osgi.framework.BundleContext in project karaf by apache.
the class GuardProxyCatalogTest method testInvocationBlocking3.
@SuppressWarnings("unchecked")
@Test
public void testInvocationBlocking3() throws Exception {
class MyService implements TestServiceAPI, TestServiceAPI2 {
public String doit(String s) {
return new StringBuilder(s).reverse().toString();
}
public String doit() {
return "Doing it";
}
}
Dictionary<String, Object> c1 = new Hashtable<>();
c1.put(Constants.SERVICE_PID, "foobar");
c1.put("service.guard", "(objectClass=" + TestServiceAPI.class.getName() + ")");
c1.put("do*", "c");
Dictionary<String, Object> c2 = new Hashtable<>();
c2.put(Constants.SERVICE_PID, "foobar2");
c2.put("service.guard", "(objectClass=" + TestServiceAPI2.class.getName() + ")");
c2.put("doit(java.lang.String)[/[tT][a]+/]", "b,d # a regex rule");
c2.put("doit(java.lang.String)", "a");
BundleContext bc = mockConfigAdminBundleContext(c1, c2);
final Object proxy = testCreateProxy(bc, new Class[] { TestServiceAPI.class, TestServiceAPI2.class }, new MyService());
// Run with the right credentials so we can test the expected roles
Subject subject = new Subject();
subject.getPrincipals().add(new RolePrincipal("c"));
Subject.doAs(subject, (PrivilegedAction<Object>) () -> {
assertEquals("Doing it", ((TestServiceAPI) proxy).doit());
return null;
});
Subject subject2 = new Subject();
subject2.getPrincipals().add(new RolePrincipal("b"));
subject2.getPrincipals().add(new RolePrincipal("f"));
Subject.doAs(subject2, (PrivilegedAction<Object>) () -> {
try {
assertEquals("Doing it", ((TestServiceAPI) proxy).doit());
fail("Should have been blocked");
} catch (SecurityException se) {
}
assertEquals("aaT", ((TestServiceAPI2) proxy).doit("Taa"));
try {
((TestServiceAPI2) proxy).doit("t");
fail("Should have been blocked");
} catch (SecurityException se) {
}
return null;
});
}
use of org.osgi.framework.BundleContext in project karaf by apache.
the class ActivatorTest method testStartActivatorNoServicesSecured.
@Test
public void testStartActivatorNoServicesSecured() throws Exception {
// keep the old properties. Note that the Properties 'copy constructor' new Properties(props)
// doesn't actually copy, hence the awkward setup here...
Properties oldProps = new Properties();
oldProps.putAll(System.getProperties());
try {
Properties newProps = removeProperties(System.getProperties(), GuardProxyCatalog.KARAF_SECURED_SERVICES_SYSPROP);
System.setProperties(newProps);
BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
EasyMock.replay(bc);
Activator a = new Activator();
a.start(bc);
assertNull(a.guardProxyCatalog);
} finally {
System.setProperties(oldProps);
}
}
use of org.osgi.framework.BundleContext in project karaf by apache.
the class GuardProxyCatalogTest method testInvocationBlocking5.
@SuppressWarnings("unchecked")
@Test
public void testInvocationBlocking5() throws Exception {
Dictionary<String, Object> c1 = new Hashtable<>();
c1.put(Constants.SERVICE_PID, "foobar");
c1.put("service.guard", "(objectClass=" + TestServiceAPI.class.getName() + ")");
c1.put("doit", "a,b");
BundleContext bc = mockConfigAdminBundleContext(c1);
final Object proxy = testCreateProxy(bc, new Class[] { TestServiceAPI2.class }, (TestServiceAPI2) String::toUpperCase);
// Invoke the service with role 'c'.
Subject subject = new Subject();
subject.getPrincipals().add(new RolePrincipal("c"));
Subject.doAs(subject, (PrivilegedAction<Object>) () -> {
assertEquals("The invocation under role 'c' should be ok, as there are no rules specified " + "for this service at all.", "HELLO", ((TestServiceAPI2) proxy).doit("hello"));
return null;
});
}
use of org.osgi.framework.BundleContext in project karaf by apache.
the class GuardProxyCatalogTest method testInvocationBlocking7.
@SuppressWarnings("unchecked")
@Test
public void testInvocationBlocking7() throws Exception {
Dictionary<String, Object> c1 = new Hashtable<>();
c1.put(Constants.SERVICE_PID, "foobar");
c1.put("service.guard", "(objectClass=" + TestServiceAPI3.class.getName() + ")");
c1.put("foo()", "a");
c1.put("bar", "b");
c1.put("*", "*");
BundleContext bc = mockConfigAdminBundleContext(c1);
final Object proxy = testCreateProxy(bc, new Class[] { TestServiceAPI3.class }, new TestService3());
Subject s1 = new Subject();
Subject.doAs(s1, (PrivilegedAction<Object>) () -> {
TestServiceAPI3 obj = (TestServiceAPI3) proxy;
assertEquals("Should have allowed this invocation for any (or no) role", -7, obj.foo(7));
try {
obj.foo();
fail("Should have been blocked");
} catch (SecurityException se) {
}
try {
obj.bar();
fail("Should have been blocked");
} catch (SecurityException se) {
}
return null;
});
Subject s2 = new Subject();
s2.getPrincipals().add(new RolePrincipal("a"));
s2.getPrincipals().add(new RolePrincipal("b"));
s2.getPrincipals().add(new RolePrincipal("d"));
Subject.doAs(s2, (PrivilegedAction<Object>) () -> {
TestServiceAPI3 obj = (TestServiceAPI3) proxy;
assertEquals(42, obj.foo());
assertEquals(99, obj.bar());
assertEquals(-32767, obj.foo(32767));
return null;
});
}
use of org.osgi.framework.BundleContext in project karaf by apache.
the class GuardProxyCatalogTest method testHandleProxificationForHook.
@SuppressWarnings("unchecked")
@Test
public void testHandleProxificationForHook() throws Exception {
Dictionary<String, Object> config = new Hashtable<>();
config.put(Constants.SERVICE_PID, GuardProxyCatalog.SERVICE_ACL_PREFIX + "foo");
config.put(GuardProxyCatalog.SERVICE_GUARD_KEY, "(a>=5)");
BundleContext bc = mockConfigAdminBundleContext(config);
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
Dictionary<String, Object> props = new Hashtable<>();
props.put(Constants.SERVICE_ID, 13L);
props.put("a", "6");
props.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
ServiceReference<?> sref2 = mockServiceReference(props);
assertFalse("Should not hide an existing proxy for this client", gpc.handleProxificationForHook(sref2));
assertEquals("No proxy should have been created", 0, gpc.proxyMap.size());
Dictionary<String, Object> props4 = new Hashtable<>();
props4.put(Constants.SERVICE_ID, 15L);
props4.put("a", "7");
ServiceReference<?> sref4 = mockServiceReference(props4);
assertTrue("Should hide a service that needs to be proxied", gpc.handleProxificationForHook(sref4));
assertEquals("Should trigger proxy creation", 1, gpc.proxyMap.size());
}
Aggregations