Skip to content

Commit

Permalink
Config and file filter mgt.
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe Demanget committed Jun 21, 2014
1 parent a368886 commit 266250e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,22 @@ this war file has been tested under java7 environnement with 4 Application serve
Glassfish: add war file to glassfishv3/glassfish/domains/domain1/autodeploy
Jetty: launch "java -jar jetty-runner warlog.war"

Installation
------------
configure the parameter "whitelist" in the file /etc/warlog.properties or the included resource src/main/resources/warlog.properties
mvn package
copy or install the war : target/warlog.war

OR
download the binary war file from http://sourceforge.net/projects/warlog


Change log
==========
V0.3.1 (2014-06-21)
-------------------
Configuration in warlog.properties and file filter

V0.3.0
------
managing file as text
Expand Down
Empty file modified jetty.bat
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>fr.warlog</groupId>
<artifactId>warlog</artifactId>
<packaging>war</packaging>
<version>0.3-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<name>warlog Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/fr/warlog/bus/FileMgt.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

import org.apache.log4j.Logger;

import fr.warlog.util.Configs;
import fr.warlog.util.Data;
import fr.warlog.util.MainUtils;
import fr.warlog.util.StandardException;
import fr.warlog.util.Utils;

/**
* File Management
* File Management.
*
* @author Philippe
*/
Expand All @@ -41,12 +42,27 @@ public List<FileNode> roots() {
private List<FileNode> toFileNodes(File[] listRoots) {
List<FileNode> res = new ArrayList<FileNode>();
for(File file:listRoots){
res.add(toFileNode(file));
if(filter(file.getAbsolutePath()))
res.add(toFileNode(file));
}
return res;
}

private long nextId(String path){
private boolean filter(String absolutePath) {
String whitelist = Configs.getParam("whitelist","");
String[] patternList=whitelist.split(",");
boolean authorized=false;
for(String pattern:patternList){
if (absolutePath.length() < pattern.length()) {
authorized |= pattern.startsWith(absolutePath);
}else{
authorized |= absolutePath.startsWith(pattern);
}
}
return authorized;
}

private long nextId(String path){
if(mapId.get(path) == null) mapId.put(path,++lastId);
return mapId.get(path);
}
Expand Down Expand Up @@ -94,12 +110,13 @@ public List<FileNode> list(FileNode root) {
}




/**
* Lit le fichier à envoyer
* Read all the file to send.
*/
public String readFile( String pMsg ) {
if (!filter(pMsg)){
throw new IllegalAccessError("File not allowed for read");
}

StringBuffer result = new StringBuffer();
BufferedReader lBis = null;
Expand Down Expand Up @@ -144,6 +161,9 @@ public String readFile( String pMsg ) {
* @param sep
*/
public Data<List<Line>> readFileLines( String pMsg , int start, int limit, String sep, int col, String spattern) {
if (!filter(pMsg)){
throw new IllegalAccessError("File not allowed for read");
}
List<Line> result = new ArrayList<Line>();
Data<List<Line>> dataRes = new Data<List<Line>>(result);
BufferedReader lBis = null;
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/fr/warlog/util/Configs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fr.warlog.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configs {
private static long lastLoad;
private static long reloadTimeout=10000;
private static Properties params;


/**
* Load warlog.properties in war, then in exec directory, then in /etc.
* the last load override the first one.
* the exec directory is often the server home,which contains the "conf" folder:
* tomcat :/usr/share/tomcat/conf
* jboss :../domain/configuration
* glassfish:[..]/domain/config
*/
public static synchronized void loadParams(){
params = new Properties();
try (InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("warlog.properties")){
params.load(resourceAsStream);
} catch (IOException e) {
}
//try to load tomcat configuration
tryLoadFile("conf/warlog.properties");
//try to load glassfish configuration
tryLoadFile("config/warlog.properties");
//try to load jboss configuration
tryLoadFile("configuration/warlog.properties");
//try to load std linux configuration
tryLoadFile("/etc/warlog.properties");
try{
reloadTimeout=Long.parseLong("configs.timeout");
}catch (NumberFormatException e){

}
//mark app as launched
if ("true".equals(params.get("warlog.lock"))){
try {
new File("warlog.lock").createNewFile();
} catch (IOException e) {
}
}
}

private static void tryLoadFile(String filename) {
try (InputStream in = new FileInputStream(filename)){
params.load(in);
} catch (IOException e) {
}
}

public static String getParam(String name, String defaultValue){
if(params == null || System.currentTimeMillis()-lastLoad>reloadTimeout){
lastLoad=System.currentTimeMillis();
loadParams();
}
return params.getProperty(name, defaultValue);
}

}
1 change: 1 addition & 0 deletions src/main/java/fr/warlog/web/FolderServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* list nodes of a folder.
* @author Philippe
*/
@SuppressWarnings("serial")
public class FolderServlet extends HttpServlet {

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/warlog.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Configuration for warlog Application
# define the allowed paths, let empty to allow all
whitelist=/tmp,/var/log,/srv,/opt,c:/tomcat7/logs
#create this file in execution path
warlog.lock=false
#reload this configuration file every (milliseconds)
configs.timeout=10000

0 comments on commit 266250e

Please sign in to comment.