diff --git a/src/fr/devinsy/logar/app/Logar.java b/src/fr/devinsy/logar/app/Logar.java
index aacb1c3..f0d4208 100644
--- a/src/fr/devinsy/logar/app/Logar.java
+++ b/src/fr/devinsy/logar/app/Logar.java
@@ -39,7 +39,7 @@ import org.april.logar.util.LineIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import anonymizer.Anonymizer;
+import fr.devinsy.logar.app.anonymizer.Anonymizer;
import fr.devinsy.logar.app.log.LogUtils;
/**
@@ -102,7 +102,7 @@ public final class Logar
{
if (file.getName().contains("access"))
{
- anonymizer.anonymize(file);
+ anonymizer.anonymizeAccessFile(file);
}
else if (file.getName().contains("error"))
{
diff --git a/src/anonymizer/AnonMap.java b/src/fr/devinsy/logar/app/anonymizer/AnonMap.java
similarity index 99%
rename from src/anonymizer/AnonMap.java
rename to src/fr/devinsy/logar/app/anonymizer/AnonMap.java
index 28b0f62..59611a1 100644
--- a/src/anonymizer/AnonMap.java
+++ b/src/fr/devinsy/logar/app/anonymizer/AnonMap.java
@@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see .
*/
-package anonymizer;
+package fr.devinsy.logar.app.anonymizer;
import java.util.HashMap;
diff --git a/src/anonymizer/AnonMapFile.java b/src/fr/devinsy/logar/app/anonymizer/AnonMapFile.java
similarity index 99%
rename from src/anonymizer/AnonMapFile.java
rename to src/fr/devinsy/logar/app/anonymizer/AnonMapFile.java
index 6e5c650..65c2b14 100644
--- a/src/anonymizer/AnonMapFile.java
+++ b/src/fr/devinsy/logar/app/anonymizer/AnonMapFile.java
@@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see .
*/
-package anonymizer;
+package fr.devinsy.logar.app.anonymizer;
import java.io.BufferedReader;
import java.io.File;
diff --git a/src/anonymizer/Anonymizer.java b/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java
similarity index 66%
rename from src/anonymizer/Anonymizer.java
rename to src/fr/devinsy/logar/app/anonymizer/Anonymizer.java
index 87a8558..530e36f 100644
--- a/src/anonymizer/Anonymizer.java
+++ b/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java
@@ -16,16 +16,18 @@
* You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see .
*/
-package anonymizer;
+package fr.devinsy.logar.app.anonymizer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.format.DateTimeParseException;
+import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
import org.april.logar.util.LineIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -36,10 +38,17 @@ import fr.devinsy.logar.app.log.LogUtils;
/**
* The Class Anonymizer.
*/
+/**
+ * @author cpm
+ *
+ */
public final class Anonymizer
{
private static Logger logger = LoggerFactory.getLogger(Anonymizer.class);
+ public static final Pattern IPV4_PATTERN = Pattern.compile("\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}");
+ public static final Pattern IPV6_PATTERN = Pattern.compile("([0-9a-f]{1,4}:{1,2}){4,7}([0-9a-f]){1,4}", Pattern.CASE_INSENSITIVE);
+
private AnonMap map;
/**
@@ -50,6 +59,32 @@ public final class Anonymizer
this.map = new AnonMap();
}
+ /**
+ * Anonymize.
+ *
+ * @param log
+ * the log
+ * @return the log
+ */
+ public Log anonymizeAccess(final Log log)
+ {
+ Log result;
+
+ String anonIp = this.map.anonymizeIp(log.getIp());
+ String anonUser = this.map.anonymizeUser(log.getUser());
+
+ String line = log.getLine().replace(log.getIp(), anonIp);
+ if (!log.getUser().equals("-"))
+ {
+ line = line.replace(log.getUser(), anonUser);
+ }
+
+ result = new Log(line, log.getDatetime(), anonIp, anonUser);
+
+ //
+ return result;
+ }
+
/**
* Anonymize.
*
@@ -58,7 +93,7 @@ public final class Anonymizer
* @param target
* the target
*/
- public void anonymize(final File source)
+ public void anonymizeAccessFile(final File source)
{
if (source == null)
{
@@ -68,10 +103,24 @@ public final class Anonymizer
{
throw new IllegalArgumentException("Parameter is not a file.");
}
+ else if (!StringUtils.containsAny(source.getName(), "access", "error"))
+ {
+ throw new IllegalArgumentException("File name does not contain 'access' or 'error'.");
+ }
else
{
System.out.println("== Anonymize log for [" + source.getName() + "]");
+ boolean isAccessFile;
+ if (source.getName().contains("access"))
+ {
+ isAccessFile = true;
+ }
+ else
+ {
+ isAccessFile = false;
+ }
+
File target;
if (source.getName().endsWith(".log.gz"))
{
@@ -93,15 +142,25 @@ public final class Anonymizer
try
{
- Log log = LogUtils.parseAccessLog(line);
- // logger.info("line={}", line);
- // logger.info("log =[{}][{}][{}]", log.getIp(),
- // log.getUser(), log.getDatetime());
+ Log anon;
+ if (isAccessFile)
+ {
+ Log log = LogUtils.parseAccessLog(line);
+ // logger.info("line={}", line);
+ // logger.info("log =[{}][{}][{}]", log.getIp(),
+ // log.getUser(), log.getDatetime());
- Log anon = anonymize(log);
- // logger.info("anon=[{}][{}][{}]", anon.getIp(),
- // anon.getUser(), anon.getDatetime());
- // logger.info("anon={}", anon);
+ anon = anonymizeAccess(log);
+ // logger.info("anon=[{}][{}][{}]", anon.getIp(),
+ // anon.getUser(), anon.getDatetime());
+ // logger.info("anon={}", anon);
+ }
+ else
+ {
+ Log log = LogUtils.parseErrorLog(line);
+
+ anon = anonymizeError(log);
+ }
out.println(anon);
}
@@ -129,13 +188,13 @@ public final class Anonymizer
}
/**
- * Anonymize.
+ * Anonymize error.
*
* @param log
* the log
* @return the log
*/
- public Log anonymize(final Log log)
+ public Log anonymizeError(final Log log)
{
Log result;
diff --git a/src/anonymizer/Ipv4Generator.java b/src/fr/devinsy/logar/app/anonymizer/Ipv4Generator.java
similarity index 99%
rename from src/anonymizer/Ipv4Generator.java
rename to src/fr/devinsy/logar/app/anonymizer/Ipv4Generator.java
index e81106e..9a6ae0f 100644
--- a/src/anonymizer/Ipv4Generator.java
+++ b/src/fr/devinsy/logar/app/anonymizer/Ipv4Generator.java
@@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see .
*/
-package anonymizer;
+package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
diff --git a/src/anonymizer/Ipv6Generator.java b/src/fr/devinsy/logar/app/anonymizer/Ipv6Generator.java
similarity index 98%
rename from src/anonymizer/Ipv6Generator.java
rename to src/fr/devinsy/logar/app/anonymizer/Ipv6Generator.java
index 3d5e94e..528ee48 100644
--- a/src/anonymizer/Ipv6Generator.java
+++ b/src/fr/devinsy/logar/app/anonymizer/Ipv6Generator.java
@@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see .
*/
-package anonymizer;
+package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
diff --git a/src/anonymizer/UserGenerator.java b/src/fr/devinsy/logar/app/anonymizer/UserGenerator.java
similarity index 98%
rename from src/anonymizer/UserGenerator.java
rename to src/fr/devinsy/logar/app/anonymizer/UserGenerator.java
index 2102026..a98ec86 100644
--- a/src/anonymizer/UserGenerator.java
+++ b/src/fr/devinsy/logar/app/anonymizer/UserGenerator.java
@@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with Logar. If not, see .
*/
-package anonymizer;
+package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
diff --git a/src/org/april/logar/cli/LogarCLI.java b/src/org/april/logar/cli/LogarCLI.java
index 0b02f6c..3428cea 100644
--- a/src/org/april/logar/cli/LogarCLI.java
+++ b/src/org/april/logar/cli/LogarCLI.java
@@ -54,12 +54,12 @@ public final class LogarCLI
message.appendln("Usage:");
message.appendln(" logar [ -h | -help | --help ]");
message.appendln(" logar [ -v | -version | --version ]");
- message.appendln(" logar anonymize source target anonymize ip and login");
- message.appendln(" logar archive source target archive previous month");
- message.appendln(" logar checksort fileordirectory check sort of an access log file");
- message.appendln(" logar check fileordirectory census bad format line in log files");
- message.appendln(" logar sort fileordirectory sort log files by datetime");
- message.appendln(" logar testarchive source test archive");
+ message.appendln(" logar anonymize fileordirectory [maptable] anonymize ip and login");
+ message.appendln(" logar archive source target archive previous month");
+ message.appendln(" logar checksort fileordirectory check sort of an access log file");
+ message.appendln(" logar check fileordirectory census bad format line in log files");
+ message.appendln(" logar sort fileordirectory sort log files by datetime");
+ message.appendln(" logar testarchive source test archive");
logger.info(message.toString());
}
diff --git a/test/org/april/logar/app/TestFoo.java b/test/org/april/logar/app/TestFoo.java
index a00e254..1b3681a 100644
--- a/test/org/april/logar/app/TestFoo.java
+++ b/test/org/april/logar/app/TestFoo.java
@@ -26,8 +26,8 @@ import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
-import anonymizer.Ipv4Generator;
import fr.devinsy.logar.app.LogarException;
+import fr.devinsy.logar.app.anonymizer.Ipv4Generator;
/**
* The Class VisitCountersTest.