Browse Source

Fixed package path.

master
Christian P. MOMON 6 months ago
parent
commit
90dc98fbc5
  1. 4
      src/fr/devinsy/logar/app/Logar.java
  2. 2
      src/fr/devinsy/logar/app/anonymizer/AnonMap.java
  3. 2
      src/fr/devinsy/logar/app/anonymizer/AnonMapFile.java
  4. 83
      src/fr/devinsy/logar/app/anonymizer/Anonymizer.java
  5. 2
      src/fr/devinsy/logar/app/anonymizer/Ipv4Generator.java
  6. 2
      src/fr/devinsy/logar/app/anonymizer/Ipv6Generator.java
  7. 2
      src/fr/devinsy/logar/app/anonymizer/UserGenerator.java
  8. 12
      src/org/april/logar/cli/LogarCLI.java
  9. 2
      test/org/april/logar/app/TestFoo.java

4
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"))
{

2
src/anonymizer/AnonMap.java → 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 <http://www.gnu.org/licenses/>.
*/
package anonymizer;
package fr.devinsy.logar.app.anonymizer;
import java.util.HashMap;

2
src/anonymizer/AnonMapFile.java → 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 <http://www.gnu.org/licenses/>.
*/
package anonymizer;
package fr.devinsy.logar.app.anonymizer;
import java.io.BufferedReader;
import java.io.File;

83
src/anonymizer/Anonymizer.java → 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 <http://www.gnu.org/licenses/>.
*/
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;

2
src/anonymizer/Ipv4Generator.java → 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 <http://www.gnu.org/licenses/>.
*/
package anonymizer;
package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;

2
src/anonymizer/Ipv6Generator.java → 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 <http://www.gnu.org/licenses/>.
*/
package anonymizer;
package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;

2
src/anonymizer/UserGenerator.java → 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 <http://www.gnu.org/licenses/>.
*/
package anonymizer;
package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;

12
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());
}

2
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.

Loading…
Cancel
Save