Reputation: 41
I have a function(MonitorLog) that contains an infinite loop; it's necessary because I want to monitor a file and do an action when there is a modification. However, then part in Drools doesn't want to start("WS is updated" don't appear). Some help please ?
package smartWssec ;
dialect "mvel"
rule "SmartWssec"
when
$log :PolicyUpdateWssec(flag == true ,changelog == true)
then
$log.UpdateXml("D:\\Mod\\policy.xml")// consequence
System.out.println("Ws-Policy Updated")
end
//////////////////////////////////////////////////////
package smartWssec;
import java.io.IOException;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class MainPolicyUpdateWssec {
public static void main(String[] args) throws IOException, InterruptedException {
//create a Session
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
try {
//create fact
PolicyUpdateWssec LogModSecurity = new PolicyUpdateWssec();
LogModSecurity.MonitorLog("D:\\Mod");
LogModSecurity.ReadLog("D:\\Mod\\Modsecurity.txt");
//insert fact and fire Rules
kSession.insert(LogModSecurity);
kSession.fireAllRules();
}
finally {
kSession.dispose();
}
}
/////////////////////////////////////////////////////////////////////////////
public class PolicyUpdateWssec {
private boolean flag;
private boolean changelog;
public boolean getchangelog(){
return changelog;
}
public void setChangelog(boolean changelog){
this.changelog=changelog;
}
public boolean getflag(){
return flag;
}
public void setflag(boolean flag){
this.flag=flag;
}
public void Update(String filename){
try (PrintStream ps = new PrintStream("D:\\Mod\\Wspolicyupdate.txt");
Stream<String> stream = Files.lines(Paths.get(filename))) {
stream.map(line -> line.replaceAll("</wsp:Policy>","<wssp:Integrity>\r\n\r\n<wssp:SignatureAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>\r\n\r\n<wssp:CanonicalizationAlgorithm URI=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/>\r\n\r\n<!-- Require the Timestamp header to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://www.bea.com/wls90/security/policy/wsee#part\">\r\n\r\nwls:SecurityHeader(wsu:Timestamp)\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n<!-- Require the message body to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://schemas.xmlsoap.org/2002/12/wsse#part\">\r\n\r\nwsp:Body()\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n</wssp:Integrity>\r\n\r\n</wsp:Policy>")).forEach(ps::println);
System.out.println("Ws-Policy updated");
} catch (IOException e) {
e.printStackTrace();
}}
public void ReadLog(String filename) throws IOException{
String line =null;
boolean flag=false;
// FileReader reads text files in the default encoding
FileReader fileReader = new FileReader(filename);
// Always wrap FileReader in BufferedReader
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
flag=line.matches("(.*)sql_injection_attacks(.*)");
if(flag==true) setflag(flag);
}
// Always close files
bufferedReader.close();
}
public void MonitorLog(String path) throws InterruptedException{
boolean changelog=false;
try {
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get(path);
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
System.out.println("Watch Service registered for : " + dir.getFileName());
WatchKey key = watcher.poll(10, TimeUnit.SECONDS);
try {
// wait for a key to be available
key = watcher.take();
} catch (InterruptedException ex) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
// get event type
WatchEvent.Kind<?> kind = event.kind();
// get file name
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
System.out.println(kind.name() + ": " + fileName);
if (kind == ENTRY_MODIFY ) {
changelog=true;
setChangelog(changelog);
System.out.println("WS-Policy is being Updated .......");
}
}
watcher.close();
}
catch (IOException ex) {
System.err.println(ex);
}
}
public void UpdateXml(String filename) {
ArrayList<String> lines = new ArrayList<String>();
String line = null;
File f1=null;
FileReader fr=null;
BufferedReader br=null;
FileWriter fw=null;
BufferedWriter out=null;
try {
f1 = new File(filename);
fr = new FileReader(f1);
br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
if (line.contains("</wsp:Policy>"))
line = line.replace("</wsp:Policy>", "<wssp:Integrity>\r\n\r\n<wssp:SignatureAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>\r\n\r\n<wssp:CanonicalizationAlgorithm URI=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/>\r\n\r\n<!-- Require the Timestamp header to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://www.bea.com/wls90/security/policy/wsee#part\">\r\n\r\nwls:SecurityHeader(wsu:Timestamp)\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n<!-- Require the message body to be signed -->\r\n\r\n<wssp:Target>\r\n\r\n<wssp:DigestAlgorithm URI=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\r\n\r\n<wssp:MessageParts Dialect=\"http://schemas.xmlsoap.org/2002/12/wsse#part\">\r\n\r\nwsp:Body()\r\n\r\n</wssp:MessageParts>\r\n\r\n</wssp:Target>\r\n\r\n</wssp:Integrity>\r\n\r\n</wsp:Policy>");
lines.add(line);
}
fw = new FileWriter(f1);
out = new BufferedWriter(fw);
for (String s : lines)
out.write(s);
out.flush();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try{
fr.close();
br.close();
out.close();
}catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}}
Upvotes: 3
Views: 768
Reputation: 31290
It is not enough to change a class member to trigger a rule. You must notify the Drools engine to make that happen. Modify your code so that you have the fact handle of the inserted fact:
FactHandle fh = kSession.insert(LogModSecurity);
and then call update after the modification:
setChangelog(changelog); // assuming this is code in PolicyUpdateWssec
kSession.update( fh, this );
I can't tell you how you integrate this due to absence of much of your code.
Edit The code in MonitorLog
appears to wait a little for a modification of a file in some directory and then tries to do something with the file that was modifier. (WatchService methods poll and take both remove something from the set of keys - you are losing one key there.) If nothing happens, the poll returns null, so an NPE is to be expected. Then, if it is a MODIFY, changelog is set to true. Then fireAllRules is called. But the rule will not fire, because flag
is never set to true.
There is no need for a rule, as far as I can see. You expect a change to a file: so wait for it, and then execute whatever needs to be done. The rule triggers due to that change (and maybe some file has to contain some specific text). There is nothing here that warrants the use of a production rule system.
Upvotes: 1