use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class SecurityPolicyTest method testAccumulateFunctionJava.
@Test
public void testAccumulateFunctionJava() throws Exception {
String drl = "package org.foo.bar\n" + "import " + MaliciousExitHelper.class.getName().replace('$', '.') + " \n" + "rule testRule dialect \"java\" \n" + " when\n" + " Number() from accumulate(Object(), " + " sum(MaliciousExitHelper.exit()))\n" + " then\n" + "end";
try {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
ks.newKieBuilder(kfs).buildAll();
ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
KieContainer kc = ks.newKieContainer(releaseId);
KieSession ksession = kc.newKieSession();
ksession.insert("foo");
ksession.fireAllRules();
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (PropertyAccessException e) {
// weak way of testing but couldn't find a better way
if (e.toString().contains("The security policy should have prevented")) {
Assert.fail("The security policy for the rule should have prevented this from executing...");
} else {
// test succeeded
}
} catch (Exception e) {
if (e.toString().contains("access denied (\"java.lang.RuntimePermission\" \"exitVM.0\")")) {
// test succeeded
} else {
throw e;
}
}
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class SecurityPolicyTest method testUntrustedMvelConsequence.
@Test
public void testUntrustedMvelConsequence() throws Exception {
String drl = "package org.foo.bar\n" + "rule R1 dialect \"mvel\" when\n" + "then\n" + " System.exit(0);" + "end\n";
try {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
ks.newKieBuilder(kfs).buildAll();
ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
KieContainer kc = ks.newKieContainer(releaseId);
KieSession ksession = kc.newKieSession();
ksession.fireAllRules();
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ShouldHavePrevented e) {
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ConsequenceException e) {
// test succeeded. the policy in place prevented the rule from executing the System.exit().
}
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class SecurityPolicyTest method testUntrustedJavaConsequence.
@Test
public void testUntrustedJavaConsequence() throws Exception {
String drl = "package org.foo.bar\n" + "rule R1 when\n" + "then\n" + " System.exit(0);" + "end\n";
try {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
ks.newKieBuilder(kfs).buildAll();
ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
KieContainer kc = ks.newKieContainer(releaseId);
KieSession ksession = kc.newKieSession();
ksession.fireAllRules();
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ShouldHavePrevented e) {
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ConsequenceException e) {
// test succeeded. the policy in place prevented the rule from executing the System.exit().
}
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class SecurityPolicyTest method testCustomAccumulate.
@Test
public void testCustomAccumulate() throws Exception {
String drl = "package org.foo.bar\n" + "rule testRule\n" + " when\n" + " Number() from accumulate(Object(), " + " init(System.exit(-1);), " + " action(System.exit(-1);), " + " reverse(System.exit(-1);), " + " result(0))\n" + " then\n" + "end";
try {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
ks.newKieBuilder(kfs).buildAll();
ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
KieContainer kc = ks.newKieContainer(releaseId);
KieSession ksession = kc.newKieSession();
ksession.fireAllRules();
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ShouldHavePrevented e) {
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (Exception e) {
// test succeeded. the policy in place prevented the rule from executing the System.exit().
}
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class SequentialTest method testSequentialWithNoLoop.
@Test(timeout = 10000L)
public void testSequentialWithNoLoop() throws Exception {
// BZ-1228098
String str = "package org.drools.compiler.test\n" + "import \n" + Message.class.getCanonicalName() + ";" + "rule R1 no-loop when\n" + " $s : String( )" + " $m : Message( )\n" + "then\n" + " modify($m) { setMessage($s) };\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write("src/main/resources/r0.drl", str);
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
if (results.hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
throw new RuntimeException(results.getMessages().toString());
}
KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
KieBaseConfiguration kieBaseConf = ks.newKieBaseConfiguration();
kieBaseConf.setOption(SequentialOption.YES);
StatelessKieSession sequentialKsession = kieContainer.newKieBase(kieBaseConf).newStatelessKieSession();
List result = (List) sequentialKsession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
assertEquals(2, result.size());
StatelessKieSession ksession = kieContainer.getKieBase().newStatelessKieSession();
result = (List) ksession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
assertEquals(2, result.size());
}
Aggregations